blob: aa9d1882a88ec468ceb77c76766c68c5f1ab1474 [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 Sharkey0c1f5cb2014-12-18 10:26:57 -080076#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070077
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -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 Kongfdd8da92018-06-07 17:52:27 -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: {
Yi Kongfdd8da92018-06-07 17:52:27 -0700114 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 Kongfdd8da92018-06-07 17:52:27 -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
Yi Kongfdd8da92018-06-07 17:52:27 -0700148 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 Kongfdd8da92018-06-07 17:52:27 -0700186 if (binder != nullptr) {
Steven Moreland3085a472018-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 Kongfdd8da92018-06-07 17:52:27 -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 Moreland3085a472018-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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700488// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700489status_t Parcel::writeInterfaceToken(const String16& interface)
490{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700491 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
492 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700493 // currently the interface identification token is just its name as a string
494 return writeString16(interface);
495}
496
Mathias Agopian83c04462009-05-22 19:00:22 -0700497bool Parcel::checkInterface(IBinder* binder) const
498{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700499 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700500}
501
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700502bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700503 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700504{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700505 int32_t strictPolicy = readInt32();
Yi Kongfdd8da92018-06-07 17:52:27 -0700506 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700507 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700508 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700509 if ((threadState->getLastTransactionBinderFlags() &
510 IBinder::FLAG_ONEWAY) != 0) {
511 // For one-way calls, the callee is running entirely
512 // disconnected from the caller, so disable StrictMode entirely.
513 // Not only does disk/network usage not impact the caller, but
514 // there's no way to commuicate back any violations anyway.
515 threadState->setStrictModePolicy(0);
516 } else {
517 threadState->setStrictModePolicy(strictPolicy);
518 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700519 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700520 if (str == interface) {
521 return true;
522 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700523 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700524 String8(interface).string(), String8(str).string());
525 return false;
526 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700527}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700528
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700529size_t Parcel::objectsCount() const
530{
531 return mObjectsSize;
532}
533
534status_t Parcel::errorCheck() const
535{
536 return mError;
537}
538
539void Parcel::setError(status_t err)
540{
541 mError = err;
542}
543
544status_t Parcel::finishWrite(size_t len)
545{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700546 if (len > INT32_MAX) {
547 // don't accept size_t values which may have come from an
548 // inadvertent conversion from a negative int.
549 return BAD_VALUE;
550 }
551
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700552 //printf("Finish write of %d\n", len);
553 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700554 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700555 if (mDataPos > mDataSize) {
556 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700557 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700558 }
559 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
560 return NO_ERROR;
561}
562
563status_t Parcel::writeUnpadded(const void* data, size_t len)
564{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700565 if (len > INT32_MAX) {
566 // don't accept size_t values which may have come from an
567 // inadvertent conversion from a negative int.
568 return BAD_VALUE;
569 }
570
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700571 size_t end = mDataPos + len;
572 if (end < mDataPos) {
573 // integer overflow
574 return BAD_VALUE;
575 }
576
577 if (end <= mDataCapacity) {
578restart_write:
579 memcpy(mData+mDataPos, data, len);
580 return finishWrite(len);
581 }
582
583 status_t err = growData(len);
584 if (err == NO_ERROR) goto restart_write;
585 return err;
586}
587
588status_t Parcel::write(const void* data, 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 void* const d = writeInplace(len);
597 if (d) {
598 memcpy(d, data, len);
599 return NO_ERROR;
600 }
601 return mError;
602}
603
604void* Parcel::writeInplace(size_t len)
605{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700606 if (len > INT32_MAX) {
607 // don't accept size_t values which may have come from an
608 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -0700609 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700610 }
611
612 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700613
614 // sanity check for integer overflow
615 if (mDataPos+padded < mDataPos) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700616 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700617 }
618
619 if ((mDataPos+padded) <= mDataCapacity) {
620restart_write:
621 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
622 uint8_t* const data = mData+mDataPos;
623
624 // Need to pad at end?
625 if (padded != len) {
626#if BYTE_ORDER == BIG_ENDIAN
627 static const uint32_t mask[4] = {
628 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
629 };
630#endif
631#if BYTE_ORDER == LITTLE_ENDIAN
632 static const uint32_t mask[4] = {
633 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
634 };
635#endif
636 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
637 // *reinterpret_cast<void**>(data+padded-4));
638 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
639 }
640
641 finishWrite(padded);
642 return data;
643 }
644
645 status_t err = growData(padded);
646 if (err == NO_ERROR) goto restart_write;
Yi Kongfdd8da92018-06-07 17:52:27 -0700647 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700648}
649
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800650status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
651 const uint8_t* strData = (uint8_t*)str.data();
652 const size_t strLen= str.length();
653 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100654 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800655 return BAD_VALUE;
656 }
657
658 status_t err = writeInt32(utf16Len);
659 if (err) {
660 return err;
661 }
662
663 // Allocate enough bytes to hold our converted string and its terminating NULL.
664 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
665 if (!dst) {
666 return NO_MEMORY;
667 }
668
Sergio Girof4607432016-07-21 14:46:35 +0100669 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800670
671 return NO_ERROR;
672}
673
674status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
675 if (!str) {
676 return writeInt32(-1);
677 }
678 return writeUtf8AsUtf16(*str);
679}
680
Casey Dahlin185d3442016-02-09 11:08:35 -0800681namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800682
Casey Dahlin185d3442016-02-09 11:08:35 -0800683template<typename T>
684status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700685{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700686 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700687 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700688 status = BAD_VALUE;
689 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700690 }
691
Casey Dahlin185d3442016-02-09 11:08:35 -0800692 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700693 if (status != OK) {
694 return status;
695 }
696
Casey Dahlin185d3442016-02-09 11:08:35 -0800697 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700698 if (!data) {
699 status = BAD_VALUE;
700 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700701 }
702
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700703 memcpy(data, val.data(), val.size());
704 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700705}
706
Casey Dahlin185d3442016-02-09 11:08:35 -0800707template<typename T>
708status_t writeByteVectorInternalPtr(Parcel* parcel,
709 const std::unique_ptr<std::vector<T>>& val)
710{
711 if (!val) {
712 return parcel->writeInt32(-1);
713 }
714
715 return writeByteVectorInternal(parcel, *val);
716}
717
718} // namespace
719
720status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
721 return writeByteVectorInternal(this, val);
722}
723
724status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
725{
726 return writeByteVectorInternalPtr(this, val);
727}
728
729status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
730 return writeByteVectorInternal(this, val);
731}
732
733status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
734{
735 return writeByteVectorInternalPtr(this, val);
736}
737
Casey Dahlin451ff582015-10-19 18:12:18 -0700738status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
739{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800740 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700741}
742
Casey Dahlinb9872622015-11-25 15:09:45 -0800743status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
744{
745 return writeNullableTypedVector(val, &Parcel::writeInt32);
746}
747
Casey Dahlin451ff582015-10-19 18:12:18 -0700748status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
749{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800750 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700751}
752
Casey Dahlinb9872622015-11-25 15:09:45 -0800753status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
754{
755 return writeNullableTypedVector(val, &Parcel::writeInt64);
756}
757
Casey Dahlin451ff582015-10-19 18:12:18 -0700758status_t Parcel::writeFloatVector(const std::vector<float>& val)
759{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800760 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700761}
762
Casey Dahlinb9872622015-11-25 15:09:45 -0800763status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
764{
765 return writeNullableTypedVector(val, &Parcel::writeFloat);
766}
767
Casey Dahlin451ff582015-10-19 18:12:18 -0700768status_t Parcel::writeDoubleVector(const std::vector<double>& val)
769{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800770 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700771}
772
Casey Dahlinb9872622015-11-25 15:09:45 -0800773status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
774{
775 return writeNullableTypedVector(val, &Parcel::writeDouble);
776}
777
Casey Dahlin451ff582015-10-19 18:12:18 -0700778status_t Parcel::writeBoolVector(const std::vector<bool>& val)
779{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800780 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700781}
782
Casey Dahlinb9872622015-11-25 15:09:45 -0800783status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
784{
785 return writeNullableTypedVector(val, &Parcel::writeBool);
786}
787
Casey Dahlin451ff582015-10-19 18:12:18 -0700788status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
789{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800790 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700791}
792
Casey Dahlinb9872622015-11-25 15:09:45 -0800793status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
794{
795 return writeNullableTypedVector(val, &Parcel::writeChar);
796}
797
Casey Dahlin451ff582015-10-19 18:12:18 -0700798status_t Parcel::writeString16Vector(const std::vector<String16>& val)
799{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800800 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700801}
802
Casey Dahlinb9872622015-11-25 15:09:45 -0800803status_t Parcel::writeString16Vector(
804 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
805{
806 return writeNullableTypedVector(val, &Parcel::writeString16);
807}
808
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800809status_t Parcel::writeUtf8VectorAsUtf16Vector(
810 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
811 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
812}
813
814status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
815 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
816}
817
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700818status_t Parcel::writeInt32(int32_t val)
819{
Andreas Huber84a6d042009-08-17 13:33:27 -0700820 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700821}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800822
823status_t Parcel::writeUint32(uint32_t val)
824{
825 return writeAligned(val);
826}
827
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700828status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700829 if (len > INT32_MAX) {
830 // don't accept size_t values which may have come from an
831 // inadvertent conversion from a negative int.
832 return BAD_VALUE;
833 }
834
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700835 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700836 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700837 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700838 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700839 if (ret == NO_ERROR) {
840 ret = write(val, len * sizeof(*val));
841 }
842 return ret;
843}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700844status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700845 if (len > INT32_MAX) {
846 // don't accept size_t values which may have come from an
847 // inadvertent conversion from a negative int.
848 return BAD_VALUE;
849 }
850
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700851 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700852 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700853 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700854 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700855 if (ret == NO_ERROR) {
856 ret = write(val, len * sizeof(*val));
857 }
858 return ret;
859}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700860
Casey Dahlind6848f52015-10-15 15:44:59 -0700861status_t Parcel::writeBool(bool val)
862{
863 return writeInt32(int32_t(val));
864}
865
866status_t Parcel::writeChar(char16_t val)
867{
868 return writeInt32(int32_t(val));
869}
870
871status_t Parcel::writeByte(int8_t val)
872{
873 return writeInt32(int32_t(val));
874}
875
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700876status_t Parcel::writeInt64(int64_t val)
877{
Andreas Huber84a6d042009-08-17 13:33:27 -0700878 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700879}
880
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700881status_t Parcel::writeUint64(uint64_t val)
882{
883 return writeAligned(val);
884}
885
Serban Constantinescuf683e012013-11-05 16:53:55 +0000886status_t Parcel::writePointer(uintptr_t val)
887{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800888 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000889}
890
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700891status_t Parcel::writeFloat(float val)
892{
Andreas Huber84a6d042009-08-17 13:33:27 -0700893 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700894}
895
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800896#if defined(__mips__) && defined(__mips_hard_float)
897
898status_t Parcel::writeDouble(double val)
899{
900 union {
901 double d;
902 unsigned long long ll;
903 } u;
904 u.d = val;
905 return writeAligned(u.ll);
906}
907
908#else
909
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700910status_t Parcel::writeDouble(double val)
911{
Andreas Huber84a6d042009-08-17 13:33:27 -0700912 return writeAligned(val);
913}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700914
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800915#endif
916
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700917status_t Parcel::writeCString(const char* str)
918{
919 return write(str, strlen(str)+1);
920}
921
922status_t Parcel::writeString8(const String8& str)
923{
924 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100925 // only write string if its length is more than zero characters,
926 // as readString8 will only read if the length field is non-zero.
927 // this is slightly different from how writeString16 works.
928 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700929 err = write(str.string(), str.bytes()+1);
930 }
931 return err;
932}
933
Casey Dahlinb9872622015-11-25 15:09:45 -0800934status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
935{
936 if (!str) {
937 return writeInt32(-1);
938 }
939
940 return writeString16(*str);
941}
942
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700943status_t Parcel::writeString16(const String16& str)
944{
945 return writeString16(str.string(), str.size());
946}
947
948status_t Parcel::writeString16(const char16_t* str, size_t len)
949{
Yi Kongfdd8da92018-06-07 17:52:27 -0700950 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700951
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700952 status_t err = writeInt32(len);
953 if (err == NO_ERROR) {
954 len *= sizeof(char16_t);
955 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
956 if (data) {
957 memcpy(data, str, len);
958 *reinterpret_cast<char16_t*>(data+len) = 0;
959 return NO_ERROR;
960 }
961 err = mError;
962 }
963 return err;
964}
965
966status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
967{
968 return flatten_binder(ProcessState::self(), val, this);
969}
970
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800971status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
972{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800973 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800974}
975
Casey Dahlinb9872622015-11-25 15:09:45 -0800976status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
977{
978 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
979}
980
981status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800982 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -0800983}
984
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800985status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800986 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800987}
988
Casey Dahlinb9872622015-11-25 15:09:45 -0800989status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
990 if (!parcelable) {
991 return writeInt32(0);
992 }
993
994 return writeParcelable(*parcelable);
995}
996
Christopher Wiley97f048d2015-11-19 06:49:05 -0800997status_t Parcel::writeParcelable(const Parcelable& parcelable) {
998 status_t status = writeInt32(1); // parcelable is not null.
999 if (status != OK) {
1000 return status;
1001 }
1002 return parcelable.writeToParcel(this);
1003}
1004
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001005status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001006{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001007 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001008 return BAD_TYPE;
1009
1010 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001011 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001012 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001014 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001015 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001017 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1018 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019
1020 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001021 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022 return err;
1023 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001024 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001025 return err;
1026}
1027
Jeff Brown93ff1f92011-11-04 19:01:44 -07001028status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001029{
1030 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001031 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001032 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001033 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001034 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001035 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001036 return writeObject(obj, true);
1037}
1038
1039status_t Parcel::writeDupFileDescriptor(int fd)
1040{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001041 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001042 if (dupFd < 0) {
1043 return -errno;
1044 }
1045 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001046 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001047 close(dupFd);
1048 }
1049 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001050}
1051
Dianne Hackborn1941a402016-08-29 12:30:43 -07001052status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1053{
1054 writeInt32(0);
1055 return writeFileDescriptor(fd, takeOwnership);
1056}
1057
Ryo Hashimotobf551892018-05-31 16:58:35 +09001058status_t Parcel::writeDupParcelFileDescriptor(int fd)
1059{
1060 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1061 if (dupFd < 0) {
1062 return -errno;
1063 }
1064 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1065 if (err != OK) {
1066 close(dupFd);
1067 }
1068 return err;
1069}
1070
Christopher Wiley2cf19952016-04-11 11:09:37 -07001071status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001072 return writeDupFileDescriptor(fd.get());
1073}
1074
Christopher Wiley2cf19952016-04-11 11:09:37 -07001075status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001076 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1077}
1078
Christopher Wiley2cf19952016-04-11 11:09:37 -07001079status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001080 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1081}
1082
Jeff Brown13b16042014-11-11 16:44:25 -08001083status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001084{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001085 if (len > INT32_MAX) {
1086 // don't accept size_t values which may have come from an
1087 // inadvertent conversion from a negative int.
1088 return BAD_VALUE;
1089 }
1090
Jeff Brown13b16042014-11-11 16:44:25 -08001091 status_t status;
1092 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001093 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001094 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001095 if (status) return status;
1096
1097 void* ptr = writeInplace(len);
1098 if (!ptr) return NO_MEMORY;
1099
Jeff Brown13b16042014-11-11 16:44:25 -08001100 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001101 return NO_ERROR;
1102 }
1103
Steve Block6807e592011-10-20 11:56:00 +01001104 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001105 int fd = ashmem_create_region("Parcel Blob", len);
1106 if (fd < 0) return NO_MEMORY;
1107
1108 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1109 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001110 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001111 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -07001112 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001113 if (ptr == MAP_FAILED) {
1114 status = -errno;
1115 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001116 if (!mutableCopy) {
1117 result = ashmem_set_prot_region(fd, PROT_READ);
1118 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001119 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001120 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001121 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001122 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001123 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001124 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001125 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001126 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001127 return NO_ERROR;
1128 }
1129 }
1130 }
1131 }
1132 ::munmap(ptr, len);
1133 }
1134 ::close(fd);
1135 return status;
1136}
1137
Jeff Brown13b16042014-11-11 16:44:25 -08001138status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1139{
1140 // Must match up with what's done in writeBlob.
1141 if (!mAllowFds) return FDS_NOT_ALLOWED;
1142 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1143 if (status) return status;
1144 return writeDupFileDescriptor(fd);
1145}
1146
Mathias Agopiane1424282013-07-29 21:24:40 -07001147status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001148{
1149 status_t err;
1150
1151 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001152 const size_t len = val.getFlattenedSize();
1153 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001154
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001155 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001156 // don't accept size_t values which may have come from an
1157 // inadvertent conversion from a negative int.
1158 return BAD_VALUE;
1159 }
1160
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001161 err = this->writeInt32(len);
1162 if (err) return err;
1163
1164 err = this->writeInt32(fd_count);
1165 if (err) return err;
1166
1167 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001168 void* const buf = this->writeInplace(len);
Yi Kongfdd8da92018-06-07 17:52:27 -07001169 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001170 return BAD_VALUE;
1171
Yi Kongfdd8da92018-06-07 17:52:27 -07001172 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001173 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001174 fds = new (std::nothrow) int[fd_count];
1175 if (fds == nullptr) {
1176 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1177 return BAD_VALUE;
1178 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001179 }
1180
1181 err = val.flatten(buf, len, fds, fd_count);
1182 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1183 err = this->writeDupFileDescriptor( fds[i] );
1184 }
1185
1186 if (fd_count) {
1187 delete [] fds;
1188 }
1189
1190 return err;
1191}
1192
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001193status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1194{
1195 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1196 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1197 if (enoughData && enoughObjects) {
1198restart_write:
1199 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001200
Christopher Tate98e67d32015-06-03 18:44:15 -07001201 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001202 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001203 if (!mAllowFds) {
1204 // fail before modifying our object index
1205 return FDS_NOT_ALLOWED;
1206 }
1207 mHasFds = mFdsKnown = true;
1208 }
1209
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001210 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001211 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001212 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001213 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001214 mObjectsSize++;
1215 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001216
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001217 return finishWrite(sizeof(flat_binder_object));
1218 }
1219
1220 if (!enoughData) {
1221 const status_t err = growData(sizeof(val));
1222 if (err != NO_ERROR) return err;
1223 }
1224 if (!enoughObjects) {
1225 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001226 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001227 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07001228 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001229 mObjects = objects;
1230 mObjectsCapacity = newSize;
1231 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001232
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001233 goto restart_write;
1234}
1235
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001236status_t Parcel::writeNoException()
1237{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001238 binder::Status status;
1239 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001240}
1241
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001242status_t Parcel::validateReadData(size_t upperBound) const
1243{
1244 // Don't allow non-object reads on object data
1245 if (mObjectsSorted || mObjectsSize <= 1) {
1246data_sorted:
1247 // Expect to check only against the next object
1248 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1249 // For some reason the current read position is greater than the next object
1250 // hint. Iterate until we find the right object
1251 size_t nextObject = mNextObjectHint;
1252 do {
1253 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1254 // Requested info overlaps with an object
1255 ALOGE("Attempt to read from protected data in Parcel %p", this);
1256 return PERMISSION_DENIED;
1257 }
1258 nextObject++;
1259 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1260 mNextObjectHint = nextObject;
1261 }
1262 return NO_ERROR;
1263 }
1264 // Quickly determine if mObjects is sorted.
1265 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1266 binder_size_t* prevObj = currObj;
1267 while (currObj > mObjects) {
1268 prevObj--;
1269 if(*prevObj > *currObj) {
1270 goto data_unsorted;
1271 }
1272 currObj--;
1273 }
1274 mObjectsSorted = true;
1275 goto data_sorted;
1276
1277data_unsorted:
1278 // Insertion Sort mObjects
1279 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1280 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1281 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1282 binder_size_t temp = *iter0;
1283 binder_size_t* iter1 = iter0 - 1;
1284 while (iter1 >= mObjects && *iter1 > temp) {
1285 *(iter1 + 1) = *iter1;
1286 iter1--;
1287 }
1288 *(iter1 + 1) = temp;
1289 }
1290 mNextObjectHint = 0;
1291 mObjectsSorted = true;
1292 goto data_sorted;
1293}
1294
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001295status_t Parcel::read(void* outData, size_t len) const
1296{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001297 if (len > INT32_MAX) {
1298 // don't accept size_t values which may have come from an
1299 // inadvertent conversion from a negative int.
1300 return BAD_VALUE;
1301 }
1302
1303 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1304 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001305 if (mObjectsSize > 0) {
1306 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001307 if(err != NO_ERROR) {
1308 // Still increment the data position by the expected length
1309 mDataPos += pad_size(len);
1310 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1311 return err;
1312 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001313 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001314 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001315 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001316 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001317 return NO_ERROR;
1318 }
1319 return NOT_ENOUGH_DATA;
1320}
1321
1322const void* Parcel::readInplace(size_t len) const
1323{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001324 if (len > INT32_MAX) {
1325 // don't accept size_t values which may have come from an
1326 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -07001327 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001328 }
1329
1330 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1331 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001332 if (mObjectsSize > 0) {
1333 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001334 if(err != NO_ERROR) {
1335 // Still increment the data position by the expected length
1336 mDataPos += pad_size(len);
1337 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Xin Lif11e2bd2018-06-08 15:11:57 -07001338 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001339 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001340 }
1341
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001342 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001343 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001344 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001345 return data;
1346 }
Yi Kongfdd8da92018-06-07 17:52:27 -07001347 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001348}
1349
Andreas Huber84a6d042009-08-17 13:33:27 -07001350template<class T>
1351status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001352 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001353
1354 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001355 if (mObjectsSize > 0) {
1356 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001357 if(err != NO_ERROR) {
1358 // Still increment the data position by the expected length
1359 mDataPos += sizeof(T);
1360 return err;
1361 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001362 }
1363
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001364 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001365 mDataPos += sizeof(T);
1366 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001367 return NO_ERROR;
1368 } else {
1369 return NOT_ENOUGH_DATA;
1370 }
1371}
1372
Andreas Huber84a6d042009-08-17 13:33:27 -07001373template<class T>
1374T Parcel::readAligned() const {
1375 T result;
1376 if (readAligned(&result) != NO_ERROR) {
1377 result = 0;
1378 }
1379
1380 return result;
1381}
1382
1383template<class T>
1384status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001385 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001386
1387 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1388restart_write:
1389 *reinterpret_cast<T*>(mData+mDataPos) = val;
1390 return finishWrite(sizeof(val));
1391 }
1392
1393 status_t err = growData(sizeof(val));
1394 if (err == NO_ERROR) goto restart_write;
1395 return err;
1396}
1397
Casey Dahlin185d3442016-02-09 11:08:35 -08001398namespace {
1399
1400template<typename T>
1401status_t readByteVectorInternal(const Parcel* parcel,
1402 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001403 val->clear();
1404
1405 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001406 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001407
1408 if (status != OK) {
1409 return status;
1410 }
1411
Christopher Wiley4db672d2015-11-10 09:44:30 -08001412 if (size < 0) {
1413 status = UNEXPECTED_NULL;
1414 return status;
1415 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001416 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001417 status = BAD_VALUE;
1418 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001419 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001420
Paul Lietar433e87b2016-09-16 10:39:32 -07001421 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001422 if (!data) {
1423 status = BAD_VALUE;
1424 return status;
1425 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001426 val->reserve(size);
1427 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001428
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001429 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001430}
1431
Casey Dahlin185d3442016-02-09 11:08:35 -08001432template<typename T>
1433status_t readByteVectorInternalPtr(
1434 const Parcel* parcel,
1435 std::unique_ptr<std::vector<T>>* val) {
1436 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001437 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001438 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001439 val->reset();
1440
1441 if (status != OK || size < 0) {
1442 return status;
1443 }
1444
Casey Dahlin185d3442016-02-09 11:08:35 -08001445 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001446 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001447
Casey Dahlin185d3442016-02-09 11:08:35 -08001448 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001449
1450 if (status != OK) {
1451 val->reset();
1452 }
1453
1454 return status;
1455}
1456
Casey Dahlin185d3442016-02-09 11:08:35 -08001457} // namespace
1458
1459status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1460 return readByteVectorInternal(this, val);
1461}
1462
1463status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1464 return readByteVectorInternal(this, val);
1465}
1466
1467status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1468 return readByteVectorInternalPtr(this, val);
1469}
1470
1471status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1472 return readByteVectorInternalPtr(this, val);
1473}
1474
Casey Dahlinb9872622015-11-25 15:09:45 -08001475status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1476 return readNullableTypedVector(val, &Parcel::readInt32);
1477}
1478
Casey Dahlin451ff582015-10-19 18:12:18 -07001479status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001480 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001481}
1482
Casey Dahlinb9872622015-11-25 15:09:45 -08001483status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1484 return readNullableTypedVector(val, &Parcel::readInt64);
1485}
1486
Casey Dahlin451ff582015-10-19 18:12:18 -07001487status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001488 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001489}
1490
Casey Dahlinb9872622015-11-25 15:09:45 -08001491status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1492 return readNullableTypedVector(val, &Parcel::readFloat);
1493}
1494
Casey Dahlin451ff582015-10-19 18:12:18 -07001495status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001496 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001497}
1498
Casey Dahlinb9872622015-11-25 15:09:45 -08001499status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1500 return readNullableTypedVector(val, &Parcel::readDouble);
1501}
1502
Casey Dahlin451ff582015-10-19 18:12:18 -07001503status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001504 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001505}
1506
Casey Dahlinb9872622015-11-25 15:09:45 -08001507status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1508 const int32_t start = dataPosition();
1509 int32_t size;
1510 status_t status = readInt32(&size);
1511 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001512
Casey Dahlinb9872622015-11-25 15:09:45 -08001513 if (status != OK || size < 0) {
1514 return status;
1515 }
1516
1517 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001518 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001519
1520 status = readBoolVector(val->get());
1521
1522 if (status != OK) {
1523 val->reset();
1524 }
1525
1526 return status;
1527}
1528
1529status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001530 int32_t size;
1531 status_t status = readInt32(&size);
1532
1533 if (status != OK) {
1534 return status;
1535 }
1536
1537 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001538 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001539 }
1540
1541 val->resize(size);
1542
1543 /* C++ bool handling means a vector of bools isn't necessarily addressable
1544 * (we might use individual bits)
1545 */
Christopher Wiley97887982015-10-27 16:33:47 -07001546 bool data;
1547 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001548 status = readBool(&data);
1549 (*val)[i] = data;
1550
1551 if (status != OK) {
1552 return status;
1553 }
1554 }
1555
1556 return OK;
1557}
1558
Casey Dahlinb9872622015-11-25 15:09:45 -08001559status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1560 return readNullableTypedVector(val, &Parcel::readChar);
1561}
1562
Casey Dahlin451ff582015-10-19 18:12:18 -07001563status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001564 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001565}
1566
Casey Dahlinb9872622015-11-25 15:09:45 -08001567status_t Parcel::readString16Vector(
1568 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1569 return readNullableTypedVector(val, &Parcel::readString16);
1570}
1571
Casey Dahlin451ff582015-10-19 18:12:18 -07001572status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001573 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001574}
1575
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001576status_t Parcel::readUtf8VectorFromUtf16Vector(
1577 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1578 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1579}
1580
1581status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1582 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1583}
Casey Dahlin451ff582015-10-19 18:12:18 -07001584
Andreas Huber84a6d042009-08-17 13:33:27 -07001585status_t Parcel::readInt32(int32_t *pArg) const
1586{
1587 return readAligned(pArg);
1588}
1589
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001590int32_t Parcel::readInt32() const
1591{
Andreas Huber84a6d042009-08-17 13:33:27 -07001592 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001593}
1594
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001595status_t Parcel::readUint32(uint32_t *pArg) const
1596{
1597 return readAligned(pArg);
1598}
1599
1600uint32_t Parcel::readUint32() const
1601{
1602 return readAligned<uint32_t>();
1603}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001604
1605status_t Parcel::readInt64(int64_t *pArg) const
1606{
Andreas Huber84a6d042009-08-17 13:33:27 -07001607 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001608}
1609
1610
1611int64_t Parcel::readInt64() const
1612{
Andreas Huber84a6d042009-08-17 13:33:27 -07001613 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001614}
1615
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001616status_t Parcel::readUint64(uint64_t *pArg) const
1617{
1618 return readAligned(pArg);
1619}
1620
1621uint64_t Parcel::readUint64() const
1622{
1623 return readAligned<uint64_t>();
1624}
1625
Serban Constantinescuf683e012013-11-05 16:53:55 +00001626status_t Parcel::readPointer(uintptr_t *pArg) const
1627{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001628 status_t ret;
1629 binder_uintptr_t ptr;
1630 ret = readAligned(&ptr);
1631 if (!ret)
1632 *pArg = ptr;
1633 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001634}
1635
1636uintptr_t Parcel::readPointer() const
1637{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001638 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001639}
1640
1641
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001642status_t Parcel::readFloat(float *pArg) const
1643{
Andreas Huber84a6d042009-08-17 13:33:27 -07001644 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001645}
1646
1647
1648float Parcel::readFloat() const
1649{
Andreas Huber84a6d042009-08-17 13:33:27 -07001650 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001651}
1652
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001653#if defined(__mips__) && defined(__mips_hard_float)
1654
1655status_t Parcel::readDouble(double *pArg) const
1656{
1657 union {
1658 double d;
1659 unsigned long long ll;
1660 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001661 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001662 status_t status;
1663 status = readAligned(&u.ll);
1664 *pArg = u.d;
1665 return status;
1666}
1667
1668double Parcel::readDouble() const
1669{
1670 union {
1671 double d;
1672 unsigned long long ll;
1673 } u;
1674 u.ll = readAligned<unsigned long long>();
1675 return u.d;
1676}
1677
1678#else
1679
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001680status_t Parcel::readDouble(double *pArg) const
1681{
Andreas Huber84a6d042009-08-17 13:33:27 -07001682 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001683}
1684
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001685double Parcel::readDouble() const
1686{
Andreas Huber84a6d042009-08-17 13:33:27 -07001687 return readAligned<double>();
1688}
1689
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001690#endif
1691
Andreas Huber84a6d042009-08-17 13:33:27 -07001692status_t Parcel::readIntPtr(intptr_t *pArg) const
1693{
1694 return readAligned(pArg);
1695}
1696
1697
1698intptr_t Parcel::readIntPtr() const
1699{
1700 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001701}
1702
Casey Dahlind6848f52015-10-15 15:44:59 -07001703status_t Parcel::readBool(bool *pArg) const
1704{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001705 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001706 status_t ret = readInt32(&tmp);
1707 *pArg = (tmp != 0);
1708 return ret;
1709}
1710
1711bool Parcel::readBool() const
1712{
1713 return readInt32() != 0;
1714}
1715
1716status_t Parcel::readChar(char16_t *pArg) const
1717{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001718 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001719 status_t ret = readInt32(&tmp);
1720 *pArg = char16_t(tmp);
1721 return ret;
1722}
1723
1724char16_t Parcel::readChar() const
1725{
1726 return char16_t(readInt32());
1727}
1728
1729status_t Parcel::readByte(int8_t *pArg) const
1730{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001731 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001732 status_t ret = readInt32(&tmp);
1733 *pArg = int8_t(tmp);
1734 return ret;
1735}
1736
1737int8_t Parcel::readByte() const
1738{
1739 return int8_t(readInt32());
1740}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001741
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001742status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1743 size_t utf16Size = 0;
1744 const char16_t* src = readString16Inplace(&utf16Size);
1745 if (!src) {
1746 return UNEXPECTED_NULL;
1747 }
1748
1749 // Save ourselves the trouble, we're done.
1750 if (utf16Size == 0u) {
1751 str->clear();
1752 return NO_ERROR;
1753 }
1754
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001755 // Allow for closing '\0'
1756 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1757 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001758 return BAD_VALUE;
1759 }
1760 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001761 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001762 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001763 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1764 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001765 return NO_ERROR;
1766}
1767
1768status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1769 const int32_t start = dataPosition();
1770 int32_t size;
1771 status_t status = readInt32(&size);
1772 str->reset();
1773
1774 if (status != OK || size < 0) {
1775 return status;
1776 }
1777
1778 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001779 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001780 return readUtf8FromUtf16(str->get());
1781}
1782
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001783const char* Parcel::readCString() const
1784{
1785 const size_t avail = mDataSize-mDataPos;
1786 if (avail > 0) {
1787 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1788 // is the string's trailing NUL within the parcel's valid bounds?
1789 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1790 if (eos) {
1791 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001792 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001793 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001794 return str;
1795 }
1796 }
Yi Kongfdd8da92018-06-07 17:52:27 -07001797 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001798}
1799
1800String8 Parcel::readString8() const
1801{
Roshan Pius87b64d22016-07-18 12:51:02 -07001802 String8 retString;
1803 status_t status = readString8(&retString);
1804 if (status != OK) {
1805 // We don't care about errors here, so just return an empty string.
1806 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001807 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001808 return retString;
1809}
1810
1811status_t Parcel::readString8(String8* pArg) const
1812{
1813 int32_t size;
1814 status_t status = readInt32(&size);
1815 if (status != OK) {
1816 return status;
1817 }
1818 // watch for potential int overflow from size+1
1819 if (size < 0 || size >= INT32_MAX) {
1820 return BAD_VALUE;
1821 }
1822 // |writeString8| writes nothing for empty string.
1823 if (size == 0) {
1824 *pArg = String8();
1825 return OK;
1826 }
1827 const char* str = (const char*)readInplace(size + 1);
Yi Kongfdd8da92018-06-07 17:52:27 -07001828 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07001829 return BAD_VALUE;
1830 }
1831 pArg->setTo(str, size);
1832 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001833}
1834
1835String16 Parcel::readString16() const
1836{
1837 size_t len;
1838 const char16_t* str = readString16Inplace(&len);
1839 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001840 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001841 return String16();
1842}
1843
Casey Dahlinb9872622015-11-25 15:09:45 -08001844status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1845{
1846 const int32_t start = dataPosition();
1847 int32_t size;
1848 status_t status = readInt32(&size);
1849 pArg->reset();
1850
1851 if (status != OK || size < 0) {
1852 return status;
1853 }
1854
1855 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001856 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001857
1858 status = readString16(pArg->get());
1859
1860 if (status != OK) {
1861 pArg->reset();
1862 }
1863
1864 return status;
1865}
1866
Casey Dahlin451ff582015-10-19 18:12:18 -07001867status_t Parcel::readString16(String16* pArg) const
1868{
1869 size_t len;
1870 const char16_t* str = readString16Inplace(&len);
1871 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001872 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001873 return 0;
1874 } else {
1875 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001876 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001877 }
1878}
1879
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001880const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1881{
1882 int32_t size = readInt32();
1883 // watch for potential int overflow from size+1
1884 if (size >= 0 && size < INT32_MAX) {
1885 *outLen = size;
1886 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07001887 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001888 return str;
1889 }
1890 }
1891 *outLen = 0;
Yi Kongfdd8da92018-06-07 17:52:27 -07001892 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001893}
1894
Casey Dahlinf0c13772015-10-27 18:33:56 -07001895status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1896{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001897 status_t status = readNullableStrongBinder(val);
1898 if (status == OK && !val->get()) {
1899 status = UNEXPECTED_NULL;
1900 }
1901 return status;
1902}
1903
1904status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1905{
Casey Dahlinf0c13772015-10-27 18:33:56 -07001906 return unflatten_binder(ProcessState::self(), *this, val);
1907}
1908
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001909sp<IBinder> Parcel::readStrongBinder() const
1910{
1911 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001912 // Note that a lot of code in Android reads binders by hand with this
1913 // method, and that code has historically been ok with getting nullptr
1914 // back (while ignoring error codes).
1915 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001916 return val;
1917}
1918
Christopher Wiley97f048d2015-11-19 06:49:05 -08001919status_t Parcel::readParcelable(Parcelable* parcelable) const {
1920 int32_t have_parcelable = 0;
1921 status_t status = readInt32(&have_parcelable);
1922 if (status != OK) {
1923 return status;
1924 }
1925 if (!have_parcelable) {
1926 return UNEXPECTED_NULL;
1927 }
1928 return parcelable->readFromParcel(this);
1929}
1930
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001931int32_t Parcel::readExceptionCode() const
1932{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001933 binder::Status status;
1934 status.readFromParcel(*this);
1935 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001936}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001937
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001938native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001939{
1940 int numFds, numInts;
1941 status_t err;
1942 err = readInt32(&numFds);
Yi Kongfdd8da92018-06-07 17:52:27 -07001943 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001944 err = readInt32(&numInts);
Yi Kongfdd8da92018-06-07 17:52:27 -07001945 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001946
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001947 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001948 if (!h) {
Yi Kongfdd8da92018-06-07 17:52:27 -07001949 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001950 }
1951
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001952 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001953 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001954 if (h->data[i] < 0) {
1955 for (int j = 0; j < i; j++) {
1956 close(h->data[j]);
1957 }
1958 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07001959 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001960 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001961 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001962 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001963 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001964 native_handle_close(h);
1965 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07001966 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001967 }
1968 return h;
1969}
1970
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001971int Parcel::readFileDescriptor() const
1972{
1973 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001974
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001975 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001976 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001977 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001978
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001979 return BAD_TYPE;
1980}
1981
Dianne Hackborn1941a402016-08-29 12:30:43 -07001982int Parcel::readParcelFileDescriptor() const
1983{
1984 int32_t hasComm = readInt32();
1985 int fd = readFileDescriptor();
1986 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001987 // detach (owned by the binder driver)
1988 int comm = readFileDescriptor();
1989
1990 // warning: this must be kept in sync with:
1991 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1992 enum ParcelFileDescriptorStatus {
1993 DETACHED = 2,
1994 };
1995
1996#if BYTE_ORDER == BIG_ENDIAN
1997 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
1998#endif
1999#if BYTE_ORDER == LITTLE_ENDIAN
2000 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2001#endif
2002
2003 ssize_t written = TEMP_FAILURE_RETRY(
2004 ::write(comm, &message, sizeof(message)));
2005
2006 if (written == -1 || written != sizeof(message)) {
2007 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2008 written, strerror(errno));
2009 return BAD_TYPE;
2010 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002011 }
2012 return fd;
2013}
2014
Christopher Wiley2cf19952016-04-11 11:09:37 -07002015status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002016{
2017 int got = readFileDescriptor();
2018
2019 if (got == BAD_TYPE) {
2020 return BAD_TYPE;
2021 }
2022
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002023 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002024
2025 if (val->get() < 0) {
2026 return BAD_VALUE;
2027 }
2028
2029 return OK;
2030}
2031
Ryo Hashimotobf551892018-05-31 16:58:35 +09002032status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2033{
2034 int got = readParcelFileDescriptor();
2035
2036 if (got == BAD_TYPE) {
2037 return BAD_TYPE;
2038 }
2039
2040 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2041
2042 if (val->get() < 0) {
2043 return BAD_VALUE;
2044 }
2045
2046 return OK;
2047}
Casey Dahlin06673e32015-11-23 13:24:23 -08002048
Christopher Wiley2cf19952016-04-11 11:09:37 -07002049status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002050 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2051}
2052
Christopher Wiley2cf19952016-04-11 11:09:37 -07002053status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002054 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2055}
2056
Jeff Brown5707dbf2011-09-23 21:17:56 -07002057status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2058{
Jeff Brown13b16042014-11-11 16:44:25 -08002059 int32_t blobType;
2060 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002061 if (status) return status;
2062
Jeff Brown13b16042014-11-11 16:44:25 -08002063 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002064 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002065 const void* ptr = readInplace(len);
2066 if (!ptr) return BAD_VALUE;
2067
Jeff Brown13b16042014-11-11 16:44:25 -08002068 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002069 return NO_ERROR;
2070 }
2071
Steve Block6807e592011-10-20 11:56:00 +01002072 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002073 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002074 int fd = readFileDescriptor();
2075 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2076
Yi Kongfdd8da92018-06-07 17:52:27 -07002077 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002078 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002079 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002080
Jeff Brown13b16042014-11-11 16:44:25 -08002081 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002082 return NO_ERROR;
2083}
2084
Mathias Agopiane1424282013-07-29 21:24:40 -07002085status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002086{
2087 // size
2088 const size_t len = this->readInt32();
2089 const size_t fd_count = this->readInt32();
2090
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002091 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002092 // don't accept size_t values which may have come from an
2093 // inadvertent conversion from a negative int.
2094 return BAD_VALUE;
2095 }
2096
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002097 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002098 void const* const buf = this->readInplace(pad_size(len));
Yi Kongfdd8da92018-06-07 17:52:27 -07002099 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002100 return BAD_VALUE;
2101
Yi Kongfdd8da92018-06-07 17:52:27 -07002102 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002103 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002104 fds = new (std::nothrow) int[fd_count];
2105 if (fds == nullptr) {
2106 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2107 return BAD_VALUE;
2108 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002109 }
2110
2111 status_t err = NO_ERROR;
2112 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002113 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002114 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002115 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002116 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 -07002117 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2118 // Close all the file descriptors that were dup-ed.
2119 for (size_t j=0; j<i ;j++) {
2120 close(fds[j]);
2121 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002122 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002123 }
2124
2125 if (err == NO_ERROR) {
2126 err = val.unflatten(buf, len, fds, fd_count);
2127 }
2128
2129 if (fd_count) {
2130 delete [] fds;
2131 }
2132
2133 return err;
2134}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002135const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2136{
2137 const size_t DPOS = mDataPos;
2138 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2139 const flat_binder_object* obj
2140 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2141 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002142 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002143 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002144 // the object list, so we don't want to check for it when
2145 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002146 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002147 return obj;
2148 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002149
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002150 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002151 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002152 const size_t N = mObjectsSize;
2153 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002154
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002155 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002156 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002157 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002158
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002159 // Start at the current hint position, looking for an object at
2160 // the current data position.
2161 if (opos < N) {
2162 while (opos < (N-1) && OBJS[opos] < DPOS) {
2163 opos++;
2164 }
2165 } else {
2166 opos = N-1;
2167 }
2168 if (OBJS[opos] == DPOS) {
2169 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002170 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002171 this, DPOS, opos);
2172 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002173 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002174 return obj;
2175 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002176
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002177 // Look backwards for it...
2178 while (opos > 0 && OBJS[opos] > DPOS) {
2179 opos--;
2180 }
2181 if (OBJS[opos] == DPOS) {
2182 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002183 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002184 this, DPOS, opos);
2185 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002186 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002187 return obj;
2188 }
2189 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002190 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 -07002191 this, DPOS);
2192 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002193 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002194}
2195
2196void Parcel::closeFileDescriptors()
2197{
2198 size_t i = mObjectsSize;
2199 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002200 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002201 }
2202 while (i > 0) {
2203 i--;
2204 const flat_binder_object* flat
2205 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002206 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002207 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002208 close(flat->handle);
2209 }
2210 }
2211}
2212
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002213uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002214{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002215 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002216}
2217
2218size_t Parcel::ipcDataSize() const
2219{
2220 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2221}
2222
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002223uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002224{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002225 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002226}
2227
2228size_t Parcel::ipcObjectsCount() const
2229{
2230 return mObjectsSize;
2231}
2232
2233void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002234 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002235{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002236 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002237 freeDataNoInit();
2238 mError = NO_ERROR;
2239 mData = const_cast<uint8_t*>(data);
2240 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002241 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002242 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002243 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002244 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002245 mObjectsSize = mObjectsCapacity = objectsCount;
2246 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002247 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002248 mOwner = relFunc;
2249 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002250 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002251 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002252 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002253 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002254 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002255 mObjectsSize = 0;
2256 break;
2257 }
2258 minOffset = offset + sizeof(flat_binder_object);
2259 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002260 scanForFds();
2261}
2262
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002263void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002264{
2265 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002266
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002267 if (errorCheck() != NO_ERROR) {
2268 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002269 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002270 } else if (dataSize() > 0) {
2271 const uint8_t* DATA = data();
2272 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002273 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002274 const size_t N = objectsCount();
2275 for (size_t i=0; i<N; i++) {
2276 const flat_binder_object* flat
2277 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2278 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002279 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002280 << " = " << flat->binder;
2281 }
2282 } else {
2283 to << "NULL";
2284 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002285
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002286 to << ")";
2287}
2288
2289void Parcel::releaseObjects()
2290{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002291 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002292 if (i == 0) {
2293 return;
2294 }
2295 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002296 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002297 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002298 while (i > 0) {
2299 i--;
2300 const flat_binder_object* flat
2301 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002302 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002303 }
2304}
2305
2306void Parcel::acquireObjects()
2307{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002308 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002309 if (i == 0) {
2310 return;
2311 }
2312 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002313 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002314 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002315 while (i > 0) {
2316 i--;
2317 const flat_binder_object* flat
2318 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002319 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002320 }
2321}
2322
2323void Parcel::freeData()
2324{
2325 freeDataNoInit();
2326 initState();
2327}
2328
2329void Parcel::freeDataNoInit()
2330{
2331 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002332 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002333 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002334 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2335 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002336 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002337 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002338 if (mData) {
2339 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002340 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002341 if (mDataCapacity <= gParcelGlobalAllocSize) {
2342 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2343 } else {
2344 gParcelGlobalAllocSize = 0;
2345 }
2346 if (gParcelGlobalAllocCount > 0) {
2347 gParcelGlobalAllocCount--;
2348 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002349 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002350 free(mData);
2351 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002352 if (mObjects) free(mObjects);
2353 }
2354}
2355
2356status_t Parcel::growData(size_t len)
2357{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002358 if (len > INT32_MAX) {
2359 // don't accept size_t values which may have come from an
2360 // inadvertent conversion from a negative int.
2361 return BAD_VALUE;
2362 }
2363
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002364 size_t newSize = ((mDataSize+len)*3)/2;
2365 return (newSize <= mDataSize)
2366 ? (status_t) NO_MEMORY
2367 : continueWrite(newSize);
2368}
2369
2370status_t Parcel::restartWrite(size_t desired)
2371{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002372 if (desired > INT32_MAX) {
2373 // don't accept size_t values which may have come from an
2374 // inadvertent conversion from a negative int.
2375 return BAD_VALUE;
2376 }
2377
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002378 if (mOwner) {
2379 freeData();
2380 return continueWrite(desired);
2381 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002382
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002383 uint8_t* data = (uint8_t*)realloc(mData, desired);
2384 if (!data && desired > mDataCapacity) {
2385 mError = NO_MEMORY;
2386 return NO_MEMORY;
2387 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002388
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002389 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002390
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002391 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002392 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002393 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002394 gParcelGlobalAllocSize += desired;
2395 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002396 if (!mData) {
2397 gParcelGlobalAllocCount++;
2398 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002399 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002400 mData = data;
2401 mDataCapacity = desired;
2402 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002403
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002404 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002405 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2406 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2407
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002408 free(mObjects);
Yi Kongfdd8da92018-06-07 17:52:27 -07002409 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002410 mObjectsSize = mObjectsCapacity = 0;
2411 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002412 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002413 mHasFds = false;
2414 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002415 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002416
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002417 return NO_ERROR;
2418}
2419
2420status_t Parcel::continueWrite(size_t desired)
2421{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002422 if (desired > INT32_MAX) {
2423 // don't accept size_t values which may have come from an
2424 // inadvertent conversion from a negative int.
2425 return BAD_VALUE;
2426 }
2427
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002428 // If shrinking, first adjust for any objects that appear
2429 // after the new data size.
2430 size_t objectsSize = mObjectsSize;
2431 if (desired < mDataSize) {
2432 if (desired == 0) {
2433 objectsSize = 0;
2434 } else {
2435 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002436 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002437 break;
2438 objectsSize--;
2439 }
2440 }
2441 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002442
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002443 if (mOwner) {
2444 // If the size is going to zero, just release the owner's data.
2445 if (desired == 0) {
2446 freeData();
2447 return NO_ERROR;
2448 }
2449
2450 // If there is a different owner, we need to take
2451 // posession.
2452 uint8_t* data = (uint8_t*)malloc(desired);
2453 if (!data) {
2454 mError = NO_MEMORY;
2455 return NO_MEMORY;
2456 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002457 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002458
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002459 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002460 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002461 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002462 free(data);
2463
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002464 mError = NO_MEMORY;
2465 return NO_MEMORY;
2466 }
2467
2468 // Little hack to only acquire references on objects
2469 // we will be keeping.
2470 size_t oldObjectsSize = mObjectsSize;
2471 mObjectsSize = objectsSize;
2472 acquireObjects();
2473 mObjectsSize = oldObjectsSize;
2474 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002475
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002476 if (mData) {
2477 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2478 }
2479 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002480 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002481 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002482 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002483 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kongfdd8da92018-06-07 17:52:27 -07002484 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002485
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002486 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002487 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002488 gParcelGlobalAllocSize += desired;
2489 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002490 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002491
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002492 mData = data;
2493 mObjects = objects;
2494 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002495 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002496 mDataCapacity = desired;
2497 mObjectsSize = mObjectsCapacity = objectsSize;
2498 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002499 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002500
2501 } else if (mData) {
2502 if (objectsSize < mObjectsSize) {
2503 // Need to release refs on any objects we are dropping.
2504 const sp<ProcessState> proc(ProcessState::self());
2505 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2506 const flat_binder_object* flat
2507 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002508 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002509 // will need to rescan because we may have lopped off the only FDs
2510 mFdsKnown = false;
2511 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002512 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002514 binder_size_t* objects =
2515 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516 if (objects) {
2517 mObjects = objects;
2518 }
2519 mObjectsSize = objectsSize;
2520 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002521 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002522 }
2523
2524 // We own the data, so we can just do a realloc().
2525 if (desired > mDataCapacity) {
2526 uint8_t* data = (uint8_t*)realloc(mData, desired);
2527 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002528 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2529 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002530 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002531 gParcelGlobalAllocSize += desired;
2532 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002533 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002534 mData = data;
2535 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002536 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002537 mError = NO_MEMORY;
2538 return NO_MEMORY;
2539 }
2540 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002541 if (mDataSize > desired) {
2542 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002543 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002544 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002545 if (mDataPos > desired) {
2546 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002547 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002548 }
2549 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002550
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002551 } else {
2552 // This is the first data. Easy!
2553 uint8_t* data = (uint8_t*)malloc(desired);
2554 if (!data) {
2555 mError = NO_MEMORY;
2556 return NO_MEMORY;
2557 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002558
Yi Kongfdd8da92018-06-07 17:52:27 -07002559 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002560 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002561 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002562 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002563
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002564 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002565 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002566 gParcelGlobalAllocSize += desired;
2567 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002568 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002569
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002570 mData = data;
2571 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002572 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2573 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002574 mDataCapacity = desired;
2575 }
2576
2577 return NO_ERROR;
2578}
2579
2580void Parcel::initState()
2581{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002582 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002583 mError = NO_ERROR;
Yi Kongfdd8da92018-06-07 17:52:27 -07002584 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002585 mDataSize = 0;
2586 mDataCapacity = 0;
2587 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002588 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2589 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kongfdd8da92018-06-07 17:52:27 -07002590 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002591 mObjectsSize = 0;
2592 mObjectsCapacity = 0;
2593 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002594 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002595 mHasFds = false;
2596 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002597 mAllowFds = true;
Yi Kongfdd8da92018-06-07 17:52:27 -07002598 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002599 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002600
2601 // racing multiple init leads only to multiple identical write
2602 if (gMaxFds == 0) {
2603 struct rlimit result;
2604 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2605 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002606 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002607 } else {
2608 ALOGW("Unable to getrlimit: %s", strerror(errno));
2609 gMaxFds = 1024;
2610 }
2611 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002612}
2613
2614void Parcel::scanForFds() const
2615{
2616 bool hasFds = false;
2617 for (size_t i=0; i<mObjectsSize; i++) {
2618 const flat_binder_object* flat
2619 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002620 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002621 hasFds = true;
2622 break;
2623 }
2624 }
2625 mHasFds = hasFds;
2626 mFdsKnown = true;
2627}
2628
Dan Sandleraa5c2342015-04-10 10:08:45 -04002629size_t Parcel::getBlobAshmemSize() const
2630{
Adrian Roos6bb31142015-10-22 16:46:12 -07002631 // This used to return the size of all blobs that were written to ashmem, now we're returning
2632 // the ashmem currently referenced by this Parcel, which should be equivalent.
2633 // TODO: Remove method once ABI can be changed.
2634 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002635}
2636
Adrian Rooscbf37262015-10-22 16:12:53 -07002637size_t Parcel::getOpenAshmemSize() const
2638{
2639 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002640}
2641
2642// --- Parcel::Blob ---
2643
2644Parcel::Blob::Blob() :
Yi Kongfdd8da92018-06-07 17:52:27 -07002645 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002646}
2647
2648Parcel::Blob::~Blob() {
2649 release();
2650}
2651
2652void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002653 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002654 ::munmap(mData, mSize);
2655 }
2656 clear();
2657}
2658
Jeff Brown13b16042014-11-11 16:44:25 -08002659void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2660 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002661 mData = data;
2662 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002663 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002664}
2665
2666void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002667 mFd = -1;
Yi Kongfdd8da92018-06-07 17:52:27 -07002668 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002669 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002670 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002671}
2672
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002673}; // namespace android