blob: 19f3606bfaa00689d6668b60d99a5696256f477f [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>
Steven Morelandbf1915b2020-07-16 22:43:02 +000023#include <linux/sched.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080024#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080029#include <sys/stat.h>
30#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070031#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080032#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070033
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/Binder.h>
35#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080036#include <binder/IPCThreadState.h>
37#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070038#include <binder/ProcessState.h>
Steven Moreland6e5a7752019-08-05 20:30:14 -070039#include <binder/Stability.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080040#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041#include <binder/TextOutput.h>
42
Mark Salyzynabed7f72016-01-27 08:02:48 -080043#include <cutils/ashmem.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
Steven Morelandbf1915b2020-07-16 22:43:02 +0000191static constexpr inline int schedPolicyMask(int policy, int priority) {
192 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
193}
194
Steven Morelanda86a3562019-08-01 23:28:34 +0000195status_t Parcel::flattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700196{
197 flat_binder_object obj;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000198 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700199
Steven Morelandbf1915b2020-07-16 22:43:02 +0000200 int schedBits = 0;
201 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
202 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700203 }
204
Yi Kong91635562018-06-07 14:38:36 -0700205 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800206 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700207 if (!local) {
208 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700209 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000210 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700211 }
212 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700213 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800214 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700215 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800216 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700217 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000218 int policy = local->getMinSchedulerPolicy();
219 int priority = local->getMinSchedulerPriority();
220
221 if (policy != 0 || priority != 0) {
222 // override value, since it is set explicitly
223 schedBits = schedPolicyMask(policy, priority);
224 }
Steven Morelandf0212002018-12-26 13:59:23 -0800225 if (local->isRequestingSid()) {
226 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
227 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700228 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800229 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
230 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 }
232 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700233 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800234 obj.binder = 0;
235 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700236 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700237
Steven Morelandbf1915b2020-07-16 22:43:02 +0000238 obj.flags |= schedBits;
239
Steven Morelanda86a3562019-08-01 23:28:34 +0000240 return finishFlattenBinder(binder, obj);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700241}
242
Steven Morelanda86a3562019-08-01 23:28:34 +0000243status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700244{
Steven Morelanda86a3562019-08-01 23:28:34 +0000245 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700246
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700247 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700248 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000249 case BINDER_TYPE_BINDER: {
250 sp<IBinder> binder = reinterpret_cast<IBinder*>(flat->cookie);
251 return finishUnflattenBinder(binder, out);
252 }
253 case BINDER_TYPE_HANDLE: {
254 sp<IBinder> binder =
255 ProcessState::self()->getStrongProxyForHandle(flat->handle);
256 return finishUnflattenBinder(binder, out);
257 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700258 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700259 }
260 return BAD_TYPE;
261}
262
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700263// ---------------------------------------------------------------------------
264
265Parcel::Parcel()
266{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800267 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700268 initState();
269}
270
271Parcel::~Parcel()
272{
273 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800274 LOG_ALLOC("Parcel %p: destroyed", this);
275}
276
277size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800278 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
279 size_t size = gParcelGlobalAllocSize;
280 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
281 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800282}
283
284size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800285 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
286 size_t count = gParcelGlobalAllocCount;
287 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
288 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700289}
290
291const uint8_t* Parcel::data() const
292{
293 return mData;
294}
295
296size_t Parcel::dataSize() const
297{
298 return (mDataSize > mDataPos ? mDataSize : mDataPos);
299}
300
301size_t Parcel::dataAvail() const
302{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700303 size_t result = dataSize() - dataPosition();
304 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700305 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700306 }
307 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700308}
309
310size_t Parcel::dataPosition() const
311{
312 return mDataPos;
313}
314
315size_t Parcel::dataCapacity() const
316{
317 return mDataCapacity;
318}
319
320status_t Parcel::setDataSize(size_t size)
321{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700322 if (size > INT32_MAX) {
323 // don't accept size_t values which may have come from an
324 // inadvertent conversion from a negative int.
325 return BAD_VALUE;
326 }
327
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700328 status_t err;
329 err = continueWrite(size);
330 if (err == NO_ERROR) {
331 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700332 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700333 }
334 return err;
335}
336
337void Parcel::setDataPosition(size_t pos) const
338{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700339 if (pos > INT32_MAX) {
340 // don't accept size_t values which may have come from an
341 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700342 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700343 }
344
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700345 mDataPos = pos;
346 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800347 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700348}
349
350status_t Parcel::setDataCapacity(size_t size)
351{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700352 if (size > INT32_MAX) {
353 // don't accept size_t values which may have come from an
354 // inadvertent conversion from a negative int.
355 return BAD_VALUE;
356 }
357
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700358 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700359 return NO_ERROR;
360}
361
362status_t Parcel::setData(const uint8_t* buffer, size_t len)
363{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700364 if (len > INT32_MAX) {
365 // don't accept size_t values which may have come from an
366 // inadvertent conversion from a negative int.
367 return BAD_VALUE;
368 }
369
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700370 status_t err = restartWrite(len);
371 if (err == NO_ERROR) {
372 memcpy(const_cast<uint8_t*>(data()), buffer, len);
373 mDataSize = len;
374 mFdsKnown = false;
375 }
376 return err;
377}
378
Andreas Huber51faf462011-04-13 10:21:56 -0700379status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700380{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700381 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700382 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800383 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700384 size_t size = parcel->mObjectsSize;
385 int startPos = mDataPos;
386 int firstIndex = -1, lastIndex = -2;
387
388 if (len == 0) {
389 return NO_ERROR;
390 }
391
Nick Kralevichb6b14232015-04-02 09:36:02 -0700392 if (len > INT32_MAX) {
393 // don't accept size_t values which may have come from an
394 // inadvertent conversion from a negative int.
395 return BAD_VALUE;
396 }
397
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700398 // range checks against the source parcel size
399 if ((offset > parcel->mDataSize)
400 || (len > parcel->mDataSize)
401 || (offset + len > parcel->mDataSize)) {
402 return BAD_VALUE;
403 }
404
405 // Count objects in range
406 for (int i = 0; i < (int) size; i++) {
407 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700408 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700409 if (firstIndex == -1) {
410 firstIndex = i;
411 }
412 lastIndex = i;
413 }
414 }
415 int numObjects = lastIndex - firstIndex + 1;
416
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700417 if ((mDataSize+len) > mDataCapacity) {
418 // grow data
419 err = growData(len);
420 if (err != NO_ERROR) {
421 return err;
422 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700423 }
424
425 // append data
426 memcpy(mData + mDataPos, data + offset, len);
427 mDataPos += len;
428 mDataSize += len;
429
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400430 err = NO_ERROR;
431
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700432 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200433 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 // grow objects
435 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +0100436 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
437 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700438 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +0100439 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800440 binder_size_t *objects =
441 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700442 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700443 return NO_MEMORY;
444 }
445 mObjects = objects;
446 mObjectsCapacity = newSize;
447 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700448
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700449 // append and acquire objects
450 int idx = mObjectsSize;
451 for (int i = firstIndex; i <= lastIndex; i++) {
452 size_t off = objects[i] - offset + startPos;
453 mObjects[idx++] = off;
454 mObjectsSize++;
455
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700456 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700457 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700458 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700460 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700461 // If this is a file descriptor, we need to dup it so the
462 // new Parcel now owns its own fd, and can declare that we
463 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800464 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800465 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700466 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400467 if (!mAllowFds) {
468 err = FDS_NOT_ALLOWED;
469 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700470 }
471 }
472 }
473
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400474 return err;
475}
476
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700477int Parcel::compareData(const Parcel& other) {
478 size_t size = dataSize();
479 if (size != other.dataSize()) {
480 return size < other.dataSize() ? -1 : 1;
481 }
482 return memcmp(data(), other.data(), size);
483}
484
Jeff Brown13b16042014-11-11 16:44:25 -0800485bool Parcel::allowFds() const
486{
487 return mAllowFds;
488}
489
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700490bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400491{
492 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700493 if (!allowFds) {
494 mAllowFds = false;
495 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400496 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700497}
498
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700499void Parcel::restoreAllowFds(bool lastValue)
500{
501 mAllowFds = lastValue;
502}
503
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700504bool Parcel::hasFileDescriptors() const
505{
506 if (!mFdsKnown) {
507 scanForFds();
508 }
509 return mHasFds;
510}
511
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000512void Parcel::updateWorkSourceRequestHeaderPosition() const {
513 // Only update the request headers once. We only want to point
514 // to the first headers read/written.
515 if (!mRequestHeaderPresent) {
516 mWorkSourceRequestHeaderPosition = dataPosition();
517 mRequestHeaderPresent = true;
518 }
519}
520
Jooyung Han1b228f42020-01-30 13:41:12 +0900521#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700522constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
523#else
524constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
525#endif
526
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700527// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700528status_t Parcel::writeInterfaceToken(const String16& interface)
529{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000530 const IPCThreadState* threadState = IPCThreadState::self();
531 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000532 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000533 writeInt32(threadState->shouldPropagateWorkSource() ?
534 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700535 writeInt32(kHeader);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700536 // currently the interface identification token is just its name as a string
537 return writeString16(interface);
538}
539
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000540bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
541{
542 if (!mRequestHeaderPresent) {
543 return false;
544 }
545
546 const size_t initialPosition = dataPosition();
547 setDataPosition(mWorkSourceRequestHeaderPosition);
548 status_t err = writeInt32(uid);
549 setDataPosition(initialPosition);
550 return err == NO_ERROR;
551}
552
Steven Morelandf1b1e492019-05-06 15:05:13 -0700553uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000554{
555 if (!mRequestHeaderPresent) {
556 return IPCThreadState::kUnsetWorkSource;
557 }
558
559 const size_t initialPosition = dataPosition();
560 setDataPosition(mWorkSourceRequestHeaderPosition);
561 uid_t uid = readInt32();
562 setDataPosition(initialPosition);
563 return uid;
564}
565
Mathias Agopian83c04462009-05-22 19:00:22 -0700566bool Parcel::checkInterface(IBinder* binder) const
567{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700568 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700569}
570
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700571bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700572 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700573{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700574 return enforceInterface(interface.string(), interface.size(), threadState);
575}
576
577bool Parcel::enforceInterface(const char16_t* interface,
578 size_t len,
579 IPCThreadState* threadState) const
580{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100581 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700582 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700583 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700584 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700585 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700586 if ((threadState->getLastTransactionBinderFlags() &
587 IBinder::FLAG_ONEWAY) != 0) {
588 // For one-way calls, the callee is running entirely
589 // disconnected from the caller, so disable StrictMode entirely.
590 // Not only does disk/network usage not impact the caller, but
591 // there's no way to commuicate back any violations anyway.
592 threadState->setStrictModePolicy(0);
593 } else {
594 threadState->setStrictModePolicy(strictPolicy);
595 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100596 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000597 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100598 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000599 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700600 // vendor header
601 int32_t header = readInt32();
602 if (header != kHeader) {
603 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
604 return false;
605 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100606 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700607 size_t parcel_interface_len;
608 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
609 if (len == parcel_interface_len &&
610 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700611 return true;
612 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700613 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700614 String8(interface, len).string(),
615 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700616 return false;
617 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700618}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700619
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620size_t Parcel::objectsCount() const
621{
622 return mObjectsSize;
623}
624
625status_t Parcel::errorCheck() const
626{
627 return mError;
628}
629
630void Parcel::setError(status_t err)
631{
632 mError = err;
633}
634
635status_t Parcel::finishWrite(size_t len)
636{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700637 if (len > INT32_MAX) {
638 // don't accept size_t values which may have come from an
639 // inadvertent conversion from a negative int.
640 return BAD_VALUE;
641 }
642
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700643 //printf("Finish write of %d\n", len);
644 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700645 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700646 if (mDataPos > mDataSize) {
647 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700648 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700649 }
650 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
651 return NO_ERROR;
652}
653
654status_t Parcel::writeUnpadded(const void* data, size_t len)
655{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700656 if (len > INT32_MAX) {
657 // don't accept size_t values which may have come from an
658 // inadvertent conversion from a negative int.
659 return BAD_VALUE;
660 }
661
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700662 size_t end = mDataPos + len;
663 if (end < mDataPos) {
664 // integer overflow
665 return BAD_VALUE;
666 }
667
668 if (end <= mDataCapacity) {
669restart_write:
670 memcpy(mData+mDataPos, data, len);
671 return finishWrite(len);
672 }
673
674 status_t err = growData(len);
675 if (err == NO_ERROR) goto restart_write;
676 return err;
677}
678
679status_t Parcel::write(const void* data, size_t len)
680{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700681 if (len > INT32_MAX) {
682 // don't accept size_t values which may have come from an
683 // inadvertent conversion from a negative int.
684 return BAD_VALUE;
685 }
686
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700687 void* const d = writeInplace(len);
688 if (d) {
689 memcpy(d, data, len);
690 return NO_ERROR;
691 }
692 return mError;
693}
694
695void* Parcel::writeInplace(size_t len)
696{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700697 if (len > INT32_MAX) {
698 // don't accept size_t values which may have come from an
699 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700700 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700701 }
702
703 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700704
705 // sanity check for integer overflow
706 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700707 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700708 }
709
710 if ((mDataPos+padded) <= mDataCapacity) {
711restart_write:
712 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
713 uint8_t* const data = mData+mDataPos;
714
715 // Need to pad at end?
716 if (padded != len) {
717#if BYTE_ORDER == BIG_ENDIAN
718 static const uint32_t mask[4] = {
719 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
720 };
721#endif
722#if BYTE_ORDER == LITTLE_ENDIAN
723 static const uint32_t mask[4] = {
724 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
725 };
726#endif
727 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
728 // *reinterpret_cast<void**>(data+padded-4));
729 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
730 }
731
732 finishWrite(padded);
733 return data;
734 }
735
736 status_t err = growData(padded);
737 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700738 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700739}
740
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800741status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
742 const uint8_t* strData = (uint8_t*)str.data();
743 const size_t strLen= str.length();
744 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100745 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800746 return BAD_VALUE;
747 }
748
749 status_t err = writeInt32(utf16Len);
750 if (err) {
751 return err;
752 }
753
754 // Allocate enough bytes to hold our converted string and its terminating NULL.
755 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
756 if (!dst) {
757 return NO_MEMORY;
758 }
759
Sergio Girof4607432016-07-21 14:46:35 +0100760 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800761
762 return NO_ERROR;
763}
764
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900765status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) {
766 if (!str) {
767 return writeInt32(-1);
768 }
769 return writeUtf8AsUtf16(*str);
770}
771
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800772status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
773 if (!str) {
774 return writeInt32(-1);
775 }
776 return writeUtf8AsUtf16(*str);
777}
778
Daniel Normand0337ef2019-09-20 15:46:03 -0700779status_t Parcel::writeByteVectorInternal(const int8_t* data, size_t size) {
780 if (size > std::numeric_limits<int32_t>::max()) {
781 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -0700782 }
783
Daniel Normand0337ef2019-09-20 15:46:03 -0700784 status_t status = writeInt32(size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700785 if (status != OK) {
786 return status;
787 }
788
Daniel Normand0337ef2019-09-20 15:46:03 -0700789 return write(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700790}
791
Casey Dahlin185d3442016-02-09 11:08:35 -0800792status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700793 return writeByteVectorInternal(val.data(), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800794}
795
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900796status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val)
797{
798 if (!val) return writeInt32(-1);
799 return writeByteVectorInternal(val->data(), val->size());
800}
801
Casey Dahlin185d3442016-02-09 11:08:35 -0800802status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
803{
Daniel Normand0337ef2019-09-20 15:46:03 -0700804 if (!val) return writeInt32(-1);
805 return writeByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800806}
807
808status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700809 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val.data()), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800810}
811
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900812status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val)
813{
814 if (!val) return writeInt32(-1);
815 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
816}
817
Casey Dahlin185d3442016-02-09 11:08:35 -0800818status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
819{
Daniel Normand0337ef2019-09-20 15:46:03 -0700820 if (!val) return writeInt32(-1);
821 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800822}
823
Casey Dahlin451ff582015-10-19 18:12:18 -0700824status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
825{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800826 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700827}
828
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900829status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val)
830{
831 return writeNullableTypedVector(val, &Parcel::writeInt32);
832}
833
Casey Dahlinb9872622015-11-25 15:09:45 -0800834status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
835{
836 return writeNullableTypedVector(val, &Parcel::writeInt32);
837}
838
Casey Dahlin451ff582015-10-19 18:12:18 -0700839status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
840{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800841 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700842}
843
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900844status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val)
845{
846 return writeNullableTypedVector(val, &Parcel::writeInt64);
847}
848
Casey Dahlinb9872622015-11-25 15:09:45 -0800849status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
850{
851 return writeNullableTypedVector(val, &Parcel::writeInt64);
852}
853
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800854status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
855{
856 return writeTypedVector(val, &Parcel::writeUint64);
857}
858
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900859status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val)
860{
861 return writeNullableTypedVector(val, &Parcel::writeUint64);
862}
863
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800864status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
865{
866 return writeNullableTypedVector(val, &Parcel::writeUint64);
867}
868
Casey Dahlin451ff582015-10-19 18:12:18 -0700869status_t Parcel::writeFloatVector(const std::vector<float>& val)
870{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800871 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700872}
873
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900874status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val)
875{
876 return writeNullableTypedVector(val, &Parcel::writeFloat);
877}
878
Casey Dahlinb9872622015-11-25 15:09:45 -0800879status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
880{
881 return writeNullableTypedVector(val, &Parcel::writeFloat);
882}
883
Casey Dahlin451ff582015-10-19 18:12:18 -0700884status_t Parcel::writeDoubleVector(const std::vector<double>& val)
885{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800886 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700887}
888
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900889status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val)
890{
891 return writeNullableTypedVector(val, &Parcel::writeDouble);
892}
893
Casey Dahlinb9872622015-11-25 15:09:45 -0800894status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
895{
896 return writeNullableTypedVector(val, &Parcel::writeDouble);
897}
898
Casey Dahlin451ff582015-10-19 18:12:18 -0700899status_t Parcel::writeBoolVector(const std::vector<bool>& val)
900{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800901 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700902}
903
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900904status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val)
905{
906 return writeNullableTypedVector(val, &Parcel::writeBool);
907}
908
Casey Dahlinb9872622015-11-25 15:09:45 -0800909status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
910{
911 return writeNullableTypedVector(val, &Parcel::writeBool);
912}
913
Casey Dahlin451ff582015-10-19 18:12:18 -0700914status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
915{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800916 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700917}
918
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900919status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val)
920{
921 return writeNullableTypedVector(val, &Parcel::writeChar);
922}
923
Casey Dahlinb9872622015-11-25 15:09:45 -0800924status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
925{
926 return writeNullableTypedVector(val, &Parcel::writeChar);
927}
928
Casey Dahlin451ff582015-10-19 18:12:18 -0700929status_t Parcel::writeString16Vector(const std::vector<String16>& val)
930{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800931 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700932}
933
Casey Dahlinb9872622015-11-25 15:09:45 -0800934status_t Parcel::writeString16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900935 const std::optional<std::vector<std::optional<String16>>>& val)
936{
937 return writeNullableTypedVector(val, &Parcel::writeString16);
938}
939
940status_t Parcel::writeString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800941 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
942{
943 return writeNullableTypedVector(val, &Parcel::writeString16);
944}
945
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800946status_t Parcel::writeUtf8VectorAsUtf16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900947 const std::optional<std::vector<std::optional<std::string>>>& val) {
948 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
949}
950
951status_t Parcel::writeUtf8VectorAsUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800952 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
953 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
954}
955
956status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
957 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
958}
959
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700960status_t Parcel::writeInt32(int32_t val)
961{
Andreas Huber84a6d042009-08-17 13:33:27 -0700962 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700963}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800964
965status_t Parcel::writeUint32(uint32_t val)
966{
967 return writeAligned(val);
968}
969
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700970status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700971 if (len > INT32_MAX) {
972 // don't accept size_t values which may have come from an
973 // inadvertent conversion from a negative int.
974 return BAD_VALUE;
975 }
976
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700977 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700978 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700979 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700980 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700981 if (ret == NO_ERROR) {
982 ret = write(val, len * sizeof(*val));
983 }
984 return ret;
985}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700986status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700987 if (len > INT32_MAX) {
988 // don't accept size_t values which may have come from an
989 // inadvertent conversion from a negative int.
990 return BAD_VALUE;
991 }
992
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700993 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700994 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700995 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700996 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700997 if (ret == NO_ERROR) {
998 ret = write(val, len * sizeof(*val));
999 }
1000 return ret;
1001}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001002
Casey Dahlind6848f52015-10-15 15:44:59 -07001003status_t Parcel::writeBool(bool val)
1004{
1005 return writeInt32(int32_t(val));
1006}
1007
1008status_t Parcel::writeChar(char16_t val)
1009{
1010 return writeInt32(int32_t(val));
1011}
1012
1013status_t Parcel::writeByte(int8_t val)
1014{
1015 return writeInt32(int32_t(val));
1016}
1017
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001018status_t Parcel::writeInt64(int64_t val)
1019{
Andreas Huber84a6d042009-08-17 13:33:27 -07001020 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001021}
1022
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001023status_t Parcel::writeUint64(uint64_t val)
1024{
1025 return writeAligned(val);
1026}
1027
Serban Constantinescuf683e012013-11-05 16:53:55 +00001028status_t Parcel::writePointer(uintptr_t val)
1029{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001030 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001031}
1032
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001033status_t Parcel::writeFloat(float val)
1034{
Andreas Huber84a6d042009-08-17 13:33:27 -07001035 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001036}
1037
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001038#if defined(__mips__) && defined(__mips_hard_float)
1039
1040status_t Parcel::writeDouble(double val)
1041{
1042 union {
1043 double d;
1044 unsigned long long ll;
1045 } u;
1046 u.d = val;
1047 return writeAligned(u.ll);
1048}
1049
1050#else
1051
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001052status_t Parcel::writeDouble(double val)
1053{
Andreas Huber84a6d042009-08-17 13:33:27 -07001054 return writeAligned(val);
1055}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001056
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001057#endif
1058
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001059status_t Parcel::writeCString(const char* str)
1060{
1061 return write(str, strlen(str)+1);
1062}
1063
1064status_t Parcel::writeString8(const String8& str)
1065{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001066 return writeString8(str.string(), str.size());
1067}
1068
1069status_t Parcel::writeString8(const char* str, size_t len)
1070{
1071 if (str == nullptr) return writeInt32(-1);
1072
1073 status_t err = writeInt32(len);
1074 if (err == NO_ERROR) {
1075 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1076 if (data) {
1077 memcpy(data, str, len);
1078 *reinterpret_cast<char*>(data+len) = 0;
1079 return NO_ERROR;
1080 }
1081 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001082 }
1083 return err;
1084}
1085
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001086status_t Parcel::writeString16(const std::optional<String16>& str)
1087{
1088 if (!str) {
1089 return writeInt32(-1);
1090 }
1091
1092 return writeString16(*str);
1093}
1094
Casey Dahlinb9872622015-11-25 15:09:45 -08001095status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1096{
1097 if (!str) {
1098 return writeInt32(-1);
1099 }
1100
1101 return writeString16(*str);
1102}
1103
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001104status_t Parcel::writeString16(const String16& str)
1105{
1106 return writeString16(str.string(), str.size());
1107}
1108
1109status_t Parcel::writeString16(const char16_t* str, size_t len)
1110{
Yi Kong91635562018-06-07 14:38:36 -07001111 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001112
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001113 status_t err = writeInt32(len);
1114 if (err == NO_ERROR) {
1115 len *= sizeof(char16_t);
1116 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1117 if (data) {
1118 memcpy(data, str, len);
1119 *reinterpret_cast<char16_t*>(data+len) = 0;
1120 return NO_ERROR;
1121 }
1122 err = mError;
1123 }
1124 return err;
1125}
1126
1127status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1128{
Steven Morelanda86a3562019-08-01 23:28:34 +00001129 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001130}
1131
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001132status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1133{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001134 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001135}
1136
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001137status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val)
1138{
1139 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1140}
1141
Casey Dahlinb9872622015-11-25 15:09:45 -08001142status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1143{
1144 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1145}
1146
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001147status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const {
1148 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
1149}
1150
Casey Dahlinb9872622015-11-25 15:09:45 -08001151status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001152 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001153}
1154
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001155status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001156 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001157}
1158
Casey Dahlinb9872622015-11-25 15:09:45 -08001159status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1160 if (!parcelable) {
1161 return writeInt32(0);
1162 }
1163
1164 return writeParcelable(*parcelable);
1165}
1166
Christopher Wiley97f048d2015-11-19 06:49:05 -08001167status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1168 status_t status = writeInt32(1); // parcelable is not null.
1169 if (status != OK) {
1170 return status;
1171 }
1172 return parcelable.writeToParcel(this);
1173}
1174
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001175status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001176{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001177 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001178 return BAD_TYPE;
1179
1180 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001181 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001182 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001184 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001185 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001187 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1188 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189
1190 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001191 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192 return err;
1193 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001194 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001195 return err;
1196}
1197
Jeff Brown93ff1f92011-11-04 19:01:44 -07001198status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001199{
1200 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001201 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001202 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001203 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001204 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001205 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001206 return writeObject(obj, true);
1207}
1208
1209status_t Parcel::writeDupFileDescriptor(int fd)
1210{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001211 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001212 if (dupFd < 0) {
1213 return -errno;
1214 }
1215 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001216 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001217 close(dupFd);
1218 }
1219 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001220}
1221
Dianne Hackborn1941a402016-08-29 12:30:43 -07001222status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1223{
1224 writeInt32(0);
1225 return writeFileDescriptor(fd, takeOwnership);
1226}
1227
Ryo Hashimotobf551892018-05-31 16:58:35 +09001228status_t Parcel::writeDupParcelFileDescriptor(int fd)
1229{
1230 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1231 if (dupFd < 0) {
1232 return -errno;
1233 }
1234 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1235 if (err != OK) {
1236 close(dupFd);
1237 }
1238 return err;
1239}
1240
Christopher Wiley2cf19952016-04-11 11:09:37 -07001241status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001242 return writeDupFileDescriptor(fd.get());
1243}
1244
Christopher Wiley2cf19952016-04-11 11:09:37 -07001245status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001246 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1247}
1248
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001249status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) {
1250 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1251}
1252
Christopher Wiley2cf19952016-04-11 11:09:37 -07001253status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001254 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1255}
1256
Jeff Brown13b16042014-11-11 16:44:25 -08001257status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001258{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001259 if (len > INT32_MAX) {
1260 // don't accept size_t values which may have come from an
1261 // inadvertent conversion from a negative int.
1262 return BAD_VALUE;
1263 }
1264
Jeff Brown13b16042014-11-11 16:44:25 -08001265 status_t status;
1266 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001267 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001268 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001269 if (status) return status;
1270
1271 void* ptr = writeInplace(len);
1272 if (!ptr) return NO_MEMORY;
1273
Jeff Brown13b16042014-11-11 16:44:25 -08001274 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001275 return NO_ERROR;
1276 }
1277
Steve Block6807e592011-10-20 11:56:00 +01001278 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001279 int fd = ashmem_create_region("Parcel Blob", len);
1280 if (fd < 0) return NO_MEMORY;
1281
1282 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1283 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001284 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001285 } else {
Yi Kong91635562018-06-07 14:38:36 -07001286 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001287 if (ptr == MAP_FAILED) {
1288 status = -errno;
1289 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001290 if (!mutableCopy) {
1291 result = ashmem_set_prot_region(fd, PROT_READ);
1292 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001293 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001294 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001295 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001296 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001297 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001298 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001299 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001300 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001301 return NO_ERROR;
1302 }
1303 }
1304 }
1305 }
1306 ::munmap(ptr, len);
1307 }
1308 ::close(fd);
1309 return status;
1310}
1311
Jeff Brown13b16042014-11-11 16:44:25 -08001312status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1313{
1314 // Must match up with what's done in writeBlob.
1315 if (!mAllowFds) return FDS_NOT_ALLOWED;
1316 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1317 if (status) return status;
1318 return writeDupFileDescriptor(fd);
1319}
1320
Mathias Agopiane1424282013-07-29 21:24:40 -07001321status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001322{
1323 status_t err;
1324
1325 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001326 const size_t len = val.getFlattenedSize();
1327 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001328
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001329 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001330 // don't accept size_t values which may have come from an
1331 // inadvertent conversion from a negative int.
1332 return BAD_VALUE;
1333 }
1334
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001335 err = this->writeInt32(len);
1336 if (err) return err;
1337
1338 err = this->writeInt32(fd_count);
1339 if (err) return err;
1340
1341 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001342 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001343 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001344 return BAD_VALUE;
1345
Yi Kong91635562018-06-07 14:38:36 -07001346 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001347 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001348 fds = new (std::nothrow) int[fd_count];
1349 if (fds == nullptr) {
1350 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1351 return BAD_VALUE;
1352 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001353 }
1354
1355 err = val.flatten(buf, len, fds, fd_count);
1356 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1357 err = this->writeDupFileDescriptor( fds[i] );
1358 }
1359
1360 if (fd_count) {
1361 delete [] fds;
1362 }
1363
1364 return err;
1365}
1366
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001367status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1368{
1369 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1370 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1371 if (enoughData && enoughObjects) {
1372restart_write:
1373 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001374
Christopher Tate98e67d32015-06-03 18:44:15 -07001375 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001376 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001377 if (!mAllowFds) {
1378 // fail before modifying our object index
1379 return FDS_NOT_ALLOWED;
1380 }
1381 mHasFds = mFdsKnown = true;
1382 }
1383
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001384 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001385 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001386 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001387 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001388 mObjectsSize++;
1389 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001390
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001391 return finishWrite(sizeof(flat_binder_object));
1392 }
1393
1394 if (!enoughData) {
1395 const status_t err = growData(sizeof(val));
1396 if (err != NO_ERROR) return err;
1397 }
1398 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001399 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1400 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001401 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001402 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001403 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001404 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001405 mObjects = objects;
1406 mObjectsCapacity = newSize;
1407 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001408
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001409 goto restart_write;
1410}
1411
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001412status_t Parcel::writeNoException()
1413{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001414 binder::Status status;
1415 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001416}
1417
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001418status_t Parcel::validateReadData(size_t upperBound) const
1419{
1420 // Don't allow non-object reads on object data
1421 if (mObjectsSorted || mObjectsSize <= 1) {
1422data_sorted:
1423 // Expect to check only against the next object
1424 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1425 // For some reason the current read position is greater than the next object
1426 // hint. Iterate until we find the right object
1427 size_t nextObject = mNextObjectHint;
1428 do {
1429 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1430 // Requested info overlaps with an object
1431 ALOGE("Attempt to read from protected data in Parcel %p", this);
1432 return PERMISSION_DENIED;
1433 }
1434 nextObject++;
1435 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1436 mNextObjectHint = nextObject;
1437 }
1438 return NO_ERROR;
1439 }
1440 // Quickly determine if mObjects is sorted.
1441 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1442 binder_size_t* prevObj = currObj;
1443 while (currObj > mObjects) {
1444 prevObj--;
1445 if(*prevObj > *currObj) {
1446 goto data_unsorted;
1447 }
1448 currObj--;
1449 }
1450 mObjectsSorted = true;
1451 goto data_sorted;
1452
1453data_unsorted:
1454 // Insertion Sort mObjects
1455 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1456 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1457 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1458 binder_size_t temp = *iter0;
1459 binder_size_t* iter1 = iter0 - 1;
1460 while (iter1 >= mObjects && *iter1 > temp) {
1461 *(iter1 + 1) = *iter1;
1462 iter1--;
1463 }
1464 *(iter1 + 1) = temp;
1465 }
1466 mNextObjectHint = 0;
1467 mObjectsSorted = true;
1468 goto data_sorted;
1469}
1470
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001471status_t Parcel::read(void* outData, size_t len) const
1472{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001473 if (len > INT32_MAX) {
1474 // don't accept size_t values which may have come from an
1475 // inadvertent conversion from a negative int.
1476 return BAD_VALUE;
1477 }
1478
1479 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1480 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001481 if (mObjectsSize > 0) {
1482 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001483 if(err != NO_ERROR) {
1484 // Still increment the data position by the expected length
1485 mDataPos += pad_size(len);
1486 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1487 return err;
1488 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001489 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001490 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001491 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001492 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001493 return NO_ERROR;
1494 }
1495 return NOT_ENOUGH_DATA;
1496}
1497
1498const void* Parcel::readInplace(size_t len) const
1499{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001500 if (len > INT32_MAX) {
1501 // don't accept size_t values which may have come from an
1502 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001503 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001504 }
1505
1506 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1507 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001508 if (mObjectsSize > 0) {
1509 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001510 if(err != NO_ERROR) {
1511 // Still increment the data position by the expected length
1512 mDataPos += pad_size(len);
1513 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001514 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001515 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001516 }
1517
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001518 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001519 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001520 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001521 return data;
1522 }
Yi Kong91635562018-06-07 14:38:36 -07001523 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001524}
1525
Andreas Huber84a6d042009-08-17 13:33:27 -07001526template<class T>
1527status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001528 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001529
1530 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001531 if (mObjectsSize > 0) {
1532 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001533 if(err != NO_ERROR) {
1534 // Still increment the data position by the expected length
1535 mDataPos += sizeof(T);
1536 return err;
1537 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001538 }
1539
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001540 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001541 mDataPos += sizeof(T);
1542 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001543 return NO_ERROR;
1544 } else {
1545 return NOT_ENOUGH_DATA;
1546 }
1547}
1548
Andreas Huber84a6d042009-08-17 13:33:27 -07001549template<class T>
1550T Parcel::readAligned() const {
1551 T result;
1552 if (readAligned(&result) != NO_ERROR) {
1553 result = 0;
1554 }
1555
1556 return result;
1557}
1558
1559template<class T>
1560status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001561 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001562
1563 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1564restart_write:
1565 *reinterpret_cast<T*>(mData+mDataPos) = val;
1566 return finishWrite(sizeof(val));
1567 }
1568
1569 status_t err = growData(sizeof(val));
1570 if (err == NO_ERROR) goto restart_write;
1571 return err;
1572}
1573
Casey Dahlin185d3442016-02-09 11:08:35 -08001574status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001575 size_t size;
1576 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1577 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001578}
1579
1580status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001581 size_t size;
1582 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1583 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001584}
1585
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001586status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const {
1587 size_t size;
1588 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1589 if (!*val) {
1590 // reserveOutVector does not create the out vector if size is < 0.
1591 // This occurs when writing a null byte vector.
1592 return OK;
1593 }
1594 return readByteVectorInternal(&**val, size);
1595}
1596
Casey Dahlin185d3442016-02-09 11:08:35 -08001597status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001598 size_t size;
1599 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001600 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001601 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001602 // This occurs when writing a null byte vector.
1603 return OK;
1604 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001605 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001606}
1607
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001608status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const {
1609 size_t size;
1610 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1611 if (!*val) {
1612 // reserveOutVector does not create the out vector if size is < 0.
1613 // This occurs when writing a null byte vector.
1614 return OK;
1615 }
1616 return readByteVectorInternal(&**val, size);
1617}
1618
Casey Dahlin185d3442016-02-09 11:08:35 -08001619status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001620 size_t size;
1621 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001622 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001623 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001624 // This occurs when writing a null byte vector.
1625 return OK;
1626 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001627 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001628}
1629
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001630status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const {
1631 return readNullableTypedVector(val, &Parcel::readInt32);
1632}
1633
Casey Dahlinb9872622015-11-25 15:09:45 -08001634status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1635 return readNullableTypedVector(val, &Parcel::readInt32);
1636}
1637
Casey Dahlin451ff582015-10-19 18:12:18 -07001638status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001639 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001640}
1641
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001642status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const {
1643 return readNullableTypedVector(val, &Parcel::readInt64);
1644}
1645
Casey Dahlinb9872622015-11-25 15:09:45 -08001646status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1647 return readNullableTypedVector(val, &Parcel::readInt64);
1648}
1649
Casey Dahlin451ff582015-10-19 18:12:18 -07001650status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001651 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001652}
1653
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001654status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const {
1655 return readNullableTypedVector(val, &Parcel::readUint64);
1656}
1657
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001658status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1659 return readNullableTypedVector(val, &Parcel::readUint64);
1660}
1661
1662status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1663 return readTypedVector(val, &Parcel::readUint64);
1664}
1665
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001666status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const {
1667 return readNullableTypedVector(val, &Parcel::readFloat);
1668}
1669
Casey Dahlinb9872622015-11-25 15:09:45 -08001670status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1671 return readNullableTypedVector(val, &Parcel::readFloat);
1672}
1673
Casey Dahlin451ff582015-10-19 18:12:18 -07001674status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001675 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001676}
1677
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001678status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const {
1679 return readNullableTypedVector(val, &Parcel::readDouble);
1680}
1681
Casey Dahlinb9872622015-11-25 15:09:45 -08001682status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1683 return readNullableTypedVector(val, &Parcel::readDouble);
1684}
1685
Casey Dahlin451ff582015-10-19 18:12:18 -07001686status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001687 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001688}
1689
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001690status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const {
1691 const int32_t start = dataPosition();
1692 int32_t size;
1693 status_t status = readInt32(&size);
1694 val->reset();
1695
1696 if (status != OK || size < 0) {
1697 return status;
1698 }
1699
1700 setDataPosition(start);
1701 val->emplace();
1702
1703 status = readBoolVector(&**val);
1704
1705 if (status != OK) {
1706 val->reset();
1707 }
1708
1709 return status;
1710}
1711
Casey Dahlinb9872622015-11-25 15:09:45 -08001712status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1713 const int32_t start = dataPosition();
1714 int32_t size;
1715 status_t status = readInt32(&size);
1716 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001717
Casey Dahlinb9872622015-11-25 15:09:45 -08001718 if (status != OK || size < 0) {
1719 return status;
1720 }
1721
1722 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001723 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001724
1725 status = readBoolVector(val->get());
1726
1727 if (status != OK) {
1728 val->reset();
1729 }
1730
1731 return status;
1732}
1733
1734status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001735 int32_t size;
1736 status_t status = readInt32(&size);
1737
1738 if (status != OK) {
1739 return status;
1740 }
1741
1742 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001743 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001744 }
1745
1746 val->resize(size);
1747
1748 /* C++ bool handling means a vector of bools isn't necessarily addressable
1749 * (we might use individual bits)
1750 */
Christopher Wiley97887982015-10-27 16:33:47 -07001751 bool data;
1752 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001753 status = readBool(&data);
1754 (*val)[i] = data;
1755
1756 if (status != OK) {
1757 return status;
1758 }
1759 }
1760
1761 return OK;
1762}
1763
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001764status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const {
1765 return readNullableTypedVector(val, &Parcel::readChar);
1766}
1767
Casey Dahlinb9872622015-11-25 15:09:45 -08001768status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1769 return readNullableTypedVector(val, &Parcel::readChar);
1770}
1771
Casey Dahlin451ff582015-10-19 18:12:18 -07001772status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001773 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001774}
1775
Casey Dahlinb9872622015-11-25 15:09:45 -08001776status_t Parcel::readString16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001777 std::optional<std::vector<std::optional<String16>>>* val) const {
1778 return readNullableTypedVector(val, &Parcel::readString16);
1779}
1780
1781status_t Parcel::readString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -08001782 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1783 return readNullableTypedVector(val, &Parcel::readString16);
1784}
1785
Casey Dahlin451ff582015-10-19 18:12:18 -07001786status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001787 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001788}
1789
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001790status_t Parcel::readUtf8VectorFromUtf16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001791 std::optional<std::vector<std::optional<std::string>>>* val) const {
1792 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1793}
1794
1795status_t Parcel::readUtf8VectorFromUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001796 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1797 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1798}
1799
1800status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1801 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1802}
Casey Dahlin451ff582015-10-19 18:12:18 -07001803
Andreas Huber84a6d042009-08-17 13:33:27 -07001804status_t Parcel::readInt32(int32_t *pArg) const
1805{
1806 return readAligned(pArg);
1807}
1808
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001809int32_t Parcel::readInt32() const
1810{
Andreas Huber84a6d042009-08-17 13:33:27 -07001811 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001812}
1813
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001814status_t Parcel::readUint32(uint32_t *pArg) const
1815{
1816 return readAligned(pArg);
1817}
1818
1819uint32_t Parcel::readUint32() const
1820{
1821 return readAligned<uint32_t>();
1822}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001823
1824status_t Parcel::readInt64(int64_t *pArg) const
1825{
Andreas Huber84a6d042009-08-17 13:33:27 -07001826 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001827}
1828
1829
1830int64_t Parcel::readInt64() const
1831{
Andreas Huber84a6d042009-08-17 13:33:27 -07001832 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001833}
1834
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001835status_t Parcel::readUint64(uint64_t *pArg) const
1836{
1837 return readAligned(pArg);
1838}
1839
1840uint64_t Parcel::readUint64() const
1841{
1842 return readAligned<uint64_t>();
1843}
1844
Serban Constantinescuf683e012013-11-05 16:53:55 +00001845status_t Parcel::readPointer(uintptr_t *pArg) const
1846{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001847 status_t ret;
1848 binder_uintptr_t ptr;
1849 ret = readAligned(&ptr);
1850 if (!ret)
1851 *pArg = ptr;
1852 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001853}
1854
1855uintptr_t Parcel::readPointer() const
1856{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001857 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001858}
1859
1860
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001861status_t Parcel::readFloat(float *pArg) const
1862{
Andreas Huber84a6d042009-08-17 13:33:27 -07001863 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001864}
1865
1866
1867float Parcel::readFloat() const
1868{
Andreas Huber84a6d042009-08-17 13:33:27 -07001869 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001870}
1871
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001872#if defined(__mips__) && defined(__mips_hard_float)
1873
1874status_t Parcel::readDouble(double *pArg) const
1875{
1876 union {
1877 double d;
1878 unsigned long long ll;
1879 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001880 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001881 status_t status;
1882 status = readAligned(&u.ll);
1883 *pArg = u.d;
1884 return status;
1885}
1886
1887double Parcel::readDouble() const
1888{
1889 union {
1890 double d;
1891 unsigned long long ll;
1892 } u;
1893 u.ll = readAligned<unsigned long long>();
1894 return u.d;
1895}
1896
1897#else
1898
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001899status_t Parcel::readDouble(double *pArg) const
1900{
Andreas Huber84a6d042009-08-17 13:33:27 -07001901 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001902}
1903
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001904double Parcel::readDouble() const
1905{
Andreas Huber84a6d042009-08-17 13:33:27 -07001906 return readAligned<double>();
1907}
1908
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001909#endif
1910
Andreas Huber84a6d042009-08-17 13:33:27 -07001911status_t Parcel::readIntPtr(intptr_t *pArg) const
1912{
1913 return readAligned(pArg);
1914}
1915
1916
1917intptr_t Parcel::readIntPtr() const
1918{
1919 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001920}
1921
Casey Dahlind6848f52015-10-15 15:44:59 -07001922status_t Parcel::readBool(bool *pArg) const
1923{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001924 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001925 status_t ret = readInt32(&tmp);
1926 *pArg = (tmp != 0);
1927 return ret;
1928}
1929
1930bool Parcel::readBool() const
1931{
1932 return readInt32() != 0;
1933}
1934
1935status_t Parcel::readChar(char16_t *pArg) const
1936{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001937 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001938 status_t ret = readInt32(&tmp);
1939 *pArg = char16_t(tmp);
1940 return ret;
1941}
1942
1943char16_t Parcel::readChar() const
1944{
1945 return char16_t(readInt32());
1946}
1947
1948status_t Parcel::readByte(int8_t *pArg) const
1949{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001950 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001951 status_t ret = readInt32(&tmp);
1952 *pArg = int8_t(tmp);
1953 return ret;
1954}
1955
1956int8_t Parcel::readByte() const
1957{
1958 return int8_t(readInt32());
1959}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001960
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001961status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1962 size_t utf16Size = 0;
1963 const char16_t* src = readString16Inplace(&utf16Size);
1964 if (!src) {
1965 return UNEXPECTED_NULL;
1966 }
1967
1968 // Save ourselves the trouble, we're done.
1969 if (utf16Size == 0u) {
1970 str->clear();
1971 return NO_ERROR;
1972 }
1973
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001974 // Allow for closing '\0'
1975 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1976 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001977 return BAD_VALUE;
1978 }
1979 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001980 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001981 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001982 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1983 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001984 return NO_ERROR;
1985}
1986
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001987status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const {
1988 const int32_t start = dataPosition();
1989 int32_t size;
1990 status_t status = readInt32(&size);
1991 str->reset();
1992
1993 if (status != OK || size < 0) {
1994 return status;
1995 }
1996
1997 setDataPosition(start);
1998 str->emplace();
1999 return readUtf8FromUtf16(&**str);
2000}
2001
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002002status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2003 const int32_t start = dataPosition();
2004 int32_t size;
2005 status_t status = readInt32(&size);
2006 str->reset();
2007
2008 if (status != OK || size < 0) {
2009 return status;
2010 }
2011
2012 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002013 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002014 return readUtf8FromUtf16(str->get());
2015}
2016
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002017const char* Parcel::readCString() const
2018{
Steven Morelandd0d4b582019-05-17 13:14:06 -07002019 if (mDataPos < mDataSize) {
2020 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002021 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2022 // is the string's trailing NUL within the parcel's valid bounds?
2023 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2024 if (eos) {
2025 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002026 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002027 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002028 return str;
2029 }
2030 }
Yi Kong91635562018-06-07 14:38:36 -07002031 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002032}
2033
2034String8 Parcel::readString8() const
2035{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002036 size_t len;
2037 const char* str = readString8Inplace(&len);
2038 if (str) return String8(str, len);
2039 ALOGE("Reading a NULL string not supported here.");
2040 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002041}
2042
2043status_t Parcel::readString8(String8* pArg) const
2044{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002045 size_t len;
2046 const char* str = readString8Inplace(&len);
2047 if (str) {
2048 pArg->setTo(str, len);
2049 return 0;
2050 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002051 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002052 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002053 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002054}
2055
2056const char* Parcel::readString8Inplace(size_t* outLen) const
2057{
2058 int32_t size = readInt32();
2059 // watch for potential int overflow from size+1
2060 if (size >= 0 && size < INT32_MAX) {
2061 *outLen = size;
2062 const char* str = (const char*)readInplace(size+1);
2063 if (str != nullptr) {
2064 return str;
2065 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002066 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002067 *outLen = 0;
2068 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002069}
2070
2071String16 Parcel::readString16() const
2072{
2073 size_t len;
2074 const char16_t* str = readString16Inplace(&len);
2075 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002076 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002077 return String16();
2078}
2079
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002080status_t Parcel::readString16(std::optional<String16>* pArg) const
2081{
2082 const int32_t start = dataPosition();
2083 int32_t size;
2084 status_t status = readInt32(&size);
2085 pArg->reset();
2086
2087 if (status != OK || size < 0) {
2088 return status;
2089 }
2090
2091 setDataPosition(start);
2092 pArg->emplace();
2093
2094 status = readString16(&**pArg);
2095
2096 if (status != OK) {
2097 pArg->reset();
2098 }
2099
2100 return status;
2101}
2102
Casey Dahlinb9872622015-11-25 15:09:45 -08002103status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2104{
2105 const int32_t start = dataPosition();
2106 int32_t size;
2107 status_t status = readInt32(&size);
2108 pArg->reset();
2109
2110 if (status != OK || size < 0) {
2111 return status;
2112 }
2113
2114 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002115 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002116
2117 status = readString16(pArg->get());
2118
2119 if (status != OK) {
2120 pArg->reset();
2121 }
2122
2123 return status;
2124}
2125
Casey Dahlin451ff582015-10-19 18:12:18 -07002126status_t Parcel::readString16(String16* pArg) const
2127{
2128 size_t len;
2129 const char16_t* str = readString16Inplace(&len);
2130 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002131 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002132 return 0;
2133 } else {
2134 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002135 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002136 }
2137}
2138
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002139const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2140{
2141 int32_t size = readInt32();
2142 // watch for potential int overflow from size+1
2143 if (size >= 0 && size < INT32_MAX) {
2144 *outLen = size;
2145 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002146 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002147 return str;
2148 }
2149 }
2150 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002151 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002152}
2153
Casey Dahlinf0c13772015-10-27 18:33:56 -07002154status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2155{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002156 status_t status = readNullableStrongBinder(val);
2157 if (status == OK && !val->get()) {
2158 status = UNEXPECTED_NULL;
2159 }
2160 return status;
2161}
2162
2163status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2164{
Steven Morelanda86a3562019-08-01 23:28:34 +00002165 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002166}
2167
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002168sp<IBinder> Parcel::readStrongBinder() const
2169{
2170 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002171 // Note that a lot of code in Android reads binders by hand with this
2172 // method, and that code has historically been ok with getting nullptr
2173 // back (while ignoring error codes).
2174 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002175 return val;
2176}
2177
Christopher Wiley97f048d2015-11-19 06:49:05 -08002178status_t Parcel::readParcelable(Parcelable* parcelable) const {
2179 int32_t have_parcelable = 0;
2180 status_t status = readInt32(&have_parcelable);
2181 if (status != OK) {
2182 return status;
2183 }
2184 if (!have_parcelable) {
2185 return UNEXPECTED_NULL;
2186 }
2187 return parcelable->readFromParcel(this);
2188}
2189
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002190int32_t Parcel::readExceptionCode() const
2191{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002192 binder::Status status;
2193 status.readFromParcel(*this);
2194 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002195}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002196
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002197native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002198{
2199 int numFds, numInts;
2200 status_t err;
2201 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002202 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002203 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002204 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002205
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002206 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002207 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002208 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002209 }
2210
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002211 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002212 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002213 if (h->data[i] < 0) {
2214 for (int j = 0; j < i; j++) {
2215 close(h->data[j]);
2216 }
2217 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002218 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002219 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002220 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002221 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002222 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002223 native_handle_close(h);
2224 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002225 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002226 }
2227 return h;
2228}
2229
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002230int Parcel::readFileDescriptor() const
2231{
2232 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002233
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002234 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002235 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002236 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002237
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002238 return BAD_TYPE;
2239}
2240
Dianne Hackborn1941a402016-08-29 12:30:43 -07002241int Parcel::readParcelFileDescriptor() const
2242{
2243 int32_t hasComm = readInt32();
2244 int fd = readFileDescriptor();
2245 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002246 // detach (owned by the binder driver)
2247 int comm = readFileDescriptor();
2248
2249 // warning: this must be kept in sync with:
2250 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2251 enum ParcelFileDescriptorStatus {
2252 DETACHED = 2,
2253 };
2254
2255#if BYTE_ORDER == BIG_ENDIAN
2256 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2257#endif
2258#if BYTE_ORDER == LITTLE_ENDIAN
2259 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2260#endif
2261
2262 ssize_t written = TEMP_FAILURE_RETRY(
2263 ::write(comm, &message, sizeof(message)));
2264
2265 if (written == -1 || written != sizeof(message)) {
2266 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2267 written, strerror(errno));
2268 return BAD_TYPE;
2269 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002270 }
2271 return fd;
2272}
2273
Christopher Wiley2cf19952016-04-11 11:09:37 -07002274status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002275{
2276 int got = readFileDescriptor();
2277
2278 if (got == BAD_TYPE) {
2279 return BAD_TYPE;
2280 }
2281
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002282 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002283
2284 if (val->get() < 0) {
2285 return BAD_VALUE;
2286 }
2287
2288 return OK;
2289}
2290
Ryo Hashimotobf551892018-05-31 16:58:35 +09002291status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2292{
2293 int got = readParcelFileDescriptor();
2294
2295 if (got == BAD_TYPE) {
2296 return BAD_TYPE;
2297 }
2298
2299 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2300
2301 if (val->get() < 0) {
2302 return BAD_VALUE;
2303 }
2304
2305 return OK;
2306}
Casey Dahlin06673e32015-11-23 13:24:23 -08002307
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002308status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const {
2309 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2310}
2311
Christopher Wiley2cf19952016-04-11 11:09:37 -07002312status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002313 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2314}
2315
Christopher Wiley2cf19952016-04-11 11:09:37 -07002316status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002317 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2318}
2319
Jeff Brown5707dbf2011-09-23 21:17:56 -07002320status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2321{
Jeff Brown13b16042014-11-11 16:44:25 -08002322 int32_t blobType;
2323 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002324 if (status) return status;
2325
Jeff Brown13b16042014-11-11 16:44:25 -08002326 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002327 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002328 const void* ptr = readInplace(len);
2329 if (!ptr) return BAD_VALUE;
2330
Jeff Brown13b16042014-11-11 16:44:25 -08002331 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002332 return NO_ERROR;
2333 }
2334
Steve Block6807e592011-10-20 11:56:00 +01002335 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002336 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002337 int fd = readFileDescriptor();
2338 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2339
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002340 if (!ashmem_valid(fd)) {
2341 ALOGE("invalid fd");
2342 return BAD_VALUE;
2343 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002344 int size = ashmem_get_size_region(fd);
2345 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002346 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002347 return BAD_VALUE;
2348 }
Yi Kong91635562018-06-07 14:38:36 -07002349 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002350 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002351 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002352
Jeff Brown13b16042014-11-11 16:44:25 -08002353 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002354 return NO_ERROR;
2355}
2356
Mathias Agopiane1424282013-07-29 21:24:40 -07002357status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002358{
2359 // size
2360 const size_t len = this->readInt32();
2361 const size_t fd_count = this->readInt32();
2362
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002363 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002364 // don't accept size_t values which may have come from an
2365 // inadvertent conversion from a negative int.
2366 return BAD_VALUE;
2367 }
2368
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002369 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002370 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002371 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002372 return BAD_VALUE;
2373
Yi Kong91635562018-06-07 14:38:36 -07002374 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002375 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002376 fds = new (std::nothrow) int[fd_count];
2377 if (fds == nullptr) {
2378 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2379 return BAD_VALUE;
2380 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002381 }
2382
2383 status_t err = NO_ERROR;
2384 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002385 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002386 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002387 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002388 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 -07002389 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2390 // Close all the file descriptors that were dup-ed.
2391 for (size_t j=0; j<i ;j++) {
2392 close(fds[j]);
2393 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002394 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002395 }
2396
2397 if (err == NO_ERROR) {
2398 err = val.unflatten(buf, len, fds, fd_count);
2399 }
2400
2401 if (fd_count) {
2402 delete [] fds;
2403 }
2404
2405 return err;
2406}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002407const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2408{
2409 const size_t DPOS = mDataPos;
2410 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2411 const flat_binder_object* obj
2412 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2413 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002414 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002415 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002416 // the object list, so we don't want to check for it when
2417 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002418 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002419 return obj;
2420 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002421
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002422 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002423 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002424 const size_t N = mObjectsSize;
2425 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002426
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002428 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002429 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002430
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002431 // Start at the current hint position, looking for an object at
2432 // the current data position.
2433 if (opos < N) {
2434 while (opos < (N-1) && OBJS[opos] < DPOS) {
2435 opos++;
2436 }
2437 } else {
2438 opos = N-1;
2439 }
2440 if (OBJS[opos] == DPOS) {
2441 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002442 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002443 this, DPOS, opos);
2444 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002445 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002446 return obj;
2447 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002448
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002449 // Look backwards for it...
2450 while (opos > 0 && OBJS[opos] > DPOS) {
2451 opos--;
2452 }
2453 if (OBJS[opos] == DPOS) {
2454 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002455 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002456 this, DPOS, opos);
2457 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002458 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002459 return obj;
2460 }
2461 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002462 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 -07002463 this, DPOS);
2464 }
Yi Kong91635562018-06-07 14:38:36 -07002465 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002466}
2467
2468void Parcel::closeFileDescriptors()
2469{
2470 size_t i = mObjectsSize;
2471 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002472 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002473 }
2474 while (i > 0) {
2475 i--;
2476 const flat_binder_object* flat
2477 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002478 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002479 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002480 close(flat->handle);
2481 }
2482 }
2483}
2484
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002485uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002486{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002487 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002488}
2489
2490size_t Parcel::ipcDataSize() const
2491{
2492 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2493}
2494
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002495uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002496{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002497 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002498}
2499
2500size_t Parcel::ipcObjectsCount() const
2501{
2502 return mObjectsSize;
2503}
2504
2505void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002506 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002507{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002508 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002509 freeDataNoInit();
2510 mError = NO_ERROR;
2511 mData = const_cast<uint8_t*>(data);
2512 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002513 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002514 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002515 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002516 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002517 mObjectsSize = mObjectsCapacity = objectsCount;
2518 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002519 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002520 mOwner = relFunc;
2521 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002522 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002523 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002524 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002525 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002526 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002527 mObjectsSize = 0;
2528 break;
2529 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002530 const flat_binder_object* flat
2531 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2532 uint32_t type = flat->hdr.type;
2533 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2534 type == BINDER_TYPE_FD)) {
2535 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2536 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2537 // recover gracefully by clearing out the objects, and releasing the objects we do
2538 // know about.
2539 android_errorWriteLog(0x534e4554, "135930648");
2540 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2541 __func__, type, (uint64_t)offset);
2542 releaseObjects();
2543 mObjectsSize = 0;
2544 break;
2545 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002546 minOffset = offset + sizeof(flat_binder_object);
2547 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002548 scanForFds();
2549}
2550
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002551void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002552{
2553 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002554
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002555 if (errorCheck() != NO_ERROR) {
2556 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002557 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002558 } else if (dataSize() > 0) {
2559 const uint8_t* DATA = data();
2560 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002561 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002562 const size_t N = objectsCount();
2563 for (size_t i=0; i<N; i++) {
2564 const flat_binder_object* flat
2565 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2566 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002567 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002568 << " = " << flat->binder;
2569 }
2570 } else {
2571 to << "NULL";
2572 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002573
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002574 to << ")";
2575}
2576
2577void Parcel::releaseObjects()
2578{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002579 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002580 if (i == 0) {
2581 return;
2582 }
2583 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002584 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002585 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002586 while (i > 0) {
2587 i--;
2588 const flat_binder_object* flat
2589 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002590 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002591 }
2592}
2593
2594void Parcel::acquireObjects()
2595{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002596 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002597 if (i == 0) {
2598 return;
2599 }
2600 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002601 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002602 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002603 while (i > 0) {
2604 i--;
2605 const flat_binder_object* flat
2606 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002607 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002608 }
2609}
2610
2611void Parcel::freeData()
2612{
2613 freeDataNoInit();
2614 initState();
2615}
2616
2617void Parcel::freeDataNoInit()
2618{
2619 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002620 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002621 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002622 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2623 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002624 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002625 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002626 if (mData) {
2627 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002628 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002629 if (mDataCapacity <= gParcelGlobalAllocSize) {
2630 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2631 } else {
2632 gParcelGlobalAllocSize = 0;
2633 }
2634 if (gParcelGlobalAllocCount > 0) {
2635 gParcelGlobalAllocCount--;
2636 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002637 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002638 free(mData);
2639 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002640 if (mObjects) free(mObjects);
2641 }
2642}
2643
2644status_t Parcel::growData(size_t len)
2645{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002646 if (len > INT32_MAX) {
2647 // don't accept size_t values which may have come from an
2648 // inadvertent conversion from a negative int.
2649 return BAD_VALUE;
2650 }
2651
Martijn Coenen93fe5182020-01-22 10:46:25 +01002652 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2653 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002654 size_t newSize = ((mDataSize+len)*3)/2;
2655 return (newSize <= mDataSize)
2656 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002657 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002658}
2659
2660status_t Parcel::restartWrite(size_t desired)
2661{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002662 if (desired > INT32_MAX) {
2663 // don't accept size_t values which may have come from an
2664 // inadvertent conversion from a negative int.
2665 return BAD_VALUE;
2666 }
2667
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002668 if (mOwner) {
2669 freeData();
2670 return continueWrite(desired);
2671 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002672
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002673 uint8_t* data = (uint8_t*)realloc(mData, desired);
2674 if (!data && desired > mDataCapacity) {
2675 mError = NO_MEMORY;
2676 return NO_MEMORY;
2677 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002678
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002679 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002680
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002681 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002682 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002683 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002684 gParcelGlobalAllocSize += desired;
2685 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002686 if (!mData) {
2687 gParcelGlobalAllocCount++;
2688 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002689 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002690 mData = data;
2691 mDataCapacity = desired;
2692 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002693
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002694 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002695 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2696 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2697
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002698 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002699 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002700 mObjectsSize = mObjectsCapacity = 0;
2701 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002702 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002703 mHasFds = false;
2704 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002705 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002706
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002707 return NO_ERROR;
2708}
2709
2710status_t Parcel::continueWrite(size_t desired)
2711{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002712 if (desired > INT32_MAX) {
2713 // don't accept size_t values which may have come from an
2714 // inadvertent conversion from a negative int.
2715 return BAD_VALUE;
2716 }
2717
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002718 // If shrinking, first adjust for any objects that appear
2719 // after the new data size.
2720 size_t objectsSize = mObjectsSize;
2721 if (desired < mDataSize) {
2722 if (desired == 0) {
2723 objectsSize = 0;
2724 } else {
2725 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002726 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002727 break;
2728 objectsSize--;
2729 }
2730 }
2731 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002732
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002733 if (mOwner) {
2734 // If the size is going to zero, just release the owner's data.
2735 if (desired == 0) {
2736 freeData();
2737 return NO_ERROR;
2738 }
2739
2740 // If there is a different owner, we need to take
2741 // posession.
2742 uint8_t* data = (uint8_t*)malloc(desired);
2743 if (!data) {
2744 mError = NO_MEMORY;
2745 return NO_MEMORY;
2746 }
Yi Kong91635562018-06-07 14:38:36 -07002747 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002748
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002749 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002750 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002751 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002752 free(data);
2753
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002754 mError = NO_MEMORY;
2755 return NO_MEMORY;
2756 }
2757
2758 // Little hack to only acquire references on objects
2759 // we will be keeping.
2760 size_t oldObjectsSize = mObjectsSize;
2761 mObjectsSize = objectsSize;
2762 acquireObjects();
2763 mObjectsSize = oldObjectsSize;
2764 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002765
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002766 if (mData) {
2767 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2768 }
2769 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002770 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002771 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002772 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002773 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002774 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002775
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002776 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002777 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002778 gParcelGlobalAllocSize += desired;
2779 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002780 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002781
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002782 mData = data;
2783 mObjects = objects;
2784 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002785 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002786 mDataCapacity = desired;
2787 mObjectsSize = mObjectsCapacity = objectsSize;
2788 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002789 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002790
2791 } else if (mData) {
2792 if (objectsSize < mObjectsSize) {
2793 // Need to release refs on any objects we are dropping.
2794 const sp<ProcessState> proc(ProcessState::self());
2795 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2796 const flat_binder_object* flat
2797 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002798 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002799 // will need to rescan because we may have lopped off the only FDs
2800 mFdsKnown = false;
2801 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002802 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002803 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002804
2805 if (objectsSize == 0) {
2806 free(mObjects);
2807 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002808 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002809 } else {
2810 binder_size_t* objects =
2811 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2812 if (objects) {
2813 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002814 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002815 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002816 }
2817 mObjectsSize = objectsSize;
2818 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002819 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002820 }
2821
2822 // We own the data, so we can just do a realloc().
2823 if (desired > mDataCapacity) {
2824 uint8_t* data = (uint8_t*)realloc(mData, desired);
2825 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002826 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2827 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002828 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002829 gParcelGlobalAllocSize += desired;
2830 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002831 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002832 mData = data;
2833 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002834 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002835 mError = NO_MEMORY;
2836 return NO_MEMORY;
2837 }
2838 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002839 if (mDataSize > desired) {
2840 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002841 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002842 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002843 if (mDataPos > desired) {
2844 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002845 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002846 }
2847 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002848
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002849 } else {
2850 // This is the first data. Easy!
2851 uint8_t* data = (uint8_t*)malloc(desired);
2852 if (!data) {
2853 mError = NO_MEMORY;
2854 return NO_MEMORY;
2855 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002856
Yi Kong91635562018-06-07 14:38:36 -07002857 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002858 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002859 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002860 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002861
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002862 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002863 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002864 gParcelGlobalAllocSize += desired;
2865 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002866 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002867
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002868 mData = data;
2869 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002870 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2871 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002872 mDataCapacity = desired;
2873 }
2874
2875 return NO_ERROR;
2876}
2877
2878void Parcel::initState()
2879{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002880 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002881 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002882 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002883 mDataSize = 0;
2884 mDataCapacity = 0;
2885 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002886 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2887 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002888 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002889 mObjectsSize = 0;
2890 mObjectsCapacity = 0;
2891 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002892 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002893 mHasFds = false;
2894 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002895 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002896 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002897 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002898 mWorkSourceRequestHeaderPosition = 0;
2899 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002900
2901 // racing multiple init leads only to multiple identical write
2902 if (gMaxFds == 0) {
2903 struct rlimit result;
2904 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2905 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002906 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002907 } else {
2908 ALOGW("Unable to getrlimit: %s", strerror(errno));
2909 gMaxFds = 1024;
2910 }
2911 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002912}
2913
2914void Parcel::scanForFds() const
2915{
2916 bool hasFds = false;
2917 for (size_t i=0; i<mObjectsSize; i++) {
2918 const flat_binder_object* flat
2919 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002920 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002921 hasFds = true;
2922 break;
2923 }
2924 }
2925 mHasFds = hasFds;
2926 mFdsKnown = true;
2927}
2928
Dan Sandleraa5c2342015-04-10 10:08:45 -04002929size_t Parcel::getBlobAshmemSize() const
2930{
Adrian Roos6bb31142015-10-22 16:46:12 -07002931 // This used to return the size of all blobs that were written to ashmem, now we're returning
2932 // the ashmem currently referenced by this Parcel, which should be equivalent.
2933 // TODO: Remove method once ABI can be changed.
2934 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002935}
2936
Adrian Rooscbf37262015-10-22 16:12:53 -07002937size_t Parcel::getOpenAshmemSize() const
2938{
2939 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002940}
2941
2942// --- Parcel::Blob ---
2943
2944Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002945 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002946}
2947
2948Parcel::Blob::~Blob() {
2949 release();
2950}
2951
2952void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002953 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002954 ::munmap(mData, mSize);
2955 }
2956 clear();
2957}
2958
Jeff Brown13b16042014-11-11 16:44:25 -08002959void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2960 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002961 mData = data;
2962 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002963 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002964}
2965
2966void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002967 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002968 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002969 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002970 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002971}
2972
Steven Moreland61ff8492019-09-26 16:05:45 -07002973} // namespace android