blob: a9c19b3d86a33c2263a89c0b8fe2e37bb52ca158 [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{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000530 return writeInterfaceToken(interface.string(), interface.size());
531}
532
533status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Olivier Gaillard91a04802018-11-14 17:32:41 +0000534 const IPCThreadState* threadState = IPCThreadState::self();
535 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000536 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000537 writeInt32(threadState->shouldPropagateWorkSource() ?
538 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700539 writeInt32(kHeader);
Steven Morelanddbc76c72020-10-01 18:02:48 +0000540
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700541 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000542 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543}
544
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000545bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
546{
547 if (!mRequestHeaderPresent) {
548 return false;
549 }
550
551 const size_t initialPosition = dataPosition();
552 setDataPosition(mWorkSourceRequestHeaderPosition);
553 status_t err = writeInt32(uid);
554 setDataPosition(initialPosition);
555 return err == NO_ERROR;
556}
557
Steven Morelandf1b1e492019-05-06 15:05:13 -0700558uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000559{
560 if (!mRequestHeaderPresent) {
561 return IPCThreadState::kUnsetWorkSource;
562 }
563
564 const size_t initialPosition = dataPosition();
565 setDataPosition(mWorkSourceRequestHeaderPosition);
566 uid_t uid = readInt32();
567 setDataPosition(initialPosition);
568 return uid;
569}
570
Mathias Agopian83c04462009-05-22 19:00:22 -0700571bool Parcel::checkInterface(IBinder* binder) const
572{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700573 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700574}
575
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700576bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700577 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700578{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700579 return enforceInterface(interface.string(), interface.size(), threadState);
580}
581
582bool Parcel::enforceInterface(const char16_t* interface,
583 size_t len,
584 IPCThreadState* threadState) const
585{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100586 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700587 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700588 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700589 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700590 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700591 if ((threadState->getLastTransactionBinderFlags() &
592 IBinder::FLAG_ONEWAY) != 0) {
593 // For one-way calls, the callee is running entirely
594 // disconnected from the caller, so disable StrictMode entirely.
595 // Not only does disk/network usage not impact the caller, but
596 // there's no way to commuicate back any violations anyway.
597 threadState->setStrictModePolicy(0);
598 } else {
599 threadState->setStrictModePolicy(strictPolicy);
600 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100601 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000602 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100603 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000604 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700605 // vendor header
606 int32_t header = readInt32();
607 if (header != kHeader) {
608 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
609 return false;
610 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100611 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700612 size_t parcel_interface_len;
613 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
614 if (len == parcel_interface_len &&
615 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700616 return true;
617 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700618 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700619 String8(interface, len).string(),
620 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700621 return false;
622 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700623}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700624
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700625size_t Parcel::objectsCount() const
626{
627 return mObjectsSize;
628}
629
630status_t Parcel::errorCheck() const
631{
632 return mError;
633}
634
635void Parcel::setError(status_t err)
636{
637 mError = err;
638}
639
640status_t Parcel::finishWrite(size_t len)
641{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700642 if (len > INT32_MAX) {
643 // don't accept size_t values which may have come from an
644 // inadvertent conversion from a negative int.
645 return BAD_VALUE;
646 }
647
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700648 //printf("Finish write of %d\n", len);
649 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700650 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700651 if (mDataPos > mDataSize) {
652 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700653 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700654 }
655 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
656 return NO_ERROR;
657}
658
659status_t Parcel::writeUnpadded(const void* data, size_t len)
660{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700661 if (len > INT32_MAX) {
662 // don't accept size_t values which may have come from an
663 // inadvertent conversion from a negative int.
664 return BAD_VALUE;
665 }
666
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700667 size_t end = mDataPos + len;
668 if (end < mDataPos) {
669 // integer overflow
670 return BAD_VALUE;
671 }
672
673 if (end <= mDataCapacity) {
674restart_write:
675 memcpy(mData+mDataPos, data, len);
676 return finishWrite(len);
677 }
678
679 status_t err = growData(len);
680 if (err == NO_ERROR) goto restart_write;
681 return err;
682}
683
684status_t Parcel::write(const void* data, size_t len)
685{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700686 if (len > INT32_MAX) {
687 // don't accept size_t values which may have come from an
688 // inadvertent conversion from a negative int.
689 return BAD_VALUE;
690 }
691
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700692 void* const d = writeInplace(len);
693 if (d) {
694 memcpy(d, data, len);
695 return NO_ERROR;
696 }
697 return mError;
698}
699
700void* Parcel::writeInplace(size_t len)
701{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700702 if (len > INT32_MAX) {
703 // don't accept size_t values which may have come from an
704 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700705 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700706 }
707
708 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700709
710 // sanity check for integer overflow
711 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700712 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700713 }
714
715 if ((mDataPos+padded) <= mDataCapacity) {
716restart_write:
717 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
718 uint8_t* const data = mData+mDataPos;
719
720 // Need to pad at end?
721 if (padded != len) {
722#if BYTE_ORDER == BIG_ENDIAN
723 static const uint32_t mask[4] = {
724 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
725 };
726#endif
727#if BYTE_ORDER == LITTLE_ENDIAN
728 static const uint32_t mask[4] = {
729 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
730 };
731#endif
732 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
733 // *reinterpret_cast<void**>(data+padded-4));
734 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
735 }
736
737 finishWrite(padded);
738 return data;
739 }
740
741 status_t err = growData(padded);
742 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700743 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700744}
745
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800746status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
747 const uint8_t* strData = (uint8_t*)str.data();
748 const size_t strLen= str.length();
749 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100750 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800751 return BAD_VALUE;
752 }
753
754 status_t err = writeInt32(utf16Len);
755 if (err) {
756 return err;
757 }
758
759 // Allocate enough bytes to hold our converted string and its terminating NULL.
760 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
761 if (!dst) {
762 return NO_MEMORY;
763 }
764
Sergio Girof4607432016-07-21 14:46:35 +0100765 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800766
767 return NO_ERROR;
768}
769
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900770status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) {
771 if (!str) {
772 return writeInt32(-1);
773 }
774 return writeUtf8AsUtf16(*str);
775}
776
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800777status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
778 if (!str) {
779 return writeInt32(-1);
780 }
781 return writeUtf8AsUtf16(*str);
782}
783
Daniel Normand0337ef2019-09-20 15:46:03 -0700784status_t Parcel::writeByteVectorInternal(const int8_t* data, size_t size) {
785 if (size > std::numeric_limits<int32_t>::max()) {
786 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -0700787 }
788
Daniel Normand0337ef2019-09-20 15:46:03 -0700789 status_t status = writeInt32(size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700790 if (status != OK) {
791 return status;
792 }
793
Daniel Normand0337ef2019-09-20 15:46:03 -0700794 return write(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700795}
796
Casey Dahlin185d3442016-02-09 11:08:35 -0800797status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700798 return writeByteVectorInternal(val.data(), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800799}
800
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900801status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val)
802{
803 if (!val) return writeInt32(-1);
804 return writeByteVectorInternal(val->data(), val->size());
805}
806
Casey Dahlin185d3442016-02-09 11:08:35 -0800807status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
808{
Daniel Normand0337ef2019-09-20 15:46:03 -0700809 if (!val) return writeInt32(-1);
810 return writeByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800811}
812
813status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700814 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val.data()), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800815}
816
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900817status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val)
818{
819 if (!val) return writeInt32(-1);
820 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
821}
822
Casey Dahlin185d3442016-02-09 11:08:35 -0800823status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
824{
Daniel Normand0337ef2019-09-20 15:46:03 -0700825 if (!val) return writeInt32(-1);
826 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800827}
828
Casey Dahlin451ff582015-10-19 18:12:18 -0700829status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
830{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800831 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700832}
833
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900834status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val)
835{
836 return writeNullableTypedVector(val, &Parcel::writeInt32);
837}
838
Casey Dahlinb9872622015-11-25 15:09:45 -0800839status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
840{
841 return writeNullableTypedVector(val, &Parcel::writeInt32);
842}
843
Casey Dahlin451ff582015-10-19 18:12:18 -0700844status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
845{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800846 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700847}
848
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900849status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val)
850{
851 return writeNullableTypedVector(val, &Parcel::writeInt64);
852}
853
Casey Dahlinb9872622015-11-25 15:09:45 -0800854status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
855{
856 return writeNullableTypedVector(val, &Parcel::writeInt64);
857}
858
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800859status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
860{
861 return writeTypedVector(val, &Parcel::writeUint64);
862}
863
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900864status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val)
865{
866 return writeNullableTypedVector(val, &Parcel::writeUint64);
867}
868
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800869status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
870{
871 return writeNullableTypedVector(val, &Parcel::writeUint64);
872}
873
Casey Dahlin451ff582015-10-19 18:12:18 -0700874status_t Parcel::writeFloatVector(const std::vector<float>& val)
875{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800876 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700877}
878
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900879status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val)
880{
881 return writeNullableTypedVector(val, &Parcel::writeFloat);
882}
883
Casey Dahlinb9872622015-11-25 15:09:45 -0800884status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
885{
886 return writeNullableTypedVector(val, &Parcel::writeFloat);
887}
888
Casey Dahlin451ff582015-10-19 18:12:18 -0700889status_t Parcel::writeDoubleVector(const std::vector<double>& val)
890{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800891 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700892}
893
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900894status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val)
895{
896 return writeNullableTypedVector(val, &Parcel::writeDouble);
897}
898
Casey Dahlinb9872622015-11-25 15:09:45 -0800899status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
900{
901 return writeNullableTypedVector(val, &Parcel::writeDouble);
902}
903
Casey Dahlin451ff582015-10-19 18:12:18 -0700904status_t Parcel::writeBoolVector(const std::vector<bool>& val)
905{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800906 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700907}
908
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900909status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val)
910{
911 return writeNullableTypedVector(val, &Parcel::writeBool);
912}
913
Casey Dahlinb9872622015-11-25 15:09:45 -0800914status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
915{
916 return writeNullableTypedVector(val, &Parcel::writeBool);
917}
918
Casey Dahlin451ff582015-10-19 18:12:18 -0700919status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
920{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800921 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700922}
923
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900924status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val)
925{
926 return writeNullableTypedVector(val, &Parcel::writeChar);
927}
928
Casey Dahlinb9872622015-11-25 15:09:45 -0800929status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
930{
931 return writeNullableTypedVector(val, &Parcel::writeChar);
932}
933
Casey Dahlin451ff582015-10-19 18:12:18 -0700934status_t Parcel::writeString16Vector(const std::vector<String16>& val)
935{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800936 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700937}
938
Casey Dahlinb9872622015-11-25 15:09:45 -0800939status_t Parcel::writeString16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900940 const std::optional<std::vector<std::optional<String16>>>& val)
941{
942 return writeNullableTypedVector(val, &Parcel::writeString16);
943}
944
945status_t Parcel::writeString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800946 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
947{
948 return writeNullableTypedVector(val, &Parcel::writeString16);
949}
950
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800951status_t Parcel::writeUtf8VectorAsUtf16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900952 const std::optional<std::vector<std::optional<std::string>>>& val) {
953 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
954}
955
956status_t Parcel::writeUtf8VectorAsUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800957 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
958 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
959}
960
961status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
962 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
963}
964
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700965status_t Parcel::writeInt32(int32_t val)
966{
Andreas Huber84a6d042009-08-17 13:33:27 -0700967 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700968}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800969
970status_t Parcel::writeUint32(uint32_t val)
971{
972 return writeAligned(val);
973}
974
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700975status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700976 if (len > INT32_MAX) {
977 // don't accept size_t values which may have come from an
978 // inadvertent conversion from a negative int.
979 return BAD_VALUE;
980 }
981
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700982 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700983 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700984 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700985 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700986 if (ret == NO_ERROR) {
987 ret = write(val, len * sizeof(*val));
988 }
989 return ret;
990}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700991status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700992 if (len > INT32_MAX) {
993 // don't accept size_t values which may have come from an
994 // inadvertent conversion from a negative int.
995 return BAD_VALUE;
996 }
997
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700998 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700999 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001000 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001001 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001002 if (ret == NO_ERROR) {
1003 ret = write(val, len * sizeof(*val));
1004 }
1005 return ret;
1006}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001007
Casey Dahlind6848f52015-10-15 15:44:59 -07001008status_t Parcel::writeBool(bool val)
1009{
1010 return writeInt32(int32_t(val));
1011}
1012
1013status_t Parcel::writeChar(char16_t val)
1014{
1015 return writeInt32(int32_t(val));
1016}
1017
1018status_t Parcel::writeByte(int8_t val)
1019{
1020 return writeInt32(int32_t(val));
1021}
1022
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001023status_t Parcel::writeInt64(int64_t val)
1024{
Andreas Huber84a6d042009-08-17 13:33:27 -07001025 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001026}
1027
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001028status_t Parcel::writeUint64(uint64_t val)
1029{
1030 return writeAligned(val);
1031}
1032
Serban Constantinescuf683e012013-11-05 16:53:55 +00001033status_t Parcel::writePointer(uintptr_t val)
1034{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001035 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001036}
1037
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001038status_t Parcel::writeFloat(float val)
1039{
Andreas Huber84a6d042009-08-17 13:33:27 -07001040 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001041}
1042
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001043#if defined(__mips__) && defined(__mips_hard_float)
1044
1045status_t Parcel::writeDouble(double val)
1046{
1047 union {
1048 double d;
1049 unsigned long long ll;
1050 } u;
1051 u.d = val;
1052 return writeAligned(u.ll);
1053}
1054
1055#else
1056
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001057status_t Parcel::writeDouble(double val)
1058{
Andreas Huber84a6d042009-08-17 13:33:27 -07001059 return writeAligned(val);
1060}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001061
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001062#endif
1063
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001064status_t Parcel::writeCString(const char* str)
1065{
1066 return write(str, strlen(str)+1);
1067}
1068
1069status_t Parcel::writeString8(const String8& str)
1070{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001071 return writeString8(str.string(), str.size());
1072}
1073
1074status_t Parcel::writeString8(const char* str, size_t len)
1075{
1076 if (str == nullptr) return writeInt32(-1);
1077
1078 status_t err = writeInt32(len);
1079 if (err == NO_ERROR) {
1080 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1081 if (data) {
1082 memcpy(data, str, len);
1083 *reinterpret_cast<char*>(data+len) = 0;
1084 return NO_ERROR;
1085 }
1086 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001087 }
1088 return err;
1089}
1090
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001091status_t Parcel::writeString16(const std::optional<String16>& str)
1092{
1093 if (!str) {
1094 return writeInt32(-1);
1095 }
1096
1097 return writeString16(*str);
1098}
1099
Casey Dahlinb9872622015-11-25 15:09:45 -08001100status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1101{
1102 if (!str) {
1103 return writeInt32(-1);
1104 }
1105
1106 return writeString16(*str);
1107}
1108
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001109status_t Parcel::writeString16(const String16& str)
1110{
1111 return writeString16(str.string(), str.size());
1112}
1113
1114status_t Parcel::writeString16(const char16_t* str, size_t len)
1115{
Yi Kong91635562018-06-07 14:38:36 -07001116 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001117
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001118 status_t err = writeInt32(len);
1119 if (err == NO_ERROR) {
1120 len *= sizeof(char16_t);
1121 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1122 if (data) {
1123 memcpy(data, str, len);
1124 *reinterpret_cast<char16_t*>(data+len) = 0;
1125 return NO_ERROR;
1126 }
1127 err = mError;
1128 }
1129 return err;
1130}
1131
1132status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1133{
Steven Morelanda86a3562019-08-01 23:28:34 +00001134 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001135}
1136
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001137status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1138{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001139 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001140}
1141
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001142status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val)
1143{
1144 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1145}
1146
Casey Dahlinb9872622015-11-25 15:09:45 -08001147status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1148{
1149 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1150}
1151
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001152status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const {
1153 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
1154}
1155
Casey Dahlinb9872622015-11-25 15:09:45 -08001156status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001157 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001158}
1159
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001160status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001161 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001162}
1163
Casey Dahlinb9872622015-11-25 15:09:45 -08001164status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1165 if (!parcelable) {
1166 return writeInt32(0);
1167 }
1168
1169 return writeParcelable(*parcelable);
1170}
1171
Christopher Wiley97f048d2015-11-19 06:49:05 -08001172status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1173 status_t status = writeInt32(1); // parcelable is not null.
1174 if (status != OK) {
1175 return status;
1176 }
1177 return parcelable.writeToParcel(this);
1178}
1179
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001180status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001181{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001182 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001183 return BAD_TYPE;
1184
1185 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001186 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001187 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001189 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001190 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001191
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001192 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1193 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194
1195 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001196 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001197 return err;
1198 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001199 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001200 return err;
1201}
1202
Jeff Brown93ff1f92011-11-04 19:01:44 -07001203status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001204{
1205 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001206 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001207 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001208 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001209 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001210 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001211 return writeObject(obj, true);
1212}
1213
1214status_t Parcel::writeDupFileDescriptor(int fd)
1215{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001216 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001217 if (dupFd < 0) {
1218 return -errno;
1219 }
1220 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001221 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001222 close(dupFd);
1223 }
1224 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001225}
1226
Dianne Hackborn1941a402016-08-29 12:30:43 -07001227status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1228{
1229 writeInt32(0);
1230 return writeFileDescriptor(fd, takeOwnership);
1231}
1232
Ryo Hashimotobf551892018-05-31 16:58:35 +09001233status_t Parcel::writeDupParcelFileDescriptor(int fd)
1234{
1235 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1236 if (dupFd < 0) {
1237 return -errno;
1238 }
1239 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1240 if (err != OK) {
1241 close(dupFd);
1242 }
1243 return err;
1244}
1245
Christopher Wiley2cf19952016-04-11 11:09:37 -07001246status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001247 return writeDupFileDescriptor(fd.get());
1248}
1249
Christopher Wiley2cf19952016-04-11 11:09:37 -07001250status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001251 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1252}
1253
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001254status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) {
1255 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1256}
1257
Christopher Wiley2cf19952016-04-11 11:09:37 -07001258status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001259 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1260}
1261
Jeff Brown13b16042014-11-11 16:44:25 -08001262status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001263{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001264 if (len > INT32_MAX) {
1265 // don't accept size_t values which may have come from an
1266 // inadvertent conversion from a negative int.
1267 return BAD_VALUE;
1268 }
1269
Jeff Brown13b16042014-11-11 16:44:25 -08001270 status_t status;
1271 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001272 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001273 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001274 if (status) return status;
1275
1276 void* ptr = writeInplace(len);
1277 if (!ptr) return NO_MEMORY;
1278
Jeff Brown13b16042014-11-11 16:44:25 -08001279 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001280 return NO_ERROR;
1281 }
1282
Steve Block6807e592011-10-20 11:56:00 +01001283 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001284 int fd = ashmem_create_region("Parcel Blob", len);
1285 if (fd < 0) return NO_MEMORY;
1286
1287 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1288 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001289 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001290 } else {
Yi Kong91635562018-06-07 14:38:36 -07001291 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001292 if (ptr == MAP_FAILED) {
1293 status = -errno;
1294 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001295 if (!mutableCopy) {
1296 result = ashmem_set_prot_region(fd, PROT_READ);
1297 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001298 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001299 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001300 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001301 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001302 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001303 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001304 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001305 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001306 return NO_ERROR;
1307 }
1308 }
1309 }
1310 }
1311 ::munmap(ptr, len);
1312 }
1313 ::close(fd);
1314 return status;
1315}
1316
Jeff Brown13b16042014-11-11 16:44:25 -08001317status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1318{
1319 // Must match up with what's done in writeBlob.
1320 if (!mAllowFds) return FDS_NOT_ALLOWED;
1321 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1322 if (status) return status;
1323 return writeDupFileDescriptor(fd);
1324}
1325
Mathias Agopiane1424282013-07-29 21:24:40 -07001326status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001327{
1328 status_t err;
1329
1330 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001331 const size_t len = val.getFlattenedSize();
1332 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001333
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001334 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001335 // don't accept size_t values which may have come from an
1336 // inadvertent conversion from a negative int.
1337 return BAD_VALUE;
1338 }
1339
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001340 err = this->writeInt32(len);
1341 if (err) return err;
1342
1343 err = this->writeInt32(fd_count);
1344 if (err) return err;
1345
1346 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001347 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001348 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001349 return BAD_VALUE;
1350
Yi Kong91635562018-06-07 14:38:36 -07001351 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001352 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001353 fds = new (std::nothrow) int[fd_count];
1354 if (fds == nullptr) {
1355 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1356 return BAD_VALUE;
1357 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001358 }
1359
1360 err = val.flatten(buf, len, fds, fd_count);
1361 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1362 err = this->writeDupFileDescriptor( fds[i] );
1363 }
1364
1365 if (fd_count) {
1366 delete [] fds;
1367 }
1368
1369 return err;
1370}
1371
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001372status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1373{
1374 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1375 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1376 if (enoughData && enoughObjects) {
1377restart_write:
1378 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001379
Christopher Tate98e67d32015-06-03 18:44:15 -07001380 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001381 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001382 if (!mAllowFds) {
1383 // fail before modifying our object index
1384 return FDS_NOT_ALLOWED;
1385 }
1386 mHasFds = mFdsKnown = true;
1387 }
1388
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001389 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001390 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001391 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001392 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001393 mObjectsSize++;
1394 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001395
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001396 return finishWrite(sizeof(flat_binder_object));
1397 }
1398
1399 if (!enoughData) {
1400 const status_t err = growData(sizeof(val));
1401 if (err != NO_ERROR) return err;
1402 }
1403 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001404 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1405 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001406 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001407 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001408 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001409 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001410 mObjects = objects;
1411 mObjectsCapacity = newSize;
1412 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001413
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001414 goto restart_write;
1415}
1416
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001417status_t Parcel::writeNoException()
1418{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001419 binder::Status status;
1420 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001421}
1422
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001423status_t Parcel::validateReadData(size_t upperBound) const
1424{
1425 // Don't allow non-object reads on object data
1426 if (mObjectsSorted || mObjectsSize <= 1) {
1427data_sorted:
1428 // Expect to check only against the next object
1429 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1430 // For some reason the current read position is greater than the next object
1431 // hint. Iterate until we find the right object
1432 size_t nextObject = mNextObjectHint;
1433 do {
1434 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1435 // Requested info overlaps with an object
1436 ALOGE("Attempt to read from protected data in Parcel %p", this);
1437 return PERMISSION_DENIED;
1438 }
1439 nextObject++;
1440 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1441 mNextObjectHint = nextObject;
1442 }
1443 return NO_ERROR;
1444 }
1445 // Quickly determine if mObjects is sorted.
1446 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1447 binder_size_t* prevObj = currObj;
1448 while (currObj > mObjects) {
1449 prevObj--;
1450 if(*prevObj > *currObj) {
1451 goto data_unsorted;
1452 }
1453 currObj--;
1454 }
1455 mObjectsSorted = true;
1456 goto data_sorted;
1457
1458data_unsorted:
1459 // Insertion Sort mObjects
1460 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1461 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1462 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1463 binder_size_t temp = *iter0;
1464 binder_size_t* iter1 = iter0 - 1;
1465 while (iter1 >= mObjects && *iter1 > temp) {
1466 *(iter1 + 1) = *iter1;
1467 iter1--;
1468 }
1469 *(iter1 + 1) = temp;
1470 }
1471 mNextObjectHint = 0;
1472 mObjectsSorted = true;
1473 goto data_sorted;
1474}
1475
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001476status_t Parcel::read(void* outData, size_t len) const
1477{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001478 if (len > INT32_MAX) {
1479 // don't accept size_t values which may have come from an
1480 // inadvertent conversion from a negative int.
1481 return BAD_VALUE;
1482 }
1483
1484 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1485 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001486 if (mObjectsSize > 0) {
1487 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001488 if(err != NO_ERROR) {
1489 // Still increment the data position by the expected length
1490 mDataPos += pad_size(len);
1491 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1492 return err;
1493 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001494 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001495 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001496 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001497 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001498 return NO_ERROR;
1499 }
1500 return NOT_ENOUGH_DATA;
1501}
1502
1503const void* Parcel::readInplace(size_t len) const
1504{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001505 if (len > INT32_MAX) {
1506 // don't accept size_t values which may have come from an
1507 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001508 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001509 }
1510
1511 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1512 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001513 if (mObjectsSize > 0) {
1514 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001515 if(err != NO_ERROR) {
1516 // Still increment the data position by the expected length
1517 mDataPos += pad_size(len);
1518 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001519 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001520 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001521 }
1522
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001523 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001524 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001525 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001526 return data;
1527 }
Yi Kong91635562018-06-07 14:38:36 -07001528 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001529}
1530
Andreas Huber84a6d042009-08-17 13:33:27 -07001531template<class T>
1532status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001533 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001534
1535 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001536 if (mObjectsSize > 0) {
1537 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001538 if(err != NO_ERROR) {
1539 // Still increment the data position by the expected length
1540 mDataPos += sizeof(T);
1541 return err;
1542 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001543 }
1544
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001545 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001546 mDataPos += sizeof(T);
1547 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001548 return NO_ERROR;
1549 } else {
1550 return NOT_ENOUGH_DATA;
1551 }
1552}
1553
Andreas Huber84a6d042009-08-17 13:33:27 -07001554template<class T>
1555T Parcel::readAligned() const {
1556 T result;
1557 if (readAligned(&result) != NO_ERROR) {
1558 result = 0;
1559 }
1560
1561 return result;
1562}
1563
1564template<class T>
1565status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001566 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001567
1568 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1569restart_write:
1570 *reinterpret_cast<T*>(mData+mDataPos) = val;
1571 return finishWrite(sizeof(val));
1572 }
1573
1574 status_t err = growData(sizeof(val));
1575 if (err == NO_ERROR) goto restart_write;
1576 return err;
1577}
1578
Casey Dahlin185d3442016-02-09 11:08:35 -08001579status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001580 size_t size;
1581 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1582 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001583}
1584
1585status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001586 size_t size;
1587 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1588 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001589}
1590
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001591status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const {
1592 size_t size;
1593 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1594 if (!*val) {
1595 // reserveOutVector does not create the out vector if size is < 0.
1596 // This occurs when writing a null byte vector.
1597 return OK;
1598 }
1599 return readByteVectorInternal(&**val, size);
1600}
1601
Casey Dahlin185d3442016-02-09 11:08:35 -08001602status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001603 size_t size;
1604 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001605 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001606 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001607 // This occurs when writing a null byte vector.
1608 return OK;
1609 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001610 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001611}
1612
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001613status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const {
1614 size_t size;
1615 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1616 if (!*val) {
1617 // reserveOutVector does not create the out vector if size is < 0.
1618 // This occurs when writing a null byte vector.
1619 return OK;
1620 }
1621 return readByteVectorInternal(&**val, size);
1622}
1623
Casey Dahlin185d3442016-02-09 11:08:35 -08001624status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001625 size_t size;
1626 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001627 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001628 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001629 // This occurs when writing a null byte vector.
1630 return OK;
1631 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001632 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001633}
1634
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001635status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const {
1636 return readNullableTypedVector(val, &Parcel::readInt32);
1637}
1638
Casey Dahlinb9872622015-11-25 15:09:45 -08001639status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1640 return readNullableTypedVector(val, &Parcel::readInt32);
1641}
1642
Casey Dahlin451ff582015-10-19 18:12:18 -07001643status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001644 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001645}
1646
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001647status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const {
1648 return readNullableTypedVector(val, &Parcel::readInt64);
1649}
1650
Casey Dahlinb9872622015-11-25 15:09:45 -08001651status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1652 return readNullableTypedVector(val, &Parcel::readInt64);
1653}
1654
Casey Dahlin451ff582015-10-19 18:12:18 -07001655status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001656 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001657}
1658
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001659status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const {
1660 return readNullableTypedVector(val, &Parcel::readUint64);
1661}
1662
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001663status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1664 return readNullableTypedVector(val, &Parcel::readUint64);
1665}
1666
1667status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1668 return readTypedVector(val, &Parcel::readUint64);
1669}
1670
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001671status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const {
1672 return readNullableTypedVector(val, &Parcel::readFloat);
1673}
1674
Casey Dahlinb9872622015-11-25 15:09:45 -08001675status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1676 return readNullableTypedVector(val, &Parcel::readFloat);
1677}
1678
Casey Dahlin451ff582015-10-19 18:12:18 -07001679status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001680 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001681}
1682
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001683status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const {
1684 return readNullableTypedVector(val, &Parcel::readDouble);
1685}
1686
Casey Dahlinb9872622015-11-25 15:09:45 -08001687status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1688 return readNullableTypedVector(val, &Parcel::readDouble);
1689}
1690
Casey Dahlin451ff582015-10-19 18:12:18 -07001691status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001692 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001693}
1694
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001695status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const {
1696 const int32_t start = dataPosition();
1697 int32_t size;
1698 status_t status = readInt32(&size);
1699 val->reset();
1700
1701 if (status != OK || size < 0) {
1702 return status;
1703 }
1704
1705 setDataPosition(start);
1706 val->emplace();
1707
1708 status = readBoolVector(&**val);
1709
1710 if (status != OK) {
1711 val->reset();
1712 }
1713
1714 return status;
1715}
1716
Casey Dahlinb9872622015-11-25 15:09:45 -08001717status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1718 const int32_t start = dataPosition();
1719 int32_t size;
1720 status_t status = readInt32(&size);
1721 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001722
Casey Dahlinb9872622015-11-25 15:09:45 -08001723 if (status != OK || size < 0) {
1724 return status;
1725 }
1726
1727 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001728 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001729
1730 status = readBoolVector(val->get());
1731
1732 if (status != OK) {
1733 val->reset();
1734 }
1735
1736 return status;
1737}
1738
1739status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001740 int32_t size;
1741 status_t status = readInt32(&size);
1742
1743 if (status != OK) {
1744 return status;
1745 }
1746
1747 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001748 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001749 }
1750
1751 val->resize(size);
1752
1753 /* C++ bool handling means a vector of bools isn't necessarily addressable
1754 * (we might use individual bits)
1755 */
Christopher Wiley97887982015-10-27 16:33:47 -07001756 bool data;
1757 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001758 status = readBool(&data);
1759 (*val)[i] = data;
1760
1761 if (status != OK) {
1762 return status;
1763 }
1764 }
1765
1766 return OK;
1767}
1768
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001769status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const {
1770 return readNullableTypedVector(val, &Parcel::readChar);
1771}
1772
Casey Dahlinb9872622015-11-25 15:09:45 -08001773status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1774 return readNullableTypedVector(val, &Parcel::readChar);
1775}
1776
Casey Dahlin451ff582015-10-19 18:12:18 -07001777status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001778 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001779}
1780
Casey Dahlinb9872622015-11-25 15:09:45 -08001781status_t Parcel::readString16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001782 std::optional<std::vector<std::optional<String16>>>* val) const {
1783 return readNullableTypedVector(val, &Parcel::readString16);
1784}
1785
1786status_t Parcel::readString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -08001787 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1788 return readNullableTypedVector(val, &Parcel::readString16);
1789}
1790
Casey Dahlin451ff582015-10-19 18:12:18 -07001791status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001792 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001793}
1794
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001795status_t Parcel::readUtf8VectorFromUtf16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001796 std::optional<std::vector<std::optional<std::string>>>* val) const {
1797 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1798}
1799
1800status_t Parcel::readUtf8VectorFromUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001801 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1802 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1803}
1804
1805status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1806 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1807}
Casey Dahlin451ff582015-10-19 18:12:18 -07001808
Andreas Huber84a6d042009-08-17 13:33:27 -07001809status_t Parcel::readInt32(int32_t *pArg) const
1810{
1811 return readAligned(pArg);
1812}
1813
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001814int32_t Parcel::readInt32() const
1815{
Andreas Huber84a6d042009-08-17 13:33:27 -07001816 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001817}
1818
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001819status_t Parcel::readUint32(uint32_t *pArg) const
1820{
1821 return readAligned(pArg);
1822}
1823
1824uint32_t Parcel::readUint32() const
1825{
1826 return readAligned<uint32_t>();
1827}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001828
1829status_t Parcel::readInt64(int64_t *pArg) const
1830{
Andreas Huber84a6d042009-08-17 13:33:27 -07001831 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001832}
1833
1834
1835int64_t Parcel::readInt64() const
1836{
Andreas Huber84a6d042009-08-17 13:33:27 -07001837 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001838}
1839
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001840status_t Parcel::readUint64(uint64_t *pArg) const
1841{
1842 return readAligned(pArg);
1843}
1844
1845uint64_t Parcel::readUint64() const
1846{
1847 return readAligned<uint64_t>();
1848}
1849
Serban Constantinescuf683e012013-11-05 16:53:55 +00001850status_t Parcel::readPointer(uintptr_t *pArg) const
1851{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001852 status_t ret;
1853 binder_uintptr_t ptr;
1854 ret = readAligned(&ptr);
1855 if (!ret)
1856 *pArg = ptr;
1857 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001858}
1859
1860uintptr_t Parcel::readPointer() const
1861{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001862 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001863}
1864
1865
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001866status_t Parcel::readFloat(float *pArg) const
1867{
Andreas Huber84a6d042009-08-17 13:33:27 -07001868 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001869}
1870
1871
1872float Parcel::readFloat() const
1873{
Andreas Huber84a6d042009-08-17 13:33:27 -07001874 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001875}
1876
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001877#if defined(__mips__) && defined(__mips_hard_float)
1878
1879status_t Parcel::readDouble(double *pArg) const
1880{
1881 union {
1882 double d;
1883 unsigned long long ll;
1884 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001885 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001886 status_t status;
1887 status = readAligned(&u.ll);
1888 *pArg = u.d;
1889 return status;
1890}
1891
1892double Parcel::readDouble() const
1893{
1894 union {
1895 double d;
1896 unsigned long long ll;
1897 } u;
1898 u.ll = readAligned<unsigned long long>();
1899 return u.d;
1900}
1901
1902#else
1903
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001904status_t Parcel::readDouble(double *pArg) const
1905{
Andreas Huber84a6d042009-08-17 13:33:27 -07001906 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001907}
1908
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001909double Parcel::readDouble() const
1910{
Andreas Huber84a6d042009-08-17 13:33:27 -07001911 return readAligned<double>();
1912}
1913
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001914#endif
1915
Andreas Huber84a6d042009-08-17 13:33:27 -07001916status_t Parcel::readIntPtr(intptr_t *pArg) const
1917{
1918 return readAligned(pArg);
1919}
1920
1921
1922intptr_t Parcel::readIntPtr() const
1923{
1924 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001925}
1926
Casey Dahlind6848f52015-10-15 15:44:59 -07001927status_t Parcel::readBool(bool *pArg) const
1928{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001929 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001930 status_t ret = readInt32(&tmp);
1931 *pArg = (tmp != 0);
1932 return ret;
1933}
1934
1935bool Parcel::readBool() const
1936{
1937 return readInt32() != 0;
1938}
1939
1940status_t Parcel::readChar(char16_t *pArg) const
1941{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001942 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001943 status_t ret = readInt32(&tmp);
1944 *pArg = char16_t(tmp);
1945 return ret;
1946}
1947
1948char16_t Parcel::readChar() const
1949{
1950 return char16_t(readInt32());
1951}
1952
1953status_t Parcel::readByte(int8_t *pArg) const
1954{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001955 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001956 status_t ret = readInt32(&tmp);
1957 *pArg = int8_t(tmp);
1958 return ret;
1959}
1960
1961int8_t Parcel::readByte() const
1962{
1963 return int8_t(readInt32());
1964}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001965
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001966status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1967 size_t utf16Size = 0;
1968 const char16_t* src = readString16Inplace(&utf16Size);
1969 if (!src) {
1970 return UNEXPECTED_NULL;
1971 }
1972
1973 // Save ourselves the trouble, we're done.
1974 if (utf16Size == 0u) {
1975 str->clear();
1976 return NO_ERROR;
1977 }
1978
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001979 // Allow for closing '\0'
1980 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1981 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001982 return BAD_VALUE;
1983 }
1984 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001985 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001986 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001987 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1988 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001989 return NO_ERROR;
1990}
1991
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001992status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const {
1993 const int32_t start = dataPosition();
1994 int32_t size;
1995 status_t status = readInt32(&size);
1996 str->reset();
1997
1998 if (status != OK || size < 0) {
1999 return status;
2000 }
2001
2002 setDataPosition(start);
2003 str->emplace();
2004 return readUtf8FromUtf16(&**str);
2005}
2006
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002007status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2008 const int32_t start = dataPosition();
2009 int32_t size;
2010 status_t status = readInt32(&size);
2011 str->reset();
2012
2013 if (status != OK || size < 0) {
2014 return status;
2015 }
2016
2017 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002018 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002019 return readUtf8FromUtf16(str->get());
2020}
2021
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002022const char* Parcel::readCString() const
2023{
Steven Morelandd0d4b582019-05-17 13:14:06 -07002024 if (mDataPos < mDataSize) {
2025 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002026 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2027 // is the string's trailing NUL within the parcel's valid bounds?
2028 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2029 if (eos) {
2030 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002031 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002032 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002033 return str;
2034 }
2035 }
Yi Kong91635562018-06-07 14:38:36 -07002036 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002037}
2038
2039String8 Parcel::readString8() const
2040{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002041 size_t len;
2042 const char* str = readString8Inplace(&len);
2043 if (str) return String8(str, len);
2044 ALOGE("Reading a NULL string not supported here.");
2045 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002046}
2047
2048status_t Parcel::readString8(String8* pArg) const
2049{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002050 size_t len;
2051 const char* str = readString8Inplace(&len);
2052 if (str) {
2053 pArg->setTo(str, len);
2054 return 0;
2055 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002056 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002057 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002058 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002059}
2060
2061const char* Parcel::readString8Inplace(size_t* outLen) const
2062{
2063 int32_t size = readInt32();
2064 // watch for potential int overflow from size+1
2065 if (size >= 0 && size < INT32_MAX) {
2066 *outLen = size;
2067 const char* str = (const char*)readInplace(size+1);
2068 if (str != nullptr) {
2069 return str;
2070 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002071 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002072 *outLen = 0;
2073 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002074}
2075
2076String16 Parcel::readString16() const
2077{
2078 size_t len;
2079 const char16_t* str = readString16Inplace(&len);
2080 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002081 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002082 return String16();
2083}
2084
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002085status_t Parcel::readString16(std::optional<String16>* pArg) const
2086{
2087 const int32_t start = dataPosition();
2088 int32_t size;
2089 status_t status = readInt32(&size);
2090 pArg->reset();
2091
2092 if (status != OK || size < 0) {
2093 return status;
2094 }
2095
2096 setDataPosition(start);
2097 pArg->emplace();
2098
2099 status = readString16(&**pArg);
2100
2101 if (status != OK) {
2102 pArg->reset();
2103 }
2104
2105 return status;
2106}
2107
Casey Dahlinb9872622015-11-25 15:09:45 -08002108status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2109{
2110 const int32_t start = dataPosition();
2111 int32_t size;
2112 status_t status = readInt32(&size);
2113 pArg->reset();
2114
2115 if (status != OK || size < 0) {
2116 return status;
2117 }
2118
2119 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002120 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002121
2122 status = readString16(pArg->get());
2123
2124 if (status != OK) {
2125 pArg->reset();
2126 }
2127
2128 return status;
2129}
2130
Casey Dahlin451ff582015-10-19 18:12:18 -07002131status_t Parcel::readString16(String16* pArg) const
2132{
2133 size_t len;
2134 const char16_t* str = readString16Inplace(&len);
2135 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002136 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002137 return 0;
2138 } else {
2139 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002140 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002141 }
2142}
2143
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002144const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2145{
2146 int32_t size = readInt32();
2147 // watch for potential int overflow from size+1
2148 if (size >= 0 && size < INT32_MAX) {
2149 *outLen = size;
2150 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002151 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002152 return str;
2153 }
2154 }
2155 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002156 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002157}
2158
Casey Dahlinf0c13772015-10-27 18:33:56 -07002159status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2160{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002161 status_t status = readNullableStrongBinder(val);
2162 if (status == OK && !val->get()) {
2163 status = UNEXPECTED_NULL;
2164 }
2165 return status;
2166}
2167
2168status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2169{
Steven Morelanda86a3562019-08-01 23:28:34 +00002170 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002171}
2172
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002173sp<IBinder> Parcel::readStrongBinder() const
2174{
2175 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002176 // Note that a lot of code in Android reads binders by hand with this
2177 // method, and that code has historically been ok with getting nullptr
2178 // back (while ignoring error codes).
2179 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002180 return val;
2181}
2182
Christopher Wiley97f048d2015-11-19 06:49:05 -08002183status_t Parcel::readParcelable(Parcelable* parcelable) const {
2184 int32_t have_parcelable = 0;
2185 status_t status = readInt32(&have_parcelable);
2186 if (status != OK) {
2187 return status;
2188 }
2189 if (!have_parcelable) {
2190 return UNEXPECTED_NULL;
2191 }
2192 return parcelable->readFromParcel(this);
2193}
2194
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002195int32_t Parcel::readExceptionCode() const
2196{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002197 binder::Status status;
2198 status.readFromParcel(*this);
2199 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002200}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002201
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002202native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002203{
2204 int numFds, numInts;
2205 status_t err;
2206 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002207 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002208 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002209 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002210
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002211 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002212 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002213 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002214 }
2215
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002216 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002217 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002218 if (h->data[i] < 0) {
2219 for (int j = 0; j < i; j++) {
2220 close(h->data[j]);
2221 }
2222 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002223 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002224 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002225 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002226 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002227 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002228 native_handle_close(h);
2229 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002230 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002231 }
2232 return h;
2233}
2234
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002235int Parcel::readFileDescriptor() const
2236{
2237 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002238
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002239 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002240 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002241 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002242
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002243 return BAD_TYPE;
2244}
2245
Dianne Hackborn1941a402016-08-29 12:30:43 -07002246int Parcel::readParcelFileDescriptor() const
2247{
2248 int32_t hasComm = readInt32();
2249 int fd = readFileDescriptor();
2250 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002251 // detach (owned by the binder driver)
2252 int comm = readFileDescriptor();
2253
2254 // warning: this must be kept in sync with:
2255 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2256 enum ParcelFileDescriptorStatus {
2257 DETACHED = 2,
2258 };
2259
2260#if BYTE_ORDER == BIG_ENDIAN
2261 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2262#endif
2263#if BYTE_ORDER == LITTLE_ENDIAN
2264 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2265#endif
2266
2267 ssize_t written = TEMP_FAILURE_RETRY(
2268 ::write(comm, &message, sizeof(message)));
2269
2270 if (written == -1 || written != sizeof(message)) {
2271 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2272 written, strerror(errno));
2273 return BAD_TYPE;
2274 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002275 }
2276 return fd;
2277}
2278
Christopher Wiley2cf19952016-04-11 11:09:37 -07002279status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002280{
2281 int got = readFileDescriptor();
2282
2283 if (got == BAD_TYPE) {
2284 return BAD_TYPE;
2285 }
2286
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002287 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002288
2289 if (val->get() < 0) {
2290 return BAD_VALUE;
2291 }
2292
2293 return OK;
2294}
2295
Ryo Hashimotobf551892018-05-31 16:58:35 +09002296status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2297{
2298 int got = readParcelFileDescriptor();
2299
2300 if (got == BAD_TYPE) {
2301 return BAD_TYPE;
2302 }
2303
2304 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2305
2306 if (val->get() < 0) {
2307 return BAD_VALUE;
2308 }
2309
2310 return OK;
2311}
Casey Dahlin06673e32015-11-23 13:24:23 -08002312
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002313status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const {
2314 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2315}
2316
Christopher Wiley2cf19952016-04-11 11:09:37 -07002317status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002318 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2319}
2320
Christopher Wiley2cf19952016-04-11 11:09:37 -07002321status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002322 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2323}
2324
Jeff Brown5707dbf2011-09-23 21:17:56 -07002325status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2326{
Jeff Brown13b16042014-11-11 16:44:25 -08002327 int32_t blobType;
2328 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002329 if (status) return status;
2330
Jeff Brown13b16042014-11-11 16:44:25 -08002331 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002332 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002333 const void* ptr = readInplace(len);
2334 if (!ptr) return BAD_VALUE;
2335
Jeff Brown13b16042014-11-11 16:44:25 -08002336 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002337 return NO_ERROR;
2338 }
2339
Steve Block6807e592011-10-20 11:56:00 +01002340 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002341 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002342 int fd = readFileDescriptor();
2343 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2344
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002345 if (!ashmem_valid(fd)) {
2346 ALOGE("invalid fd");
2347 return BAD_VALUE;
2348 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002349 int size = ashmem_get_size_region(fd);
2350 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002351 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002352 return BAD_VALUE;
2353 }
Yi Kong91635562018-06-07 14:38:36 -07002354 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002355 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002356 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002357
Jeff Brown13b16042014-11-11 16:44:25 -08002358 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002359 return NO_ERROR;
2360}
2361
Mathias Agopiane1424282013-07-29 21:24:40 -07002362status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002363{
2364 // size
2365 const size_t len = this->readInt32();
2366 const size_t fd_count = this->readInt32();
2367
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002368 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002369 // don't accept size_t values which may have come from an
2370 // inadvertent conversion from a negative int.
2371 return BAD_VALUE;
2372 }
2373
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002374 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002375 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002376 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002377 return BAD_VALUE;
2378
Yi Kong91635562018-06-07 14:38:36 -07002379 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002380 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002381 fds = new (std::nothrow) int[fd_count];
2382 if (fds == nullptr) {
2383 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2384 return BAD_VALUE;
2385 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002386 }
2387
2388 status_t err = NO_ERROR;
2389 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002390 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002391 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002392 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002393 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 -07002394 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2395 // Close all the file descriptors that were dup-ed.
2396 for (size_t j=0; j<i ;j++) {
2397 close(fds[j]);
2398 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002399 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002400 }
2401
2402 if (err == NO_ERROR) {
2403 err = val.unflatten(buf, len, fds, fd_count);
2404 }
2405
2406 if (fd_count) {
2407 delete [] fds;
2408 }
2409
2410 return err;
2411}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002412const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2413{
2414 const size_t DPOS = mDataPos;
2415 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2416 const flat_binder_object* obj
2417 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2418 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002419 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002420 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421 // the object list, so we don't want to check for it when
2422 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002423 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002424 return obj;
2425 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002426
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002428 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002429 const size_t N = mObjectsSize;
2430 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002431
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002432 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002433 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002434 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002435
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002436 // Start at the current hint position, looking for an object at
2437 // the current data position.
2438 if (opos < N) {
2439 while (opos < (N-1) && OBJS[opos] < DPOS) {
2440 opos++;
2441 }
2442 } else {
2443 opos = N-1;
2444 }
2445 if (OBJS[opos] == DPOS) {
2446 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002447 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002448 this, DPOS, opos);
2449 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002450 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002451 return obj;
2452 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002453
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002454 // Look backwards for it...
2455 while (opos > 0 && OBJS[opos] > DPOS) {
2456 opos--;
2457 }
2458 if (OBJS[opos] == DPOS) {
2459 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002460 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002461 this, DPOS, opos);
2462 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002463 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002464 return obj;
2465 }
2466 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002467 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 -07002468 this, DPOS);
2469 }
Yi Kong91635562018-06-07 14:38:36 -07002470 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002471}
2472
2473void Parcel::closeFileDescriptors()
2474{
2475 size_t i = mObjectsSize;
2476 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002477 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002478 }
2479 while (i > 0) {
2480 i--;
2481 const flat_binder_object* flat
2482 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002483 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002484 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002485 close(flat->handle);
2486 }
2487 }
2488}
2489
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002490uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002491{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002492 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002493}
2494
2495size_t Parcel::ipcDataSize() const
2496{
2497 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2498}
2499
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002500uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002501{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002502 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002503}
2504
2505size_t Parcel::ipcObjectsCount() const
2506{
2507 return mObjectsSize;
2508}
2509
2510void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002511 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002512{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002513 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002514 freeDataNoInit();
2515 mError = NO_ERROR;
2516 mData = const_cast<uint8_t*>(data);
2517 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002518 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002519 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002520 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002521 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002522 mObjectsSize = mObjectsCapacity = objectsCount;
2523 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002524 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002525 mOwner = relFunc;
2526 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002527 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002528 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002529 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002530 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002531 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002532 mObjectsSize = 0;
2533 break;
2534 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002535 const flat_binder_object* flat
2536 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2537 uint32_t type = flat->hdr.type;
2538 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2539 type == BINDER_TYPE_FD)) {
2540 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2541 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2542 // recover gracefully by clearing out the objects, and releasing the objects we do
2543 // know about.
2544 android_errorWriteLog(0x534e4554, "135930648");
2545 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2546 __func__, type, (uint64_t)offset);
2547 releaseObjects();
2548 mObjectsSize = 0;
2549 break;
2550 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002551 minOffset = offset + sizeof(flat_binder_object);
2552 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002553 scanForFds();
2554}
2555
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002556void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002557{
2558 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002559
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002560 if (errorCheck() != NO_ERROR) {
2561 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002562 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002563 } else if (dataSize() > 0) {
2564 const uint8_t* DATA = data();
2565 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002566 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002567 const size_t N = objectsCount();
2568 for (size_t i=0; i<N; i++) {
2569 const flat_binder_object* flat
2570 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2571 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002572 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002573 << " = " << flat->binder;
2574 }
2575 } else {
2576 to << "NULL";
2577 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002578
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002579 to << ")";
2580}
2581
2582void Parcel::releaseObjects()
2583{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002584 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002585 if (i == 0) {
2586 return;
2587 }
2588 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002589 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002590 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002591 while (i > 0) {
2592 i--;
2593 const flat_binder_object* flat
2594 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002595 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002596 }
2597}
2598
2599void Parcel::acquireObjects()
2600{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002601 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002602 if (i == 0) {
2603 return;
2604 }
2605 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002606 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002607 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002608 while (i > 0) {
2609 i--;
2610 const flat_binder_object* flat
2611 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002612 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002613 }
2614}
2615
2616void Parcel::freeData()
2617{
2618 freeDataNoInit();
2619 initState();
2620}
2621
2622void Parcel::freeDataNoInit()
2623{
2624 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002625 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002626 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002627 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2628 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002629 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002630 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002631 if (mData) {
2632 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002633 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002634 if (mDataCapacity <= gParcelGlobalAllocSize) {
2635 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2636 } else {
2637 gParcelGlobalAllocSize = 0;
2638 }
2639 if (gParcelGlobalAllocCount > 0) {
2640 gParcelGlobalAllocCount--;
2641 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002642 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002643 free(mData);
2644 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002645 if (mObjects) free(mObjects);
2646 }
2647}
2648
2649status_t Parcel::growData(size_t len)
2650{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002651 if (len > INT32_MAX) {
2652 // don't accept size_t values which may have come from an
2653 // inadvertent conversion from a negative int.
2654 return BAD_VALUE;
2655 }
2656
Martijn Coenen93fe5182020-01-22 10:46:25 +01002657 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2658 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002659 size_t newSize = ((mDataSize+len)*3)/2;
2660 return (newSize <= mDataSize)
2661 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002662 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002663}
2664
2665status_t Parcel::restartWrite(size_t desired)
2666{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002667 if (desired > INT32_MAX) {
2668 // don't accept size_t values which may have come from an
2669 // inadvertent conversion from a negative int.
2670 return BAD_VALUE;
2671 }
2672
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002673 if (mOwner) {
2674 freeData();
2675 return continueWrite(desired);
2676 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002677
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002678 uint8_t* data = (uint8_t*)realloc(mData, desired);
2679 if (!data && desired > mDataCapacity) {
2680 mError = NO_MEMORY;
2681 return NO_MEMORY;
2682 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002683
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002684 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002685
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002686 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002687 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002688 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002689 gParcelGlobalAllocSize += desired;
2690 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002691 if (!mData) {
2692 gParcelGlobalAllocCount++;
2693 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002694 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002695 mData = data;
2696 mDataCapacity = desired;
2697 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002698
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002699 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002700 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2701 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2702
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002703 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002704 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002705 mObjectsSize = mObjectsCapacity = 0;
2706 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002707 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002708 mHasFds = false;
2709 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002710 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002711
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002712 return NO_ERROR;
2713}
2714
2715status_t Parcel::continueWrite(size_t desired)
2716{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002717 if (desired > INT32_MAX) {
2718 // don't accept size_t values which may have come from an
2719 // inadvertent conversion from a negative int.
2720 return BAD_VALUE;
2721 }
2722
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002723 // If shrinking, first adjust for any objects that appear
2724 // after the new data size.
2725 size_t objectsSize = mObjectsSize;
2726 if (desired < mDataSize) {
2727 if (desired == 0) {
2728 objectsSize = 0;
2729 } else {
2730 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002731 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002732 break;
2733 objectsSize--;
2734 }
2735 }
2736 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002737
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002738 if (mOwner) {
2739 // If the size is going to zero, just release the owner's data.
2740 if (desired == 0) {
2741 freeData();
2742 return NO_ERROR;
2743 }
2744
2745 // If there is a different owner, we need to take
2746 // posession.
2747 uint8_t* data = (uint8_t*)malloc(desired);
2748 if (!data) {
2749 mError = NO_MEMORY;
2750 return NO_MEMORY;
2751 }
Yi Kong91635562018-06-07 14:38:36 -07002752 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002753
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002754 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002755 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002756 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002757 free(data);
2758
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002759 mError = NO_MEMORY;
2760 return NO_MEMORY;
2761 }
2762
2763 // Little hack to only acquire references on objects
2764 // we will be keeping.
2765 size_t oldObjectsSize = mObjectsSize;
2766 mObjectsSize = objectsSize;
2767 acquireObjects();
2768 mObjectsSize = oldObjectsSize;
2769 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002770
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002771 if (mData) {
2772 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2773 }
2774 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002775 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002776 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002777 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002778 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002779 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002780
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002781 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002782 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002783 gParcelGlobalAllocSize += desired;
2784 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002785 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002786
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002787 mData = data;
2788 mObjects = objects;
2789 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002790 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002791 mDataCapacity = desired;
2792 mObjectsSize = mObjectsCapacity = objectsSize;
2793 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002794 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002795
2796 } else if (mData) {
2797 if (objectsSize < mObjectsSize) {
2798 // Need to release refs on any objects we are dropping.
2799 const sp<ProcessState> proc(ProcessState::self());
2800 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2801 const flat_binder_object* flat
2802 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002803 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002804 // will need to rescan because we may have lopped off the only FDs
2805 mFdsKnown = false;
2806 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002807 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002808 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002809
2810 if (objectsSize == 0) {
2811 free(mObjects);
2812 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002813 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002814 } else {
2815 binder_size_t* objects =
2816 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2817 if (objects) {
2818 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002819 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002820 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002821 }
2822 mObjectsSize = objectsSize;
2823 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002824 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002825 }
2826
2827 // We own the data, so we can just do a realloc().
2828 if (desired > mDataCapacity) {
2829 uint8_t* data = (uint8_t*)realloc(mData, desired);
2830 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002831 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2832 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002833 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002834 gParcelGlobalAllocSize += desired;
2835 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002836 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002837 mData = data;
2838 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002839 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002840 mError = NO_MEMORY;
2841 return NO_MEMORY;
2842 }
2843 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002844 if (mDataSize > desired) {
2845 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002846 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002847 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002848 if (mDataPos > desired) {
2849 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002850 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002851 }
2852 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002853
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002854 } else {
2855 // This is the first data. Easy!
2856 uint8_t* data = (uint8_t*)malloc(desired);
2857 if (!data) {
2858 mError = NO_MEMORY;
2859 return NO_MEMORY;
2860 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002861
Yi Kong91635562018-06-07 14:38:36 -07002862 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002863 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002864 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002865 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002866
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002867 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002868 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002869 gParcelGlobalAllocSize += desired;
2870 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002871 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002872
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002873 mData = data;
2874 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002875 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2876 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002877 mDataCapacity = desired;
2878 }
2879
2880 return NO_ERROR;
2881}
2882
2883void Parcel::initState()
2884{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002885 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002886 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002887 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002888 mDataSize = 0;
2889 mDataCapacity = 0;
2890 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002891 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2892 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002893 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002894 mObjectsSize = 0;
2895 mObjectsCapacity = 0;
2896 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002897 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002898 mHasFds = false;
2899 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002900 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002901 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002902 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002903 mWorkSourceRequestHeaderPosition = 0;
2904 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002905
2906 // racing multiple init leads only to multiple identical write
2907 if (gMaxFds == 0) {
2908 struct rlimit result;
2909 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2910 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002911 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002912 } else {
2913 ALOGW("Unable to getrlimit: %s", strerror(errno));
2914 gMaxFds = 1024;
2915 }
2916 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002917}
2918
2919void Parcel::scanForFds() const
2920{
2921 bool hasFds = false;
2922 for (size_t i=0; i<mObjectsSize; i++) {
2923 const flat_binder_object* flat
2924 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002925 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002926 hasFds = true;
2927 break;
2928 }
2929 }
2930 mHasFds = hasFds;
2931 mFdsKnown = true;
2932}
2933
Dan Sandleraa5c2342015-04-10 10:08:45 -04002934size_t Parcel::getBlobAshmemSize() const
2935{
Adrian Roos6bb31142015-10-22 16:46:12 -07002936 // This used to return the size of all blobs that were written to ashmem, now we're returning
2937 // the ashmem currently referenced by this Parcel, which should be equivalent.
2938 // TODO: Remove method once ABI can be changed.
2939 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002940}
2941
Adrian Rooscbf37262015-10-22 16:12:53 -07002942size_t Parcel::getOpenAshmemSize() const
2943{
2944 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002945}
2946
2947// --- Parcel::Blob ---
2948
2949Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002950 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002951}
2952
2953Parcel::Blob::~Blob() {
2954 release();
2955}
2956
2957void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002958 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002959 ::munmap(mData, mSize);
2960 }
2961 clear();
2962}
2963
Jeff Brown13b16042014-11-11 16:44:25 -08002964void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2965 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002966 mData = data;
2967 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002968 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002969}
2970
2971void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002972 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002973 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002974 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002975 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002976}
2977
Steven Moreland61ff8492019-09-26 16:05:45 -07002978} // namespace android