blob: 20d22954e5c8e0554717b5eaf9c1423bddf834ab [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
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700497// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700498status_t Parcel::writeInterfaceToken(const String16& interface)
499{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000500 const IPCThreadState* threadState = IPCThreadState::self();
501 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000502 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000503 writeInt32(threadState->shouldPropagateWorkSource() ?
504 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700505 // currently the interface identification token is just its name as a string
506 return writeString16(interface);
507}
508
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000509bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
510{
511 if (!mRequestHeaderPresent) {
512 return false;
513 }
514
515 const size_t initialPosition = dataPosition();
516 setDataPosition(mWorkSourceRequestHeaderPosition);
517 status_t err = writeInt32(uid);
518 setDataPosition(initialPosition);
519 return err == NO_ERROR;
520}
521
522uid_t Parcel::readCallingWorkSourceUid()
523{
524 if (!mRequestHeaderPresent) {
525 return IPCThreadState::kUnsetWorkSource;
526 }
527
528 const size_t initialPosition = dataPosition();
529 setDataPosition(mWorkSourceRequestHeaderPosition);
530 uid_t uid = readInt32();
531 setDataPosition(initialPosition);
532 return uid;
533}
534
Mathias Agopian83c04462009-05-22 19:00:22 -0700535bool Parcel::checkInterface(IBinder* binder) const
536{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700537 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700538}
539
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700540bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700541 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700542{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100543 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700544 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700545 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700546 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700547 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700548 if ((threadState->getLastTransactionBinderFlags() &
549 IBinder::FLAG_ONEWAY) != 0) {
550 // For one-way calls, the callee is running entirely
551 // disconnected from the caller, so disable StrictMode entirely.
552 // Not only does disk/network usage not impact the caller, but
553 // there's no way to commuicate back any violations anyway.
554 threadState->setStrictModePolicy(0);
555 } else {
556 threadState->setStrictModePolicy(strictPolicy);
557 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100558 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000559 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100560 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000561 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100562 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700563 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700564 if (str == interface) {
565 return true;
566 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700567 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700568 String8(interface).string(), String8(str).string());
569 return false;
570 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700571}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700572
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700573size_t Parcel::objectsCount() const
574{
575 return mObjectsSize;
576}
577
578status_t Parcel::errorCheck() const
579{
580 return mError;
581}
582
583void Parcel::setError(status_t err)
584{
585 mError = err;
586}
587
588status_t Parcel::finishWrite(size_t len)
589{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700590 if (len > INT32_MAX) {
591 // don't accept size_t values which may have come from an
592 // inadvertent conversion from a negative int.
593 return BAD_VALUE;
594 }
595
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700596 //printf("Finish write of %d\n", len);
597 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700598 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700599 if (mDataPos > mDataSize) {
600 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700601 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700602 }
603 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
604 return NO_ERROR;
605}
606
607status_t Parcel::writeUnpadded(const void* data, size_t len)
608{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700609 if (len > INT32_MAX) {
610 // don't accept size_t values which may have come from an
611 // inadvertent conversion from a negative int.
612 return BAD_VALUE;
613 }
614
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700615 size_t end = mDataPos + len;
616 if (end < mDataPos) {
617 // integer overflow
618 return BAD_VALUE;
619 }
620
621 if (end <= mDataCapacity) {
622restart_write:
623 memcpy(mData+mDataPos, data, len);
624 return finishWrite(len);
625 }
626
627 status_t err = growData(len);
628 if (err == NO_ERROR) goto restart_write;
629 return err;
630}
631
632status_t Parcel::write(const void* data, size_t len)
633{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700634 if (len > INT32_MAX) {
635 // don't accept size_t values which may have come from an
636 // inadvertent conversion from a negative int.
637 return BAD_VALUE;
638 }
639
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700640 void* const d = writeInplace(len);
641 if (d) {
642 memcpy(d, data, len);
643 return NO_ERROR;
644 }
645 return mError;
646}
647
648void* Parcel::writeInplace(size_t len)
649{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700650 if (len > INT32_MAX) {
651 // don't accept size_t values which may have come from an
652 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700653 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700654 }
655
656 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700657
658 // sanity check for integer overflow
659 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700660 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700661 }
662
663 if ((mDataPos+padded) <= mDataCapacity) {
664restart_write:
665 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
666 uint8_t* const data = mData+mDataPos;
667
668 // Need to pad at end?
669 if (padded != len) {
670#if BYTE_ORDER == BIG_ENDIAN
671 static const uint32_t mask[4] = {
672 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
673 };
674#endif
675#if BYTE_ORDER == LITTLE_ENDIAN
676 static const uint32_t mask[4] = {
677 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
678 };
679#endif
680 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
681 // *reinterpret_cast<void**>(data+padded-4));
682 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
683 }
684
685 finishWrite(padded);
686 return data;
687 }
688
689 status_t err = growData(padded);
690 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700691 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700692}
693
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800694status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
695 const uint8_t* strData = (uint8_t*)str.data();
696 const size_t strLen= str.length();
697 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100698 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800699 return BAD_VALUE;
700 }
701
702 status_t err = writeInt32(utf16Len);
703 if (err) {
704 return err;
705 }
706
707 // Allocate enough bytes to hold our converted string and its terminating NULL.
708 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
709 if (!dst) {
710 return NO_MEMORY;
711 }
712
Sergio Girof4607432016-07-21 14:46:35 +0100713 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800714
715 return NO_ERROR;
716}
717
718status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
719 if (!str) {
720 return writeInt32(-1);
721 }
722 return writeUtf8AsUtf16(*str);
723}
724
Casey Dahlin185d3442016-02-09 11:08:35 -0800725namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800726
Casey Dahlin185d3442016-02-09 11:08:35 -0800727template<typename T>
728status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700729{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700730 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700731 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700732 status = BAD_VALUE;
733 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700734 }
735
Casey Dahlin185d3442016-02-09 11:08:35 -0800736 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700737 if (status != OK) {
738 return status;
739 }
740
Casey Dahlin185d3442016-02-09 11:08:35 -0800741 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700742 if (!data) {
743 status = BAD_VALUE;
744 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700745 }
746
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700747 memcpy(data, val.data(), val.size());
748 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700749}
750
Casey Dahlin185d3442016-02-09 11:08:35 -0800751template<typename T>
752status_t writeByteVectorInternalPtr(Parcel* parcel,
753 const std::unique_ptr<std::vector<T>>& val)
754{
755 if (!val) {
756 return parcel->writeInt32(-1);
757 }
758
759 return writeByteVectorInternal(parcel, *val);
760}
761
762} // namespace
763
764status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
765 return writeByteVectorInternal(this, val);
766}
767
768status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
769{
770 return writeByteVectorInternalPtr(this, val);
771}
772
773status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
774 return writeByteVectorInternal(this, val);
775}
776
777status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
778{
779 return writeByteVectorInternalPtr(this, val);
780}
781
Casey Dahlin451ff582015-10-19 18:12:18 -0700782status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
783{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800784 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700785}
786
Casey Dahlinb9872622015-11-25 15:09:45 -0800787status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
788{
789 return writeNullableTypedVector(val, &Parcel::writeInt32);
790}
791
Casey Dahlin451ff582015-10-19 18:12:18 -0700792status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
793{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800794 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700795}
796
Casey Dahlinb9872622015-11-25 15:09:45 -0800797status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
798{
799 return writeNullableTypedVector(val, &Parcel::writeInt64);
800}
801
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800802status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
803{
804 return writeTypedVector(val, &Parcel::writeUint64);
805}
806
807status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
808{
809 return writeNullableTypedVector(val, &Parcel::writeUint64);
810}
811
Casey Dahlin451ff582015-10-19 18:12:18 -0700812status_t Parcel::writeFloatVector(const std::vector<float>& val)
813{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800814 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700815}
816
Casey Dahlinb9872622015-11-25 15:09:45 -0800817status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
818{
819 return writeNullableTypedVector(val, &Parcel::writeFloat);
820}
821
Casey Dahlin451ff582015-10-19 18:12:18 -0700822status_t Parcel::writeDoubleVector(const std::vector<double>& val)
823{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800824 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700825}
826
Casey Dahlinb9872622015-11-25 15:09:45 -0800827status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
828{
829 return writeNullableTypedVector(val, &Parcel::writeDouble);
830}
831
Casey Dahlin451ff582015-10-19 18:12:18 -0700832status_t Parcel::writeBoolVector(const std::vector<bool>& val)
833{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800834 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700835}
836
Casey Dahlinb9872622015-11-25 15:09:45 -0800837status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
838{
839 return writeNullableTypedVector(val, &Parcel::writeBool);
840}
841
Casey Dahlin451ff582015-10-19 18:12:18 -0700842status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
843{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800844 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700845}
846
Casey Dahlinb9872622015-11-25 15:09:45 -0800847status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
848{
849 return writeNullableTypedVector(val, &Parcel::writeChar);
850}
851
Casey Dahlin451ff582015-10-19 18:12:18 -0700852status_t Parcel::writeString16Vector(const std::vector<String16>& val)
853{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800854 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700855}
856
Casey Dahlinb9872622015-11-25 15:09:45 -0800857status_t Parcel::writeString16Vector(
858 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
859{
860 return writeNullableTypedVector(val, &Parcel::writeString16);
861}
862
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800863status_t Parcel::writeUtf8VectorAsUtf16Vector(
864 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
865 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
866}
867
868status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
869 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
870}
871
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700872status_t Parcel::writeInt32(int32_t val)
873{
Andreas Huber84a6d042009-08-17 13:33:27 -0700874 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700875}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800876
877status_t Parcel::writeUint32(uint32_t val)
878{
879 return writeAligned(val);
880}
881
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700882status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700883 if (len > INT32_MAX) {
884 // don't accept size_t values which may have come from an
885 // inadvertent conversion from a negative int.
886 return BAD_VALUE;
887 }
888
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700889 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700890 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700891 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700892 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700893 if (ret == NO_ERROR) {
894 ret = write(val, len * sizeof(*val));
895 }
896 return ret;
897}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700898status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700899 if (len > INT32_MAX) {
900 // don't accept size_t values which may have come from an
901 // inadvertent conversion from a negative int.
902 return BAD_VALUE;
903 }
904
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700905 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700906 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700907 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700908 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700909 if (ret == NO_ERROR) {
910 ret = write(val, len * sizeof(*val));
911 }
912 return ret;
913}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700914
Casey Dahlind6848f52015-10-15 15:44:59 -0700915status_t Parcel::writeBool(bool val)
916{
917 return writeInt32(int32_t(val));
918}
919
920status_t Parcel::writeChar(char16_t val)
921{
922 return writeInt32(int32_t(val));
923}
924
925status_t Parcel::writeByte(int8_t val)
926{
927 return writeInt32(int32_t(val));
928}
929
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700930status_t Parcel::writeInt64(int64_t val)
931{
Andreas Huber84a6d042009-08-17 13:33:27 -0700932 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700933}
934
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700935status_t Parcel::writeUint64(uint64_t val)
936{
937 return writeAligned(val);
938}
939
Serban Constantinescuf683e012013-11-05 16:53:55 +0000940status_t Parcel::writePointer(uintptr_t val)
941{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800942 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000943}
944
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700945status_t Parcel::writeFloat(float val)
946{
Andreas Huber84a6d042009-08-17 13:33:27 -0700947 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700948}
949
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800950#if defined(__mips__) && defined(__mips_hard_float)
951
952status_t Parcel::writeDouble(double val)
953{
954 union {
955 double d;
956 unsigned long long ll;
957 } u;
958 u.d = val;
959 return writeAligned(u.ll);
960}
961
962#else
963
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700964status_t Parcel::writeDouble(double val)
965{
Andreas Huber84a6d042009-08-17 13:33:27 -0700966 return writeAligned(val);
967}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700968
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800969#endif
970
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700971status_t Parcel::writeCString(const char* str)
972{
973 return write(str, strlen(str)+1);
974}
975
976status_t Parcel::writeString8(const String8& str)
977{
978 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100979 // only write string if its length is more than zero characters,
980 // as readString8 will only read if the length field is non-zero.
981 // this is slightly different from how writeString16 works.
982 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700983 err = write(str.string(), str.bytes()+1);
984 }
985 return err;
986}
987
Casey Dahlinb9872622015-11-25 15:09:45 -0800988status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
989{
990 if (!str) {
991 return writeInt32(-1);
992 }
993
994 return writeString16(*str);
995}
996
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700997status_t Parcel::writeString16(const String16& str)
998{
999 return writeString16(str.string(), str.size());
1000}
1001
1002status_t Parcel::writeString16(const char16_t* str, size_t len)
1003{
Yi Kong91635562018-06-07 14:38:36 -07001004 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001005
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001006 status_t err = writeInt32(len);
1007 if (err == NO_ERROR) {
1008 len *= sizeof(char16_t);
1009 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1010 if (data) {
1011 memcpy(data, str, len);
1012 *reinterpret_cast<char16_t*>(data+len) = 0;
1013 return NO_ERROR;
1014 }
1015 err = mError;
1016 }
1017 return err;
1018}
1019
1020status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1021{
1022 return flatten_binder(ProcessState::self(), val, this);
1023}
1024
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001025status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1026{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001027 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001028}
1029
Casey Dahlinb9872622015-11-25 15:09:45 -08001030status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1031{
1032 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1033}
1034
1035status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001036 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001037}
1038
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001039status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001040 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001041}
1042
Casey Dahlinb9872622015-11-25 15:09:45 -08001043status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1044 if (!parcelable) {
1045 return writeInt32(0);
1046 }
1047
1048 return writeParcelable(*parcelable);
1049}
1050
Christopher Wiley97f048d2015-11-19 06:49:05 -08001051status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1052 status_t status = writeInt32(1); // parcelable is not null.
1053 if (status != OK) {
1054 return status;
1055 }
1056 return parcelable.writeToParcel(this);
1057}
1058
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001059status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001060{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001061 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001062 return BAD_TYPE;
1063
1064 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001065 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001066 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001068 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001069 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001071 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1072 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073
1074 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001075 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076 return err;
1077 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001078 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001079 return err;
1080}
1081
Jeff Brown93ff1f92011-11-04 19:01:44 -07001082status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001083{
1084 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001085 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001086 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001087 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001088 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001089 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001090 return writeObject(obj, true);
1091}
1092
1093status_t Parcel::writeDupFileDescriptor(int fd)
1094{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001095 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001096 if (dupFd < 0) {
1097 return -errno;
1098 }
1099 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001100 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001101 close(dupFd);
1102 }
1103 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001104}
1105
Dianne Hackborn1941a402016-08-29 12:30:43 -07001106status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1107{
1108 writeInt32(0);
1109 return writeFileDescriptor(fd, takeOwnership);
1110}
1111
Ryo Hashimotobf551892018-05-31 16:58:35 +09001112status_t Parcel::writeDupParcelFileDescriptor(int fd)
1113{
1114 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1115 if (dupFd < 0) {
1116 return -errno;
1117 }
1118 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1119 if (err != OK) {
1120 close(dupFd);
1121 }
1122 return err;
1123}
1124
Christopher Wiley2cf19952016-04-11 11:09:37 -07001125status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001126 return writeDupFileDescriptor(fd.get());
1127}
1128
Christopher Wiley2cf19952016-04-11 11:09:37 -07001129status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001130 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1131}
1132
Christopher Wiley2cf19952016-04-11 11:09:37 -07001133status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001134 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1135}
1136
Jeff Brown13b16042014-11-11 16:44:25 -08001137status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001138{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001139 if (len > INT32_MAX) {
1140 // don't accept size_t values which may have come from an
1141 // inadvertent conversion from a negative int.
1142 return BAD_VALUE;
1143 }
1144
Jeff Brown13b16042014-11-11 16:44:25 -08001145 status_t status;
1146 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001147 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001148 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001149 if (status) return status;
1150
1151 void* ptr = writeInplace(len);
1152 if (!ptr) return NO_MEMORY;
1153
Jeff Brown13b16042014-11-11 16:44:25 -08001154 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001155 return NO_ERROR;
1156 }
1157
Steve Block6807e592011-10-20 11:56:00 +01001158 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001159 int fd = ashmem_create_region("Parcel Blob", len);
1160 if (fd < 0) return NO_MEMORY;
1161
1162 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1163 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001164 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001165 } else {
Yi Kong91635562018-06-07 14:38:36 -07001166 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001167 if (ptr == MAP_FAILED) {
1168 status = -errno;
1169 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001170 if (!mutableCopy) {
1171 result = ashmem_set_prot_region(fd, PROT_READ);
1172 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001173 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001174 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001175 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001176 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001177 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001178 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001179 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001180 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001181 return NO_ERROR;
1182 }
1183 }
1184 }
1185 }
1186 ::munmap(ptr, len);
1187 }
1188 ::close(fd);
1189 return status;
1190}
1191
Jeff Brown13b16042014-11-11 16:44:25 -08001192status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1193{
1194 // Must match up with what's done in writeBlob.
1195 if (!mAllowFds) return FDS_NOT_ALLOWED;
1196 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1197 if (status) return status;
1198 return writeDupFileDescriptor(fd);
1199}
1200
Mathias Agopiane1424282013-07-29 21:24:40 -07001201status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001202{
1203 status_t err;
1204
1205 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001206 const size_t len = val.getFlattenedSize();
1207 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001208
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001209 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001210 // don't accept size_t values which may have come from an
1211 // inadvertent conversion from a negative int.
1212 return BAD_VALUE;
1213 }
1214
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001215 err = this->writeInt32(len);
1216 if (err) return err;
1217
1218 err = this->writeInt32(fd_count);
1219 if (err) return err;
1220
1221 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001222 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001223 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001224 return BAD_VALUE;
1225
Yi Kong91635562018-06-07 14:38:36 -07001226 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001227 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001228 fds = new (std::nothrow) int[fd_count];
1229 if (fds == nullptr) {
1230 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1231 return BAD_VALUE;
1232 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001233 }
1234
1235 err = val.flatten(buf, len, fds, fd_count);
1236 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1237 err = this->writeDupFileDescriptor( fds[i] );
1238 }
1239
1240 if (fd_count) {
1241 delete [] fds;
1242 }
1243
1244 return err;
1245}
1246
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001247status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1248{
1249 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1250 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1251 if (enoughData && enoughObjects) {
1252restart_write:
1253 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001254
Christopher Tate98e67d32015-06-03 18:44:15 -07001255 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001256 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001257 if (!mAllowFds) {
1258 // fail before modifying our object index
1259 return FDS_NOT_ALLOWED;
1260 }
1261 mHasFds = mFdsKnown = true;
1262 }
1263
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001264 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001265 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001266 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001267 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001268 mObjectsSize++;
1269 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001270
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001271 return finishWrite(sizeof(flat_binder_object));
1272 }
1273
1274 if (!enoughData) {
1275 const status_t err = growData(sizeof(val));
1276 if (err != NO_ERROR) return err;
1277 }
1278 if (!enoughObjects) {
1279 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001280 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001281 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001282 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001283 mObjects = objects;
1284 mObjectsCapacity = newSize;
1285 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001286
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001287 goto restart_write;
1288}
1289
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001290status_t Parcel::writeNoException()
1291{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001292 binder::Status status;
1293 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001294}
1295
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001296status_t Parcel::validateReadData(size_t upperBound) const
1297{
1298 // Don't allow non-object reads on object data
1299 if (mObjectsSorted || mObjectsSize <= 1) {
1300data_sorted:
1301 // Expect to check only against the next object
1302 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1303 // For some reason the current read position is greater than the next object
1304 // hint. Iterate until we find the right object
1305 size_t nextObject = mNextObjectHint;
1306 do {
1307 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1308 // Requested info overlaps with an object
1309 ALOGE("Attempt to read from protected data in Parcel %p", this);
1310 return PERMISSION_DENIED;
1311 }
1312 nextObject++;
1313 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1314 mNextObjectHint = nextObject;
1315 }
1316 return NO_ERROR;
1317 }
1318 // Quickly determine if mObjects is sorted.
1319 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1320 binder_size_t* prevObj = currObj;
1321 while (currObj > mObjects) {
1322 prevObj--;
1323 if(*prevObj > *currObj) {
1324 goto data_unsorted;
1325 }
1326 currObj--;
1327 }
1328 mObjectsSorted = true;
1329 goto data_sorted;
1330
1331data_unsorted:
1332 // Insertion Sort mObjects
1333 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1334 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1335 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1336 binder_size_t temp = *iter0;
1337 binder_size_t* iter1 = iter0 - 1;
1338 while (iter1 >= mObjects && *iter1 > temp) {
1339 *(iter1 + 1) = *iter1;
1340 iter1--;
1341 }
1342 *(iter1 + 1) = temp;
1343 }
1344 mNextObjectHint = 0;
1345 mObjectsSorted = true;
1346 goto data_sorted;
1347}
1348
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001349status_t Parcel::read(void* outData, size_t len) const
1350{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001351 if (len > INT32_MAX) {
1352 // don't accept size_t values which may have come from an
1353 // inadvertent conversion from a negative int.
1354 return BAD_VALUE;
1355 }
1356
1357 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1358 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001359 if (mObjectsSize > 0) {
1360 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001361 if(err != NO_ERROR) {
1362 // Still increment the data position by the expected length
1363 mDataPos += pad_size(len);
1364 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1365 return err;
1366 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001367 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001368 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001369 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001370 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001371 return NO_ERROR;
1372 }
1373 return NOT_ENOUGH_DATA;
1374}
1375
1376const void* Parcel::readInplace(size_t len) const
1377{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001378 if (len > INT32_MAX) {
1379 // don't accept size_t values which may have come from an
1380 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001381 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001382 }
1383
1384 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1385 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001386 if (mObjectsSize > 0) {
1387 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001388 if(err != NO_ERROR) {
1389 // Still increment the data position by the expected length
1390 mDataPos += pad_size(len);
1391 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001392 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001393 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001394 }
1395
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001396 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001397 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001398 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001399 return data;
1400 }
Yi Kong91635562018-06-07 14:38:36 -07001401 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001402}
1403
Andreas Huber84a6d042009-08-17 13:33:27 -07001404template<class T>
1405status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001406 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001407
1408 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001409 if (mObjectsSize > 0) {
1410 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001411 if(err != NO_ERROR) {
1412 // Still increment the data position by the expected length
1413 mDataPos += sizeof(T);
1414 return err;
1415 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001416 }
1417
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001418 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001419 mDataPos += sizeof(T);
1420 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001421 return NO_ERROR;
1422 } else {
1423 return NOT_ENOUGH_DATA;
1424 }
1425}
1426
Andreas Huber84a6d042009-08-17 13:33:27 -07001427template<class T>
1428T Parcel::readAligned() const {
1429 T result;
1430 if (readAligned(&result) != NO_ERROR) {
1431 result = 0;
1432 }
1433
1434 return result;
1435}
1436
1437template<class T>
1438status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001439 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001440
1441 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1442restart_write:
1443 *reinterpret_cast<T*>(mData+mDataPos) = val;
1444 return finishWrite(sizeof(val));
1445 }
1446
1447 status_t err = growData(sizeof(val));
1448 if (err == NO_ERROR) goto restart_write;
1449 return err;
1450}
1451
Casey Dahlin185d3442016-02-09 11:08:35 -08001452namespace {
1453
1454template<typename T>
1455status_t readByteVectorInternal(const Parcel* parcel,
1456 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001457 val->clear();
1458
1459 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001460 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001461
1462 if (status != OK) {
1463 return status;
1464 }
1465
Christopher Wiley4db672d2015-11-10 09:44:30 -08001466 if (size < 0) {
1467 status = UNEXPECTED_NULL;
1468 return status;
1469 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001470 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001471 status = BAD_VALUE;
1472 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001473 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001474
Paul Lietar433e87b2016-09-16 10:39:32 -07001475 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001476 if (!data) {
1477 status = BAD_VALUE;
1478 return status;
1479 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001480 val->reserve(size);
1481 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001482
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001483 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001484}
1485
Casey Dahlin185d3442016-02-09 11:08:35 -08001486template<typename T>
1487status_t readByteVectorInternalPtr(
1488 const Parcel* parcel,
1489 std::unique_ptr<std::vector<T>>* val) {
1490 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001491 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001492 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001493 val->reset();
1494
1495 if (status != OK || size < 0) {
1496 return status;
1497 }
1498
Casey Dahlin185d3442016-02-09 11:08:35 -08001499 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001500 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001501
Casey Dahlin185d3442016-02-09 11:08:35 -08001502 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001503
1504 if (status != OK) {
1505 val->reset();
1506 }
1507
1508 return status;
1509}
1510
Casey Dahlin185d3442016-02-09 11:08:35 -08001511} // namespace
1512
1513status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1514 return readByteVectorInternal(this, val);
1515}
1516
1517status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1518 return readByteVectorInternal(this, val);
1519}
1520
1521status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1522 return readByteVectorInternalPtr(this, val);
1523}
1524
1525status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1526 return readByteVectorInternalPtr(this, val);
1527}
1528
Casey Dahlinb9872622015-11-25 15:09:45 -08001529status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1530 return readNullableTypedVector(val, &Parcel::readInt32);
1531}
1532
Casey Dahlin451ff582015-10-19 18:12:18 -07001533status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001534 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001535}
1536
Casey Dahlinb9872622015-11-25 15:09:45 -08001537status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1538 return readNullableTypedVector(val, &Parcel::readInt64);
1539}
1540
Casey Dahlin451ff582015-10-19 18:12:18 -07001541status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001542 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001543}
1544
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001545status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1546 return readNullableTypedVector(val, &Parcel::readUint64);
1547}
1548
1549status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1550 return readTypedVector(val, &Parcel::readUint64);
1551}
1552
Casey Dahlinb9872622015-11-25 15:09:45 -08001553status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1554 return readNullableTypedVector(val, &Parcel::readFloat);
1555}
1556
Casey Dahlin451ff582015-10-19 18:12:18 -07001557status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001558 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001559}
1560
Casey Dahlinb9872622015-11-25 15:09:45 -08001561status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1562 return readNullableTypedVector(val, &Parcel::readDouble);
1563}
1564
Casey Dahlin451ff582015-10-19 18:12:18 -07001565status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001566 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001567}
1568
Casey Dahlinb9872622015-11-25 15:09:45 -08001569status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1570 const int32_t start = dataPosition();
1571 int32_t size;
1572 status_t status = readInt32(&size);
1573 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001574
Casey Dahlinb9872622015-11-25 15:09:45 -08001575 if (status != OK || size < 0) {
1576 return status;
1577 }
1578
1579 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001580 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001581
1582 status = readBoolVector(val->get());
1583
1584 if (status != OK) {
1585 val->reset();
1586 }
1587
1588 return status;
1589}
1590
1591status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001592 int32_t size;
1593 status_t status = readInt32(&size);
1594
1595 if (status != OK) {
1596 return status;
1597 }
1598
1599 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001600 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001601 }
1602
1603 val->resize(size);
1604
1605 /* C++ bool handling means a vector of bools isn't necessarily addressable
1606 * (we might use individual bits)
1607 */
Christopher Wiley97887982015-10-27 16:33:47 -07001608 bool data;
1609 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001610 status = readBool(&data);
1611 (*val)[i] = data;
1612
1613 if (status != OK) {
1614 return status;
1615 }
1616 }
1617
1618 return OK;
1619}
1620
Casey Dahlinb9872622015-11-25 15:09:45 -08001621status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1622 return readNullableTypedVector(val, &Parcel::readChar);
1623}
1624
Casey Dahlin451ff582015-10-19 18:12:18 -07001625status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001626 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001627}
1628
Casey Dahlinb9872622015-11-25 15:09:45 -08001629status_t Parcel::readString16Vector(
1630 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1631 return readNullableTypedVector(val, &Parcel::readString16);
1632}
1633
Casey Dahlin451ff582015-10-19 18:12:18 -07001634status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001635 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001636}
1637
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001638status_t Parcel::readUtf8VectorFromUtf16Vector(
1639 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1640 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1641}
1642
1643status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1644 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1645}
Casey Dahlin451ff582015-10-19 18:12:18 -07001646
Andreas Huber84a6d042009-08-17 13:33:27 -07001647status_t Parcel::readInt32(int32_t *pArg) const
1648{
1649 return readAligned(pArg);
1650}
1651
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001652int32_t Parcel::readInt32() const
1653{
Andreas Huber84a6d042009-08-17 13:33:27 -07001654 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001655}
1656
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001657status_t Parcel::readUint32(uint32_t *pArg) const
1658{
1659 return readAligned(pArg);
1660}
1661
1662uint32_t Parcel::readUint32() const
1663{
1664 return readAligned<uint32_t>();
1665}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001666
1667status_t Parcel::readInt64(int64_t *pArg) const
1668{
Andreas Huber84a6d042009-08-17 13:33:27 -07001669 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001670}
1671
1672
1673int64_t Parcel::readInt64() const
1674{
Andreas Huber84a6d042009-08-17 13:33:27 -07001675 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001676}
1677
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001678status_t Parcel::readUint64(uint64_t *pArg) const
1679{
1680 return readAligned(pArg);
1681}
1682
1683uint64_t Parcel::readUint64() const
1684{
1685 return readAligned<uint64_t>();
1686}
1687
Serban Constantinescuf683e012013-11-05 16:53:55 +00001688status_t Parcel::readPointer(uintptr_t *pArg) const
1689{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001690 status_t ret;
1691 binder_uintptr_t ptr;
1692 ret = readAligned(&ptr);
1693 if (!ret)
1694 *pArg = ptr;
1695 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001696}
1697
1698uintptr_t Parcel::readPointer() const
1699{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001700 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001701}
1702
1703
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001704status_t Parcel::readFloat(float *pArg) const
1705{
Andreas Huber84a6d042009-08-17 13:33:27 -07001706 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001707}
1708
1709
1710float Parcel::readFloat() const
1711{
Andreas Huber84a6d042009-08-17 13:33:27 -07001712 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001713}
1714
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001715#if defined(__mips__) && defined(__mips_hard_float)
1716
1717status_t Parcel::readDouble(double *pArg) const
1718{
1719 union {
1720 double d;
1721 unsigned long long ll;
1722 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001723 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001724 status_t status;
1725 status = readAligned(&u.ll);
1726 *pArg = u.d;
1727 return status;
1728}
1729
1730double Parcel::readDouble() const
1731{
1732 union {
1733 double d;
1734 unsigned long long ll;
1735 } u;
1736 u.ll = readAligned<unsigned long long>();
1737 return u.d;
1738}
1739
1740#else
1741
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001742status_t Parcel::readDouble(double *pArg) const
1743{
Andreas Huber84a6d042009-08-17 13:33:27 -07001744 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001745}
1746
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001747double Parcel::readDouble() const
1748{
Andreas Huber84a6d042009-08-17 13:33:27 -07001749 return readAligned<double>();
1750}
1751
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001752#endif
1753
Andreas Huber84a6d042009-08-17 13:33:27 -07001754status_t Parcel::readIntPtr(intptr_t *pArg) const
1755{
1756 return readAligned(pArg);
1757}
1758
1759
1760intptr_t Parcel::readIntPtr() const
1761{
1762 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001763}
1764
Casey Dahlind6848f52015-10-15 15:44:59 -07001765status_t Parcel::readBool(bool *pArg) const
1766{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001767 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001768 status_t ret = readInt32(&tmp);
1769 *pArg = (tmp != 0);
1770 return ret;
1771}
1772
1773bool Parcel::readBool() const
1774{
1775 return readInt32() != 0;
1776}
1777
1778status_t Parcel::readChar(char16_t *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 = char16_t(tmp);
1783 return ret;
1784}
1785
1786char16_t Parcel::readChar() const
1787{
1788 return char16_t(readInt32());
1789}
1790
1791status_t Parcel::readByte(int8_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 = int8_t(tmp);
1796 return ret;
1797}
1798
1799int8_t Parcel::readByte() const
1800{
1801 return int8_t(readInt32());
1802}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001803
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001804status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1805 size_t utf16Size = 0;
1806 const char16_t* src = readString16Inplace(&utf16Size);
1807 if (!src) {
1808 return UNEXPECTED_NULL;
1809 }
1810
1811 // Save ourselves the trouble, we're done.
1812 if (utf16Size == 0u) {
1813 str->clear();
1814 return NO_ERROR;
1815 }
1816
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001817 // Allow for closing '\0'
1818 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1819 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001820 return BAD_VALUE;
1821 }
1822 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001823 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001824 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001825 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1826 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001827 return NO_ERROR;
1828}
1829
1830status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1831 const int32_t start = dataPosition();
1832 int32_t size;
1833 status_t status = readInt32(&size);
1834 str->reset();
1835
1836 if (status != OK || size < 0) {
1837 return status;
1838 }
1839
1840 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001841 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001842 return readUtf8FromUtf16(str->get());
1843}
1844
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001845const char* Parcel::readCString() const
1846{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001847 if (mDataPos < mDataSize) {
1848 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001849 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1850 // is the string's trailing NUL within the parcel's valid bounds?
1851 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1852 if (eos) {
1853 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001854 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001855 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001856 return str;
1857 }
1858 }
Yi Kong91635562018-06-07 14:38:36 -07001859 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001860}
1861
1862String8 Parcel::readString8() const
1863{
Roshan Pius87b64d22016-07-18 12:51:02 -07001864 String8 retString;
1865 status_t status = readString8(&retString);
1866 if (status != OK) {
1867 // We don't care about errors here, so just return an empty string.
1868 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001869 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001870 return retString;
1871}
1872
1873status_t Parcel::readString8(String8* pArg) const
1874{
1875 int32_t size;
1876 status_t status = readInt32(&size);
1877 if (status != OK) {
1878 return status;
1879 }
1880 // watch for potential int overflow from size+1
1881 if (size < 0 || size >= INT32_MAX) {
1882 return BAD_VALUE;
1883 }
1884 // |writeString8| writes nothing for empty string.
1885 if (size == 0) {
1886 *pArg = String8();
1887 return OK;
1888 }
1889 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07001890 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07001891 return BAD_VALUE;
1892 }
1893 pArg->setTo(str, size);
1894 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001895}
1896
1897String16 Parcel::readString16() const
1898{
1899 size_t len;
1900 const char16_t* str = readString16Inplace(&len);
1901 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001902 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001903 return String16();
1904}
1905
Casey Dahlinb9872622015-11-25 15:09:45 -08001906status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1907{
1908 const int32_t start = dataPosition();
1909 int32_t size;
1910 status_t status = readInt32(&size);
1911 pArg->reset();
1912
1913 if (status != OK || size < 0) {
1914 return status;
1915 }
1916
1917 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001918 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001919
1920 status = readString16(pArg->get());
1921
1922 if (status != OK) {
1923 pArg->reset();
1924 }
1925
1926 return status;
1927}
1928
Casey Dahlin451ff582015-10-19 18:12:18 -07001929status_t Parcel::readString16(String16* pArg) const
1930{
1931 size_t len;
1932 const char16_t* str = readString16Inplace(&len);
1933 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001934 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001935 return 0;
1936 } else {
1937 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001938 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001939 }
1940}
1941
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001942const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1943{
1944 int32_t size = readInt32();
1945 // watch for potential int overflow from size+1
1946 if (size >= 0 && size < INT32_MAX) {
1947 *outLen = size;
1948 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001949 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001950 return str;
1951 }
1952 }
1953 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001954 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001955}
1956
Casey Dahlinf0c13772015-10-27 18:33:56 -07001957status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1958{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001959 status_t status = readNullableStrongBinder(val);
1960 if (status == OK && !val->get()) {
1961 status = UNEXPECTED_NULL;
1962 }
1963 return status;
1964}
1965
1966status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1967{
Casey Dahlinf0c13772015-10-27 18:33:56 -07001968 return unflatten_binder(ProcessState::self(), *this, val);
1969}
1970
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001971sp<IBinder> Parcel::readStrongBinder() const
1972{
1973 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001974 // Note that a lot of code in Android reads binders by hand with this
1975 // method, and that code has historically been ok with getting nullptr
1976 // back (while ignoring error codes).
1977 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001978 return val;
1979}
1980
Christopher Wiley97f048d2015-11-19 06:49:05 -08001981status_t Parcel::readParcelable(Parcelable* parcelable) const {
1982 int32_t have_parcelable = 0;
1983 status_t status = readInt32(&have_parcelable);
1984 if (status != OK) {
1985 return status;
1986 }
1987 if (!have_parcelable) {
1988 return UNEXPECTED_NULL;
1989 }
1990 return parcelable->readFromParcel(this);
1991}
1992
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001993int32_t Parcel::readExceptionCode() const
1994{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001995 binder::Status status;
1996 status.readFromParcel(*this);
1997 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001998}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001999
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002000native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002001{
2002 int numFds, numInts;
2003 status_t err;
2004 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002005 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002006 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002007 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002008
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002009 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002010 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002011 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002012 }
2013
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002014 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002015 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002016 if (h->data[i] < 0) {
2017 for (int j = 0; j < i; j++) {
2018 close(h->data[j]);
2019 }
2020 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002021 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002022 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002023 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002024 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002025 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002026 native_handle_close(h);
2027 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002028 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002029 }
2030 return h;
2031}
2032
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002033int Parcel::readFileDescriptor() const
2034{
2035 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002036
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002037 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002038 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002039 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002040
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002041 return BAD_TYPE;
2042}
2043
Dianne Hackborn1941a402016-08-29 12:30:43 -07002044int Parcel::readParcelFileDescriptor() const
2045{
2046 int32_t hasComm = readInt32();
2047 int fd = readFileDescriptor();
2048 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002049 // detach (owned by the binder driver)
2050 int comm = readFileDescriptor();
2051
2052 // warning: this must be kept in sync with:
2053 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2054 enum ParcelFileDescriptorStatus {
2055 DETACHED = 2,
2056 };
2057
2058#if BYTE_ORDER == BIG_ENDIAN
2059 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2060#endif
2061#if BYTE_ORDER == LITTLE_ENDIAN
2062 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2063#endif
2064
2065 ssize_t written = TEMP_FAILURE_RETRY(
2066 ::write(comm, &message, sizeof(message)));
2067
2068 if (written == -1 || written != sizeof(message)) {
2069 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2070 written, strerror(errno));
2071 return BAD_TYPE;
2072 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002073 }
2074 return fd;
2075}
2076
Christopher Wiley2cf19952016-04-11 11:09:37 -07002077status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002078{
2079 int got = readFileDescriptor();
2080
2081 if (got == BAD_TYPE) {
2082 return BAD_TYPE;
2083 }
2084
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002085 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002086
2087 if (val->get() < 0) {
2088 return BAD_VALUE;
2089 }
2090
2091 return OK;
2092}
2093
Ryo Hashimotobf551892018-05-31 16:58:35 +09002094status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2095{
2096 int got = readParcelFileDescriptor();
2097
2098 if (got == BAD_TYPE) {
2099 return BAD_TYPE;
2100 }
2101
2102 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2103
2104 if (val->get() < 0) {
2105 return BAD_VALUE;
2106 }
2107
2108 return OK;
2109}
Casey Dahlin06673e32015-11-23 13:24:23 -08002110
Christopher Wiley2cf19952016-04-11 11:09:37 -07002111status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002112 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2113}
2114
Christopher Wiley2cf19952016-04-11 11:09:37 -07002115status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002116 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2117}
2118
Jeff Brown5707dbf2011-09-23 21:17:56 -07002119status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2120{
Jeff Brown13b16042014-11-11 16:44:25 -08002121 int32_t blobType;
2122 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002123 if (status) return status;
2124
Jeff Brown13b16042014-11-11 16:44:25 -08002125 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002126 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002127 const void* ptr = readInplace(len);
2128 if (!ptr) return BAD_VALUE;
2129
Jeff Brown13b16042014-11-11 16:44:25 -08002130 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002131 return NO_ERROR;
2132 }
2133
Steve Block6807e592011-10-20 11:56:00 +01002134 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002135 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002136 int fd = readFileDescriptor();
2137 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2138
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002139 if (!ashmem_valid(fd)) {
2140 ALOGE("invalid fd");
2141 return BAD_VALUE;
2142 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002143 int size = ashmem_get_size_region(fd);
2144 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002145 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002146 return BAD_VALUE;
2147 }
Yi Kong91635562018-06-07 14:38:36 -07002148 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002149 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002150 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002151
Jeff Brown13b16042014-11-11 16:44:25 -08002152 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002153 return NO_ERROR;
2154}
2155
Mathias Agopiane1424282013-07-29 21:24:40 -07002156status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002157{
2158 // size
2159 const size_t len = this->readInt32();
2160 const size_t fd_count = this->readInt32();
2161
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002162 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002163 // don't accept size_t values which may have come from an
2164 // inadvertent conversion from a negative int.
2165 return BAD_VALUE;
2166 }
2167
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002168 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002169 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002170 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002171 return BAD_VALUE;
2172
Yi Kong91635562018-06-07 14:38:36 -07002173 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002174 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002175 fds = new (std::nothrow) int[fd_count];
2176 if (fds == nullptr) {
2177 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2178 return BAD_VALUE;
2179 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002180 }
2181
2182 status_t err = NO_ERROR;
2183 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002184 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002185 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002186 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002187 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 -07002188 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2189 // Close all the file descriptors that were dup-ed.
2190 for (size_t j=0; j<i ;j++) {
2191 close(fds[j]);
2192 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002193 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002194 }
2195
2196 if (err == NO_ERROR) {
2197 err = val.unflatten(buf, len, fds, fd_count);
2198 }
2199
2200 if (fd_count) {
2201 delete [] fds;
2202 }
2203
2204 return err;
2205}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002206const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2207{
2208 const size_t DPOS = mDataPos;
2209 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2210 const flat_binder_object* obj
2211 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2212 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002213 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002214 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002215 // the object list, so we don't want to check for it when
2216 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002217 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002218 return obj;
2219 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002220
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002221 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002222 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002223 const size_t N = mObjectsSize;
2224 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002225
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002226 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002227 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002228 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002229
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002230 // Start at the current hint position, looking for an object at
2231 // the current data position.
2232 if (opos < N) {
2233 while (opos < (N-1) && OBJS[opos] < DPOS) {
2234 opos++;
2235 }
2236 } else {
2237 opos = N-1;
2238 }
2239 if (OBJS[opos] == DPOS) {
2240 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002241 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002242 this, DPOS, opos);
2243 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002244 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002245 return obj;
2246 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002247
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002248 // Look backwards for it...
2249 while (opos > 0 && OBJS[opos] > DPOS) {
2250 opos--;
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 backward 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 }
2260 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002261 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 -07002262 this, DPOS);
2263 }
Yi Kong91635562018-06-07 14:38:36 -07002264 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002265}
2266
2267void Parcel::closeFileDescriptors()
2268{
2269 size_t i = mObjectsSize;
2270 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002271 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002272 }
2273 while (i > 0) {
2274 i--;
2275 const flat_binder_object* flat
2276 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002277 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002278 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002279 close(flat->handle);
2280 }
2281 }
2282}
2283
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002284uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002285{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002286 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002287}
2288
2289size_t Parcel::ipcDataSize() const
2290{
2291 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2292}
2293
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002294uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002295{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002296 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002297}
2298
2299size_t Parcel::ipcObjectsCount() const
2300{
2301 return mObjectsSize;
2302}
2303
2304void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002305 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002306{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002307 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002308 freeDataNoInit();
2309 mError = NO_ERROR;
2310 mData = const_cast<uint8_t*>(data);
2311 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002312 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002313 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002314 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002315 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002316 mObjectsSize = mObjectsCapacity = objectsCount;
2317 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002318 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002319 mOwner = relFunc;
2320 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002321 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002322 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002323 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002324 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002325 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002326 mObjectsSize = 0;
2327 break;
2328 }
2329 minOffset = offset + sizeof(flat_binder_object);
2330 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002331 scanForFds();
2332}
2333
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002334void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002335{
2336 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002337
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002338 if (errorCheck() != NO_ERROR) {
2339 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002340 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002341 } else if (dataSize() > 0) {
2342 const uint8_t* DATA = data();
2343 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002344 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002345 const size_t N = objectsCount();
2346 for (size_t i=0; i<N; i++) {
2347 const flat_binder_object* flat
2348 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2349 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002350 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002351 << " = " << flat->binder;
2352 }
2353 } else {
2354 to << "NULL";
2355 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002356
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002357 to << ")";
2358}
2359
2360void Parcel::releaseObjects()
2361{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002362 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002363 if (i == 0) {
2364 return;
2365 }
2366 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002367 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002368 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002369 while (i > 0) {
2370 i--;
2371 const flat_binder_object* flat
2372 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002373 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002374 }
2375}
2376
2377void Parcel::acquireObjects()
2378{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002379 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002380 if (i == 0) {
2381 return;
2382 }
2383 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002384 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002385 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002386 while (i > 0) {
2387 i--;
2388 const flat_binder_object* flat
2389 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002390 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002391 }
2392}
2393
2394void Parcel::freeData()
2395{
2396 freeDataNoInit();
2397 initState();
2398}
2399
2400void Parcel::freeDataNoInit()
2401{
2402 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002403 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002404 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002405 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2406 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002407 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002408 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002409 if (mData) {
2410 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002411 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002412 if (mDataCapacity <= gParcelGlobalAllocSize) {
2413 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2414 } else {
2415 gParcelGlobalAllocSize = 0;
2416 }
2417 if (gParcelGlobalAllocCount > 0) {
2418 gParcelGlobalAllocCount--;
2419 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002420 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002421 free(mData);
2422 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002423 if (mObjects) free(mObjects);
2424 }
2425}
2426
2427status_t Parcel::growData(size_t len)
2428{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002429 if (len > INT32_MAX) {
2430 // don't accept size_t values which may have come from an
2431 // inadvertent conversion from a negative int.
2432 return BAD_VALUE;
2433 }
2434
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435 size_t newSize = ((mDataSize+len)*3)/2;
2436 return (newSize <= mDataSize)
2437 ? (status_t) NO_MEMORY
2438 : continueWrite(newSize);
2439}
2440
2441status_t Parcel::restartWrite(size_t desired)
2442{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002443 if (desired > INT32_MAX) {
2444 // don't accept size_t values which may have come from an
2445 // inadvertent conversion from a negative int.
2446 return BAD_VALUE;
2447 }
2448
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002449 if (mOwner) {
2450 freeData();
2451 return continueWrite(desired);
2452 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002453
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002454 uint8_t* data = (uint8_t*)realloc(mData, desired);
2455 if (!data && desired > mDataCapacity) {
2456 mError = NO_MEMORY;
2457 return NO_MEMORY;
2458 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002459
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002460 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002461
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002462 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002463 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002464 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002465 gParcelGlobalAllocSize += desired;
2466 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002467 if (!mData) {
2468 gParcelGlobalAllocCount++;
2469 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002470 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002471 mData = data;
2472 mDataCapacity = desired;
2473 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002474
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002475 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002476 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2477 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2478
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002480 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002481 mObjectsSize = mObjectsCapacity = 0;
2482 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002483 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002484 mHasFds = false;
2485 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002486 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002487
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002488 return NO_ERROR;
2489}
2490
2491status_t Parcel::continueWrite(size_t desired)
2492{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002493 if (desired > INT32_MAX) {
2494 // don't accept size_t values which may have come from an
2495 // inadvertent conversion from a negative int.
2496 return BAD_VALUE;
2497 }
2498
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002499 // If shrinking, first adjust for any objects that appear
2500 // after the new data size.
2501 size_t objectsSize = mObjectsSize;
2502 if (desired < mDataSize) {
2503 if (desired == 0) {
2504 objectsSize = 0;
2505 } else {
2506 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002507 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508 break;
2509 objectsSize--;
2510 }
2511 }
2512 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002513
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002514 if (mOwner) {
2515 // If the size is going to zero, just release the owner's data.
2516 if (desired == 0) {
2517 freeData();
2518 return NO_ERROR;
2519 }
2520
2521 // If there is a different owner, we need to take
2522 // posession.
2523 uint8_t* data = (uint8_t*)malloc(desired);
2524 if (!data) {
2525 mError = NO_MEMORY;
2526 return NO_MEMORY;
2527 }
Yi Kong91635562018-06-07 14:38:36 -07002528 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002529
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002530 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002531 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002532 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002533 free(data);
2534
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002535 mError = NO_MEMORY;
2536 return NO_MEMORY;
2537 }
2538
2539 // Little hack to only acquire references on objects
2540 // we will be keeping.
2541 size_t oldObjectsSize = mObjectsSize;
2542 mObjectsSize = objectsSize;
2543 acquireObjects();
2544 mObjectsSize = oldObjectsSize;
2545 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002546
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002547 if (mData) {
2548 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2549 }
2550 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002551 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002552 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002553 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002554 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002555 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002556
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002557 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002558 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002559 gParcelGlobalAllocSize += desired;
2560 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002561 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002562
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002563 mData = data;
2564 mObjects = objects;
2565 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002566 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002567 mDataCapacity = desired;
2568 mObjectsSize = mObjectsCapacity = objectsSize;
2569 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002570 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002571
2572 } else if (mData) {
2573 if (objectsSize < mObjectsSize) {
2574 // Need to release refs on any objects we are dropping.
2575 const sp<ProcessState> proc(ProcessState::self());
2576 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2577 const flat_binder_object* flat
2578 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002579 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002580 // will need to rescan because we may have lopped off the only FDs
2581 mFdsKnown = false;
2582 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002583 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002584 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002585
2586 if (objectsSize == 0) {
2587 free(mObjects);
2588 mObjects = nullptr;
2589 } else {
2590 binder_size_t* objects =
2591 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2592 if (objects) {
2593 mObjects = objects;
2594 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002595 }
2596 mObjectsSize = objectsSize;
2597 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002598 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002599 }
2600
2601 // We own the data, so we can just do a realloc().
2602 if (desired > mDataCapacity) {
2603 uint8_t* data = (uint8_t*)realloc(mData, desired);
2604 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002605 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2606 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002607 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002608 gParcelGlobalAllocSize += desired;
2609 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002610 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002611 mData = data;
2612 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002613 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002614 mError = NO_MEMORY;
2615 return NO_MEMORY;
2616 }
2617 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002618 if (mDataSize > desired) {
2619 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002620 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002621 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002622 if (mDataPos > desired) {
2623 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002624 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002625 }
2626 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002627
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002628 } else {
2629 // This is the first data. Easy!
2630 uint8_t* data = (uint8_t*)malloc(desired);
2631 if (!data) {
2632 mError = NO_MEMORY;
2633 return NO_MEMORY;
2634 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002635
Yi Kong91635562018-06-07 14:38:36 -07002636 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002637 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002638 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002639 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002640
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002641 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002642 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002643 gParcelGlobalAllocSize += desired;
2644 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002645 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002646
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002647 mData = data;
2648 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002649 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2650 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002651 mDataCapacity = desired;
2652 }
2653
2654 return NO_ERROR;
2655}
2656
2657void Parcel::initState()
2658{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002659 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002660 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002661 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002662 mDataSize = 0;
2663 mDataCapacity = 0;
2664 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002665 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2666 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002667 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002668 mObjectsSize = 0;
2669 mObjectsCapacity = 0;
2670 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002671 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002672 mHasFds = false;
2673 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002674 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002675 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002676 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002677 mWorkSourceRequestHeaderPosition = 0;
2678 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002679
2680 // racing multiple init leads only to multiple identical write
2681 if (gMaxFds == 0) {
2682 struct rlimit result;
2683 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2684 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002685 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002686 } else {
2687 ALOGW("Unable to getrlimit: %s", strerror(errno));
2688 gMaxFds = 1024;
2689 }
2690 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002691}
2692
2693void Parcel::scanForFds() const
2694{
2695 bool hasFds = false;
2696 for (size_t i=0; i<mObjectsSize; i++) {
2697 const flat_binder_object* flat
2698 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002699 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002700 hasFds = true;
2701 break;
2702 }
2703 }
2704 mHasFds = hasFds;
2705 mFdsKnown = true;
2706}
2707
Dan Sandleraa5c2342015-04-10 10:08:45 -04002708size_t Parcel::getBlobAshmemSize() const
2709{
Adrian Roos6bb31142015-10-22 16:46:12 -07002710 // This used to return the size of all blobs that were written to ashmem, now we're returning
2711 // the ashmem currently referenced by this Parcel, which should be equivalent.
2712 // TODO: Remove method once ABI can be changed.
2713 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002714}
2715
Adrian Rooscbf37262015-10-22 16:12:53 -07002716size_t Parcel::getOpenAshmemSize() const
2717{
2718 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002719}
2720
2721// --- Parcel::Blob ---
2722
2723Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002724 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002725}
2726
2727Parcel::Blob::~Blob() {
2728 release();
2729}
2730
2731void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002732 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002733 ::munmap(mData, mSize);
2734 }
2735 clear();
2736}
2737
Jeff Brown13b16042014-11-11 16:44:25 -08002738void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2739 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002740 mData = data;
2741 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002742 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002743}
2744
2745void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002746 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002747 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002748 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002749 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002750}
2751
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002752}; // namespace android