blob: 1d94bd6b99cc304925a788cc5f431dd36531ac66 [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>
Steven Moreland6e5a7752019-08-05 20:30:14 -070038#include <binder/Stability.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080039#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070040#include <binder/TextOutput.h>
41
Mark Salyzynabed7f72016-01-27 08:02:48 -080042#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070043#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080044#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080046#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String8.h>
48#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049
Mathias Agopian208059f2009-05-18 15:08:03 -070050#include <private/binder/binder_module.h>
Steven Morelanda4853cd2019-07-12 15:44:37 -070051#include "Static.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080054//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080055#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080056//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070057
58// ---------------------------------------------------------------------------
59
Nick Kralevichb6b14232015-04-02 09:36:02 -070060// This macro should never be used at runtime, as a too large value
61// of s could cause an integer overflow. Instead, you should always
62// use the wrapper function pad_size()
63#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
64
65static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070066 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070067 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070068 }
69 return PAD_SIZE_UNSAFE(s);
70}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070071
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070072// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060073#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070074
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075namespace android {
76
Steven Moreland7b102262019-08-01 15:48:43 -070077// many things compile this into prebuilts on the stack
78static_assert(sizeof(Parcel) == 60 || sizeof(Parcel) == 120);
79
Dianne Hackborna4cff882014-11-13 17:07:40 -080080static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
81static size_t gParcelGlobalAllocSize = 0;
82static size_t gParcelGlobalAllocCount = 0;
83
Christopher Tatee4e0ae82016-03-24 16:03:44 -070084static size_t gMaxFds = 0;
85
Jeff Brown13b16042014-11-11 16:44:25 -080086// Maximum size of a blob to transfer in-place.
87static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
88
89enum {
90 BLOB_INPLACE = 0,
91 BLOB_ASHMEM_IMMUTABLE = 1,
92 BLOB_ASHMEM_MUTABLE = 2,
93};
94
Steven Morelandb1c81202019-04-05 18:49:55 -070095static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070096 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070097{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -070098 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070099 case BINDER_TYPE_BINDER:
100 if (obj.binder) {
101 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800102 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700103 }
104 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700105 case BINDER_TYPE_HANDLE: {
106 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700107 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
109 b->incStrong(who);
110 }
111 return;
112 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700113 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000114 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700115 // If we own an ashmem fd, keep track of how much memory it refers to.
116 int size = ashmem_get_size_region(obj.handle);
117 if (size > 0) {
118 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700119 }
120 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121 return;
122 }
123 }
124
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700125 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700126}
127
Adrian Roos6bb31142015-10-22 16:46:12 -0700128static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700129 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700130{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700131 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700132 case BINDER_TYPE_BINDER:
133 if (obj.binder) {
134 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800135 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136 }
137 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700138 case BINDER_TYPE_HANDLE: {
139 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700140 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700141 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
142 b->decStrong(who);
143 }
144 return;
145 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700146 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800147 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000148 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700149 int size = ashmem_get_size_region(obj.handle);
150 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800151 // ashmem size might have changed since last time it was accounted for, e.g.
152 // in acquire_object(). Value of *outAshmemSize is not critical since we are
153 // releasing the object anyway. Check for integer overflow condition.
154 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700155 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700156 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800157
158 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700159 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 return;
161 }
162 }
163
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700164 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165}
166
Steven Morelanda86a3562019-08-01 23:28:34 +0000167status_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 Morelanda86a3562019-08-01 23:28:34 +0000170 status_t status = writeObject(flat, false);
171 if (status != OK) return status;
172
Steven Moreland6e5a7752019-08-05 20:30:14 -0700173 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Morelanda86a3562019-08-01 23:28:34 +0000174 return writeInt32(internal::Stability::get(binder.get()));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700175}
176
Steven Morelanda86a3562019-08-01 23:28:34 +0000177status_t Parcel::finishUnflattenBinder(
178 const sp<IBinder>& binder, sp<IBinder>* out) const
179{
180 int32_t stability;
181 status_t status = readInt32(&stability);
182 if (status != OK) return status;
183
Steven Moreland05929552019-07-31 17:51:25 -0700184 status = internal::Stability::set(binder.get(), stability, true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000185 if (status != OK) return status;
186
187 *out = binder;
188 return OK;
189}
190
191status_t Parcel::flattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700192{
193 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700194
Martijn Coenen2b631742017-05-05 11:16:59 -0700195 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
196 /* minimum priority for all nodes is nice 0 */
197 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
198 } else {
199 /* minimum priority for all nodes is MAX_NICE(19) */
200 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
201 }
202
Yi Kong91635562018-06-07 14:38:36 -0700203 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800204 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205 if (!local) {
206 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700207 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000208 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700209 }
210 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700211 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800212 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700213 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800214 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700215 } else {
Steven Morelandf0212002018-12-26 13:59:23 -0800216 if (local->isRequestingSid()) {
217 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
218 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700219 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800220 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
221 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700222 }
223 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700224 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800225 obj.binder = 0;
226 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700228
Steven Morelanda86a3562019-08-01 23:28:34 +0000229 return finishFlattenBinder(binder, obj);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230}
231
Steven Morelanda86a3562019-08-01 23:28:34 +0000232status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700233{
Steven Morelanda86a3562019-08-01 23:28:34 +0000234 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700235
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700236 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700237 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000238 case BINDER_TYPE_BINDER: {
239 sp<IBinder> binder = reinterpret_cast<IBinder*>(flat->cookie);
240 return finishUnflattenBinder(binder, out);
241 }
242 case BINDER_TYPE_HANDLE: {
243 sp<IBinder> binder =
244 ProcessState::self()->getStrongProxyForHandle(flat->handle);
245 return finishUnflattenBinder(binder, out);
246 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700247 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 }
249 return BAD_TYPE;
250}
251
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700252// ---------------------------------------------------------------------------
253
254Parcel::Parcel()
255{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800256 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 initState();
258}
259
260Parcel::~Parcel()
261{
262 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800263 LOG_ALLOC("Parcel %p: destroyed", this);
264}
265
266size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800267 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
268 size_t size = gParcelGlobalAllocSize;
269 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
270 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800271}
272
273size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800274 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
275 size_t count = gParcelGlobalAllocCount;
276 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
277 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700278}
279
280const uint8_t* Parcel::data() const
281{
282 return mData;
283}
284
285size_t Parcel::dataSize() const
286{
287 return (mDataSize > mDataPos ? mDataSize : mDataPos);
288}
289
290size_t Parcel::dataAvail() const
291{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700292 size_t result = dataSize() - dataPosition();
293 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700294 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700295 }
296 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700297}
298
299size_t Parcel::dataPosition() const
300{
301 return mDataPos;
302}
303
304size_t Parcel::dataCapacity() const
305{
306 return mDataCapacity;
307}
308
309status_t Parcel::setDataSize(size_t size)
310{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700311 if (size > INT32_MAX) {
312 // don't accept size_t values which may have come from an
313 // inadvertent conversion from a negative int.
314 return BAD_VALUE;
315 }
316
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700317 status_t err;
318 err = continueWrite(size);
319 if (err == NO_ERROR) {
320 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700321 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700322 }
323 return err;
324}
325
326void Parcel::setDataPosition(size_t pos) const
327{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700328 if (pos > INT32_MAX) {
329 // don't accept size_t values which may have come from an
330 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700331 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700332 }
333
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700334 mDataPos = pos;
335 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800336 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700337}
338
339status_t Parcel::setDataCapacity(size_t size)
340{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700341 if (size > INT32_MAX) {
342 // don't accept size_t values which may have come from an
343 // inadvertent conversion from a negative int.
344 return BAD_VALUE;
345 }
346
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700347 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700348 return NO_ERROR;
349}
350
351status_t Parcel::setData(const uint8_t* buffer, size_t len)
352{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700353 if (len > INT32_MAX) {
354 // don't accept size_t values which may have come from an
355 // inadvertent conversion from a negative int.
356 return BAD_VALUE;
357 }
358
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700359 status_t err = restartWrite(len);
360 if (err == NO_ERROR) {
361 memcpy(const_cast<uint8_t*>(data()), buffer, len);
362 mDataSize = len;
363 mFdsKnown = false;
364 }
365 return err;
366}
367
Andreas Huber51faf462011-04-13 10:21:56 -0700368status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700369{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700370 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700371 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800372 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700373 size_t size = parcel->mObjectsSize;
374 int startPos = mDataPos;
375 int firstIndex = -1, lastIndex = -2;
376
377 if (len == 0) {
378 return NO_ERROR;
379 }
380
Nick Kralevichb6b14232015-04-02 09:36:02 -0700381 if (len > INT32_MAX) {
382 // don't accept size_t values which may have come from an
383 // inadvertent conversion from a negative int.
384 return BAD_VALUE;
385 }
386
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700387 // range checks against the source parcel size
388 if ((offset > parcel->mDataSize)
389 || (len > parcel->mDataSize)
390 || (offset + len > parcel->mDataSize)) {
391 return BAD_VALUE;
392 }
393
394 // Count objects in range
395 for (int i = 0; i < (int) size; i++) {
396 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700397 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700398 if (firstIndex == -1) {
399 firstIndex = i;
400 }
401 lastIndex = i;
402 }
403 }
404 int numObjects = lastIndex - firstIndex + 1;
405
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700406 if ((mDataSize+len) > mDataCapacity) {
407 // grow data
408 err = growData(len);
409 if (err != NO_ERROR) {
410 return err;
411 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700412 }
413
414 // append data
415 memcpy(mData + mDataPos, data + offset, len);
416 mDataPos += len;
417 mDataSize += len;
418
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400419 err = NO_ERROR;
420
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700421 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200422 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700423 // grow objects
424 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700425 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700426 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800427 binder_size_t *objects =
428 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700429 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700430 return NO_MEMORY;
431 }
432 mObjects = objects;
433 mObjectsCapacity = newSize;
434 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700435
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700436 // append and acquire objects
437 int idx = mObjectsSize;
438 for (int i = firstIndex; i <= lastIndex; i++) {
439 size_t off = objects[i] - offset + startPos;
440 mObjects[idx++] = off;
441 mObjectsSize++;
442
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700443 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700444 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700445 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700446
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700447 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700448 // If this is a file descriptor, we need to dup it so the
449 // new Parcel now owns its own fd, and can declare that we
450 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800451 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800452 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700453 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400454 if (!mAllowFds) {
455 err = FDS_NOT_ALLOWED;
456 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700457 }
458 }
459 }
460
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400461 return err;
462}
463
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700464int Parcel::compareData(const Parcel& other) {
465 size_t size = dataSize();
466 if (size != other.dataSize()) {
467 return size < other.dataSize() ? -1 : 1;
468 }
469 return memcmp(data(), other.data(), size);
470}
471
Jeff Brown13b16042014-11-11 16:44:25 -0800472bool Parcel::allowFds() const
473{
474 return mAllowFds;
475}
476
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700477bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400478{
479 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700480 if (!allowFds) {
481 mAllowFds = false;
482 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400483 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700484}
485
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700486void Parcel::restoreAllowFds(bool lastValue)
487{
488 mAllowFds = lastValue;
489}
490
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700491bool Parcel::hasFileDescriptors() const
492{
493 if (!mFdsKnown) {
494 scanForFds();
495 }
496 return mHasFds;
497}
498
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000499void Parcel::updateWorkSourceRequestHeaderPosition() const {
500 // Only update the request headers once. We only want to point
501 // to the first headers read/written.
502 if (!mRequestHeaderPresent) {
503 mWorkSourceRequestHeaderPosition = dataPosition();
504 mRequestHeaderPresent = true;
505 }
506}
507
Jooyung Han1b228f42020-01-30 13:41:12 +0900508#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700509constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
510#else
511constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
512#endif
513
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700514// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700515status_t Parcel::writeInterfaceToken(const String16& interface)
516{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000517 const IPCThreadState* threadState = IPCThreadState::self();
518 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000519 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000520 writeInt32(threadState->shouldPropagateWorkSource() ?
521 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700522 writeInt32(kHeader);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700523 // currently the interface identification token is just its name as a string
524 return writeString16(interface);
525}
526
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000527bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
528{
529 if (!mRequestHeaderPresent) {
530 return false;
531 }
532
533 const size_t initialPosition = dataPosition();
534 setDataPosition(mWorkSourceRequestHeaderPosition);
535 status_t err = writeInt32(uid);
536 setDataPosition(initialPosition);
537 return err == NO_ERROR;
538}
539
Steven Morelandf1b1e492019-05-06 15:05:13 -0700540uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000541{
542 if (!mRequestHeaderPresent) {
543 return IPCThreadState::kUnsetWorkSource;
544 }
545
546 const size_t initialPosition = dataPosition();
547 setDataPosition(mWorkSourceRequestHeaderPosition);
548 uid_t uid = readInt32();
549 setDataPosition(initialPosition);
550 return uid;
551}
552
Mathias Agopian83c04462009-05-22 19:00:22 -0700553bool Parcel::checkInterface(IBinder* binder) const
554{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700555 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700556}
557
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700558bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700559 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700560{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700561 return enforceInterface(interface.string(), interface.size(), threadState);
562}
563
564bool Parcel::enforceInterface(const char16_t* interface,
565 size_t len,
566 IPCThreadState* threadState) const
567{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100568 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700569 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700570 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700571 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700572 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700573 if ((threadState->getLastTransactionBinderFlags() &
574 IBinder::FLAG_ONEWAY) != 0) {
575 // For one-way calls, the callee is running entirely
576 // disconnected from the caller, so disable StrictMode entirely.
577 // Not only does disk/network usage not impact the caller, but
578 // there's no way to commuicate back any violations anyway.
579 threadState->setStrictModePolicy(0);
580 } else {
581 threadState->setStrictModePolicy(strictPolicy);
582 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100583 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000584 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100585 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000586 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700587 // vendor header
588 int32_t header = readInt32();
589 if (header != kHeader) {
590 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
591 return false;
592 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100593 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700594 size_t parcel_interface_len;
595 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
596 if (len == parcel_interface_len &&
597 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700598 return true;
599 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700600 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700601 String8(interface, len).string(),
602 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700603 return false;
604 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700605}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700606
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700607size_t Parcel::objectsCount() const
608{
609 return mObjectsSize;
610}
611
612status_t Parcel::errorCheck() const
613{
614 return mError;
615}
616
617void Parcel::setError(status_t err)
618{
619 mError = err;
620}
621
622status_t Parcel::finishWrite(size_t len)
623{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700624 if (len > INT32_MAX) {
625 // don't accept size_t values which may have come from an
626 // inadvertent conversion from a negative int.
627 return BAD_VALUE;
628 }
629
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700630 //printf("Finish write of %d\n", len);
631 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700632 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700633 if (mDataPos > mDataSize) {
634 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700635 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700636 }
637 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
638 return NO_ERROR;
639}
640
641status_t Parcel::writeUnpadded(const void* data, size_t len)
642{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700643 if (len > INT32_MAX) {
644 // don't accept size_t values which may have come from an
645 // inadvertent conversion from a negative int.
646 return BAD_VALUE;
647 }
648
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700649 size_t end = mDataPos + len;
650 if (end < mDataPos) {
651 // integer overflow
652 return BAD_VALUE;
653 }
654
655 if (end <= mDataCapacity) {
656restart_write:
657 memcpy(mData+mDataPos, data, len);
658 return finishWrite(len);
659 }
660
661 status_t err = growData(len);
662 if (err == NO_ERROR) goto restart_write;
663 return err;
664}
665
666status_t Parcel::write(const void* data, size_t len)
667{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700668 if (len > INT32_MAX) {
669 // don't accept size_t values which may have come from an
670 // inadvertent conversion from a negative int.
671 return BAD_VALUE;
672 }
673
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700674 void* const d = writeInplace(len);
675 if (d) {
676 memcpy(d, data, len);
677 return NO_ERROR;
678 }
679 return mError;
680}
681
682void* Parcel::writeInplace(size_t len)
683{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700684 if (len > INT32_MAX) {
685 // don't accept size_t values which may have come from an
686 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700687 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700688 }
689
690 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700691
692 // sanity check for integer overflow
693 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700694 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700695 }
696
697 if ((mDataPos+padded) <= mDataCapacity) {
698restart_write:
699 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
700 uint8_t* const data = mData+mDataPos;
701
702 // Need to pad at end?
703 if (padded != len) {
704#if BYTE_ORDER == BIG_ENDIAN
705 static const uint32_t mask[4] = {
706 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
707 };
708#endif
709#if BYTE_ORDER == LITTLE_ENDIAN
710 static const uint32_t mask[4] = {
711 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
712 };
713#endif
714 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
715 // *reinterpret_cast<void**>(data+padded-4));
716 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
717 }
718
719 finishWrite(padded);
720 return data;
721 }
722
723 status_t err = growData(padded);
724 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700725 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700726}
727
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800728status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
729 const uint8_t* strData = (uint8_t*)str.data();
730 const size_t strLen= str.length();
731 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100732 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800733 return BAD_VALUE;
734 }
735
736 status_t err = writeInt32(utf16Len);
737 if (err) {
738 return err;
739 }
740
741 // Allocate enough bytes to hold our converted string and its terminating NULL.
742 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
743 if (!dst) {
744 return NO_MEMORY;
745 }
746
Sergio Girof4607432016-07-21 14:46:35 +0100747 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800748
749 return NO_ERROR;
750}
751
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900752status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) {
753 if (!str) {
754 return writeInt32(-1);
755 }
756 return writeUtf8AsUtf16(*str);
757}
758
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800759status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
760 if (!str) {
761 return writeInt32(-1);
762 }
763 return writeUtf8AsUtf16(*str);
764}
765
Daniel Normand0337ef2019-09-20 15:46:03 -0700766status_t Parcel::writeByteVectorInternal(const int8_t* data, size_t size) {
767 if (size > std::numeric_limits<int32_t>::max()) {
768 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -0700769 }
770
Daniel Normand0337ef2019-09-20 15:46:03 -0700771 status_t status = writeInt32(size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700772 if (status != OK) {
773 return status;
774 }
775
Daniel Normand0337ef2019-09-20 15:46:03 -0700776 return write(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700777}
778
Casey Dahlin185d3442016-02-09 11:08:35 -0800779status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700780 return writeByteVectorInternal(val.data(), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800781}
782
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900783status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val)
784{
785 if (!val) return writeInt32(-1);
786 return writeByteVectorInternal(val->data(), val->size());
787}
788
Casey Dahlin185d3442016-02-09 11:08:35 -0800789status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
790{
Daniel Normand0337ef2019-09-20 15:46:03 -0700791 if (!val) return writeInt32(-1);
792 return writeByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800793}
794
795status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700796 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val.data()), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800797}
798
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900799status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val)
800{
801 if (!val) return writeInt32(-1);
802 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
803}
804
Casey Dahlin185d3442016-02-09 11:08:35 -0800805status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
806{
Daniel Normand0337ef2019-09-20 15:46:03 -0700807 if (!val) return writeInt32(-1);
808 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800809}
810
Casey Dahlin451ff582015-10-19 18:12:18 -0700811status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
812{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800813 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700814}
815
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900816status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val)
817{
818 return writeNullableTypedVector(val, &Parcel::writeInt32);
819}
820
Casey Dahlinb9872622015-11-25 15:09:45 -0800821status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
822{
823 return writeNullableTypedVector(val, &Parcel::writeInt32);
824}
825
Casey Dahlin451ff582015-10-19 18:12:18 -0700826status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
827{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800828 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700829}
830
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900831status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val)
832{
833 return writeNullableTypedVector(val, &Parcel::writeInt64);
834}
835
Casey Dahlinb9872622015-11-25 15:09:45 -0800836status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
837{
838 return writeNullableTypedVector(val, &Parcel::writeInt64);
839}
840
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800841status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
842{
843 return writeTypedVector(val, &Parcel::writeUint64);
844}
845
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900846status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val)
847{
848 return writeNullableTypedVector(val, &Parcel::writeUint64);
849}
850
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800851status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
852{
853 return writeNullableTypedVector(val, &Parcel::writeUint64);
854}
855
Casey Dahlin451ff582015-10-19 18:12:18 -0700856status_t Parcel::writeFloatVector(const std::vector<float>& val)
857{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800858 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700859}
860
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900861status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val)
862{
863 return writeNullableTypedVector(val, &Parcel::writeFloat);
864}
865
Casey Dahlinb9872622015-11-25 15:09:45 -0800866status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
867{
868 return writeNullableTypedVector(val, &Parcel::writeFloat);
869}
870
Casey Dahlin451ff582015-10-19 18:12:18 -0700871status_t Parcel::writeDoubleVector(const std::vector<double>& val)
872{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800873 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700874}
875
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900876status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val)
877{
878 return writeNullableTypedVector(val, &Parcel::writeDouble);
879}
880
Casey Dahlinb9872622015-11-25 15:09:45 -0800881status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
882{
883 return writeNullableTypedVector(val, &Parcel::writeDouble);
884}
885
Casey Dahlin451ff582015-10-19 18:12:18 -0700886status_t Parcel::writeBoolVector(const std::vector<bool>& val)
887{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800888 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700889}
890
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900891status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val)
892{
893 return writeNullableTypedVector(val, &Parcel::writeBool);
894}
895
Casey Dahlinb9872622015-11-25 15:09:45 -0800896status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
897{
898 return writeNullableTypedVector(val, &Parcel::writeBool);
899}
900
Casey Dahlin451ff582015-10-19 18:12:18 -0700901status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
902{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800903 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700904}
905
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900906status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val)
907{
908 return writeNullableTypedVector(val, &Parcel::writeChar);
909}
910
Casey Dahlinb9872622015-11-25 15:09:45 -0800911status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
912{
913 return writeNullableTypedVector(val, &Parcel::writeChar);
914}
915
Casey Dahlin451ff582015-10-19 18:12:18 -0700916status_t Parcel::writeString16Vector(const std::vector<String16>& val)
917{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800918 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700919}
920
Casey Dahlinb9872622015-11-25 15:09:45 -0800921status_t Parcel::writeString16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900922 const std::optional<std::vector<std::optional<String16>>>& val)
923{
924 return writeNullableTypedVector(val, &Parcel::writeString16);
925}
926
927status_t Parcel::writeString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800928 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
929{
930 return writeNullableTypedVector(val, &Parcel::writeString16);
931}
932
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800933status_t Parcel::writeUtf8VectorAsUtf16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900934 const std::optional<std::vector<std::optional<std::string>>>& val) {
935 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
936}
937
938status_t Parcel::writeUtf8VectorAsUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800939 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
940 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
941}
942
943status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
944 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
945}
946
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700947status_t Parcel::writeInt32(int32_t val)
948{
Andreas Huber84a6d042009-08-17 13:33:27 -0700949 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700950}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800951
952status_t Parcel::writeUint32(uint32_t val)
953{
954 return writeAligned(val);
955}
956
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700957status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700958 if (len > INT32_MAX) {
959 // don't accept size_t values which may have come from an
960 // inadvertent conversion from a negative int.
961 return BAD_VALUE;
962 }
963
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700964 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700965 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700966 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700967 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700968 if (ret == NO_ERROR) {
969 ret = write(val, len * sizeof(*val));
970 }
971 return ret;
972}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700973status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700974 if (len > INT32_MAX) {
975 // don't accept size_t values which may have come from an
976 // inadvertent conversion from a negative int.
977 return BAD_VALUE;
978 }
979
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700980 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700981 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700982 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700983 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700984 if (ret == NO_ERROR) {
985 ret = write(val, len * sizeof(*val));
986 }
987 return ret;
988}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700989
Casey Dahlind6848f52015-10-15 15:44:59 -0700990status_t Parcel::writeBool(bool val)
991{
992 return writeInt32(int32_t(val));
993}
994
995status_t Parcel::writeChar(char16_t val)
996{
997 return writeInt32(int32_t(val));
998}
999
1000status_t Parcel::writeByte(int8_t val)
1001{
1002 return writeInt32(int32_t(val));
1003}
1004
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001005status_t Parcel::writeInt64(int64_t val)
1006{
Andreas Huber84a6d042009-08-17 13:33:27 -07001007 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001008}
1009
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001010status_t Parcel::writeUint64(uint64_t val)
1011{
1012 return writeAligned(val);
1013}
1014
Serban Constantinescuf683e012013-11-05 16:53:55 +00001015status_t Parcel::writePointer(uintptr_t val)
1016{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001017 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001018}
1019
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001020status_t Parcel::writeFloat(float val)
1021{
Andreas Huber84a6d042009-08-17 13:33:27 -07001022 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001023}
1024
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001025#if defined(__mips__) && defined(__mips_hard_float)
1026
1027status_t Parcel::writeDouble(double val)
1028{
1029 union {
1030 double d;
1031 unsigned long long ll;
1032 } u;
1033 u.d = val;
1034 return writeAligned(u.ll);
1035}
1036
1037#else
1038
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001039status_t Parcel::writeDouble(double val)
1040{
Andreas Huber84a6d042009-08-17 13:33:27 -07001041 return writeAligned(val);
1042}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001043
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001044#endif
1045
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001046status_t Parcel::writeCString(const char* str)
1047{
1048 return write(str, strlen(str)+1);
1049}
1050
1051status_t Parcel::writeString8(const String8& str)
1052{
1053 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001054 // only write string if its length is more than zero characters,
1055 // as readString8 will only read if the length field is non-zero.
1056 // this is slightly different from how writeString16 works.
1057 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001058 err = write(str.string(), str.bytes()+1);
1059 }
1060 return err;
1061}
1062
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001063status_t Parcel::writeString16(const std::optional<String16>& str)
1064{
1065 if (!str) {
1066 return writeInt32(-1);
1067 }
1068
1069 return writeString16(*str);
1070}
1071
Casey Dahlinb9872622015-11-25 15:09:45 -08001072status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1073{
1074 if (!str) {
1075 return writeInt32(-1);
1076 }
1077
1078 return writeString16(*str);
1079}
1080
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001081status_t Parcel::writeString16(const String16& str)
1082{
1083 return writeString16(str.string(), str.size());
1084}
1085
1086status_t Parcel::writeString16(const char16_t* str, size_t len)
1087{
Yi Kong91635562018-06-07 14:38:36 -07001088 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001089
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001090 status_t err = writeInt32(len);
1091 if (err == NO_ERROR) {
1092 len *= sizeof(char16_t);
1093 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1094 if (data) {
1095 memcpy(data, str, len);
1096 *reinterpret_cast<char16_t*>(data+len) = 0;
1097 return NO_ERROR;
1098 }
1099 err = mError;
1100 }
1101 return err;
1102}
1103
1104status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1105{
Steven Morelanda86a3562019-08-01 23:28:34 +00001106 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001107}
1108
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001109status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1110{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001111 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001112}
1113
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001114status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val)
1115{
1116 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1117}
1118
Casey Dahlinb9872622015-11-25 15:09:45 -08001119status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1120{
1121 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1122}
1123
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001124status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const {
1125 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
1126}
1127
Casey Dahlinb9872622015-11-25 15:09:45 -08001128status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001129 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001130}
1131
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001132status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001133 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001134}
1135
Casey Dahlinb9872622015-11-25 15:09:45 -08001136status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1137 if (!parcelable) {
1138 return writeInt32(0);
1139 }
1140
1141 return writeParcelable(*parcelable);
1142}
1143
Christopher Wiley97f048d2015-11-19 06:49:05 -08001144status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1145 status_t status = writeInt32(1); // parcelable is not null.
1146 if (status != OK) {
1147 return status;
1148 }
1149 return parcelable.writeToParcel(this);
1150}
1151
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001152status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001153{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001154 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001155 return BAD_TYPE;
1156
1157 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001158 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001159 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001161 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001162 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001163
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001164 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1165 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166
1167 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001168 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169 return err;
1170 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001171 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001172 return err;
1173}
1174
Jeff Brown93ff1f92011-11-04 19:01:44 -07001175status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001176{
1177 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001178 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001179 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001180 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001181 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001182 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001183 return writeObject(obj, true);
1184}
1185
1186status_t Parcel::writeDupFileDescriptor(int fd)
1187{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001188 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001189 if (dupFd < 0) {
1190 return -errno;
1191 }
1192 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001193 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001194 close(dupFd);
1195 }
1196 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001197}
1198
Dianne Hackborn1941a402016-08-29 12:30:43 -07001199status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1200{
1201 writeInt32(0);
1202 return writeFileDescriptor(fd, takeOwnership);
1203}
1204
Ryo Hashimotobf551892018-05-31 16:58:35 +09001205status_t Parcel::writeDupParcelFileDescriptor(int fd)
1206{
1207 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1208 if (dupFd < 0) {
1209 return -errno;
1210 }
1211 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1212 if (err != OK) {
1213 close(dupFd);
1214 }
1215 return err;
1216}
1217
Christopher Wiley2cf19952016-04-11 11:09:37 -07001218status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001219 return writeDupFileDescriptor(fd.get());
1220}
1221
Christopher Wiley2cf19952016-04-11 11:09:37 -07001222status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001223 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1224}
1225
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001226status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) {
1227 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1228}
1229
Christopher Wiley2cf19952016-04-11 11:09:37 -07001230status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001231 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1232}
1233
Jeff Brown13b16042014-11-11 16:44:25 -08001234status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001235{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001236 if (len > INT32_MAX) {
1237 // don't accept size_t values which may have come from an
1238 // inadvertent conversion from a negative int.
1239 return BAD_VALUE;
1240 }
1241
Jeff Brown13b16042014-11-11 16:44:25 -08001242 status_t status;
1243 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001244 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001245 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001246 if (status) return status;
1247
1248 void* ptr = writeInplace(len);
1249 if (!ptr) return NO_MEMORY;
1250
Jeff Brown13b16042014-11-11 16:44:25 -08001251 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001252 return NO_ERROR;
1253 }
1254
Steve Block6807e592011-10-20 11:56:00 +01001255 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001256 int fd = ashmem_create_region("Parcel Blob", len);
1257 if (fd < 0) return NO_MEMORY;
1258
1259 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1260 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001261 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001262 } else {
Yi Kong91635562018-06-07 14:38:36 -07001263 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001264 if (ptr == MAP_FAILED) {
1265 status = -errno;
1266 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001267 if (!mutableCopy) {
1268 result = ashmem_set_prot_region(fd, PROT_READ);
1269 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001270 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001271 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001272 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001273 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001274 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001275 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001276 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001277 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001278 return NO_ERROR;
1279 }
1280 }
1281 }
1282 }
1283 ::munmap(ptr, len);
1284 }
1285 ::close(fd);
1286 return status;
1287}
1288
Jeff Brown13b16042014-11-11 16:44:25 -08001289status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1290{
1291 // Must match up with what's done in writeBlob.
1292 if (!mAllowFds) return FDS_NOT_ALLOWED;
1293 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1294 if (status) return status;
1295 return writeDupFileDescriptor(fd);
1296}
1297
Mathias Agopiane1424282013-07-29 21:24:40 -07001298status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001299{
1300 status_t err;
1301
1302 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001303 const size_t len = val.getFlattenedSize();
1304 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001305
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001306 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001307 // don't accept size_t values which may have come from an
1308 // inadvertent conversion from a negative int.
1309 return BAD_VALUE;
1310 }
1311
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001312 err = this->writeInt32(len);
1313 if (err) return err;
1314
1315 err = this->writeInt32(fd_count);
1316 if (err) return err;
1317
1318 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001319 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001320 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001321 return BAD_VALUE;
1322
Yi Kong91635562018-06-07 14:38:36 -07001323 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001324 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001325 fds = new (std::nothrow) int[fd_count];
1326 if (fds == nullptr) {
1327 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1328 return BAD_VALUE;
1329 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001330 }
1331
1332 err = val.flatten(buf, len, fds, fd_count);
1333 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1334 err = this->writeDupFileDescriptor( fds[i] );
1335 }
1336
1337 if (fd_count) {
1338 delete [] fds;
1339 }
1340
1341 return err;
1342}
1343
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001344status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1345{
1346 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1347 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1348 if (enoughData && enoughObjects) {
1349restart_write:
1350 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001351
Christopher Tate98e67d32015-06-03 18:44:15 -07001352 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001353 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001354 if (!mAllowFds) {
1355 // fail before modifying our object index
1356 return FDS_NOT_ALLOWED;
1357 }
1358 mHasFds = mFdsKnown = true;
1359 }
1360
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001361 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001362 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001363 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001364 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001365 mObjectsSize++;
1366 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001367
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001368 return finishWrite(sizeof(flat_binder_object));
1369 }
1370
1371 if (!enoughData) {
1372 const status_t err = growData(sizeof(val));
1373 if (err != NO_ERROR) return err;
1374 }
1375 if (!enoughObjects) {
1376 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001377 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001378 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001379 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001380 mObjects = objects;
1381 mObjectsCapacity = newSize;
1382 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001383
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001384 goto restart_write;
1385}
1386
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001387status_t Parcel::writeNoException()
1388{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001389 binder::Status status;
1390 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001391}
1392
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001393status_t Parcel::validateReadData(size_t upperBound) const
1394{
1395 // Don't allow non-object reads on object data
1396 if (mObjectsSorted || mObjectsSize <= 1) {
1397data_sorted:
1398 // Expect to check only against the next object
1399 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1400 // For some reason the current read position is greater than the next object
1401 // hint. Iterate until we find the right object
1402 size_t nextObject = mNextObjectHint;
1403 do {
1404 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1405 // Requested info overlaps with an object
1406 ALOGE("Attempt to read from protected data in Parcel %p", this);
1407 return PERMISSION_DENIED;
1408 }
1409 nextObject++;
1410 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1411 mNextObjectHint = nextObject;
1412 }
1413 return NO_ERROR;
1414 }
1415 // Quickly determine if mObjects is sorted.
1416 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1417 binder_size_t* prevObj = currObj;
1418 while (currObj > mObjects) {
1419 prevObj--;
1420 if(*prevObj > *currObj) {
1421 goto data_unsorted;
1422 }
1423 currObj--;
1424 }
1425 mObjectsSorted = true;
1426 goto data_sorted;
1427
1428data_unsorted:
1429 // Insertion Sort mObjects
1430 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1431 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1432 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1433 binder_size_t temp = *iter0;
1434 binder_size_t* iter1 = iter0 - 1;
1435 while (iter1 >= mObjects && *iter1 > temp) {
1436 *(iter1 + 1) = *iter1;
1437 iter1--;
1438 }
1439 *(iter1 + 1) = temp;
1440 }
1441 mNextObjectHint = 0;
1442 mObjectsSorted = true;
1443 goto data_sorted;
1444}
1445
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001446status_t Parcel::read(void* outData, size_t len) const
1447{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001448 if (len > INT32_MAX) {
1449 // don't accept size_t values which may have come from an
1450 // inadvertent conversion from a negative int.
1451 return BAD_VALUE;
1452 }
1453
1454 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1455 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001456 if (mObjectsSize > 0) {
1457 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001458 if(err != NO_ERROR) {
1459 // Still increment the data position by the expected length
1460 mDataPos += pad_size(len);
1461 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1462 return err;
1463 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001464 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001465 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001466 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001467 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001468 return NO_ERROR;
1469 }
1470 return NOT_ENOUGH_DATA;
1471}
1472
1473const void* Parcel::readInplace(size_t len) const
1474{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001475 if (len > INT32_MAX) {
1476 // don't accept size_t values which may have come from an
1477 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001478 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001479 }
1480
1481 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1482 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001483 if (mObjectsSize > 0) {
1484 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001485 if(err != NO_ERROR) {
1486 // Still increment the data position by the expected length
1487 mDataPos += pad_size(len);
1488 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001489 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001490 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001491 }
1492
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001493 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001494 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001495 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001496 return data;
1497 }
Yi Kong91635562018-06-07 14:38:36 -07001498 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001499}
1500
Andreas Huber84a6d042009-08-17 13:33:27 -07001501template<class T>
1502status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001503 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001504
1505 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001506 if (mObjectsSize > 0) {
1507 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001508 if(err != NO_ERROR) {
1509 // Still increment the data position by the expected length
1510 mDataPos += sizeof(T);
1511 return err;
1512 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001513 }
1514
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001515 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001516 mDataPos += sizeof(T);
1517 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001518 return NO_ERROR;
1519 } else {
1520 return NOT_ENOUGH_DATA;
1521 }
1522}
1523
Andreas Huber84a6d042009-08-17 13:33:27 -07001524template<class T>
1525T Parcel::readAligned() const {
1526 T result;
1527 if (readAligned(&result) != NO_ERROR) {
1528 result = 0;
1529 }
1530
1531 return result;
1532}
1533
1534template<class T>
1535status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001536 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001537
1538 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1539restart_write:
1540 *reinterpret_cast<T*>(mData+mDataPos) = val;
1541 return finishWrite(sizeof(val));
1542 }
1543
1544 status_t err = growData(sizeof(val));
1545 if (err == NO_ERROR) goto restart_write;
1546 return err;
1547}
1548
Casey Dahlin185d3442016-02-09 11:08:35 -08001549status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001550 size_t size;
1551 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1552 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001553}
1554
1555status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001556 size_t size;
1557 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1558 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001559}
1560
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001561status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const {
1562 size_t size;
1563 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1564 if (!*val) {
1565 // reserveOutVector does not create the out vector if size is < 0.
1566 // This occurs when writing a null byte vector.
1567 return OK;
1568 }
1569 return readByteVectorInternal(&**val, size);
1570}
1571
Casey Dahlin185d3442016-02-09 11:08:35 -08001572status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001573 size_t size;
1574 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001575 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001576 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001577 // This occurs when writing a null byte vector.
1578 return OK;
1579 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001580 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001581}
1582
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001583status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const {
1584 size_t size;
1585 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1586 if (!*val) {
1587 // reserveOutVector does not create the out vector if size is < 0.
1588 // This occurs when writing a null byte vector.
1589 return OK;
1590 }
1591 return readByteVectorInternal(&**val, size);
1592}
1593
Casey Dahlin185d3442016-02-09 11:08:35 -08001594status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001595 size_t size;
1596 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001597 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001598 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001599 // This occurs when writing a null byte vector.
1600 return OK;
1601 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001602 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001603}
1604
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001605status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const {
1606 return readNullableTypedVector(val, &Parcel::readInt32);
1607}
1608
Casey Dahlinb9872622015-11-25 15:09:45 -08001609status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1610 return readNullableTypedVector(val, &Parcel::readInt32);
1611}
1612
Casey Dahlin451ff582015-10-19 18:12:18 -07001613status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001614 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001615}
1616
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001617status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const {
1618 return readNullableTypedVector(val, &Parcel::readInt64);
1619}
1620
Casey Dahlinb9872622015-11-25 15:09:45 -08001621status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1622 return readNullableTypedVector(val, &Parcel::readInt64);
1623}
1624
Casey Dahlin451ff582015-10-19 18:12:18 -07001625status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001626 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001627}
1628
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001629status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const {
1630 return readNullableTypedVector(val, &Parcel::readUint64);
1631}
1632
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001633status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1634 return readNullableTypedVector(val, &Parcel::readUint64);
1635}
1636
1637status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1638 return readTypedVector(val, &Parcel::readUint64);
1639}
1640
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001641status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const {
1642 return readNullableTypedVector(val, &Parcel::readFloat);
1643}
1644
Casey Dahlinb9872622015-11-25 15:09:45 -08001645status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1646 return readNullableTypedVector(val, &Parcel::readFloat);
1647}
1648
Casey Dahlin451ff582015-10-19 18:12:18 -07001649status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001650 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001651}
1652
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001653status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const {
1654 return readNullableTypedVector(val, &Parcel::readDouble);
1655}
1656
Casey Dahlinb9872622015-11-25 15:09:45 -08001657status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1658 return readNullableTypedVector(val, &Parcel::readDouble);
1659}
1660
Casey Dahlin451ff582015-10-19 18:12:18 -07001661status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001662 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001663}
1664
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001665status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const {
1666 const int32_t start = dataPosition();
1667 int32_t size;
1668 status_t status = readInt32(&size);
1669 val->reset();
1670
1671 if (status != OK || size < 0) {
1672 return status;
1673 }
1674
1675 setDataPosition(start);
1676 val->emplace();
1677
1678 status = readBoolVector(&**val);
1679
1680 if (status != OK) {
1681 val->reset();
1682 }
1683
1684 return status;
1685}
1686
Casey Dahlinb9872622015-11-25 15:09:45 -08001687status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1688 const int32_t start = dataPosition();
1689 int32_t size;
1690 status_t status = readInt32(&size);
1691 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001692
Casey Dahlinb9872622015-11-25 15:09:45 -08001693 if (status != OK || size < 0) {
1694 return status;
1695 }
1696
1697 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001698 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001699
1700 status = readBoolVector(val->get());
1701
1702 if (status != OK) {
1703 val->reset();
1704 }
1705
1706 return status;
1707}
1708
1709status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001710 int32_t size;
1711 status_t status = readInt32(&size);
1712
1713 if (status != OK) {
1714 return status;
1715 }
1716
1717 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001718 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001719 }
1720
1721 val->resize(size);
1722
1723 /* C++ bool handling means a vector of bools isn't necessarily addressable
1724 * (we might use individual bits)
1725 */
Christopher Wiley97887982015-10-27 16:33:47 -07001726 bool data;
1727 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001728 status = readBool(&data);
1729 (*val)[i] = data;
1730
1731 if (status != OK) {
1732 return status;
1733 }
1734 }
1735
1736 return OK;
1737}
1738
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001739status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const {
1740 return readNullableTypedVector(val, &Parcel::readChar);
1741}
1742
Casey Dahlinb9872622015-11-25 15:09:45 -08001743status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1744 return readNullableTypedVector(val, &Parcel::readChar);
1745}
1746
Casey Dahlin451ff582015-10-19 18:12:18 -07001747status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001748 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001749}
1750
Casey Dahlinb9872622015-11-25 15:09:45 -08001751status_t Parcel::readString16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001752 std::optional<std::vector<std::optional<String16>>>* val) const {
1753 return readNullableTypedVector(val, &Parcel::readString16);
1754}
1755
1756status_t Parcel::readString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -08001757 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1758 return readNullableTypedVector(val, &Parcel::readString16);
1759}
1760
Casey Dahlin451ff582015-10-19 18:12:18 -07001761status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001762 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001763}
1764
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001765status_t Parcel::readUtf8VectorFromUtf16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001766 std::optional<std::vector<std::optional<std::string>>>* val) const {
1767 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1768}
1769
1770status_t Parcel::readUtf8VectorFromUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001771 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1772 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1773}
1774
1775status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1776 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1777}
Casey Dahlin451ff582015-10-19 18:12:18 -07001778
Andreas Huber84a6d042009-08-17 13:33:27 -07001779status_t Parcel::readInt32(int32_t *pArg) const
1780{
1781 return readAligned(pArg);
1782}
1783
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001784int32_t Parcel::readInt32() const
1785{
Andreas Huber84a6d042009-08-17 13:33:27 -07001786 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001787}
1788
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001789status_t Parcel::readUint32(uint32_t *pArg) const
1790{
1791 return readAligned(pArg);
1792}
1793
1794uint32_t Parcel::readUint32() const
1795{
1796 return readAligned<uint32_t>();
1797}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001798
1799status_t Parcel::readInt64(int64_t *pArg) const
1800{
Andreas Huber84a6d042009-08-17 13:33:27 -07001801 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001802}
1803
1804
1805int64_t Parcel::readInt64() const
1806{
Andreas Huber84a6d042009-08-17 13:33:27 -07001807 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001808}
1809
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001810status_t Parcel::readUint64(uint64_t *pArg) const
1811{
1812 return readAligned(pArg);
1813}
1814
1815uint64_t Parcel::readUint64() const
1816{
1817 return readAligned<uint64_t>();
1818}
1819
Serban Constantinescuf683e012013-11-05 16:53:55 +00001820status_t Parcel::readPointer(uintptr_t *pArg) const
1821{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001822 status_t ret;
1823 binder_uintptr_t ptr;
1824 ret = readAligned(&ptr);
1825 if (!ret)
1826 *pArg = ptr;
1827 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001828}
1829
1830uintptr_t Parcel::readPointer() const
1831{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001832 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001833}
1834
1835
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001836status_t Parcel::readFloat(float *pArg) const
1837{
Andreas Huber84a6d042009-08-17 13:33:27 -07001838 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001839}
1840
1841
1842float Parcel::readFloat() const
1843{
Andreas Huber84a6d042009-08-17 13:33:27 -07001844 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001845}
1846
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001847#if defined(__mips__) && defined(__mips_hard_float)
1848
1849status_t Parcel::readDouble(double *pArg) const
1850{
1851 union {
1852 double d;
1853 unsigned long long ll;
1854 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001855 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001856 status_t status;
1857 status = readAligned(&u.ll);
1858 *pArg = u.d;
1859 return status;
1860}
1861
1862double Parcel::readDouble() const
1863{
1864 union {
1865 double d;
1866 unsigned long long ll;
1867 } u;
1868 u.ll = readAligned<unsigned long long>();
1869 return u.d;
1870}
1871
1872#else
1873
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001874status_t Parcel::readDouble(double *pArg) const
1875{
Andreas Huber84a6d042009-08-17 13:33:27 -07001876 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001877}
1878
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001879double Parcel::readDouble() const
1880{
Andreas Huber84a6d042009-08-17 13:33:27 -07001881 return readAligned<double>();
1882}
1883
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001884#endif
1885
Andreas Huber84a6d042009-08-17 13:33:27 -07001886status_t Parcel::readIntPtr(intptr_t *pArg) const
1887{
1888 return readAligned(pArg);
1889}
1890
1891
1892intptr_t Parcel::readIntPtr() const
1893{
1894 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001895}
1896
Casey Dahlind6848f52015-10-15 15:44:59 -07001897status_t Parcel::readBool(bool *pArg) const
1898{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001899 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001900 status_t ret = readInt32(&tmp);
1901 *pArg = (tmp != 0);
1902 return ret;
1903}
1904
1905bool Parcel::readBool() const
1906{
1907 return readInt32() != 0;
1908}
1909
1910status_t Parcel::readChar(char16_t *pArg) const
1911{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001912 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001913 status_t ret = readInt32(&tmp);
1914 *pArg = char16_t(tmp);
1915 return ret;
1916}
1917
1918char16_t Parcel::readChar() const
1919{
1920 return char16_t(readInt32());
1921}
1922
1923status_t Parcel::readByte(int8_t *pArg) const
1924{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001925 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001926 status_t ret = readInt32(&tmp);
1927 *pArg = int8_t(tmp);
1928 return ret;
1929}
1930
1931int8_t Parcel::readByte() const
1932{
1933 return int8_t(readInt32());
1934}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001935
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001936status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1937 size_t utf16Size = 0;
1938 const char16_t* src = readString16Inplace(&utf16Size);
1939 if (!src) {
1940 return UNEXPECTED_NULL;
1941 }
1942
1943 // Save ourselves the trouble, we're done.
1944 if (utf16Size == 0u) {
1945 str->clear();
1946 return NO_ERROR;
1947 }
1948
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001949 // Allow for closing '\0'
1950 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1951 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001952 return BAD_VALUE;
1953 }
1954 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001955 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001956 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001957 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1958 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001959 return NO_ERROR;
1960}
1961
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001962status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const {
1963 const int32_t start = dataPosition();
1964 int32_t size;
1965 status_t status = readInt32(&size);
1966 str->reset();
1967
1968 if (status != OK || size < 0) {
1969 return status;
1970 }
1971
1972 setDataPosition(start);
1973 str->emplace();
1974 return readUtf8FromUtf16(&**str);
1975}
1976
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001977status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1978 const int32_t start = dataPosition();
1979 int32_t size;
1980 status_t status = readInt32(&size);
1981 str->reset();
1982
1983 if (status != OK || size < 0) {
1984 return status;
1985 }
1986
1987 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001988 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001989 return readUtf8FromUtf16(str->get());
1990}
1991
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001992const char* Parcel::readCString() const
1993{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001994 if (mDataPos < mDataSize) {
1995 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001996 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1997 // is the string's trailing NUL within the parcel's valid bounds?
1998 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1999 if (eos) {
2000 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002001 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002002 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002003 return str;
2004 }
2005 }
Yi Kong91635562018-06-07 14:38:36 -07002006 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002007}
2008
2009String8 Parcel::readString8() const
2010{
Roshan Pius87b64d22016-07-18 12:51:02 -07002011 String8 retString;
2012 status_t status = readString8(&retString);
2013 if (status != OK) {
2014 // We don't care about errors here, so just return an empty string.
2015 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002016 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002017 return retString;
2018}
2019
2020status_t Parcel::readString8(String8* pArg) const
2021{
2022 int32_t size;
2023 status_t status = readInt32(&size);
2024 if (status != OK) {
2025 return status;
2026 }
2027 // watch for potential int overflow from size+1
2028 if (size < 0 || size >= INT32_MAX) {
2029 return BAD_VALUE;
2030 }
2031 // |writeString8| writes nothing for empty string.
2032 if (size == 0) {
2033 *pArg = String8();
2034 return OK;
2035 }
2036 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07002037 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002038 return BAD_VALUE;
2039 }
2040 pArg->setTo(str, size);
2041 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002042}
2043
2044String16 Parcel::readString16() const
2045{
2046 size_t len;
2047 const char16_t* str = readString16Inplace(&len);
2048 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002049 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002050 return String16();
2051}
2052
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002053status_t Parcel::readString16(std::optional<String16>* pArg) const
2054{
2055 const int32_t start = dataPosition();
2056 int32_t size;
2057 status_t status = readInt32(&size);
2058 pArg->reset();
2059
2060 if (status != OK || size < 0) {
2061 return status;
2062 }
2063
2064 setDataPosition(start);
2065 pArg->emplace();
2066
2067 status = readString16(&**pArg);
2068
2069 if (status != OK) {
2070 pArg->reset();
2071 }
2072
2073 return status;
2074}
2075
Casey Dahlinb9872622015-11-25 15:09:45 -08002076status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2077{
2078 const int32_t start = dataPosition();
2079 int32_t size;
2080 status_t status = readInt32(&size);
2081 pArg->reset();
2082
2083 if (status != OK || size < 0) {
2084 return status;
2085 }
2086
2087 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002088 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002089
2090 status = readString16(pArg->get());
2091
2092 if (status != OK) {
2093 pArg->reset();
2094 }
2095
2096 return status;
2097}
2098
Casey Dahlin451ff582015-10-19 18:12:18 -07002099status_t Parcel::readString16(String16* pArg) const
2100{
2101 size_t len;
2102 const char16_t* str = readString16Inplace(&len);
2103 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002104 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002105 return 0;
2106 } else {
2107 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002108 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002109 }
2110}
2111
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002112const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2113{
2114 int32_t size = readInt32();
2115 // watch for potential int overflow from size+1
2116 if (size >= 0 && size < INT32_MAX) {
2117 *outLen = size;
2118 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002119 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002120 return str;
2121 }
2122 }
2123 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002124 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002125}
2126
Casey Dahlinf0c13772015-10-27 18:33:56 -07002127status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2128{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002129 status_t status = readNullableStrongBinder(val);
2130 if (status == OK && !val->get()) {
2131 status = UNEXPECTED_NULL;
2132 }
2133 return status;
2134}
2135
2136status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2137{
Steven Morelanda86a3562019-08-01 23:28:34 +00002138 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002139}
2140
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141sp<IBinder> Parcel::readStrongBinder() const
2142{
2143 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002144 // Note that a lot of code in Android reads binders by hand with this
2145 // method, and that code has historically been ok with getting nullptr
2146 // back (while ignoring error codes).
2147 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002148 return val;
2149}
2150
Christopher Wiley97f048d2015-11-19 06:49:05 -08002151status_t Parcel::readParcelable(Parcelable* parcelable) const {
2152 int32_t have_parcelable = 0;
2153 status_t status = readInt32(&have_parcelable);
2154 if (status != OK) {
2155 return status;
2156 }
2157 if (!have_parcelable) {
2158 return UNEXPECTED_NULL;
2159 }
2160 return parcelable->readFromParcel(this);
2161}
2162
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002163int32_t Parcel::readExceptionCode() const
2164{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002165 binder::Status status;
2166 status.readFromParcel(*this);
2167 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002168}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002169
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002170native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002171{
2172 int numFds, numInts;
2173 status_t err;
2174 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002175 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002176 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002177 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002178
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002179 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002180 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002181 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002182 }
2183
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002184 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002185 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002186 if (h->data[i] < 0) {
2187 for (int j = 0; j < i; j++) {
2188 close(h->data[j]);
2189 }
2190 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002191 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002192 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002193 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002194 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002195 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002196 native_handle_close(h);
2197 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002198 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002199 }
2200 return h;
2201}
2202
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002203int Parcel::readFileDescriptor() const
2204{
2205 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002206
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002207 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002208 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002209 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002210
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002211 return BAD_TYPE;
2212}
2213
Dianne Hackborn1941a402016-08-29 12:30:43 -07002214int Parcel::readParcelFileDescriptor() const
2215{
2216 int32_t hasComm = readInt32();
2217 int fd = readFileDescriptor();
2218 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002219 // detach (owned by the binder driver)
2220 int comm = readFileDescriptor();
2221
2222 // warning: this must be kept in sync with:
2223 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2224 enum ParcelFileDescriptorStatus {
2225 DETACHED = 2,
2226 };
2227
2228#if BYTE_ORDER == BIG_ENDIAN
2229 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2230#endif
2231#if BYTE_ORDER == LITTLE_ENDIAN
2232 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2233#endif
2234
2235 ssize_t written = TEMP_FAILURE_RETRY(
2236 ::write(comm, &message, sizeof(message)));
2237
2238 if (written == -1 || written != sizeof(message)) {
2239 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2240 written, strerror(errno));
2241 return BAD_TYPE;
2242 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002243 }
2244 return fd;
2245}
2246
Christopher Wiley2cf19952016-04-11 11:09:37 -07002247status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002248{
2249 int got = readFileDescriptor();
2250
2251 if (got == BAD_TYPE) {
2252 return BAD_TYPE;
2253 }
2254
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002255 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002256
2257 if (val->get() < 0) {
2258 return BAD_VALUE;
2259 }
2260
2261 return OK;
2262}
2263
Ryo Hashimotobf551892018-05-31 16:58:35 +09002264status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2265{
2266 int got = readParcelFileDescriptor();
2267
2268 if (got == BAD_TYPE) {
2269 return BAD_TYPE;
2270 }
2271
2272 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2273
2274 if (val->get() < 0) {
2275 return BAD_VALUE;
2276 }
2277
2278 return OK;
2279}
Casey Dahlin06673e32015-11-23 13:24:23 -08002280
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002281status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const {
2282 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2283}
2284
Christopher Wiley2cf19952016-04-11 11:09:37 -07002285status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002286 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2287}
2288
Christopher Wiley2cf19952016-04-11 11:09:37 -07002289status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002290 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2291}
2292
Jeff Brown5707dbf2011-09-23 21:17:56 -07002293status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2294{
Jeff Brown13b16042014-11-11 16:44:25 -08002295 int32_t blobType;
2296 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002297 if (status) return status;
2298
Jeff Brown13b16042014-11-11 16:44:25 -08002299 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002300 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002301 const void* ptr = readInplace(len);
2302 if (!ptr) return BAD_VALUE;
2303
Jeff Brown13b16042014-11-11 16:44:25 -08002304 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002305 return NO_ERROR;
2306 }
2307
Steve Block6807e592011-10-20 11:56:00 +01002308 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002309 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002310 int fd = readFileDescriptor();
2311 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2312
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002313 if (!ashmem_valid(fd)) {
2314 ALOGE("invalid fd");
2315 return BAD_VALUE;
2316 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002317 int size = ashmem_get_size_region(fd);
2318 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002319 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002320 return BAD_VALUE;
2321 }
Yi Kong91635562018-06-07 14:38:36 -07002322 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002323 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002324 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002325
Jeff Brown13b16042014-11-11 16:44:25 -08002326 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002327 return NO_ERROR;
2328}
2329
Mathias Agopiane1424282013-07-29 21:24:40 -07002330status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002331{
2332 // size
2333 const size_t len = this->readInt32();
2334 const size_t fd_count = this->readInt32();
2335
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002336 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002337 // don't accept size_t values which may have come from an
2338 // inadvertent conversion from a negative int.
2339 return BAD_VALUE;
2340 }
2341
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002342 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002343 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002344 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002345 return BAD_VALUE;
2346
Yi Kong91635562018-06-07 14:38:36 -07002347 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002348 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002349 fds = new (std::nothrow) int[fd_count];
2350 if (fds == nullptr) {
2351 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2352 return BAD_VALUE;
2353 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002354 }
2355
2356 status_t err = NO_ERROR;
2357 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002358 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002359 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002360 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002361 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 -07002362 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2363 // Close all the file descriptors that were dup-ed.
2364 for (size_t j=0; j<i ;j++) {
2365 close(fds[j]);
2366 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002367 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002368 }
2369
2370 if (err == NO_ERROR) {
2371 err = val.unflatten(buf, len, fds, fd_count);
2372 }
2373
2374 if (fd_count) {
2375 delete [] fds;
2376 }
2377
2378 return err;
2379}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002380const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2381{
2382 const size_t DPOS = mDataPos;
2383 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2384 const flat_binder_object* obj
2385 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2386 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002387 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002388 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002389 // the object list, so we don't want to check for it when
2390 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002391 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002392 return obj;
2393 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002394
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002395 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002396 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002397 const size_t N = mObjectsSize;
2398 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002399
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002400 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002401 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002402 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002403
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002404 // Start at the current hint position, looking for an object at
2405 // the current data position.
2406 if (opos < N) {
2407 while (opos < (N-1) && OBJS[opos] < DPOS) {
2408 opos++;
2409 }
2410 } else {
2411 opos = N-1;
2412 }
2413 if (OBJS[opos] == DPOS) {
2414 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002415 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002416 this, DPOS, opos);
2417 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002418 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002419 return obj;
2420 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002421
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002422 // Look backwards for it...
2423 while (opos > 0 && OBJS[opos] > DPOS) {
2424 opos--;
2425 }
2426 if (OBJS[opos] == DPOS) {
2427 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002428 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002429 this, DPOS, opos);
2430 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002431 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002432 return obj;
2433 }
2434 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002435 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 -07002436 this, DPOS);
2437 }
Yi Kong91635562018-06-07 14:38:36 -07002438 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002439}
2440
2441void Parcel::closeFileDescriptors()
2442{
2443 size_t i = mObjectsSize;
2444 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002445 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002446 }
2447 while (i > 0) {
2448 i--;
2449 const flat_binder_object* flat
2450 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002451 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002452 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002453 close(flat->handle);
2454 }
2455 }
2456}
2457
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002458uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002459{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002460 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002461}
2462
2463size_t Parcel::ipcDataSize() const
2464{
2465 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2466}
2467
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002468uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002469{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002470 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002471}
2472
2473size_t Parcel::ipcObjectsCount() const
2474{
2475 return mObjectsSize;
2476}
2477
2478void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002479 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002480{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002481 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002482 freeDataNoInit();
2483 mError = NO_ERROR;
2484 mData = const_cast<uint8_t*>(data);
2485 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002486 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002487 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002488 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002489 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002490 mObjectsSize = mObjectsCapacity = objectsCount;
2491 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002492 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002493 mOwner = relFunc;
2494 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002495 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002496 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002497 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002498 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002499 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002500 mObjectsSize = 0;
2501 break;
2502 }
2503 minOffset = offset + sizeof(flat_binder_object);
2504 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002505 scanForFds();
2506}
2507
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002508void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002509{
2510 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002511
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002512 if (errorCheck() != NO_ERROR) {
2513 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002514 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002515 } else if (dataSize() > 0) {
2516 const uint8_t* DATA = data();
2517 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002518 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002519 const size_t N = objectsCount();
2520 for (size_t i=0; i<N; i++) {
2521 const flat_binder_object* flat
2522 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2523 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002524 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002525 << " = " << flat->binder;
2526 }
2527 } else {
2528 to << "NULL";
2529 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002530
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002531 to << ")";
2532}
2533
2534void Parcel::releaseObjects()
2535{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002536 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002537 if (i == 0) {
2538 return;
2539 }
2540 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002541 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002542 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002543 while (i > 0) {
2544 i--;
2545 const flat_binder_object* flat
2546 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002547 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002548 }
2549}
2550
2551void Parcel::acquireObjects()
2552{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002553 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002554 if (i == 0) {
2555 return;
2556 }
2557 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002558 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002559 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002560 while (i > 0) {
2561 i--;
2562 const flat_binder_object* flat
2563 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002564 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002565 }
2566}
2567
2568void Parcel::freeData()
2569{
2570 freeDataNoInit();
2571 initState();
2572}
2573
2574void Parcel::freeDataNoInit()
2575{
2576 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002577 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002578 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002579 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2580 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002581 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002582 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002583 if (mData) {
2584 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002585 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002586 if (mDataCapacity <= gParcelGlobalAllocSize) {
2587 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2588 } else {
2589 gParcelGlobalAllocSize = 0;
2590 }
2591 if (gParcelGlobalAllocCount > 0) {
2592 gParcelGlobalAllocCount--;
2593 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002594 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002595 free(mData);
2596 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002597 if (mObjects) free(mObjects);
2598 }
2599}
2600
2601status_t Parcel::growData(size_t len)
2602{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002603 if (len > INT32_MAX) {
2604 // don't accept size_t values which may have come from an
2605 // inadvertent conversion from a negative int.
2606 return BAD_VALUE;
2607 }
2608
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002609 size_t newSize = ((mDataSize+len)*3)/2;
2610 return (newSize <= mDataSize)
2611 ? (status_t) NO_MEMORY
2612 : continueWrite(newSize);
2613}
2614
2615status_t Parcel::restartWrite(size_t desired)
2616{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002617 if (desired > INT32_MAX) {
2618 // don't accept size_t values which may have come from an
2619 // inadvertent conversion from a negative int.
2620 return BAD_VALUE;
2621 }
2622
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002623 if (mOwner) {
2624 freeData();
2625 return continueWrite(desired);
2626 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002627
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002628 uint8_t* data = (uint8_t*)realloc(mData, desired);
2629 if (!data && desired > mDataCapacity) {
2630 mError = NO_MEMORY;
2631 return NO_MEMORY;
2632 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002633
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002634 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002635
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002636 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002637 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002638 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002639 gParcelGlobalAllocSize += desired;
2640 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002641 if (!mData) {
2642 gParcelGlobalAllocCount++;
2643 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002644 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002645 mData = data;
2646 mDataCapacity = desired;
2647 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002648
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002649 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002650 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2651 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2652
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002653 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002654 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002655 mObjectsSize = mObjectsCapacity = 0;
2656 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002657 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002658 mHasFds = false;
2659 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002660 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002661
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002662 return NO_ERROR;
2663}
2664
2665status_t Parcel::continueWrite(size_t desired)
2666{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002667 if (desired > INT32_MAX) {
2668 // don't accept size_t values which may have come from an
2669 // inadvertent conversion from a negative int.
2670 return BAD_VALUE;
2671 }
2672
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002673 // If shrinking, first adjust for any objects that appear
2674 // after the new data size.
2675 size_t objectsSize = mObjectsSize;
2676 if (desired < mDataSize) {
2677 if (desired == 0) {
2678 objectsSize = 0;
2679 } else {
2680 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002681 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002682 break;
2683 objectsSize--;
2684 }
2685 }
2686 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002687
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002688 if (mOwner) {
2689 // If the size is going to zero, just release the owner's data.
2690 if (desired == 0) {
2691 freeData();
2692 return NO_ERROR;
2693 }
2694
2695 // If there is a different owner, we need to take
2696 // posession.
2697 uint8_t* data = (uint8_t*)malloc(desired);
2698 if (!data) {
2699 mError = NO_MEMORY;
2700 return NO_MEMORY;
2701 }
Yi Kong91635562018-06-07 14:38:36 -07002702 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002703
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002704 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002705 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002706 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002707 free(data);
2708
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002709 mError = NO_MEMORY;
2710 return NO_MEMORY;
2711 }
2712
2713 // Little hack to only acquire references on objects
2714 // we will be keeping.
2715 size_t oldObjectsSize = mObjectsSize;
2716 mObjectsSize = objectsSize;
2717 acquireObjects();
2718 mObjectsSize = oldObjectsSize;
2719 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002720
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002721 if (mData) {
2722 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2723 }
2724 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002725 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002726 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002727 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002728 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002729 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002730
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002731 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002732 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002733 gParcelGlobalAllocSize += desired;
2734 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002735 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002736
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002737 mData = data;
2738 mObjects = objects;
2739 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002740 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002741 mDataCapacity = desired;
2742 mObjectsSize = mObjectsCapacity = objectsSize;
2743 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002744 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002745
2746 } else if (mData) {
2747 if (objectsSize < mObjectsSize) {
2748 // Need to release refs on any objects we are dropping.
2749 const sp<ProcessState> proc(ProcessState::self());
2750 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2751 const flat_binder_object* flat
2752 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002753 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002754 // will need to rescan because we may have lopped off the only FDs
2755 mFdsKnown = false;
2756 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002757 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002758 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002759
2760 if (objectsSize == 0) {
2761 free(mObjects);
2762 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002763 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002764 } else {
2765 binder_size_t* objects =
2766 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2767 if (objects) {
2768 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002769 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002770 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002771 }
2772 mObjectsSize = objectsSize;
2773 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002774 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002775 }
2776
2777 // We own the data, so we can just do a realloc().
2778 if (desired > mDataCapacity) {
2779 uint8_t* data = (uint8_t*)realloc(mData, desired);
2780 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002781 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2782 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002783 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002784 gParcelGlobalAllocSize += desired;
2785 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002786 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002787 mData = data;
2788 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002789 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002790 mError = NO_MEMORY;
2791 return NO_MEMORY;
2792 }
2793 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002794 if (mDataSize > desired) {
2795 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002796 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002797 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002798 if (mDataPos > desired) {
2799 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002800 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002801 }
2802 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002803
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002804 } else {
2805 // This is the first data. Easy!
2806 uint8_t* data = (uint8_t*)malloc(desired);
2807 if (!data) {
2808 mError = NO_MEMORY;
2809 return NO_MEMORY;
2810 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002811
Yi Kong91635562018-06-07 14:38:36 -07002812 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002813 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002814 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002815 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002816
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002817 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002818 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002819 gParcelGlobalAllocSize += desired;
2820 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002821 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002822
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002823 mData = data;
2824 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002825 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2826 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002827 mDataCapacity = desired;
2828 }
2829
2830 return NO_ERROR;
2831}
2832
2833void Parcel::initState()
2834{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002835 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002836 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002837 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002838 mDataSize = 0;
2839 mDataCapacity = 0;
2840 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002841 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2842 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002843 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002844 mObjectsSize = 0;
2845 mObjectsCapacity = 0;
2846 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002847 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002848 mHasFds = false;
2849 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002850 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002851 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002852 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002853 mWorkSourceRequestHeaderPosition = 0;
2854 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002855
2856 // racing multiple init leads only to multiple identical write
2857 if (gMaxFds == 0) {
2858 struct rlimit result;
2859 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2860 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002861 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002862 } else {
2863 ALOGW("Unable to getrlimit: %s", strerror(errno));
2864 gMaxFds = 1024;
2865 }
2866 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002867}
2868
2869void Parcel::scanForFds() const
2870{
2871 bool hasFds = false;
2872 for (size_t i=0; i<mObjectsSize; i++) {
2873 const flat_binder_object* flat
2874 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002875 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002876 hasFds = true;
2877 break;
2878 }
2879 }
2880 mHasFds = hasFds;
2881 mFdsKnown = true;
2882}
2883
Dan Sandleraa5c2342015-04-10 10:08:45 -04002884size_t Parcel::getBlobAshmemSize() const
2885{
Adrian Roos6bb31142015-10-22 16:46:12 -07002886 // This used to return the size of all blobs that were written to ashmem, now we're returning
2887 // the ashmem currently referenced by this Parcel, which should be equivalent.
2888 // TODO: Remove method once ABI can be changed.
2889 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002890}
2891
Adrian Rooscbf37262015-10-22 16:12:53 -07002892size_t Parcel::getOpenAshmemSize() const
2893{
2894 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002895}
2896
2897// --- Parcel::Blob ---
2898
2899Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002900 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002901}
2902
2903Parcel::Blob::~Blob() {
2904 release();
2905}
2906
2907void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002908 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002909 ::munmap(mData, mSize);
2910 }
2911 clear();
2912}
2913
Jeff Brown13b16042014-11-11 16:44:25 -08002914void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2915 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002916 mData = data;
2917 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002918 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002919}
2920
2921void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002922 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002923 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002924 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002925 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002926}
2927
Steven Moreland61ff8492019-09-26 16:05:45 -07002928} // namespace android