blob: f07f82afbc6c479c60cc5f8c4798578866010d85 [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) {
Martijn Coenenda2f2fd2020-01-22 10:46:25 +0100425 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
426 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700427 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenenda2f2fd2020-01-22 10:46:25 +0100428 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800429 binder_size_t *objects =
430 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700431 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700432 return NO_MEMORY;
433 }
434 mObjects = objects;
435 mObjectsCapacity = newSize;
436 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700437
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700438 // append and acquire objects
439 int idx = mObjectsSize;
440 for (int i = firstIndex; i <= lastIndex; i++) {
441 size_t off = objects[i] - offset + startPos;
442 mObjects[idx++] = off;
443 mObjectsSize++;
444
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700445 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700446 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700447 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700449 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700450 // If this is a file descriptor, we need to dup it so the
451 // new Parcel now owns its own fd, and can declare that we
452 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800453 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800454 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700455 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400456 if (!mAllowFds) {
457 err = FDS_NOT_ALLOWED;
458 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 }
460 }
461 }
462
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400463 return err;
464}
465
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700466int Parcel::compareData(const Parcel& other) {
467 size_t size = dataSize();
468 if (size != other.dataSize()) {
469 return size < other.dataSize() ? -1 : 1;
470 }
471 return memcmp(data(), other.data(), size);
472}
473
Jeff Brown13b16042014-11-11 16:44:25 -0800474bool Parcel::allowFds() const
475{
476 return mAllowFds;
477}
478
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700479bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400480{
481 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700482 if (!allowFds) {
483 mAllowFds = false;
484 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400485 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700486}
487
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700488void Parcel::restoreAllowFds(bool lastValue)
489{
490 mAllowFds = lastValue;
491}
492
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700493bool Parcel::hasFileDescriptors() const
494{
495 if (!mFdsKnown) {
496 scanForFds();
497 }
498 return mHasFds;
499}
500
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000501void Parcel::updateWorkSourceRequestHeaderPosition() const {
502 // Only update the request headers once. We only want to point
503 // to the first headers read/written.
504 if (!mRequestHeaderPresent) {
505 mWorkSourceRequestHeaderPosition = dataPosition();
506 mRequestHeaderPresent = true;
507 }
508}
509
Jooyung Han1b228f42020-01-30 13:41:12 +0900510#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Moreland0f452742019-07-31 15:50:51 +0000511constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
512#else
513constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
514#endif
515
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700516// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700517status_t Parcel::writeInterfaceToken(const String16& interface)
518{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000519 const IPCThreadState* threadState = IPCThreadState::self();
520 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000521 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000522 writeInt32(threadState->shouldPropagateWorkSource() ?
523 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Moreland0f452742019-07-31 15:50:51 +0000524 writeInt32(kHeader);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700525 // currently the interface identification token is just its name as a string
526 return writeString16(interface);
527}
528
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000529bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
530{
531 if (!mRequestHeaderPresent) {
532 return false;
533 }
534
535 const size_t initialPosition = dataPosition();
536 setDataPosition(mWorkSourceRequestHeaderPosition);
537 status_t err = writeInt32(uid);
538 setDataPosition(initialPosition);
539 return err == NO_ERROR;
540}
541
Steven Moreland0891c9b2019-05-06 15:05:13 -0700542uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000543{
544 if (!mRequestHeaderPresent) {
545 return IPCThreadState::kUnsetWorkSource;
546 }
547
548 const size_t initialPosition = dataPosition();
549 setDataPosition(mWorkSourceRequestHeaderPosition);
550 uid_t uid = readInt32();
551 setDataPosition(initialPosition);
552 return uid;
553}
554
Mathias Agopian83c04462009-05-22 19:00:22 -0700555bool Parcel::checkInterface(IBinder* binder) const
556{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700557 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700558}
559
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700560bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700561 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700562{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700563 return enforceInterface(interface.string(), interface.size(), threadState);
564}
565
566bool Parcel::enforceInterface(const char16_t* interface,
567 size_t len,
568 IPCThreadState* threadState) const
569{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100570 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700571 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700572 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700573 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700574 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700575 if ((threadState->getLastTransactionBinderFlags() &
576 IBinder::FLAG_ONEWAY) != 0) {
577 // For one-way calls, the callee is running entirely
578 // disconnected from the caller, so disable StrictMode entirely.
579 // Not only does disk/network usage not impact the caller, but
580 // there's no way to commuicate back any violations anyway.
581 threadState->setStrictModePolicy(0);
582 } else {
583 threadState->setStrictModePolicy(strictPolicy);
584 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100585 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000586 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100587 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000588 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Moreland0f452742019-07-31 15:50:51 +0000589 // vendor header
590 int32_t header = readInt32();
591 if (header != kHeader) {
592 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
593 return false;
594 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100595 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700596 size_t parcel_interface_len;
597 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
598 if (len == parcel_interface_len &&
599 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700600 return true;
601 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700602 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700603 String8(interface, len).string(),
604 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700605 return false;
606 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700607}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700608
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700609size_t Parcel::objectsCount() const
610{
611 return mObjectsSize;
612}
613
614status_t Parcel::errorCheck() const
615{
616 return mError;
617}
618
619void Parcel::setError(status_t err)
620{
621 mError = err;
622}
623
624status_t Parcel::finishWrite(size_t len)
625{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700626 if (len > INT32_MAX) {
627 // don't accept size_t values which may have come from an
628 // inadvertent conversion from a negative int.
629 return BAD_VALUE;
630 }
631
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700632 //printf("Finish write of %d\n", len);
633 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700634 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700635 if (mDataPos > mDataSize) {
636 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700637 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700638 }
639 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
640 return NO_ERROR;
641}
642
643status_t Parcel::writeUnpadded(const void* data, size_t len)
644{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700645 if (len > INT32_MAX) {
646 // don't accept size_t values which may have come from an
647 // inadvertent conversion from a negative int.
648 return BAD_VALUE;
649 }
650
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700651 size_t end = mDataPos + len;
652 if (end < mDataPos) {
653 // integer overflow
654 return BAD_VALUE;
655 }
656
657 if (end <= mDataCapacity) {
658restart_write:
659 memcpy(mData+mDataPos, data, len);
660 return finishWrite(len);
661 }
662
663 status_t err = growData(len);
664 if (err == NO_ERROR) goto restart_write;
665 return err;
666}
667
668status_t Parcel::write(const void* data, size_t len)
669{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700670 if (len > INT32_MAX) {
671 // don't accept size_t values which may have come from an
672 // inadvertent conversion from a negative int.
673 return BAD_VALUE;
674 }
675
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700676 void* const d = writeInplace(len);
677 if (d) {
678 memcpy(d, data, len);
679 return NO_ERROR;
680 }
681 return mError;
682}
683
684void* Parcel::writeInplace(size_t len)
685{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700686 if (len > INT32_MAX) {
687 // don't accept size_t values which may have come from an
688 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700689 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700690 }
691
692 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700693
694 // sanity check for integer overflow
695 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700696 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700697 }
698
699 if ((mDataPos+padded) <= mDataCapacity) {
700restart_write:
701 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
702 uint8_t* const data = mData+mDataPos;
703
704 // Need to pad at end?
705 if (padded != len) {
706#if BYTE_ORDER == BIG_ENDIAN
707 static const uint32_t mask[4] = {
708 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
709 };
710#endif
711#if BYTE_ORDER == LITTLE_ENDIAN
712 static const uint32_t mask[4] = {
713 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
714 };
715#endif
716 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
717 // *reinterpret_cast<void**>(data+padded-4));
718 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
719 }
720
721 finishWrite(padded);
722 return data;
723 }
724
725 status_t err = growData(padded);
726 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700727 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700728}
729
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800730status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
731 const uint8_t* strData = (uint8_t*)str.data();
732 const size_t strLen= str.length();
733 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100734 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800735 return BAD_VALUE;
736 }
737
738 status_t err = writeInt32(utf16Len);
739 if (err) {
740 return err;
741 }
742
743 // Allocate enough bytes to hold our converted string and its terminating NULL.
744 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
745 if (!dst) {
746 return NO_MEMORY;
747 }
748
Sergio Girof4607432016-07-21 14:46:35 +0100749 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800750
751 return NO_ERROR;
752}
753
Jooyung Han2d5878e2020-01-23 12:45:10 +0900754status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) {
755 if (!str) {
756 return writeInt32(-1);
757 }
758 return writeUtf8AsUtf16(*str);
759}
760
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800761status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
762 if (!str) {
763 return writeInt32(-1);
764 }
765 return writeUtf8AsUtf16(*str);
766}
767
Daniel Normand0337ef2019-09-20 15:46:03 -0700768status_t Parcel::writeByteVectorInternal(const int8_t* data, size_t size) {
769 if (size > std::numeric_limits<int32_t>::max()) {
770 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -0700771 }
772
Daniel Normand0337ef2019-09-20 15:46:03 -0700773 status_t status = writeInt32(size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700774 if (status != OK) {
775 return status;
776 }
777
Daniel Normand0337ef2019-09-20 15:46:03 -0700778 return write(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700779}
780
Casey Dahlin185d3442016-02-09 11:08:35 -0800781status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700782 return writeByteVectorInternal(val.data(), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800783}
784
Jooyung Han2d5878e2020-01-23 12:45:10 +0900785status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val)
786{
787 if (!val) return writeInt32(-1);
788 return writeByteVectorInternal(val->data(), val->size());
789}
790
Casey Dahlin185d3442016-02-09 11:08:35 -0800791status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
792{
Daniel Normand0337ef2019-09-20 15:46:03 -0700793 if (!val) return writeInt32(-1);
794 return writeByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800795}
796
797status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700798 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val.data()), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800799}
800
Jooyung Han2d5878e2020-01-23 12:45:10 +0900801status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val)
802{
803 if (!val) return writeInt32(-1);
804 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
805}
806
Casey Dahlin185d3442016-02-09 11:08:35 -0800807status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
808{
Daniel Normand0337ef2019-09-20 15:46:03 -0700809 if (!val) return writeInt32(-1);
810 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800811}
812
Casey Dahlin451ff582015-10-19 18:12:18 -0700813status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
814{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800815 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700816}
817
Jooyung Han2d5878e2020-01-23 12:45:10 +0900818status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val)
819{
820 return writeNullableTypedVector(val, &Parcel::writeInt32);
821}
822
Casey Dahlinb9872622015-11-25 15:09:45 -0800823status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
824{
825 return writeNullableTypedVector(val, &Parcel::writeInt32);
826}
827
Casey Dahlin451ff582015-10-19 18:12:18 -0700828status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
829{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800830 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700831}
832
Jooyung Han2d5878e2020-01-23 12:45:10 +0900833status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val)
834{
835 return writeNullableTypedVector(val, &Parcel::writeInt64);
836}
837
Casey Dahlinb9872622015-11-25 15:09:45 -0800838status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
839{
840 return writeNullableTypedVector(val, &Parcel::writeInt64);
841}
842
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800843status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
844{
845 return writeTypedVector(val, &Parcel::writeUint64);
846}
847
Jooyung Han2d5878e2020-01-23 12:45:10 +0900848status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val)
849{
850 return writeNullableTypedVector(val, &Parcel::writeUint64);
851}
852
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800853status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
854{
855 return writeNullableTypedVector(val, &Parcel::writeUint64);
856}
857
Casey Dahlin451ff582015-10-19 18:12:18 -0700858status_t Parcel::writeFloatVector(const std::vector<float>& val)
859{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800860 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700861}
862
Jooyung Han2d5878e2020-01-23 12:45:10 +0900863status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val)
864{
865 return writeNullableTypedVector(val, &Parcel::writeFloat);
866}
867
Casey Dahlinb9872622015-11-25 15:09:45 -0800868status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
869{
870 return writeNullableTypedVector(val, &Parcel::writeFloat);
871}
872
Casey Dahlin451ff582015-10-19 18:12:18 -0700873status_t Parcel::writeDoubleVector(const std::vector<double>& val)
874{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800875 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700876}
877
Jooyung Han2d5878e2020-01-23 12:45:10 +0900878status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val)
879{
880 return writeNullableTypedVector(val, &Parcel::writeDouble);
881}
882
Casey Dahlinb9872622015-11-25 15:09:45 -0800883status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
884{
885 return writeNullableTypedVector(val, &Parcel::writeDouble);
886}
887
Casey Dahlin451ff582015-10-19 18:12:18 -0700888status_t Parcel::writeBoolVector(const std::vector<bool>& val)
889{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800890 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700891}
892
Jooyung Han2d5878e2020-01-23 12:45:10 +0900893status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val)
894{
895 return writeNullableTypedVector(val, &Parcel::writeBool);
896}
897
Casey Dahlinb9872622015-11-25 15:09:45 -0800898status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
899{
900 return writeNullableTypedVector(val, &Parcel::writeBool);
901}
902
Casey Dahlin451ff582015-10-19 18:12:18 -0700903status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
904{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800905 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700906}
907
Jooyung Han2d5878e2020-01-23 12:45:10 +0900908status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val)
909{
910 return writeNullableTypedVector(val, &Parcel::writeChar);
911}
912
Casey Dahlinb9872622015-11-25 15:09:45 -0800913status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
914{
915 return writeNullableTypedVector(val, &Parcel::writeChar);
916}
917
Casey Dahlin451ff582015-10-19 18:12:18 -0700918status_t Parcel::writeString16Vector(const std::vector<String16>& val)
919{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800920 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700921}
922
Casey Dahlinb9872622015-11-25 15:09:45 -0800923status_t Parcel::writeString16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +0900924 const std::optional<std::vector<std::optional<String16>>>& val)
925{
926 return writeNullableTypedVector(val, &Parcel::writeString16);
927}
928
929status_t Parcel::writeString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800930 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
931{
932 return writeNullableTypedVector(val, &Parcel::writeString16);
933}
934
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800935status_t Parcel::writeUtf8VectorAsUtf16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +0900936 const std::optional<std::vector<std::optional<std::string>>>& val) {
937 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
938}
939
940status_t Parcel::writeUtf8VectorAsUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800941 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
942 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
943}
944
945status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
946 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
947}
948
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700949status_t Parcel::writeInt32(int32_t val)
950{
Andreas Huber84a6d042009-08-17 13:33:27 -0700951 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700952}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800953
954status_t Parcel::writeUint32(uint32_t val)
955{
956 return writeAligned(val);
957}
958
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700959status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700960 if (len > INT32_MAX) {
961 // don't accept size_t values which may have come from an
962 // inadvertent conversion from a negative int.
963 return BAD_VALUE;
964 }
965
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700966 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700967 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700968 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700969 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700970 if (ret == NO_ERROR) {
971 ret = write(val, len * sizeof(*val));
972 }
973 return ret;
974}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700975status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700976 if (len > INT32_MAX) {
977 // don't accept size_t values which may have come from an
978 // inadvertent conversion from a negative int.
979 return BAD_VALUE;
980 }
981
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700982 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700983 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700984 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700985 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700986 if (ret == NO_ERROR) {
987 ret = write(val, len * sizeof(*val));
988 }
989 return ret;
990}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700991
Casey Dahlind6848f52015-10-15 15:44:59 -0700992status_t Parcel::writeBool(bool val)
993{
994 return writeInt32(int32_t(val));
995}
996
997status_t Parcel::writeChar(char16_t val)
998{
999 return writeInt32(int32_t(val));
1000}
1001
1002status_t Parcel::writeByte(int8_t val)
1003{
1004 return writeInt32(int32_t(val));
1005}
1006
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001007status_t Parcel::writeInt64(int64_t val)
1008{
Andreas Huber84a6d042009-08-17 13:33:27 -07001009 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001010}
1011
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001012status_t Parcel::writeUint64(uint64_t val)
1013{
1014 return writeAligned(val);
1015}
1016
Serban Constantinescuf683e012013-11-05 16:53:55 +00001017status_t Parcel::writePointer(uintptr_t val)
1018{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001019 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001020}
1021
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001022status_t Parcel::writeFloat(float val)
1023{
Andreas Huber84a6d042009-08-17 13:33:27 -07001024 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001025}
1026
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001027#if defined(__mips__) && defined(__mips_hard_float)
1028
1029status_t Parcel::writeDouble(double val)
1030{
1031 union {
1032 double d;
1033 unsigned long long ll;
1034 } u;
1035 u.d = val;
1036 return writeAligned(u.ll);
1037}
1038
1039#else
1040
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001041status_t Parcel::writeDouble(double val)
1042{
Andreas Huber84a6d042009-08-17 13:33:27 -07001043 return writeAligned(val);
1044}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001045
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001046#endif
1047
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001048status_t Parcel::writeCString(const char* str)
1049{
1050 return write(str, strlen(str)+1);
1051}
1052
1053status_t Parcel::writeString8(const String8& str)
1054{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001055 return writeString8(str.string(), str.size());
1056}
1057
1058status_t Parcel::writeString8(const char* str, size_t len)
1059{
1060 if (str == nullptr) return writeInt32(-1);
1061
1062 status_t err = writeInt32(len);
1063 if (err == NO_ERROR) {
1064 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1065 if (data) {
1066 memcpy(data, str, len);
1067 *reinterpret_cast<char*>(data+len) = 0;
1068 return NO_ERROR;
1069 }
1070 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001071 }
1072 return err;
1073}
1074
Jooyung Han2d5878e2020-01-23 12:45:10 +09001075status_t Parcel::writeString16(const std::optional<String16>& str)
1076{
1077 if (!str) {
1078 return writeInt32(-1);
1079 }
1080
1081 return writeString16(*str);
1082}
1083
Casey Dahlinb9872622015-11-25 15:09:45 -08001084status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1085{
1086 if (!str) {
1087 return writeInt32(-1);
1088 }
1089
1090 return writeString16(*str);
1091}
1092
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001093status_t Parcel::writeString16(const String16& str)
1094{
1095 return writeString16(str.string(), str.size());
1096}
1097
1098status_t Parcel::writeString16(const char16_t* str, size_t len)
1099{
Yi Kong91635562018-06-07 14:38:36 -07001100 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001101
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001102 status_t err = writeInt32(len);
1103 if (err == NO_ERROR) {
1104 len *= sizeof(char16_t);
1105 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1106 if (data) {
1107 memcpy(data, str, len);
1108 *reinterpret_cast<char16_t*>(data+len) = 0;
1109 return NO_ERROR;
1110 }
1111 err = mError;
1112 }
1113 return err;
1114}
1115
1116status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1117{
Steven Morelanda86a3562019-08-01 23:28:34 +00001118 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001119}
1120
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001121status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1122{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001123 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001124}
1125
Jooyung Han2d5878e2020-01-23 12:45:10 +09001126status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val)
1127{
1128 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1129}
1130
Casey Dahlinb9872622015-11-25 15:09:45 -08001131status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1132{
1133 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1134}
1135
Jooyung Han2d5878e2020-01-23 12:45:10 +09001136status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const {
1137 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
1138}
1139
Casey Dahlinb9872622015-11-25 15:09:45 -08001140status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001141 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001142}
1143
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001144status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001145 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001146}
1147
Casey Dahlinb9872622015-11-25 15:09:45 -08001148status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1149 if (!parcelable) {
1150 return writeInt32(0);
1151 }
1152
1153 return writeParcelable(*parcelable);
1154}
1155
Christopher Wiley97f048d2015-11-19 06:49:05 -08001156status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1157 status_t status = writeInt32(1); // parcelable is not null.
1158 if (status != OK) {
1159 return status;
1160 }
1161 return parcelable.writeToParcel(this);
1162}
1163
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001164status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001165{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001166 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001167 return BAD_TYPE;
1168
1169 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001170 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001171 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001172
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001173 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001174 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001176 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1177 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178
1179 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001180 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181 return err;
1182 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001183 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001184 return err;
1185}
1186
Jeff Brown93ff1f92011-11-04 19:01:44 -07001187status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001188{
1189 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001190 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001191 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001192 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001193 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001194 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001195 return writeObject(obj, true);
1196}
1197
1198status_t Parcel::writeDupFileDescriptor(int fd)
1199{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001200 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001201 if (dupFd < 0) {
1202 return -errno;
1203 }
1204 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001205 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001206 close(dupFd);
1207 }
1208 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001209}
1210
Dianne Hackborn1941a402016-08-29 12:30:43 -07001211status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1212{
1213 writeInt32(0);
1214 return writeFileDescriptor(fd, takeOwnership);
1215}
1216
Ryo Hashimotobf551892018-05-31 16:58:35 +09001217status_t Parcel::writeDupParcelFileDescriptor(int fd)
1218{
1219 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1220 if (dupFd < 0) {
1221 return -errno;
1222 }
1223 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1224 if (err != OK) {
1225 close(dupFd);
1226 }
1227 return err;
1228}
1229
Christopher Wiley2cf19952016-04-11 11:09:37 -07001230status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001231 return writeDupFileDescriptor(fd.get());
1232}
1233
Christopher Wiley2cf19952016-04-11 11:09:37 -07001234status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001235 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1236}
1237
Jooyung Han2d5878e2020-01-23 12:45:10 +09001238status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) {
1239 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1240}
1241
Christopher Wiley2cf19952016-04-11 11:09:37 -07001242status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001243 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1244}
1245
Jeff Brown13b16042014-11-11 16:44:25 -08001246status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001247{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001248 if (len > INT32_MAX) {
1249 // don't accept size_t values which may have come from an
1250 // inadvertent conversion from a negative int.
1251 return BAD_VALUE;
1252 }
1253
Jeff Brown13b16042014-11-11 16:44:25 -08001254 status_t status;
1255 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001256 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001257 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001258 if (status) return status;
1259
1260 void* ptr = writeInplace(len);
1261 if (!ptr) return NO_MEMORY;
1262
Jeff Brown13b16042014-11-11 16:44:25 -08001263 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001264 return NO_ERROR;
1265 }
1266
Steve Block6807e592011-10-20 11:56:00 +01001267 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001268 int fd = ashmem_create_region("Parcel Blob", len);
1269 if (fd < 0) return NO_MEMORY;
1270
1271 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1272 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001273 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001274 } else {
Yi Kong91635562018-06-07 14:38:36 -07001275 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001276 if (ptr == MAP_FAILED) {
1277 status = -errno;
1278 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001279 if (!mutableCopy) {
1280 result = ashmem_set_prot_region(fd, PROT_READ);
1281 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001282 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001283 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001284 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001285 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001286 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001287 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001288 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001289 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001290 return NO_ERROR;
1291 }
1292 }
1293 }
1294 }
1295 ::munmap(ptr, len);
1296 }
1297 ::close(fd);
1298 return status;
1299}
1300
Jeff Brown13b16042014-11-11 16:44:25 -08001301status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1302{
1303 // Must match up with what's done in writeBlob.
1304 if (!mAllowFds) return FDS_NOT_ALLOWED;
1305 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1306 if (status) return status;
1307 return writeDupFileDescriptor(fd);
1308}
1309
Mathias Agopiane1424282013-07-29 21:24:40 -07001310status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001311{
1312 status_t err;
1313
1314 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001315 const size_t len = val.getFlattenedSize();
1316 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001317
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001318 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001319 // don't accept size_t values which may have come from an
1320 // inadvertent conversion from a negative int.
1321 return BAD_VALUE;
1322 }
1323
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001324 err = this->writeInt32(len);
1325 if (err) return err;
1326
1327 err = this->writeInt32(fd_count);
1328 if (err) return err;
1329
1330 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001331 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001332 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001333 return BAD_VALUE;
1334
Yi Kong91635562018-06-07 14:38:36 -07001335 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001336 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001337 fds = new (std::nothrow) int[fd_count];
1338 if (fds == nullptr) {
1339 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1340 return BAD_VALUE;
1341 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001342 }
1343
1344 err = val.flatten(buf, len, fds, fd_count);
1345 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1346 err = this->writeDupFileDescriptor( fds[i] );
1347 }
1348
1349 if (fd_count) {
1350 delete [] fds;
1351 }
1352
1353 return err;
1354}
1355
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001356status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1357{
1358 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1359 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1360 if (enoughData && enoughObjects) {
1361restart_write:
1362 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001363
Christopher Tate98e67d32015-06-03 18:44:15 -07001364 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001365 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001366 if (!mAllowFds) {
1367 // fail before modifying our object index
1368 return FDS_NOT_ALLOWED;
1369 }
1370 mHasFds = mFdsKnown = true;
1371 }
1372
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001373 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001374 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001375 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001376 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001377 mObjectsSize++;
1378 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001379
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001380 return finishWrite(sizeof(flat_binder_object));
1381 }
1382
1383 if (!enoughData) {
1384 const status_t err = growData(sizeof(val));
1385 if (err != NO_ERROR) return err;
1386 }
1387 if (!enoughObjects) {
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01001388 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1389 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001390 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01001391 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001392 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001393 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001394 mObjects = objects;
1395 mObjectsCapacity = newSize;
1396 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001397
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001398 goto restart_write;
1399}
1400
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001401status_t Parcel::writeNoException()
1402{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001403 binder::Status status;
1404 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001405}
1406
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001407status_t Parcel::validateReadData(size_t upperBound) const
1408{
1409 // Don't allow non-object reads on object data
1410 if (mObjectsSorted || mObjectsSize <= 1) {
1411data_sorted:
1412 // Expect to check only against the next object
1413 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1414 // For some reason the current read position is greater than the next object
1415 // hint. Iterate until we find the right object
1416 size_t nextObject = mNextObjectHint;
1417 do {
1418 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1419 // Requested info overlaps with an object
1420 ALOGE("Attempt to read from protected data in Parcel %p", this);
1421 return PERMISSION_DENIED;
1422 }
1423 nextObject++;
1424 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1425 mNextObjectHint = nextObject;
1426 }
1427 return NO_ERROR;
1428 }
1429 // Quickly determine if mObjects is sorted.
1430 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1431 binder_size_t* prevObj = currObj;
1432 while (currObj > mObjects) {
1433 prevObj--;
1434 if(*prevObj > *currObj) {
1435 goto data_unsorted;
1436 }
1437 currObj--;
1438 }
1439 mObjectsSorted = true;
1440 goto data_sorted;
1441
1442data_unsorted:
1443 // Insertion Sort mObjects
1444 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1445 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1446 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1447 binder_size_t temp = *iter0;
1448 binder_size_t* iter1 = iter0 - 1;
1449 while (iter1 >= mObjects && *iter1 > temp) {
1450 *(iter1 + 1) = *iter1;
1451 iter1--;
1452 }
1453 *(iter1 + 1) = temp;
1454 }
1455 mNextObjectHint = 0;
1456 mObjectsSorted = true;
1457 goto data_sorted;
1458}
1459
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001460status_t Parcel::read(void* outData, size_t len) const
1461{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001462 if (len > INT32_MAX) {
1463 // don't accept size_t values which may have come from an
1464 // inadvertent conversion from a negative int.
1465 return BAD_VALUE;
1466 }
1467
1468 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1469 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001470 if (mObjectsSize > 0) {
1471 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001472 if(err != NO_ERROR) {
1473 // Still increment the data position by the expected length
1474 mDataPos += pad_size(len);
1475 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1476 return err;
1477 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001478 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001479 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001480 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001481 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001482 return NO_ERROR;
1483 }
1484 return NOT_ENOUGH_DATA;
1485}
1486
1487const void* Parcel::readInplace(size_t len) const
1488{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001489 if (len > INT32_MAX) {
1490 // don't accept size_t values which may have come from an
1491 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001492 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001493 }
1494
1495 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1496 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001497 if (mObjectsSize > 0) {
1498 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001499 if(err != NO_ERROR) {
1500 // Still increment the data position by the expected length
1501 mDataPos += pad_size(len);
1502 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001503 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001504 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001505 }
1506
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001507 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001508 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001509 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001510 return data;
1511 }
Yi Kong91635562018-06-07 14:38:36 -07001512 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001513}
1514
Andreas Huber84a6d042009-08-17 13:33:27 -07001515template<class T>
1516status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001517 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001518
1519 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001520 if (mObjectsSize > 0) {
1521 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001522 if(err != NO_ERROR) {
1523 // Still increment the data position by the expected length
1524 mDataPos += sizeof(T);
1525 return err;
1526 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001527 }
1528
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001529 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001530 mDataPos += sizeof(T);
1531 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001532 return NO_ERROR;
1533 } else {
1534 return NOT_ENOUGH_DATA;
1535 }
1536}
1537
Andreas Huber84a6d042009-08-17 13:33:27 -07001538template<class T>
1539T Parcel::readAligned() const {
1540 T result;
1541 if (readAligned(&result) != NO_ERROR) {
1542 result = 0;
1543 }
1544
1545 return result;
1546}
1547
1548template<class T>
1549status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001550 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001551
1552 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1553restart_write:
1554 *reinterpret_cast<T*>(mData+mDataPos) = val;
1555 return finishWrite(sizeof(val));
1556 }
1557
1558 status_t err = growData(sizeof(val));
1559 if (err == NO_ERROR) goto restart_write;
1560 return err;
1561}
1562
Casey Dahlin185d3442016-02-09 11:08:35 -08001563status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001564 size_t size;
1565 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1566 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001567}
1568
1569status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001570 size_t size;
1571 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1572 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001573}
1574
Jooyung Han2d5878e2020-01-23 12:45:10 +09001575status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const {
1576 size_t size;
1577 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1578 if (!*val) {
1579 // reserveOutVector does not create the out vector if size is < 0.
1580 // This occurs when writing a null byte vector.
1581 return OK;
1582 }
1583 return readByteVectorInternal(&**val, size);
1584}
1585
Casey Dahlin185d3442016-02-09 11:08:35 -08001586status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001587 size_t size;
1588 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001589 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001590 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001591 // This occurs when writing a null byte vector.
1592 return OK;
1593 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001594 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001595}
1596
Jooyung Han2d5878e2020-01-23 12:45:10 +09001597status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const {
1598 size_t size;
1599 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1600 if (!*val) {
1601 // reserveOutVector does not create the out vector if size is < 0.
1602 // This occurs when writing a null byte vector.
1603 return OK;
1604 }
1605 return readByteVectorInternal(&**val, size);
1606}
1607
Casey Dahlin185d3442016-02-09 11:08:35 -08001608status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001609 size_t size;
1610 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001611 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001612 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001613 // This occurs when writing a null byte vector.
1614 return OK;
1615 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001616 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001617}
1618
Jooyung Han2d5878e2020-01-23 12:45:10 +09001619status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const {
1620 return readNullableTypedVector(val, &Parcel::readInt32);
1621}
1622
Casey Dahlinb9872622015-11-25 15:09:45 -08001623status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1624 return readNullableTypedVector(val, &Parcel::readInt32);
1625}
1626
Casey Dahlin451ff582015-10-19 18:12:18 -07001627status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001628 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001629}
1630
Jooyung Han2d5878e2020-01-23 12:45:10 +09001631status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const {
1632 return readNullableTypedVector(val, &Parcel::readInt64);
1633}
1634
Casey Dahlinb9872622015-11-25 15:09:45 -08001635status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1636 return readNullableTypedVector(val, &Parcel::readInt64);
1637}
1638
Casey Dahlin451ff582015-10-19 18:12:18 -07001639status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001640 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001641}
1642
Jooyung Han2d5878e2020-01-23 12:45:10 +09001643status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const {
1644 return readNullableTypedVector(val, &Parcel::readUint64);
1645}
1646
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001647status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1648 return readNullableTypedVector(val, &Parcel::readUint64);
1649}
1650
1651status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1652 return readTypedVector(val, &Parcel::readUint64);
1653}
1654
Jooyung Han2d5878e2020-01-23 12:45:10 +09001655status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const {
1656 return readNullableTypedVector(val, &Parcel::readFloat);
1657}
1658
Casey Dahlinb9872622015-11-25 15:09:45 -08001659status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1660 return readNullableTypedVector(val, &Parcel::readFloat);
1661}
1662
Casey Dahlin451ff582015-10-19 18:12:18 -07001663status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001664 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001665}
1666
Jooyung Han2d5878e2020-01-23 12:45:10 +09001667status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const {
1668 return readNullableTypedVector(val, &Parcel::readDouble);
1669}
1670
Casey Dahlinb9872622015-11-25 15:09:45 -08001671status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1672 return readNullableTypedVector(val, &Parcel::readDouble);
1673}
1674
Casey Dahlin451ff582015-10-19 18:12:18 -07001675status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001676 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001677}
1678
Jooyung Han2d5878e2020-01-23 12:45:10 +09001679status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const {
1680 const int32_t start = dataPosition();
1681 int32_t size;
1682 status_t status = readInt32(&size);
1683 val->reset();
1684
1685 if (status != OK || size < 0) {
1686 return status;
1687 }
1688
1689 setDataPosition(start);
1690 val->emplace();
1691
1692 status = readBoolVector(&**val);
1693
1694 if (status != OK) {
1695 val->reset();
1696 }
1697
1698 return status;
1699}
1700
Casey Dahlinb9872622015-11-25 15:09:45 -08001701status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1702 const int32_t start = dataPosition();
1703 int32_t size;
1704 status_t status = readInt32(&size);
1705 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001706
Casey Dahlinb9872622015-11-25 15:09:45 -08001707 if (status != OK || size < 0) {
1708 return status;
1709 }
1710
1711 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001712 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001713
1714 status = readBoolVector(val->get());
1715
1716 if (status != OK) {
1717 val->reset();
1718 }
1719
1720 return status;
1721}
1722
1723status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001724 int32_t size;
1725 status_t status = readInt32(&size);
1726
1727 if (status != OK) {
1728 return status;
1729 }
1730
1731 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001732 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001733 }
1734
1735 val->resize(size);
1736
1737 /* C++ bool handling means a vector of bools isn't necessarily addressable
1738 * (we might use individual bits)
1739 */
Christopher Wiley97887982015-10-27 16:33:47 -07001740 bool data;
1741 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001742 status = readBool(&data);
1743 (*val)[i] = data;
1744
1745 if (status != OK) {
1746 return status;
1747 }
1748 }
1749
1750 return OK;
1751}
1752
Jooyung Han2d5878e2020-01-23 12:45:10 +09001753status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const {
1754 return readNullableTypedVector(val, &Parcel::readChar);
1755}
1756
Casey Dahlinb9872622015-11-25 15:09:45 -08001757status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1758 return readNullableTypedVector(val, &Parcel::readChar);
1759}
1760
Casey Dahlin451ff582015-10-19 18:12:18 -07001761status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001762 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001763}
1764
Casey Dahlinb9872622015-11-25 15:09:45 -08001765status_t Parcel::readString16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +09001766 std::optional<std::vector<std::optional<String16>>>* val) const {
1767 return readNullableTypedVector(val, &Parcel::readString16);
1768}
1769
1770status_t Parcel::readString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -08001771 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1772 return readNullableTypedVector(val, &Parcel::readString16);
1773}
1774
Casey Dahlin451ff582015-10-19 18:12:18 -07001775status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001776 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001777}
1778
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001779status_t Parcel::readUtf8VectorFromUtf16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +09001780 std::optional<std::vector<std::optional<std::string>>>* val) const {
1781 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1782}
1783
1784status_t Parcel::readUtf8VectorFromUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001785 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1786 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1787}
1788
1789status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1790 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1791}
Casey Dahlin451ff582015-10-19 18:12:18 -07001792
Andreas Huber84a6d042009-08-17 13:33:27 -07001793status_t Parcel::readInt32(int32_t *pArg) const
1794{
1795 return readAligned(pArg);
1796}
1797
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001798int32_t Parcel::readInt32() const
1799{
Andreas Huber84a6d042009-08-17 13:33:27 -07001800 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001801}
1802
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001803status_t Parcel::readUint32(uint32_t *pArg) const
1804{
1805 return readAligned(pArg);
1806}
1807
1808uint32_t Parcel::readUint32() const
1809{
1810 return readAligned<uint32_t>();
1811}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001812
1813status_t Parcel::readInt64(int64_t *pArg) const
1814{
Andreas Huber84a6d042009-08-17 13:33:27 -07001815 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001816}
1817
1818
1819int64_t Parcel::readInt64() const
1820{
Andreas Huber84a6d042009-08-17 13:33:27 -07001821 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001822}
1823
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001824status_t Parcel::readUint64(uint64_t *pArg) const
1825{
1826 return readAligned(pArg);
1827}
1828
1829uint64_t Parcel::readUint64() const
1830{
1831 return readAligned<uint64_t>();
1832}
1833
Serban Constantinescuf683e012013-11-05 16:53:55 +00001834status_t Parcel::readPointer(uintptr_t *pArg) const
1835{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001836 status_t ret;
1837 binder_uintptr_t ptr;
1838 ret = readAligned(&ptr);
1839 if (!ret)
1840 *pArg = ptr;
1841 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001842}
1843
1844uintptr_t Parcel::readPointer() const
1845{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001846 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001847}
1848
1849
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001850status_t Parcel::readFloat(float *pArg) const
1851{
Andreas Huber84a6d042009-08-17 13:33:27 -07001852 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001853}
1854
1855
1856float Parcel::readFloat() const
1857{
Andreas Huber84a6d042009-08-17 13:33:27 -07001858 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001859}
1860
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001861#if defined(__mips__) && defined(__mips_hard_float)
1862
1863status_t Parcel::readDouble(double *pArg) const
1864{
1865 union {
1866 double d;
1867 unsigned long long ll;
1868 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001869 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001870 status_t status;
1871 status = readAligned(&u.ll);
1872 *pArg = u.d;
1873 return status;
1874}
1875
1876double Parcel::readDouble() const
1877{
1878 union {
1879 double d;
1880 unsigned long long ll;
1881 } u;
1882 u.ll = readAligned<unsigned long long>();
1883 return u.d;
1884}
1885
1886#else
1887
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001888status_t Parcel::readDouble(double *pArg) const
1889{
Andreas Huber84a6d042009-08-17 13:33:27 -07001890 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001891}
1892
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001893double Parcel::readDouble() const
1894{
Andreas Huber84a6d042009-08-17 13:33:27 -07001895 return readAligned<double>();
1896}
1897
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001898#endif
1899
Andreas Huber84a6d042009-08-17 13:33:27 -07001900status_t Parcel::readIntPtr(intptr_t *pArg) const
1901{
1902 return readAligned(pArg);
1903}
1904
1905
1906intptr_t Parcel::readIntPtr() const
1907{
1908 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001909}
1910
Casey Dahlind6848f52015-10-15 15:44:59 -07001911status_t Parcel::readBool(bool *pArg) const
1912{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001913 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001914 status_t ret = readInt32(&tmp);
1915 *pArg = (tmp != 0);
1916 return ret;
1917}
1918
1919bool Parcel::readBool() const
1920{
1921 return readInt32() != 0;
1922}
1923
1924status_t Parcel::readChar(char16_t *pArg) const
1925{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001926 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001927 status_t ret = readInt32(&tmp);
1928 *pArg = char16_t(tmp);
1929 return ret;
1930}
1931
1932char16_t Parcel::readChar() const
1933{
1934 return char16_t(readInt32());
1935}
1936
1937status_t Parcel::readByte(int8_t *pArg) const
1938{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001939 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001940 status_t ret = readInt32(&tmp);
1941 *pArg = int8_t(tmp);
1942 return ret;
1943}
1944
1945int8_t Parcel::readByte() const
1946{
1947 return int8_t(readInt32());
1948}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001949
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001950status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1951 size_t utf16Size = 0;
1952 const char16_t* src = readString16Inplace(&utf16Size);
1953 if (!src) {
1954 return UNEXPECTED_NULL;
1955 }
1956
1957 // Save ourselves the trouble, we're done.
1958 if (utf16Size == 0u) {
1959 str->clear();
1960 return NO_ERROR;
1961 }
1962
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001963 // Allow for closing '\0'
1964 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1965 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001966 return BAD_VALUE;
1967 }
1968 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001969 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001970 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001971 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1972 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001973 return NO_ERROR;
1974}
1975
Jooyung Han2d5878e2020-01-23 12:45:10 +09001976status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const {
1977 const int32_t start = dataPosition();
1978 int32_t size;
1979 status_t status = readInt32(&size);
1980 str->reset();
1981
1982 if (status != OK || size < 0) {
1983 return status;
1984 }
1985
1986 setDataPosition(start);
1987 str->emplace();
1988 return readUtf8FromUtf16(&**str);
1989}
1990
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001991status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1992 const int32_t start = dataPosition();
1993 int32_t size;
1994 status_t status = readInt32(&size);
1995 str->reset();
1996
1997 if (status != OK || size < 0) {
1998 return status;
1999 }
2000
2001 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002002 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002003 return readUtf8FromUtf16(str->get());
2004}
2005
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002006const char* Parcel::readCString() const
2007{
Steven Morelandf5e6c7e2019-05-17 13:14:06 -07002008 if (mDataPos < mDataSize) {
2009 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002010 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2011 // is the string's trailing NUL within the parcel's valid bounds?
2012 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2013 if (eos) {
2014 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002015 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002016 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002017 return str;
2018 }
2019 }
Yi Kong91635562018-06-07 14:38:36 -07002020 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002021}
2022
2023String8 Parcel::readString8() const
2024{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002025 size_t len;
2026 const char* str = readString8Inplace(&len);
2027 if (str) return String8(str, len);
2028 ALOGE("Reading a NULL string not supported here.");
2029 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002030}
2031
2032status_t Parcel::readString8(String8* pArg) const
2033{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002034 size_t len;
2035 const char* str = readString8Inplace(&len);
2036 if (str) {
2037 pArg->setTo(str, len);
2038 return 0;
2039 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002040 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002041 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002042 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002043}
2044
2045const char* Parcel::readString8Inplace(size_t* outLen) const
2046{
2047 int32_t size = readInt32();
2048 // watch for potential int overflow from size+1
2049 if (size >= 0 && size < INT32_MAX) {
2050 *outLen = size;
2051 const char* str = (const char*)readInplace(size+1);
2052 if (str != nullptr) {
2053 return str;
2054 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002055 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002056 *outLen = 0;
2057 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002058}
2059
2060String16 Parcel::readString16() const
2061{
2062 size_t len;
2063 const char16_t* str = readString16Inplace(&len);
2064 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002065 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002066 return String16();
2067}
2068
Jooyung Han2d5878e2020-01-23 12:45:10 +09002069status_t Parcel::readString16(std::optional<String16>* pArg) const
2070{
2071 const int32_t start = dataPosition();
2072 int32_t size;
2073 status_t status = readInt32(&size);
2074 pArg->reset();
2075
2076 if (status != OK || size < 0) {
2077 return status;
2078 }
2079
2080 setDataPosition(start);
2081 pArg->emplace();
2082
2083 status = readString16(&**pArg);
2084
2085 if (status != OK) {
2086 pArg->reset();
2087 }
2088
2089 return status;
2090}
2091
Casey Dahlinb9872622015-11-25 15:09:45 -08002092status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2093{
2094 const int32_t start = dataPosition();
2095 int32_t size;
2096 status_t status = readInt32(&size);
2097 pArg->reset();
2098
2099 if (status != OK || size < 0) {
2100 return status;
2101 }
2102
2103 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002104 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002105
2106 status = readString16(pArg->get());
2107
2108 if (status != OK) {
2109 pArg->reset();
2110 }
2111
2112 return status;
2113}
2114
Casey Dahlin451ff582015-10-19 18:12:18 -07002115status_t Parcel::readString16(String16* pArg) const
2116{
2117 size_t len;
2118 const char16_t* str = readString16Inplace(&len);
2119 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002120 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002121 return 0;
2122 } else {
2123 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002124 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002125 }
2126}
2127
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002128const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2129{
2130 int32_t size = readInt32();
2131 // watch for potential int overflow from size+1
2132 if (size >= 0 && size < INT32_MAX) {
2133 *outLen = size;
2134 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002135 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002136 return str;
2137 }
2138 }
2139 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002140 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141}
2142
Casey Dahlinf0c13772015-10-27 18:33:56 -07002143status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2144{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002145 status_t status = readNullableStrongBinder(val);
2146 if (status == OK && !val->get()) {
2147 status = UNEXPECTED_NULL;
2148 }
2149 return status;
2150}
2151
2152status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2153{
Steven Morelanda86a3562019-08-01 23:28:34 +00002154 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002155}
2156
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002157sp<IBinder> Parcel::readStrongBinder() const
2158{
2159 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002160 // Note that a lot of code in Android reads binders by hand with this
2161 // method, and that code has historically been ok with getting nullptr
2162 // back (while ignoring error codes).
2163 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002164 return val;
2165}
2166
Christopher Wiley97f048d2015-11-19 06:49:05 -08002167status_t Parcel::readParcelable(Parcelable* parcelable) const {
2168 int32_t have_parcelable = 0;
2169 status_t status = readInt32(&have_parcelable);
2170 if (status != OK) {
2171 return status;
2172 }
2173 if (!have_parcelable) {
2174 return UNEXPECTED_NULL;
2175 }
2176 return parcelable->readFromParcel(this);
2177}
2178
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002179int32_t Parcel::readExceptionCode() const
2180{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002181 binder::Status status;
2182 status.readFromParcel(*this);
2183 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002184}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002185
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002186native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002187{
2188 int numFds, numInts;
2189 status_t err;
2190 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002191 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002192 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002193 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002194
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002195 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002196 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002197 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002198 }
2199
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002200 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002201 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002202 if (h->data[i] < 0) {
2203 for (int j = 0; j < i; j++) {
2204 close(h->data[j]);
2205 }
2206 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002207 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002208 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002209 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002210 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002211 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002212 native_handle_close(h);
2213 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002214 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002215 }
2216 return h;
2217}
2218
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002219int Parcel::readFileDescriptor() const
2220{
2221 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002222
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002223 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002224 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002225 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002226
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002227 return BAD_TYPE;
2228}
2229
Dianne Hackborn1941a402016-08-29 12:30:43 -07002230int Parcel::readParcelFileDescriptor() const
2231{
2232 int32_t hasComm = readInt32();
2233 int fd = readFileDescriptor();
2234 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002235 // detach (owned by the binder driver)
2236 int comm = readFileDescriptor();
2237
2238 // warning: this must be kept in sync with:
2239 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2240 enum ParcelFileDescriptorStatus {
2241 DETACHED = 2,
2242 };
2243
2244#if BYTE_ORDER == BIG_ENDIAN
2245 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2246#endif
2247#if BYTE_ORDER == LITTLE_ENDIAN
2248 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2249#endif
2250
2251 ssize_t written = TEMP_FAILURE_RETRY(
2252 ::write(comm, &message, sizeof(message)));
2253
2254 if (written == -1 || written != sizeof(message)) {
2255 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2256 written, strerror(errno));
2257 return BAD_TYPE;
2258 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002259 }
2260 return fd;
2261}
2262
Christopher Wiley2cf19952016-04-11 11:09:37 -07002263status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002264{
2265 int got = readFileDescriptor();
2266
2267 if (got == BAD_TYPE) {
2268 return BAD_TYPE;
2269 }
2270
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002271 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002272
2273 if (val->get() < 0) {
2274 return BAD_VALUE;
2275 }
2276
2277 return OK;
2278}
2279
Ryo Hashimotobf551892018-05-31 16:58:35 +09002280status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2281{
2282 int got = readParcelFileDescriptor();
2283
2284 if (got == BAD_TYPE) {
2285 return BAD_TYPE;
2286 }
2287
2288 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2289
2290 if (val->get() < 0) {
2291 return BAD_VALUE;
2292 }
2293
2294 return OK;
2295}
Casey Dahlin06673e32015-11-23 13:24:23 -08002296
Jooyung Han2d5878e2020-01-23 12:45:10 +09002297status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const {
2298 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2299}
2300
Christopher Wiley2cf19952016-04-11 11:09:37 -07002301status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002302 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2303}
2304
Christopher Wiley2cf19952016-04-11 11:09:37 -07002305status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002306 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2307}
2308
Jeff Brown5707dbf2011-09-23 21:17:56 -07002309status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2310{
Jeff Brown13b16042014-11-11 16:44:25 -08002311 int32_t blobType;
2312 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002313 if (status) return status;
2314
Jeff Brown13b16042014-11-11 16:44:25 -08002315 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002316 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002317 const void* ptr = readInplace(len);
2318 if (!ptr) return BAD_VALUE;
2319
Jeff Brown13b16042014-11-11 16:44:25 -08002320 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002321 return NO_ERROR;
2322 }
2323
Steve Block6807e592011-10-20 11:56:00 +01002324 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002325 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002326 int fd = readFileDescriptor();
2327 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2328
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002329 if (!ashmem_valid(fd)) {
2330 ALOGE("invalid fd");
2331 return BAD_VALUE;
2332 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002333 int size = ashmem_get_size_region(fd);
2334 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002335 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002336 return BAD_VALUE;
2337 }
Yi Kong91635562018-06-07 14:38:36 -07002338 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002339 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002340 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002341
Jeff Brown13b16042014-11-11 16:44:25 -08002342 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002343 return NO_ERROR;
2344}
2345
Mathias Agopiane1424282013-07-29 21:24:40 -07002346status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002347{
2348 // size
2349 const size_t len = this->readInt32();
2350 const size_t fd_count = this->readInt32();
2351
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002352 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002353 // don't accept size_t values which may have come from an
2354 // inadvertent conversion from a negative int.
2355 return BAD_VALUE;
2356 }
2357
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002358 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002359 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002360 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002361 return BAD_VALUE;
2362
Yi Kong91635562018-06-07 14:38:36 -07002363 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002364 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002365 fds = new (std::nothrow) int[fd_count];
2366 if (fds == nullptr) {
2367 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2368 return BAD_VALUE;
2369 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002370 }
2371
2372 status_t err = NO_ERROR;
2373 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002374 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002375 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002376 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002377 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 -07002378 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2379 // Close all the file descriptors that were dup-ed.
2380 for (size_t j=0; j<i ;j++) {
2381 close(fds[j]);
2382 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002383 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002384 }
2385
2386 if (err == NO_ERROR) {
2387 err = val.unflatten(buf, len, fds, fd_count);
2388 }
2389
2390 if (fd_count) {
2391 delete [] fds;
2392 }
2393
2394 return err;
2395}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002396const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2397{
2398 const size_t DPOS = mDataPos;
2399 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2400 const flat_binder_object* obj
2401 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2402 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002403 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002404 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002405 // the object list, so we don't want to check for it when
2406 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002407 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002408 return obj;
2409 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002410
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002411 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002412 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002413 const size_t N = mObjectsSize;
2414 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002415
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002416 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002417 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002418 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002419
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002420 // Start at the current hint position, looking for an object at
2421 // the current data position.
2422 if (opos < N) {
2423 while (opos < (N-1) && OBJS[opos] < DPOS) {
2424 opos++;
2425 }
2426 } else {
2427 opos = N-1;
2428 }
2429 if (OBJS[opos] == DPOS) {
2430 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002431 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002432 this, DPOS, opos);
2433 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002434 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435 return obj;
2436 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002437
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002438 // Look backwards for it...
2439 while (opos > 0 && OBJS[opos] > DPOS) {
2440 opos--;
2441 }
2442 if (OBJS[opos] == DPOS) {
2443 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002444 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002445 this, DPOS, opos);
2446 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002447 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002448 return obj;
2449 }
2450 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002451 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 -07002452 this, DPOS);
2453 }
Yi Kong91635562018-06-07 14:38:36 -07002454 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002455}
2456
2457void Parcel::closeFileDescriptors()
2458{
2459 size_t i = mObjectsSize;
2460 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002461 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002462 }
2463 while (i > 0) {
2464 i--;
2465 const flat_binder_object* flat
2466 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002467 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002468 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002469 close(flat->handle);
2470 }
2471 }
2472}
2473
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002474uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002475{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002476 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002477}
2478
2479size_t Parcel::ipcDataSize() const
2480{
2481 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2482}
2483
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002484uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002485{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002486 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002487}
2488
2489size_t Parcel::ipcObjectsCount() const
2490{
2491 return mObjectsSize;
2492}
2493
2494void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002495 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002496{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002497 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002498 freeDataNoInit();
2499 mError = NO_ERROR;
2500 mData = const_cast<uint8_t*>(data);
2501 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002502 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002503 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002504 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002505 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002506 mObjectsSize = mObjectsCapacity = objectsCount;
2507 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002508 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002509 mOwner = relFunc;
2510 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002511 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002512 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002513 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002514 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002515 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002516 mObjectsSize = 0;
2517 break;
2518 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002519 const flat_binder_object* flat
2520 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2521 uint32_t type = flat->hdr.type;
2522 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2523 type == BINDER_TYPE_FD)) {
2524 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2525 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2526 // recover gracefully by clearing out the objects, and releasing the objects we do
2527 // know about.
2528 android_errorWriteLog(0x534e4554, "135930648");
2529 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2530 __func__, type, (uint64_t)offset);
2531 releaseObjects();
2532 mObjectsSize = 0;
2533 break;
2534 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002535 minOffset = offset + sizeof(flat_binder_object);
2536 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002537 scanForFds();
2538}
2539
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002540void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002541{
2542 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002543
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544 if (errorCheck() != NO_ERROR) {
2545 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002546 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002547 } else if (dataSize() > 0) {
2548 const uint8_t* DATA = data();
2549 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002550 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002551 const size_t N = objectsCount();
2552 for (size_t i=0; i<N; i++) {
2553 const flat_binder_object* flat
2554 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2555 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002556 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002557 << " = " << flat->binder;
2558 }
2559 } else {
2560 to << "NULL";
2561 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002562
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002563 to << ")";
2564}
2565
2566void Parcel::releaseObjects()
2567{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002568 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002569 if (i == 0) {
2570 return;
2571 }
2572 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002573 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002574 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002575 while (i > 0) {
2576 i--;
2577 const flat_binder_object* flat
2578 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002579 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002580 }
2581}
2582
2583void Parcel::acquireObjects()
2584{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002585 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002586 if (i == 0) {
2587 return;
2588 }
2589 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002590 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002591 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002592 while (i > 0) {
2593 i--;
2594 const flat_binder_object* flat
2595 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002596 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002597 }
2598}
2599
2600void Parcel::freeData()
2601{
2602 freeDataNoInit();
2603 initState();
2604}
2605
2606void Parcel::freeDataNoInit()
2607{
2608 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002609 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002610 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002611 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2612 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002613 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002614 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002615 if (mData) {
2616 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002617 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002618 if (mDataCapacity <= gParcelGlobalAllocSize) {
2619 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2620 } else {
2621 gParcelGlobalAllocSize = 0;
2622 }
2623 if (gParcelGlobalAllocCount > 0) {
2624 gParcelGlobalAllocCount--;
2625 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002626 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002627 free(mData);
2628 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002629 if (mObjects) free(mObjects);
2630 }
2631}
2632
2633status_t Parcel::growData(size_t len)
2634{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002635 if (len > INT32_MAX) {
2636 // don't accept size_t values which may have come from an
2637 // inadvertent conversion from a negative int.
2638 return BAD_VALUE;
2639 }
2640
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01002641 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2642 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002643 size_t newSize = ((mDataSize+len)*3)/2;
2644 return (newSize <= mDataSize)
2645 ? (status_t) NO_MEMORY
2646 : continueWrite(newSize);
2647}
2648
2649status_t Parcel::restartWrite(size_t desired)
2650{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002651 if (desired > INT32_MAX) {
2652 // don't accept size_t values which may have come from an
2653 // inadvertent conversion from a negative int.
2654 return BAD_VALUE;
2655 }
2656
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002657 if (mOwner) {
2658 freeData();
2659 return continueWrite(desired);
2660 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002661
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002662 uint8_t* data = (uint8_t*)realloc(mData, desired);
2663 if (!data && desired > mDataCapacity) {
2664 mError = NO_MEMORY;
2665 return NO_MEMORY;
2666 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002667
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002668 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002669
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002670 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002671 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002672 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002673 gParcelGlobalAllocSize += desired;
2674 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002675 if (!mData) {
2676 gParcelGlobalAllocCount++;
2677 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002678 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002679 mData = data;
2680 mDataCapacity = desired;
2681 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002682
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002683 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002684 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2685 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2686
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002687 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002688 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002689 mObjectsSize = mObjectsCapacity = 0;
2690 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002691 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002692 mHasFds = false;
2693 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002694 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002695
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002696 return NO_ERROR;
2697}
2698
2699status_t Parcel::continueWrite(size_t desired)
2700{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002701 if (desired > INT32_MAX) {
2702 // don't accept size_t values which may have come from an
2703 // inadvertent conversion from a negative int.
2704 return BAD_VALUE;
2705 }
2706
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002707 // If shrinking, first adjust for any objects that appear
2708 // after the new data size.
2709 size_t objectsSize = mObjectsSize;
2710 if (desired < mDataSize) {
2711 if (desired == 0) {
2712 objectsSize = 0;
2713 } else {
2714 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002715 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002716 break;
2717 objectsSize--;
2718 }
2719 }
2720 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002721
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002722 if (mOwner) {
2723 // If the size is going to zero, just release the owner's data.
2724 if (desired == 0) {
2725 freeData();
2726 return NO_ERROR;
2727 }
2728
2729 // If there is a different owner, we need to take
2730 // posession.
2731 uint8_t* data = (uint8_t*)malloc(desired);
2732 if (!data) {
2733 mError = NO_MEMORY;
2734 return NO_MEMORY;
2735 }
Yi Kong91635562018-06-07 14:38:36 -07002736 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002737
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002738 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002739 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002740 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002741 free(data);
2742
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002743 mError = NO_MEMORY;
2744 return NO_MEMORY;
2745 }
2746
2747 // Little hack to only acquire references on objects
2748 // we will be keeping.
2749 size_t oldObjectsSize = mObjectsSize;
2750 mObjectsSize = objectsSize;
2751 acquireObjects();
2752 mObjectsSize = oldObjectsSize;
2753 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002754
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002755 if (mData) {
2756 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2757 }
2758 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002759 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002760 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002761 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002762 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002763 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002764
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002765 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002766 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002767 gParcelGlobalAllocSize += desired;
2768 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002769 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002770
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002771 mData = data;
2772 mObjects = objects;
2773 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002774 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002775 mDataCapacity = desired;
2776 mObjectsSize = mObjectsCapacity = objectsSize;
2777 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002778 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002779
2780 } else if (mData) {
2781 if (objectsSize < mObjectsSize) {
2782 // Need to release refs on any objects we are dropping.
2783 const sp<ProcessState> proc(ProcessState::self());
2784 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2785 const flat_binder_object* flat
2786 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002787 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002788 // will need to rescan because we may have lopped off the only FDs
2789 mFdsKnown = false;
2790 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002791 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002792 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002793
2794 if (objectsSize == 0) {
2795 free(mObjects);
2796 mObjects = nullptr;
Michael Wachenschwanzc67d9f32019-10-15 11:49:22 -07002797 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002798 } else {
2799 binder_size_t* objects =
2800 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2801 if (objects) {
2802 mObjects = objects;
Michael Wachenschwanzc67d9f32019-10-15 11:49:22 -07002803 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002804 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002805 }
2806 mObjectsSize = objectsSize;
2807 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002808 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002809 }
2810
2811 // We own the data, so we can just do a realloc().
2812 if (desired > mDataCapacity) {
2813 uint8_t* data = (uint8_t*)realloc(mData, desired);
2814 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002815 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2816 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002817 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002818 gParcelGlobalAllocSize += desired;
2819 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002820 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002821 mData = data;
2822 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002823 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002824 mError = NO_MEMORY;
2825 return NO_MEMORY;
2826 }
2827 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002828 if (mDataSize > desired) {
2829 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002830 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002831 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002832 if (mDataPos > desired) {
2833 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002834 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002835 }
2836 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002837
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002838 } else {
2839 // This is the first data. Easy!
2840 uint8_t* data = (uint8_t*)malloc(desired);
2841 if (!data) {
2842 mError = NO_MEMORY;
2843 return NO_MEMORY;
2844 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002845
Yi Kong91635562018-06-07 14:38:36 -07002846 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002847 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002848 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002849 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002850
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002851 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002852 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002853 gParcelGlobalAllocSize += desired;
2854 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002855 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002856
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002857 mData = data;
2858 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002859 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2860 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002861 mDataCapacity = desired;
2862 }
2863
2864 return NO_ERROR;
2865}
2866
2867void Parcel::initState()
2868{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002869 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002870 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002871 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002872 mDataSize = 0;
2873 mDataCapacity = 0;
2874 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002875 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2876 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002877 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002878 mObjectsSize = 0;
2879 mObjectsCapacity = 0;
2880 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002881 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002882 mHasFds = false;
2883 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002884 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002885 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002886 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002887 mWorkSourceRequestHeaderPosition = 0;
2888 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002889
2890 // racing multiple init leads only to multiple identical write
2891 if (gMaxFds == 0) {
2892 struct rlimit result;
2893 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2894 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002895 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002896 } else {
2897 ALOGW("Unable to getrlimit: %s", strerror(errno));
2898 gMaxFds = 1024;
2899 }
2900 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002901}
2902
2903void Parcel::scanForFds() const
2904{
2905 bool hasFds = false;
2906 for (size_t i=0; i<mObjectsSize; i++) {
2907 const flat_binder_object* flat
2908 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002909 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002910 hasFds = true;
2911 break;
2912 }
2913 }
2914 mHasFds = hasFds;
2915 mFdsKnown = true;
2916}
2917
Dan Sandleraa5c2342015-04-10 10:08:45 -04002918size_t Parcel::getBlobAshmemSize() const
2919{
Adrian Roos6bb31142015-10-22 16:46:12 -07002920 // This used to return the size of all blobs that were written to ashmem, now we're returning
2921 // the ashmem currently referenced by this Parcel, which should be equivalent.
2922 // TODO: Remove method once ABI can be changed.
2923 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002924}
2925
Adrian Rooscbf37262015-10-22 16:12:53 -07002926size_t Parcel::getOpenAshmemSize() const
2927{
2928 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002929}
2930
2931// --- Parcel::Blob ---
2932
2933Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002934 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002935}
2936
2937Parcel::Blob::~Blob() {
2938 release();
2939}
2940
2941void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002942 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002943 ::munmap(mData, mSize);
2944 }
2945 clear();
2946}
2947
Jeff Brown13b16042014-11-11 16:44:25 -08002948void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2949 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002950 mData = data;
2951 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002952 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002953}
2954
2955void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002956 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002957 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002958 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002959 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002960}
2961
Steven Moreland6511af52019-09-26 16:05:45 -07002962} // namespace android