blob: 51ca7b4f02801a9f67322baedb686cfaa9ac2433 [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
Steven Morelanddea3cf92019-07-16 18:06:55 -0700167status_t Parcel::finishFlattenBinder(
168 const sp<IBinder>& /*binder*/, const flat_binder_object& flat)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169{
Steven Morelanddea3cf92019-07-16 18:06:55 -0700170 status_t status = writeObject(flat, false);
171 if (status != OK) return status;
172
173 // Cannot change wire protocol w/o SM update
174 // return writeInt32(internal::Stability::get(binder.get()));
175 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700176}
177
Steven Morelanddea3cf92019-07-16 18:06:55 -0700178status_t Parcel::finishUnflattenBinder(
179 const sp<IBinder>& binder, sp<IBinder>* out) const
180{
181 // int32_t stability;
182 // Cannot change wire protocol w/o SM update
183 // status_t status = readInt32(&stability);
184 // if (status != OK) return status;
185
186 // if (!internal::Stability::check(stability, mRequiredStability)) {
187 // return BAD_TYPE;
188 // }
189
190 // status = internal::Stability::set(binder.get(), stability);
191 // if (status != OK) return status;
192
193 *out = binder;
194 return OK;
195}
196
197status_t Parcel::flattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700198{
199 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700200
Martijn Coenen2b631742017-05-05 11:16:59 -0700201 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
202 /* minimum priority for all nodes is nice 0 */
203 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
204 } else {
205 /* minimum priority for all nodes is MAX_NICE(19) */
206 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
207 }
208
Yi Kongfdd8da92018-06-07 17:52:27 -0700209 if (binder != nullptr) {
Steven Moreland3085a472018-12-26 13:59:23 -0800210 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700211 if (!local) {
212 BpBinder *proxy = binder->remoteBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700213 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000214 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700215 }
216 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700217 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800218 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700219 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800220 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700221 } else {
Steven Moreland3085a472018-12-26 13:59:23 -0800222 if (local->isRequestingSid()) {
223 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
224 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700225 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800226 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
227 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700228 }
229 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700230 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800231 obj.binder = 0;
232 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700233 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700234
Steven Morelanddea3cf92019-07-16 18:06:55 -0700235 return finishFlattenBinder(binder, obj);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700236}
237
Steven Morelanddea3cf92019-07-16 18:06:55 -0700238status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700239{
Steven Morelanddea3cf92019-07-16 18:06:55 -0700240 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700241
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700242 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700243 switch (flat->hdr.type) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700244 case BINDER_TYPE_BINDER: {
245 sp<IBinder> binder = reinterpret_cast<IBinder*>(flat->cookie);
246 return finishUnflattenBinder(binder, out);
247 }
248 case BINDER_TYPE_HANDLE: {
249 sp<IBinder> binder =
250 ProcessState::self()->getStrongProxyForHandle(flat->handle);
251 return finishUnflattenBinder(binder, out);
252 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700253 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700254 }
255 return BAD_TYPE;
256}
257
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258// ---------------------------------------------------------------------------
259
260Parcel::Parcel()
261{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800262 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700263 initState();
264}
265
266Parcel::~Parcel()
267{
268 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800269 LOG_ALLOC("Parcel %p: destroyed", this);
270}
271
272size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800273 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
274 size_t size = gParcelGlobalAllocSize;
275 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
276 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800277}
278
279size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800280 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
281 size_t count = gParcelGlobalAllocCount;
282 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
283 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700284}
285
286const uint8_t* Parcel::data() const
287{
288 return mData;
289}
290
291size_t Parcel::dataSize() const
292{
293 return (mDataSize > mDataPos ? mDataSize : mDataPos);
294}
295
296size_t Parcel::dataAvail() const
297{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700298 size_t result = dataSize() - dataPosition();
299 if (result > INT32_MAX) {
300 abort();
301 }
302 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700303}
304
305size_t Parcel::dataPosition() const
306{
307 return mDataPos;
308}
309
310size_t Parcel::dataCapacity() const
311{
312 return mDataCapacity;
313}
314
315status_t Parcel::setDataSize(size_t size)
316{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700317 if (size > INT32_MAX) {
318 // don't accept size_t values which may have come from an
319 // inadvertent conversion from a negative int.
320 return BAD_VALUE;
321 }
322
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700323 status_t err;
324 err = continueWrite(size);
325 if (err == NO_ERROR) {
326 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700327 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700328 }
329 return err;
330}
331
332void Parcel::setDataPosition(size_t pos) const
333{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700334 if (pos > INT32_MAX) {
335 // don't accept size_t values which may have come from an
336 // inadvertent conversion from a negative int.
337 abort();
338 }
339
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700340 mDataPos = pos;
341 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800342 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700343}
344
345status_t Parcel::setDataCapacity(size_t size)
346{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700347 if (size > INT32_MAX) {
348 // don't accept size_t values which may have come from an
349 // inadvertent conversion from a negative int.
350 return BAD_VALUE;
351 }
352
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700353 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700354 return NO_ERROR;
355}
356
Steven Morelanddea3cf92019-07-16 18:06:55 -0700357void Parcel::setTransactingBinder(const sp<IBinder>& binder) const {
358 mRequiredStability = internal::Stability::get(binder.get());
359}
360
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700361status_t Parcel::setData(const uint8_t* buffer, size_t len)
362{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700363 if (len > INT32_MAX) {
364 // don't accept size_t values which may have come from an
365 // inadvertent conversion from a negative int.
366 return BAD_VALUE;
367 }
368
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700369 status_t err = restartWrite(len);
370 if (err == NO_ERROR) {
371 memcpy(const_cast<uint8_t*>(data()), buffer, len);
372 mDataSize = len;
373 mFdsKnown = false;
374 }
375 return err;
376}
377
Andreas Huber51faf462011-04-13 10:21:56 -0700378status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700379{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700380 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700381 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800382 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700383 size_t size = parcel->mObjectsSize;
384 int startPos = mDataPos;
385 int firstIndex = -1, lastIndex = -2;
386
387 if (len == 0) {
388 return NO_ERROR;
389 }
390
Nick Kralevichb6b14232015-04-02 09:36:02 -0700391 if (len > INT32_MAX) {
392 // don't accept size_t values which may have come from an
393 // inadvertent conversion from a negative int.
394 return BAD_VALUE;
395 }
396
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700397 // range checks against the source parcel size
398 if ((offset > parcel->mDataSize)
399 || (len > parcel->mDataSize)
400 || (offset + len > parcel->mDataSize)) {
401 return BAD_VALUE;
402 }
403
404 // Count objects in range
405 for (int i = 0; i < (int) size; i++) {
406 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700407 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700408 if (firstIndex == -1) {
409 firstIndex = i;
410 }
411 lastIndex = i;
412 }
413 }
414 int numObjects = lastIndex - firstIndex + 1;
415
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700416 if ((mDataSize+len) > mDataCapacity) {
417 // grow data
418 err = growData(len);
419 if (err != NO_ERROR) {
420 return err;
421 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700422 }
423
424 // append data
425 memcpy(mData + mDataPos, data + offset, len);
426 mDataPos += len;
427 mDataSize += len;
428
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400429 err = NO_ERROR;
430
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700431 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200432 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700433 // grow objects
434 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700435 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700436 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800437 binder_size_t *objects =
438 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kongfdd8da92018-06-07 17:52:27 -0700439 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700440 return NO_MEMORY;
441 }
442 mObjects = objects;
443 mObjectsCapacity = newSize;
444 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700445
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700446 // append and acquire objects
447 int idx = mObjectsSize;
448 for (int i = firstIndex; i <= lastIndex; i++) {
449 size_t off = objects[i] - offset + startPos;
450 mObjects[idx++] = off;
451 mObjectsSize++;
452
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700453 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700454 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700455 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700456
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700457 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700458 // If this is a file descriptor, we need to dup it so the
459 // new Parcel now owns its own fd, and can declare that we
460 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800461 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800462 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700463 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400464 if (!mAllowFds) {
465 err = FDS_NOT_ALLOWED;
466 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700467 }
468 }
469 }
470
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400471 return err;
472}
473
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700474int Parcel::compareData(const Parcel& other) {
475 size_t size = dataSize();
476 if (size != other.dataSize()) {
477 return size < other.dataSize() ? -1 : 1;
478 }
479 return memcmp(data(), other.data(), size);
480}
481
Jeff Brown13b16042014-11-11 16:44:25 -0800482bool Parcel::allowFds() const
483{
484 return mAllowFds;
485}
486
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700487bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400488{
489 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700490 if (!allowFds) {
491 mAllowFds = false;
492 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400493 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700494}
495
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700496void Parcel::restoreAllowFds(bool lastValue)
497{
498 mAllowFds = lastValue;
499}
500
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700501bool Parcel::hasFileDescriptors() const
502{
503 if (!mFdsKnown) {
504 scanForFds();
505 }
506 return mHasFds;
507}
508
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700509// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700510status_t Parcel::writeInterfaceToken(const String16& interface)
511{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700512 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
513 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700514 // currently the interface identification token is just its name as a string
515 return writeString16(interface);
516}
517
Mathias Agopian83c04462009-05-22 19:00:22 -0700518bool Parcel::checkInterface(IBinder* binder) const
519{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700520 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700521}
522
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700523bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700524 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700525{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700526 int32_t strictPolicy = readInt32();
Yi Kongfdd8da92018-06-07 17:52:27 -0700527 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700528 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700529 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700530 if ((threadState->getLastTransactionBinderFlags() &
531 IBinder::FLAG_ONEWAY) != 0) {
532 // For one-way calls, the callee is running entirely
533 // disconnected from the caller, so disable StrictMode entirely.
534 // Not only does disk/network usage not impact the caller, but
535 // there's no way to commuicate back any violations anyway.
536 threadState->setStrictModePolicy(0);
537 } else {
538 threadState->setStrictModePolicy(strictPolicy);
539 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700540 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700541 if (str == interface) {
542 return true;
543 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700544 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700545 String8(interface).string(), String8(str).string());
546 return false;
547 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700548}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700549
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700550size_t Parcel::objectsCount() const
551{
552 return mObjectsSize;
553}
554
555status_t Parcel::errorCheck() const
556{
557 return mError;
558}
559
560void Parcel::setError(status_t err)
561{
562 mError = err;
563}
564
565status_t Parcel::finishWrite(size_t len)
566{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700567 if (len > INT32_MAX) {
568 // don't accept size_t values which may have come from an
569 // inadvertent conversion from a negative int.
570 return BAD_VALUE;
571 }
572
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700573 //printf("Finish write of %d\n", len);
574 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700575 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700576 if (mDataPos > mDataSize) {
577 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700578 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700579 }
580 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
581 return NO_ERROR;
582}
583
584status_t Parcel::writeUnpadded(const void* data, size_t len)
585{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700586 if (len > INT32_MAX) {
587 // don't accept size_t values which may have come from an
588 // inadvertent conversion from a negative int.
589 return BAD_VALUE;
590 }
591
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700592 size_t end = mDataPos + len;
593 if (end < mDataPos) {
594 // integer overflow
595 return BAD_VALUE;
596 }
597
598 if (end <= mDataCapacity) {
599restart_write:
600 memcpy(mData+mDataPos, data, len);
601 return finishWrite(len);
602 }
603
604 status_t err = growData(len);
605 if (err == NO_ERROR) goto restart_write;
606 return err;
607}
608
609status_t Parcel::write(const void* data, size_t len)
610{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700611 if (len > INT32_MAX) {
612 // don't accept size_t values which may have come from an
613 // inadvertent conversion from a negative int.
614 return BAD_VALUE;
615 }
616
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700617 void* const d = writeInplace(len);
618 if (d) {
619 memcpy(d, data, len);
620 return NO_ERROR;
621 }
622 return mError;
623}
624
625void* Parcel::writeInplace(size_t len)
626{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700627 if (len > INT32_MAX) {
628 // don't accept size_t values which may have come from an
629 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -0700630 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700631 }
632
633 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700634
635 // sanity check for integer overflow
636 if (mDataPos+padded < mDataPos) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700637 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700638 }
639
640 if ((mDataPos+padded) <= mDataCapacity) {
641restart_write:
642 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
643 uint8_t* const data = mData+mDataPos;
644
645 // Need to pad at end?
646 if (padded != len) {
647#if BYTE_ORDER == BIG_ENDIAN
648 static const uint32_t mask[4] = {
649 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
650 };
651#endif
652#if BYTE_ORDER == LITTLE_ENDIAN
653 static const uint32_t mask[4] = {
654 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
655 };
656#endif
657 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
658 // *reinterpret_cast<void**>(data+padded-4));
659 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
660 }
661
662 finishWrite(padded);
663 return data;
664 }
665
666 status_t err = growData(padded);
667 if (err == NO_ERROR) goto restart_write;
Yi Kongfdd8da92018-06-07 17:52:27 -0700668 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700669}
670
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800671status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
672 const uint8_t* strData = (uint8_t*)str.data();
673 const size_t strLen= str.length();
674 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100675 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800676 return BAD_VALUE;
677 }
678
679 status_t err = writeInt32(utf16Len);
680 if (err) {
681 return err;
682 }
683
684 // Allocate enough bytes to hold our converted string and its terminating NULL.
685 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
686 if (!dst) {
687 return NO_MEMORY;
688 }
689
Sergio Girof4607432016-07-21 14:46:35 +0100690 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800691
692 return NO_ERROR;
693}
694
695status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
696 if (!str) {
697 return writeInt32(-1);
698 }
699 return writeUtf8AsUtf16(*str);
700}
701
Casey Dahlin185d3442016-02-09 11:08:35 -0800702namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800703
Casey Dahlin185d3442016-02-09 11:08:35 -0800704template<typename T>
705status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700706{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700707 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700708 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700709 status = BAD_VALUE;
710 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700711 }
712
Casey Dahlin185d3442016-02-09 11:08:35 -0800713 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700714 if (status != OK) {
715 return status;
716 }
717
Casey Dahlin185d3442016-02-09 11:08:35 -0800718 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700719 if (!data) {
720 status = BAD_VALUE;
721 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700722 }
723
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700724 memcpy(data, val.data(), val.size());
725 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700726}
727
Casey Dahlin185d3442016-02-09 11:08:35 -0800728template<typename T>
729status_t writeByteVectorInternalPtr(Parcel* parcel,
730 const std::unique_ptr<std::vector<T>>& val)
731{
732 if (!val) {
733 return parcel->writeInt32(-1);
734 }
735
736 return writeByteVectorInternal(parcel, *val);
737}
738
739} // namespace
740
741status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
742 return writeByteVectorInternal(this, val);
743}
744
745status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
746{
747 return writeByteVectorInternalPtr(this, val);
748}
749
750status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
751 return writeByteVectorInternal(this, val);
752}
753
754status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
755{
756 return writeByteVectorInternalPtr(this, val);
757}
758
Casey Dahlin451ff582015-10-19 18:12:18 -0700759status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
760{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800761 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700762}
763
Casey Dahlinb9872622015-11-25 15:09:45 -0800764status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
765{
766 return writeNullableTypedVector(val, &Parcel::writeInt32);
767}
768
Casey Dahlin451ff582015-10-19 18:12:18 -0700769status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
770{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800771 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700772}
773
Casey Dahlinb9872622015-11-25 15:09:45 -0800774status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
775{
776 return writeNullableTypedVector(val, &Parcel::writeInt64);
777}
778
Casey Dahlin451ff582015-10-19 18:12:18 -0700779status_t Parcel::writeFloatVector(const std::vector<float>& val)
780{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800781 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700782}
783
Casey Dahlinb9872622015-11-25 15:09:45 -0800784status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
785{
786 return writeNullableTypedVector(val, &Parcel::writeFloat);
787}
788
Casey Dahlin451ff582015-10-19 18:12:18 -0700789status_t Parcel::writeDoubleVector(const std::vector<double>& val)
790{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800791 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700792}
793
Casey Dahlinb9872622015-11-25 15:09:45 -0800794status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
795{
796 return writeNullableTypedVector(val, &Parcel::writeDouble);
797}
798
Casey Dahlin451ff582015-10-19 18:12:18 -0700799status_t Parcel::writeBoolVector(const std::vector<bool>& val)
800{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800801 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700802}
803
Casey Dahlinb9872622015-11-25 15:09:45 -0800804status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
805{
806 return writeNullableTypedVector(val, &Parcel::writeBool);
807}
808
Casey Dahlin451ff582015-10-19 18:12:18 -0700809status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
810{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800811 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700812}
813
Casey Dahlinb9872622015-11-25 15:09:45 -0800814status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
815{
816 return writeNullableTypedVector(val, &Parcel::writeChar);
817}
818
Casey Dahlin451ff582015-10-19 18:12:18 -0700819status_t Parcel::writeString16Vector(const std::vector<String16>& val)
820{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800821 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700822}
823
Casey Dahlinb9872622015-11-25 15:09:45 -0800824status_t Parcel::writeString16Vector(
825 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
826{
827 return writeNullableTypedVector(val, &Parcel::writeString16);
828}
829
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800830status_t Parcel::writeUtf8VectorAsUtf16Vector(
831 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
832 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
833}
834
835status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
836 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
837}
838
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700839status_t Parcel::writeInt32(int32_t val)
840{
Andreas Huber84a6d042009-08-17 13:33:27 -0700841 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700842}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800843
844status_t Parcel::writeUint32(uint32_t val)
845{
846 return writeAligned(val);
847}
848
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700849status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700850 if (len > INT32_MAX) {
851 // don't accept size_t values which may have come from an
852 // inadvertent conversion from a negative int.
853 return BAD_VALUE;
854 }
855
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700856 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700857 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700858 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700859 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700860 if (ret == NO_ERROR) {
861 ret = write(val, len * sizeof(*val));
862 }
863 return ret;
864}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700865status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700866 if (len > INT32_MAX) {
867 // don't accept size_t values which may have come from an
868 // inadvertent conversion from a negative int.
869 return BAD_VALUE;
870 }
871
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700872 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700873 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700874 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700875 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700876 if (ret == NO_ERROR) {
877 ret = write(val, len * sizeof(*val));
878 }
879 return ret;
880}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700881
Casey Dahlind6848f52015-10-15 15:44:59 -0700882status_t Parcel::writeBool(bool val)
883{
884 return writeInt32(int32_t(val));
885}
886
887status_t Parcel::writeChar(char16_t val)
888{
889 return writeInt32(int32_t(val));
890}
891
892status_t Parcel::writeByte(int8_t val)
893{
894 return writeInt32(int32_t(val));
895}
896
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700897status_t Parcel::writeInt64(int64_t val)
898{
Andreas Huber84a6d042009-08-17 13:33:27 -0700899 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700900}
901
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700902status_t Parcel::writeUint64(uint64_t val)
903{
904 return writeAligned(val);
905}
906
Serban Constantinescuf683e012013-11-05 16:53:55 +0000907status_t Parcel::writePointer(uintptr_t val)
908{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800909 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000910}
911
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700912status_t Parcel::writeFloat(float val)
913{
Andreas Huber84a6d042009-08-17 13:33:27 -0700914 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700915}
916
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800917#if defined(__mips__) && defined(__mips_hard_float)
918
919status_t Parcel::writeDouble(double val)
920{
921 union {
922 double d;
923 unsigned long long ll;
924 } u;
925 u.d = val;
926 return writeAligned(u.ll);
927}
928
929#else
930
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700931status_t Parcel::writeDouble(double val)
932{
Andreas Huber84a6d042009-08-17 13:33:27 -0700933 return writeAligned(val);
934}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700935
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800936#endif
937
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700938status_t Parcel::writeCString(const char* str)
939{
940 return write(str, strlen(str)+1);
941}
942
943status_t Parcel::writeString8(const String8& str)
944{
945 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100946 // only write string if its length is more than zero characters,
947 // as readString8 will only read if the length field is non-zero.
948 // this is slightly different from how writeString16 works.
949 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700950 err = write(str.string(), str.bytes()+1);
951 }
952 return err;
953}
954
Casey Dahlinb9872622015-11-25 15:09:45 -0800955status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
956{
957 if (!str) {
958 return writeInt32(-1);
959 }
960
961 return writeString16(*str);
962}
963
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700964status_t Parcel::writeString16(const String16& str)
965{
966 return writeString16(str.string(), str.size());
967}
968
969status_t Parcel::writeString16(const char16_t* str, size_t len)
970{
Yi Kongfdd8da92018-06-07 17:52:27 -0700971 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700972
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700973 status_t err = writeInt32(len);
974 if (err == NO_ERROR) {
975 len *= sizeof(char16_t);
976 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
977 if (data) {
978 memcpy(data, str, len);
979 *reinterpret_cast<char16_t*>(data+len) = 0;
980 return NO_ERROR;
981 }
982 err = mError;
983 }
984 return err;
985}
986
987status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
988{
Steven Morelanddea3cf92019-07-16 18:06:55 -0700989 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700990}
991
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800992status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
993{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800994 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800995}
996
Casey Dahlinb9872622015-11-25 15:09:45 -0800997status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
998{
999 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1000}
1001
1002status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001003 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001004}
1005
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001006status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001007 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001008}
1009
Casey Dahlinb9872622015-11-25 15:09:45 -08001010status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1011 if (!parcelable) {
1012 return writeInt32(0);
1013 }
1014
1015 return writeParcelable(*parcelable);
1016}
1017
Christopher Wiley97f048d2015-11-19 06:49:05 -08001018status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1019 status_t status = writeInt32(1); // parcelable is not null.
1020 if (status != OK) {
1021 return status;
1022 }
1023 return parcelable.writeToParcel(this);
1024}
1025
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001026status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001027{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001028 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001029 return BAD_TYPE;
1030
1031 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001032 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001033 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001035 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001036 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001038 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1039 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040
1041 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001042 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043 return err;
1044 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001045 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001046 return err;
1047}
1048
Jeff Brown93ff1f92011-11-04 19:01:44 -07001049status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001050{
1051 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001052 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001053 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001054 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001055 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001056 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001057 return writeObject(obj, true);
1058}
1059
1060status_t Parcel::writeDupFileDescriptor(int fd)
1061{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001062 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001063 if (dupFd < 0) {
1064 return -errno;
1065 }
1066 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001067 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001068 close(dupFd);
1069 }
1070 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001071}
1072
Dianne Hackborn1941a402016-08-29 12:30:43 -07001073status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1074{
1075 writeInt32(0);
1076 return writeFileDescriptor(fd, takeOwnership);
1077}
1078
Ryo Hashimotobf551892018-05-31 16:58:35 +09001079status_t Parcel::writeDupParcelFileDescriptor(int fd)
1080{
1081 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1082 if (dupFd < 0) {
1083 return -errno;
1084 }
1085 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1086 if (err != OK) {
1087 close(dupFd);
1088 }
1089 return err;
1090}
1091
Christopher Wiley2cf19952016-04-11 11:09:37 -07001092status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001093 return writeDupFileDescriptor(fd.get());
1094}
1095
Christopher Wiley2cf19952016-04-11 11:09:37 -07001096status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001097 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1098}
1099
Christopher Wiley2cf19952016-04-11 11:09:37 -07001100status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001101 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1102}
1103
Jeff Brown13b16042014-11-11 16:44:25 -08001104status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001105{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001106 if (len > INT32_MAX) {
1107 // don't accept size_t values which may have come from an
1108 // inadvertent conversion from a negative int.
1109 return BAD_VALUE;
1110 }
1111
Jeff Brown13b16042014-11-11 16:44:25 -08001112 status_t status;
1113 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001114 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001115 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001116 if (status) return status;
1117
1118 void* ptr = writeInplace(len);
1119 if (!ptr) return NO_MEMORY;
1120
Jeff Brown13b16042014-11-11 16:44:25 -08001121 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001122 return NO_ERROR;
1123 }
1124
Steve Block6807e592011-10-20 11:56:00 +01001125 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001126 int fd = ashmem_create_region("Parcel Blob", len);
1127 if (fd < 0) return NO_MEMORY;
1128
1129 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1130 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001131 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001132 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -07001133 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001134 if (ptr == MAP_FAILED) {
1135 status = -errno;
1136 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001137 if (!mutableCopy) {
1138 result = ashmem_set_prot_region(fd, PROT_READ);
1139 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001140 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001141 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001142 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001143 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001144 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001145 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001146 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001147 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001148 return NO_ERROR;
1149 }
1150 }
1151 }
1152 }
1153 ::munmap(ptr, len);
1154 }
1155 ::close(fd);
1156 return status;
1157}
1158
Jeff Brown13b16042014-11-11 16:44:25 -08001159status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1160{
1161 // Must match up with what's done in writeBlob.
1162 if (!mAllowFds) return FDS_NOT_ALLOWED;
1163 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1164 if (status) return status;
1165 return writeDupFileDescriptor(fd);
1166}
1167
Mathias Agopiane1424282013-07-29 21:24:40 -07001168status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001169{
1170 status_t err;
1171
1172 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001173 const size_t len = val.getFlattenedSize();
1174 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001175
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001176 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001177 // don't accept size_t values which may have come from an
1178 // inadvertent conversion from a negative int.
1179 return BAD_VALUE;
1180 }
1181
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001182 err = this->writeInt32(len);
1183 if (err) return err;
1184
1185 err = this->writeInt32(fd_count);
1186 if (err) return err;
1187
1188 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001189 void* const buf = this->writeInplace(len);
Yi Kongfdd8da92018-06-07 17:52:27 -07001190 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001191 return BAD_VALUE;
1192
Yi Kongfdd8da92018-06-07 17:52:27 -07001193 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001194 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001195 fds = new (std::nothrow) int[fd_count];
1196 if (fds == nullptr) {
1197 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1198 return BAD_VALUE;
1199 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001200 }
1201
1202 err = val.flatten(buf, len, fds, fd_count);
1203 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1204 err = this->writeDupFileDescriptor( fds[i] );
1205 }
1206
1207 if (fd_count) {
1208 delete [] fds;
1209 }
1210
1211 return err;
1212}
1213
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001214status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1215{
1216 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1217 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1218 if (enoughData && enoughObjects) {
1219restart_write:
1220 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001221
Christopher Tate98e67d32015-06-03 18:44:15 -07001222 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001223 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001224 if (!mAllowFds) {
1225 // fail before modifying our object index
1226 return FDS_NOT_ALLOWED;
1227 }
1228 mHasFds = mFdsKnown = true;
1229 }
1230
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001231 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001232 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001233 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001234 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001235 mObjectsSize++;
1236 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001237
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001238 return finishWrite(sizeof(flat_binder_object));
1239 }
1240
1241 if (!enoughData) {
1242 const status_t err = growData(sizeof(val));
1243 if (err != NO_ERROR) return err;
1244 }
1245 if (!enoughObjects) {
1246 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001247 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001248 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07001249 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001250 mObjects = objects;
1251 mObjectsCapacity = newSize;
1252 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001253
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001254 goto restart_write;
1255}
1256
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001257status_t Parcel::writeNoException()
1258{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001259 binder::Status status;
1260 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001261}
1262
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001263status_t Parcel::validateReadData(size_t upperBound) const
1264{
1265 // Don't allow non-object reads on object data
1266 if (mObjectsSorted || mObjectsSize <= 1) {
1267data_sorted:
1268 // Expect to check only against the next object
1269 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1270 // For some reason the current read position is greater than the next object
1271 // hint. Iterate until we find the right object
1272 size_t nextObject = mNextObjectHint;
1273 do {
1274 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1275 // Requested info overlaps with an object
1276 ALOGE("Attempt to read from protected data in Parcel %p", this);
1277 return PERMISSION_DENIED;
1278 }
1279 nextObject++;
1280 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1281 mNextObjectHint = nextObject;
1282 }
1283 return NO_ERROR;
1284 }
1285 // Quickly determine if mObjects is sorted.
1286 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1287 binder_size_t* prevObj = currObj;
1288 while (currObj > mObjects) {
1289 prevObj--;
1290 if(*prevObj > *currObj) {
1291 goto data_unsorted;
1292 }
1293 currObj--;
1294 }
1295 mObjectsSorted = true;
1296 goto data_sorted;
1297
1298data_unsorted:
1299 // Insertion Sort mObjects
1300 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1301 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1302 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1303 binder_size_t temp = *iter0;
1304 binder_size_t* iter1 = iter0 - 1;
1305 while (iter1 >= mObjects && *iter1 > temp) {
1306 *(iter1 + 1) = *iter1;
1307 iter1--;
1308 }
1309 *(iter1 + 1) = temp;
1310 }
1311 mNextObjectHint = 0;
1312 mObjectsSorted = true;
1313 goto data_sorted;
1314}
1315
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001316status_t Parcel::read(void* outData, size_t len) const
1317{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001318 if (len > INT32_MAX) {
1319 // don't accept size_t values which may have come from an
1320 // inadvertent conversion from a negative int.
1321 return BAD_VALUE;
1322 }
1323
1324 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1325 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001326 if (mObjectsSize > 0) {
1327 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001328 if(err != NO_ERROR) {
1329 // Still increment the data position by the expected length
1330 mDataPos += pad_size(len);
1331 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1332 return err;
1333 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001334 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001335 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001336 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001337 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001338 return NO_ERROR;
1339 }
1340 return NOT_ENOUGH_DATA;
1341}
1342
1343const void* Parcel::readInplace(size_t len) const
1344{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001345 if (len > INT32_MAX) {
1346 // don't accept size_t values which may have come from an
1347 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -07001348 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001349 }
1350
1351 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1352 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001353 if (mObjectsSize > 0) {
1354 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001355 if(err != NO_ERROR) {
1356 // Still increment the data position by the expected length
1357 mDataPos += pad_size(len);
1358 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Xin Lif11e2bd2018-06-08 15:11:57 -07001359 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001360 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001361 }
1362
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001363 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001364 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001365 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001366 return data;
1367 }
Yi Kongfdd8da92018-06-07 17:52:27 -07001368 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001369}
1370
Andreas Huber84a6d042009-08-17 13:33:27 -07001371template<class T>
1372status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001373 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001374
1375 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001376 if (mObjectsSize > 0) {
1377 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001378 if(err != NO_ERROR) {
1379 // Still increment the data position by the expected length
1380 mDataPos += sizeof(T);
1381 return err;
1382 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001383 }
1384
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001385 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001386 mDataPos += sizeof(T);
1387 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001388 return NO_ERROR;
1389 } else {
1390 return NOT_ENOUGH_DATA;
1391 }
1392}
1393
Andreas Huber84a6d042009-08-17 13:33:27 -07001394template<class T>
1395T Parcel::readAligned() const {
1396 T result;
1397 if (readAligned(&result) != NO_ERROR) {
1398 result = 0;
1399 }
1400
1401 return result;
1402}
1403
1404template<class T>
1405status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001406 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001407
1408 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1409restart_write:
1410 *reinterpret_cast<T*>(mData+mDataPos) = val;
1411 return finishWrite(sizeof(val));
1412 }
1413
1414 status_t err = growData(sizeof(val));
1415 if (err == NO_ERROR) goto restart_write;
1416 return err;
1417}
1418
Casey Dahlin185d3442016-02-09 11:08:35 -08001419namespace {
1420
1421template<typename T>
1422status_t readByteVectorInternal(const Parcel* parcel,
1423 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001424 val->clear();
1425
1426 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001427 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001428
1429 if (status != OK) {
1430 return status;
1431 }
1432
Christopher Wiley4db672d2015-11-10 09:44:30 -08001433 if (size < 0) {
1434 status = UNEXPECTED_NULL;
1435 return status;
1436 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001437 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001438 status = BAD_VALUE;
1439 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001440 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001441
Paul Lietar433e87b2016-09-16 10:39:32 -07001442 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001443 if (!data) {
1444 status = BAD_VALUE;
1445 return status;
1446 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001447 val->reserve(size);
1448 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001449
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001450 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001451}
1452
Casey Dahlin185d3442016-02-09 11:08:35 -08001453template<typename T>
1454status_t readByteVectorInternalPtr(
1455 const Parcel* parcel,
1456 std::unique_ptr<std::vector<T>>* val) {
1457 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001458 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001459 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001460 val->reset();
1461
1462 if (status != OK || size < 0) {
1463 return status;
1464 }
1465
Casey Dahlin185d3442016-02-09 11:08:35 -08001466 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001467 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001468
Casey Dahlin185d3442016-02-09 11:08:35 -08001469 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001470
1471 if (status != OK) {
1472 val->reset();
1473 }
1474
1475 return status;
1476}
1477
Casey Dahlin185d3442016-02-09 11:08:35 -08001478} // namespace
1479
1480status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1481 return readByteVectorInternal(this, val);
1482}
1483
1484status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1485 return readByteVectorInternal(this, val);
1486}
1487
1488status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1489 return readByteVectorInternalPtr(this, val);
1490}
1491
1492status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1493 return readByteVectorInternalPtr(this, val);
1494}
1495
Casey Dahlinb9872622015-11-25 15:09:45 -08001496status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1497 return readNullableTypedVector(val, &Parcel::readInt32);
1498}
1499
Casey Dahlin451ff582015-10-19 18:12:18 -07001500status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001501 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001502}
1503
Casey Dahlinb9872622015-11-25 15:09:45 -08001504status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1505 return readNullableTypedVector(val, &Parcel::readInt64);
1506}
1507
Casey Dahlin451ff582015-10-19 18:12:18 -07001508status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001509 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001510}
1511
Casey Dahlinb9872622015-11-25 15:09:45 -08001512status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1513 return readNullableTypedVector(val, &Parcel::readFloat);
1514}
1515
Casey Dahlin451ff582015-10-19 18:12:18 -07001516status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001517 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001518}
1519
Casey Dahlinb9872622015-11-25 15:09:45 -08001520status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1521 return readNullableTypedVector(val, &Parcel::readDouble);
1522}
1523
Casey Dahlin451ff582015-10-19 18:12:18 -07001524status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001525 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001526}
1527
Casey Dahlinb9872622015-11-25 15:09:45 -08001528status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1529 const int32_t start = dataPosition();
1530 int32_t size;
1531 status_t status = readInt32(&size);
1532 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001533
Casey Dahlinb9872622015-11-25 15:09:45 -08001534 if (status != OK || size < 0) {
1535 return status;
1536 }
1537
1538 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001539 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001540
1541 status = readBoolVector(val->get());
1542
1543 if (status != OK) {
1544 val->reset();
1545 }
1546
1547 return status;
1548}
1549
1550status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001551 int32_t size;
1552 status_t status = readInt32(&size);
1553
1554 if (status != OK) {
1555 return status;
1556 }
1557
1558 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001559 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001560 }
1561
1562 val->resize(size);
1563
1564 /* C++ bool handling means a vector of bools isn't necessarily addressable
1565 * (we might use individual bits)
1566 */
Christopher Wiley97887982015-10-27 16:33:47 -07001567 bool data;
1568 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001569 status = readBool(&data);
1570 (*val)[i] = data;
1571
1572 if (status != OK) {
1573 return status;
1574 }
1575 }
1576
1577 return OK;
1578}
1579
Casey Dahlinb9872622015-11-25 15:09:45 -08001580status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1581 return readNullableTypedVector(val, &Parcel::readChar);
1582}
1583
Casey Dahlin451ff582015-10-19 18:12:18 -07001584status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001585 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001586}
1587
Casey Dahlinb9872622015-11-25 15:09:45 -08001588status_t Parcel::readString16Vector(
1589 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1590 return readNullableTypedVector(val, &Parcel::readString16);
1591}
1592
Casey Dahlin451ff582015-10-19 18:12:18 -07001593status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001594 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001595}
1596
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001597status_t Parcel::readUtf8VectorFromUtf16Vector(
1598 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1599 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1600}
1601
1602status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1603 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1604}
Casey Dahlin451ff582015-10-19 18:12:18 -07001605
Andreas Huber84a6d042009-08-17 13:33:27 -07001606status_t Parcel::readInt32(int32_t *pArg) const
1607{
1608 return readAligned(pArg);
1609}
1610
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001611int32_t Parcel::readInt32() const
1612{
Andreas Huber84a6d042009-08-17 13:33:27 -07001613 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001614}
1615
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001616status_t Parcel::readUint32(uint32_t *pArg) const
1617{
1618 return readAligned(pArg);
1619}
1620
1621uint32_t Parcel::readUint32() const
1622{
1623 return readAligned<uint32_t>();
1624}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001625
1626status_t Parcel::readInt64(int64_t *pArg) const
1627{
Andreas Huber84a6d042009-08-17 13:33:27 -07001628 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001629}
1630
1631
1632int64_t Parcel::readInt64() const
1633{
Andreas Huber84a6d042009-08-17 13:33:27 -07001634 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001635}
1636
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001637status_t Parcel::readUint64(uint64_t *pArg) const
1638{
1639 return readAligned(pArg);
1640}
1641
1642uint64_t Parcel::readUint64() const
1643{
1644 return readAligned<uint64_t>();
1645}
1646
Serban Constantinescuf683e012013-11-05 16:53:55 +00001647status_t Parcel::readPointer(uintptr_t *pArg) const
1648{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001649 status_t ret;
1650 binder_uintptr_t ptr;
1651 ret = readAligned(&ptr);
1652 if (!ret)
1653 *pArg = ptr;
1654 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001655}
1656
1657uintptr_t Parcel::readPointer() const
1658{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001659 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001660}
1661
1662
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001663status_t Parcel::readFloat(float *pArg) const
1664{
Andreas Huber84a6d042009-08-17 13:33:27 -07001665 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001666}
1667
1668
1669float Parcel::readFloat() const
1670{
Andreas Huber84a6d042009-08-17 13:33:27 -07001671 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001672}
1673
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001674#if defined(__mips__) && defined(__mips_hard_float)
1675
1676status_t Parcel::readDouble(double *pArg) const
1677{
1678 union {
1679 double d;
1680 unsigned long long ll;
1681 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001682 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001683 status_t status;
1684 status = readAligned(&u.ll);
1685 *pArg = u.d;
1686 return status;
1687}
1688
1689double Parcel::readDouble() const
1690{
1691 union {
1692 double d;
1693 unsigned long long ll;
1694 } u;
1695 u.ll = readAligned<unsigned long long>();
1696 return u.d;
1697}
1698
1699#else
1700
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001701status_t Parcel::readDouble(double *pArg) const
1702{
Andreas Huber84a6d042009-08-17 13:33:27 -07001703 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001704}
1705
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001706double Parcel::readDouble() const
1707{
Andreas Huber84a6d042009-08-17 13:33:27 -07001708 return readAligned<double>();
1709}
1710
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001711#endif
1712
Andreas Huber84a6d042009-08-17 13:33:27 -07001713status_t Parcel::readIntPtr(intptr_t *pArg) const
1714{
1715 return readAligned(pArg);
1716}
1717
1718
1719intptr_t Parcel::readIntPtr() const
1720{
1721 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001722}
1723
Casey Dahlind6848f52015-10-15 15:44:59 -07001724status_t Parcel::readBool(bool *pArg) const
1725{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001726 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001727 status_t ret = readInt32(&tmp);
1728 *pArg = (tmp != 0);
1729 return ret;
1730}
1731
1732bool Parcel::readBool() const
1733{
1734 return readInt32() != 0;
1735}
1736
1737status_t Parcel::readChar(char16_t *pArg) const
1738{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001739 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001740 status_t ret = readInt32(&tmp);
1741 *pArg = char16_t(tmp);
1742 return ret;
1743}
1744
1745char16_t Parcel::readChar() const
1746{
1747 return char16_t(readInt32());
1748}
1749
1750status_t Parcel::readByte(int8_t *pArg) const
1751{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001752 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001753 status_t ret = readInt32(&tmp);
1754 *pArg = int8_t(tmp);
1755 return ret;
1756}
1757
1758int8_t Parcel::readByte() const
1759{
1760 return int8_t(readInt32());
1761}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001762
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001763status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1764 size_t utf16Size = 0;
1765 const char16_t* src = readString16Inplace(&utf16Size);
1766 if (!src) {
1767 return UNEXPECTED_NULL;
1768 }
1769
1770 // Save ourselves the trouble, we're done.
1771 if (utf16Size == 0u) {
1772 str->clear();
1773 return NO_ERROR;
1774 }
1775
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001776 // Allow for closing '\0'
1777 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1778 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001779 return BAD_VALUE;
1780 }
1781 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001782 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001783 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001784 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1785 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001786 return NO_ERROR;
1787}
1788
1789status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1790 const int32_t start = dataPosition();
1791 int32_t size;
1792 status_t status = readInt32(&size);
1793 str->reset();
1794
1795 if (status != OK || size < 0) {
1796 return status;
1797 }
1798
1799 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001800 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001801 return readUtf8FromUtf16(str->get());
1802}
1803
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001804const char* Parcel::readCString() const
1805{
1806 const size_t avail = mDataSize-mDataPos;
1807 if (avail > 0) {
1808 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1809 // is the string's trailing NUL within the parcel's valid bounds?
1810 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1811 if (eos) {
1812 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001813 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001814 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001815 return str;
1816 }
1817 }
Yi Kongfdd8da92018-06-07 17:52:27 -07001818 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001819}
1820
1821String8 Parcel::readString8() const
1822{
Roshan Pius87b64d22016-07-18 12:51:02 -07001823 String8 retString;
1824 status_t status = readString8(&retString);
1825 if (status != OK) {
1826 // We don't care about errors here, so just return an empty string.
1827 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001828 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001829 return retString;
1830}
1831
1832status_t Parcel::readString8(String8* pArg) const
1833{
1834 int32_t size;
1835 status_t status = readInt32(&size);
1836 if (status != OK) {
1837 return status;
1838 }
1839 // watch for potential int overflow from size+1
1840 if (size < 0 || size >= INT32_MAX) {
1841 return BAD_VALUE;
1842 }
1843 // |writeString8| writes nothing for empty string.
1844 if (size == 0) {
1845 *pArg = String8();
1846 return OK;
1847 }
1848 const char* str = (const char*)readInplace(size + 1);
Yi Kongfdd8da92018-06-07 17:52:27 -07001849 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07001850 return BAD_VALUE;
1851 }
1852 pArg->setTo(str, size);
1853 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001854}
1855
1856String16 Parcel::readString16() const
1857{
1858 size_t len;
1859 const char16_t* str = readString16Inplace(&len);
1860 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001861 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001862 return String16();
1863}
1864
Casey Dahlinb9872622015-11-25 15:09:45 -08001865status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1866{
1867 const int32_t start = dataPosition();
1868 int32_t size;
1869 status_t status = readInt32(&size);
1870 pArg->reset();
1871
1872 if (status != OK || size < 0) {
1873 return status;
1874 }
1875
1876 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001877 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001878
1879 status = readString16(pArg->get());
1880
1881 if (status != OK) {
1882 pArg->reset();
1883 }
1884
1885 return status;
1886}
1887
Casey Dahlin451ff582015-10-19 18:12:18 -07001888status_t Parcel::readString16(String16* pArg) const
1889{
1890 size_t len;
1891 const char16_t* str = readString16Inplace(&len);
1892 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001893 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001894 return 0;
1895 } else {
1896 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001897 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001898 }
1899}
1900
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001901const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1902{
1903 int32_t size = readInt32();
1904 // watch for potential int overflow from size+1
1905 if (size >= 0 && size < INT32_MAX) {
1906 *outLen = size;
1907 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07001908 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001909 return str;
1910 }
1911 }
1912 *outLen = 0;
Yi Kongfdd8da92018-06-07 17:52:27 -07001913 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001914}
1915
Casey Dahlinf0c13772015-10-27 18:33:56 -07001916status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1917{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001918 status_t status = readNullableStrongBinder(val);
1919 if (status == OK && !val->get()) {
1920 status = UNEXPECTED_NULL;
1921 }
1922 return status;
1923}
1924
1925status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1926{
Steven Morelanddea3cf92019-07-16 18:06:55 -07001927 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001928}
1929
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001930sp<IBinder> Parcel::readStrongBinder() const
1931{
1932 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001933 // Note that a lot of code in Android reads binders by hand with this
1934 // method, and that code has historically been ok with getting nullptr
1935 // back (while ignoring error codes).
1936 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001937 return val;
1938}
1939
Christopher Wiley97f048d2015-11-19 06:49:05 -08001940status_t Parcel::readParcelable(Parcelable* parcelable) const {
1941 int32_t have_parcelable = 0;
1942 status_t status = readInt32(&have_parcelable);
1943 if (status != OK) {
1944 return status;
1945 }
1946 if (!have_parcelable) {
1947 return UNEXPECTED_NULL;
1948 }
1949 return parcelable->readFromParcel(this);
1950}
1951
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001952int32_t Parcel::readExceptionCode() const
1953{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001954 binder::Status status;
1955 status.readFromParcel(*this);
1956 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001957}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001958
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001959native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001960{
1961 int numFds, numInts;
1962 status_t err;
1963 err = readInt32(&numFds);
Yi Kongfdd8da92018-06-07 17:52:27 -07001964 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001965 err = readInt32(&numInts);
Yi Kongfdd8da92018-06-07 17:52:27 -07001966 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001967
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001968 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001969 if (!h) {
Yi Kongfdd8da92018-06-07 17:52:27 -07001970 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001971 }
1972
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001973 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001974 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001975 if (h->data[i] < 0) {
1976 for (int j = 0; j < i; j++) {
1977 close(h->data[j]);
1978 }
1979 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07001980 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001981 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001982 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001983 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001984 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001985 native_handle_close(h);
1986 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07001987 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001988 }
1989 return h;
1990}
1991
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001992int Parcel::readFileDescriptor() const
1993{
1994 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001995
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001996 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001997 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001998 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001999
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002000 return BAD_TYPE;
2001}
2002
Dianne Hackborn1941a402016-08-29 12:30:43 -07002003int Parcel::readParcelFileDescriptor() const
2004{
2005 int32_t hasComm = readInt32();
2006 int fd = readFileDescriptor();
2007 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002008 // detach (owned by the binder driver)
2009 int comm = readFileDescriptor();
2010
2011 // warning: this must be kept in sync with:
2012 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2013 enum ParcelFileDescriptorStatus {
2014 DETACHED = 2,
2015 };
2016
2017#if BYTE_ORDER == BIG_ENDIAN
2018 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2019#endif
2020#if BYTE_ORDER == LITTLE_ENDIAN
2021 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2022#endif
2023
2024 ssize_t written = TEMP_FAILURE_RETRY(
2025 ::write(comm, &message, sizeof(message)));
2026
2027 if (written == -1 || written != sizeof(message)) {
2028 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2029 written, strerror(errno));
2030 return BAD_TYPE;
2031 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002032 }
2033 return fd;
2034}
2035
Christopher Wiley2cf19952016-04-11 11:09:37 -07002036status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002037{
2038 int got = readFileDescriptor();
2039
2040 if (got == BAD_TYPE) {
2041 return BAD_TYPE;
2042 }
2043
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002044 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002045
2046 if (val->get() < 0) {
2047 return BAD_VALUE;
2048 }
2049
2050 return OK;
2051}
2052
Ryo Hashimotobf551892018-05-31 16:58:35 +09002053status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2054{
2055 int got = readParcelFileDescriptor();
2056
2057 if (got == BAD_TYPE) {
2058 return BAD_TYPE;
2059 }
2060
2061 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2062
2063 if (val->get() < 0) {
2064 return BAD_VALUE;
2065 }
2066
2067 return OK;
2068}
Casey Dahlin06673e32015-11-23 13:24:23 -08002069
Christopher Wiley2cf19952016-04-11 11:09:37 -07002070status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002071 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2072}
2073
Christopher Wiley2cf19952016-04-11 11:09:37 -07002074status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002075 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2076}
2077
Jeff Brown5707dbf2011-09-23 21:17:56 -07002078status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2079{
Jeff Brown13b16042014-11-11 16:44:25 -08002080 int32_t blobType;
2081 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002082 if (status) return status;
2083
Jeff Brown13b16042014-11-11 16:44:25 -08002084 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002085 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002086 const void* ptr = readInplace(len);
2087 if (!ptr) return BAD_VALUE;
2088
Jeff Brown13b16042014-11-11 16:44:25 -08002089 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002090 return NO_ERROR;
2091 }
2092
Steve Block6807e592011-10-20 11:56:00 +01002093 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002094 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002095 int fd = readFileDescriptor();
2096 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2097
Yi Kongfdd8da92018-06-07 17:52:27 -07002098 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002099 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002100 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002101
Jeff Brown13b16042014-11-11 16:44:25 -08002102 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002103 return NO_ERROR;
2104}
2105
Mathias Agopiane1424282013-07-29 21:24:40 -07002106status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002107{
2108 // size
2109 const size_t len = this->readInt32();
2110 const size_t fd_count = this->readInt32();
2111
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002112 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002113 // don't accept size_t values which may have come from an
2114 // inadvertent conversion from a negative int.
2115 return BAD_VALUE;
2116 }
2117
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002118 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002119 void const* const buf = this->readInplace(pad_size(len));
Yi Kongfdd8da92018-06-07 17:52:27 -07002120 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002121 return BAD_VALUE;
2122
Yi Kongfdd8da92018-06-07 17:52:27 -07002123 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002124 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002125 fds = new (std::nothrow) int[fd_count];
2126 if (fds == nullptr) {
2127 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2128 return BAD_VALUE;
2129 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002130 }
2131
2132 status_t err = NO_ERROR;
2133 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002134 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002135 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002136 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002137 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 -07002138 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2139 // Close all the file descriptors that were dup-ed.
2140 for (size_t j=0; j<i ;j++) {
2141 close(fds[j]);
2142 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002143 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002144 }
2145
2146 if (err == NO_ERROR) {
2147 err = val.unflatten(buf, len, fds, fd_count);
2148 }
2149
2150 if (fd_count) {
2151 delete [] fds;
2152 }
2153
2154 return err;
2155}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002156const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2157{
2158 const size_t DPOS = mDataPos;
2159 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2160 const flat_binder_object* obj
2161 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2162 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002163 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002164 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002165 // the object list, so we don't want to check for it when
2166 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002167 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002168 return obj;
2169 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002170
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002171 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002172 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002173 const size_t N = mObjectsSize;
2174 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002175
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002176 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002177 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002178 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002179
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002180 // Start at the current hint position, looking for an object at
2181 // the current data position.
2182 if (opos < N) {
2183 while (opos < (N-1) && OBJS[opos] < DPOS) {
2184 opos++;
2185 }
2186 } else {
2187 opos = N-1;
2188 }
2189 if (OBJS[opos] == DPOS) {
2190 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002191 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002192 this, DPOS, opos);
2193 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002194 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002195 return obj;
2196 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002197
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002198 // Look backwards for it...
2199 while (opos > 0 && OBJS[opos] > DPOS) {
2200 opos--;
2201 }
2202 if (OBJS[opos] == DPOS) {
2203 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002204 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002205 this, DPOS, opos);
2206 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002207 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002208 return obj;
2209 }
2210 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002211 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 -07002212 this, DPOS);
2213 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002214 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002215}
2216
2217void Parcel::closeFileDescriptors()
2218{
2219 size_t i = mObjectsSize;
2220 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002221 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002222 }
2223 while (i > 0) {
2224 i--;
2225 const flat_binder_object* flat
2226 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002227 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002228 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002229 close(flat->handle);
2230 }
2231 }
2232}
2233
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002234uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002235{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002236 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002237}
2238
2239size_t Parcel::ipcDataSize() const
2240{
2241 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2242}
2243
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002244uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002245{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002246 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002247}
2248
2249size_t Parcel::ipcObjectsCount() const
2250{
2251 return mObjectsSize;
2252}
2253
2254void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002255 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002256{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002257 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002258 freeDataNoInit();
2259 mError = NO_ERROR;
2260 mData = const_cast<uint8_t*>(data);
2261 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002262 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002263 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002264 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002265 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002266 mObjectsSize = mObjectsCapacity = objectsCount;
2267 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002268 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002269 mOwner = relFunc;
2270 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002271 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002272 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002273 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002274 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002275 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002276 mObjectsSize = 0;
2277 break;
2278 }
2279 minOffset = offset + sizeof(flat_binder_object);
2280 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002281 scanForFds();
2282}
2283
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002284void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002285{
2286 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002287
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002288 if (errorCheck() != NO_ERROR) {
2289 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002290 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002291 } else if (dataSize() > 0) {
2292 const uint8_t* DATA = data();
2293 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002294 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002295 const size_t N = objectsCount();
2296 for (size_t i=0; i<N; i++) {
2297 const flat_binder_object* flat
2298 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2299 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002300 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002301 << " = " << flat->binder;
2302 }
2303 } else {
2304 to << "NULL";
2305 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002306
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002307 to << ")";
2308}
2309
2310void Parcel::releaseObjects()
2311{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002312 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002313 if (i == 0) {
2314 return;
2315 }
2316 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002317 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002318 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002319 while (i > 0) {
2320 i--;
2321 const flat_binder_object* flat
2322 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002323 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002324 }
2325}
2326
2327void Parcel::acquireObjects()
2328{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002329 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002330 if (i == 0) {
2331 return;
2332 }
2333 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002334 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002335 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002336 while (i > 0) {
2337 i--;
2338 const flat_binder_object* flat
2339 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002340 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002341 }
2342}
2343
2344void Parcel::freeData()
2345{
2346 freeDataNoInit();
2347 initState();
2348}
2349
2350void Parcel::freeDataNoInit()
2351{
2352 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002353 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002354 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002355 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2356 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002357 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002358 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002359 if (mData) {
2360 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002361 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002362 if (mDataCapacity <= gParcelGlobalAllocSize) {
2363 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2364 } else {
2365 gParcelGlobalAllocSize = 0;
2366 }
2367 if (gParcelGlobalAllocCount > 0) {
2368 gParcelGlobalAllocCount--;
2369 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002370 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002371 free(mData);
2372 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002373 if (mObjects) free(mObjects);
2374 }
2375}
2376
2377status_t Parcel::growData(size_t len)
2378{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002379 if (len > INT32_MAX) {
2380 // don't accept size_t values which may have come from an
2381 // inadvertent conversion from a negative int.
2382 return BAD_VALUE;
2383 }
2384
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002385 size_t newSize = ((mDataSize+len)*3)/2;
2386 return (newSize <= mDataSize)
2387 ? (status_t) NO_MEMORY
2388 : continueWrite(newSize);
2389}
2390
2391status_t Parcel::restartWrite(size_t desired)
2392{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002393 if (desired > INT32_MAX) {
2394 // don't accept size_t values which may have come from an
2395 // inadvertent conversion from a negative int.
2396 return BAD_VALUE;
2397 }
2398
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002399 if (mOwner) {
2400 freeData();
2401 return continueWrite(desired);
2402 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002403
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002404 uint8_t* data = (uint8_t*)realloc(mData, desired);
2405 if (!data && desired > mDataCapacity) {
2406 mError = NO_MEMORY;
2407 return NO_MEMORY;
2408 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002409
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002410 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002411
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002412 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002413 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002414 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002415 gParcelGlobalAllocSize += desired;
2416 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002417 if (!mData) {
2418 gParcelGlobalAllocCount++;
2419 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002420 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421 mData = data;
2422 mDataCapacity = desired;
2423 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002424
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002425 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002426 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2427 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2428
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002429 free(mObjects);
Yi Kongfdd8da92018-06-07 17:52:27 -07002430 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002431 mObjectsSize = mObjectsCapacity = 0;
2432 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002433 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002434 mHasFds = false;
2435 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002436 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002437
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002438 return NO_ERROR;
2439}
2440
2441status_t Parcel::continueWrite(size_t desired)
2442{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002443 if (desired > INT32_MAX) {
2444 // don't accept size_t values which may have come from an
2445 // inadvertent conversion from a negative int.
2446 return BAD_VALUE;
2447 }
2448
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002449 // If shrinking, first adjust for any objects that appear
2450 // after the new data size.
2451 size_t objectsSize = mObjectsSize;
2452 if (desired < mDataSize) {
2453 if (desired == 0) {
2454 objectsSize = 0;
2455 } else {
2456 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002457 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002458 break;
2459 objectsSize--;
2460 }
2461 }
2462 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002463
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002464 if (mOwner) {
2465 // If the size is going to zero, just release the owner's data.
2466 if (desired == 0) {
2467 freeData();
2468 return NO_ERROR;
2469 }
2470
2471 // If there is a different owner, we need to take
2472 // posession.
2473 uint8_t* data = (uint8_t*)malloc(desired);
2474 if (!data) {
2475 mError = NO_MEMORY;
2476 return NO_MEMORY;
2477 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002478 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002479
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002480 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002481 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002482 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002483 free(data);
2484
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002485 mError = NO_MEMORY;
2486 return NO_MEMORY;
2487 }
2488
2489 // Little hack to only acquire references on objects
2490 // we will be keeping.
2491 size_t oldObjectsSize = mObjectsSize;
2492 mObjectsSize = objectsSize;
2493 acquireObjects();
2494 mObjectsSize = oldObjectsSize;
2495 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002496
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002497 if (mData) {
2498 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2499 }
2500 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002501 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002502 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002503 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002504 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kongfdd8da92018-06-07 17:52:27 -07002505 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002506
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002507 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002508 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002509 gParcelGlobalAllocSize += desired;
2510 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002511 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002512
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513 mData = data;
2514 mObjects = objects;
2515 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002516 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002517 mDataCapacity = desired;
2518 mObjectsSize = mObjectsCapacity = objectsSize;
2519 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002520 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002521
2522 } else if (mData) {
2523 if (objectsSize < mObjectsSize) {
2524 // Need to release refs on any objects we are dropping.
2525 const sp<ProcessState> proc(ProcessState::self());
2526 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2527 const flat_binder_object* flat
2528 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002529 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002530 // will need to rescan because we may have lopped off the only FDs
2531 mFdsKnown = false;
2532 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002533 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002534 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002535 binder_size_t* objects =
2536 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002537 if (objects) {
2538 mObjects = objects;
2539 }
2540 mObjectsSize = objectsSize;
2541 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002542 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002543 }
2544
2545 // We own the data, so we can just do a realloc().
2546 if (desired > mDataCapacity) {
2547 uint8_t* data = (uint8_t*)realloc(mData, desired);
2548 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002549 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2550 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002551 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002552 gParcelGlobalAllocSize += desired;
2553 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002554 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002555 mData = data;
2556 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002557 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002558 mError = NO_MEMORY;
2559 return NO_MEMORY;
2560 }
2561 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002562 if (mDataSize > desired) {
2563 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002564 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002565 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002566 if (mDataPos > desired) {
2567 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002568 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002569 }
2570 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002571
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002572 } else {
2573 // This is the first data. Easy!
2574 uint8_t* data = (uint8_t*)malloc(desired);
2575 if (!data) {
2576 mError = NO_MEMORY;
2577 return NO_MEMORY;
2578 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002579
Yi Kongfdd8da92018-06-07 17:52:27 -07002580 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002581 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002582 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002583 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002584
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002585 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002586 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002587 gParcelGlobalAllocSize += desired;
2588 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002589 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002590
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002591 mData = data;
2592 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002593 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2594 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002595 mDataCapacity = desired;
2596 }
2597
2598 return NO_ERROR;
2599}
2600
2601void Parcel::initState()
2602{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002603 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002604 mError = NO_ERROR;
Yi Kongfdd8da92018-06-07 17:52:27 -07002605 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002606 mDataSize = 0;
2607 mDataCapacity = 0;
2608 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002609 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2610 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kongfdd8da92018-06-07 17:52:27 -07002611 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002612 mObjectsSize = 0;
2613 mObjectsCapacity = 0;
2614 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002615 mObjectsSorted = false;
Steven Morelanddea3cf92019-07-16 18:06:55 -07002616 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002617 mHasFds = false;
2618 mFdsKnown = true;
Steven Morelanddea3cf92019-07-16 18:06:55 -07002619 mRequiredStability = internal::Stability::UNDECLARED;
Yi Kongfdd8da92018-06-07 17:52:27 -07002620 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002621 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002622
2623 // racing multiple init leads only to multiple identical write
2624 if (gMaxFds == 0) {
2625 struct rlimit result;
2626 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2627 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002628 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002629 } else {
2630 ALOGW("Unable to getrlimit: %s", strerror(errno));
2631 gMaxFds = 1024;
2632 }
2633 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002634}
2635
2636void Parcel::scanForFds() const
2637{
2638 bool hasFds = false;
2639 for (size_t i=0; i<mObjectsSize; i++) {
2640 const flat_binder_object* flat
2641 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002642 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002643 hasFds = true;
2644 break;
2645 }
2646 }
2647 mHasFds = hasFds;
2648 mFdsKnown = true;
2649}
2650
Dan Sandleraa5c2342015-04-10 10:08:45 -04002651size_t Parcel::getBlobAshmemSize() const
2652{
Adrian Roos6bb31142015-10-22 16:46:12 -07002653 // This used to return the size of all blobs that were written to ashmem, now we're returning
2654 // the ashmem currently referenced by this Parcel, which should be equivalent.
2655 // TODO: Remove method once ABI can be changed.
2656 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002657}
2658
Adrian Rooscbf37262015-10-22 16:12:53 -07002659size_t Parcel::getOpenAshmemSize() const
2660{
2661 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002662}
2663
2664// --- Parcel::Blob ---
2665
2666Parcel::Blob::Blob() :
Yi Kongfdd8da92018-06-07 17:52:27 -07002667 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002668}
2669
2670Parcel::Blob::~Blob() {
2671 release();
2672}
2673
2674void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002675 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002676 ::munmap(mData, mSize);
2677 }
2678 clear();
2679}
2680
Jeff Brown13b16042014-11-11 16:44:25 -08002681void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2682 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002683 mData = data;
2684 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002685 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002686}
2687
2688void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002689 mFd = -1;
Yi Kongfdd8da92018-06-07 17:52:27 -07002690 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002691 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002692 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002693}
2694
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002695}; // namespace android