blob: b1dddd14ecda65d994ee7e6212cd977eac64779e [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>
Steven Moreland3af936a2021-03-26 03:05:38 +000044#include <cutils/compiler.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080045#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#include <utils/Log.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String16.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000048#include <utils/String8.h>
49#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070050
Mathias Agopian208059f2009-05-18 15:08:03 -070051#include <private/binder/binder_module.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000052#include "RpcState.h"
Steven Morelanda4853cd2019-07-12 15:44:37 -070053#include "Static.h"
Steven Morelandf183fdd2020-10-27 00:12:12 +000054#include "Utils.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070055
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070056#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080057//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080058#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080059//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070060
61// ---------------------------------------------------------------------------
62
Nick Kralevichb6b14232015-04-02 09:36:02 -070063// This macro should never be used at runtime, as a too large value
64// of s could cause an integer overflow. Instead, you should always
65// use the wrapper function pad_size()
66#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
67
68static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070069 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070070 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070071 }
72 return PAD_SIZE_UNSAFE(s);
73}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070074
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070075// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060076#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070077
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070078namespace android {
79
Steven Moreland7b102262019-08-01 15:48:43 -070080// many things compile this into prebuilts on the stack
81static_assert(sizeof(Parcel) == 60 || sizeof(Parcel) == 120);
82
Jeff Sharkey8994c182020-09-11 12:07:10 -060083static std::atomic<size_t> gParcelGlobalAllocCount;
84static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -080085
Christopher Tatee4e0ae82016-03-24 16:03:44 -070086static size_t gMaxFds = 0;
87
Jeff Brown13b16042014-11-11 16:44:25 -080088// Maximum size of a blob to transfer in-place.
89static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
90
91enum {
92 BLOB_INPLACE = 0,
93 BLOB_ASHMEM_IMMUTABLE = 1,
94 BLOB_ASHMEM_MUTABLE = 2,
95};
96
Steven Morelandb1c81202019-04-05 18:49:55 -070097static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070098 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070099{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700100 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700101 case BINDER_TYPE_BINDER:
102 if (obj.binder) {
103 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800104 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700105 }
106 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700107 case BINDER_TYPE_HANDLE: {
108 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700109 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700110 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
111 b->incStrong(who);
112 }
113 return;
114 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700115 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000116 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700117 // If we own an ashmem fd, keep track of how much memory it refers to.
118 int size = ashmem_get_size_region(obj.handle);
119 if (size > 0) {
120 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700121 }
122 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700123 return;
124 }
125 }
126
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700127 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700128}
129
Adrian Roos6bb31142015-10-22 16:46:12 -0700130static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700131 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700132{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700133 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700134 case BINDER_TYPE_BINDER:
135 if (obj.binder) {
136 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800137 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700138 }
139 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700140 case BINDER_TYPE_HANDLE: {
141 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700142 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700143 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
144 b->decStrong(who);
145 }
146 return;
147 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700148 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800149 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000150 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700151 int size = ashmem_get_size_region(obj.handle);
152 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800153 // ashmem size might have changed since last time it was accounted for, e.g.
154 // in acquire_object(). Value of *outAshmemSize is not critical since we are
155 // releasing the object anyway. Check for integer overflow condition.
156 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700157 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700158 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800159
160 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700161 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700162 return;
163 }
164 }
165
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700166 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700167}
168
Steven Moreland34b48cb2020-12-01 22:45:38 +0000169status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700170{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700171 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland89ddfc52020-11-13 02:39:26 +0000172 auto category = internal::Stability::getCategory(binder.get());
173 return writeInt32(category.repr());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700174}
175
Steven Morelanda86a3562019-08-01 23:28:34 +0000176status_t Parcel::finishUnflattenBinder(
177 const sp<IBinder>& binder, sp<IBinder>* out) const
178{
179 int32_t stability;
180 status_t status = readInt32(&stability);
181 if (status != OK) return status;
182
Steven Moreland89ddfc52020-11-13 02:39:26 +0000183 status = internal::Stability::setRepr(binder.get(), stability, true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000184 if (status != OK) return status;
185
186 *out = binder;
187 return OK;
188}
189
Steven Morelandbf1915b2020-07-16 22:43:02 +0000190static constexpr inline int schedPolicyMask(int policy, int priority) {
191 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
192}
193
Steven Morelanda86a3562019-08-01 23:28:34 +0000194status_t Parcel::flattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700195{
Steven Moreland5553ac42020-11-11 02:14:45 +0000196 if (isForRpc()) {
197 if (binder) {
198 status_t status = writeInt32(1); // non-null
199 if (status != OK) return status;
200 RpcAddress address = RpcAddress::zero();
201 status = mConnection->state()->onBinderLeaving(mConnection, binder, &address);
202 if (status != OK) return status;
203 status = address.writeToParcel(this);
204 if (status != OK) return status;
205 } else {
206 status_t status = writeInt32(0); // null
207 if (status != OK) return status;
208 }
209 return finishFlattenBinder(binder);
210 }
211
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700212 flat_binder_object obj;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000213 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700214
Steven Morelandbf1915b2020-07-16 22:43:02 +0000215 int schedBits = 0;
216 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
217 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700218 }
219
Yi Kong91635562018-06-07 14:38:36 -0700220 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800221 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700222 if (!local) {
223 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700224 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000225 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000226 } else {
227 if (proxy->isRpcBinder()) {
228 ALOGE("Sending a socket binder over RPC is prohibited");
229 return INVALID_OPERATION;
230 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000232 const int32_t handle = proxy ? proxy->getPrivateAccessorForId().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700233 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800234 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800236 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700237 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000238 int policy = local->getMinSchedulerPolicy();
239 int priority = local->getMinSchedulerPriority();
240
241 if (policy != 0 || priority != 0) {
242 // override value, since it is set explicitly
243 schedBits = schedPolicyMask(policy, priority);
244 }
Steven Morelandf0212002018-12-26 13:59:23 -0800245 if (local->isRequestingSid()) {
246 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
247 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000248 if (local->isInheritRt()) {
249 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
250 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700251 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800252 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
253 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700254 }
255 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700256 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800257 obj.binder = 0;
258 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700259 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700260
Steven Morelandbf1915b2020-07-16 22:43:02 +0000261 obj.flags |= schedBits;
262
Steven Moreland34b48cb2020-12-01 22:45:38 +0000263 status_t status = writeObject(obj, false);
264 if (status != OK) return status;
265
266 return finishFlattenBinder(binder);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700267}
268
Steven Morelanda86a3562019-08-01 23:28:34 +0000269status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700270{
Steven Moreland5553ac42020-11-11 02:14:45 +0000271 if (isForRpc()) {
272 LOG_ALWAYS_FATAL_IF(mConnection == nullptr,
273 "RpcConnection required to read from remote parcel");
274
275 int32_t isNull;
276 status_t status = readInt32(&isNull);
277 if (status != OK) return status;
278
279 sp<IBinder> binder;
280
281 if (isNull & 1) {
282 auto addr = RpcAddress::zero();
283 status_t status = addr.readFromParcel(*this);
284 if (status != OK) return status;
285 binder = mConnection->state()->onBinderEntering(mConnection, addr);
286 }
287
288 return finishUnflattenBinder(binder, out);
289 }
290
Steven Morelanda86a3562019-08-01 23:28:34 +0000291 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700292
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700293 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700294 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000295 case BINDER_TYPE_BINDER: {
296 sp<IBinder> binder = reinterpret_cast<IBinder*>(flat->cookie);
297 return finishUnflattenBinder(binder, out);
298 }
299 case BINDER_TYPE_HANDLE: {
300 sp<IBinder> binder =
301 ProcessState::self()->getStrongProxyForHandle(flat->handle);
302 return finishUnflattenBinder(binder, out);
303 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700304 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700305 }
306 return BAD_TYPE;
307}
308
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700309// ---------------------------------------------------------------------------
310
311Parcel::Parcel()
312{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800313 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700314 initState();
315}
316
317Parcel::~Parcel()
318{
319 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800320 LOG_ALLOC("Parcel %p: destroyed", this);
321}
322
323size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600324 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800325}
326
327size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600328 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700329}
330
331const uint8_t* Parcel::data() const
332{
333 return mData;
334}
335
336size_t Parcel::dataSize() const
337{
338 return (mDataSize > mDataPos ? mDataSize : mDataPos);
339}
340
341size_t Parcel::dataAvail() const
342{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700343 size_t result = dataSize() - dataPosition();
344 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700345 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700346 }
347 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700348}
349
350size_t Parcel::dataPosition() const
351{
352 return mDataPos;
353}
354
355size_t Parcel::dataCapacity() const
356{
357 return mDataCapacity;
358}
359
360status_t Parcel::setDataSize(size_t size)
361{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700362 if (size > INT32_MAX) {
363 // don't accept size_t values which may have come from an
364 // inadvertent conversion from a negative int.
365 return BAD_VALUE;
366 }
367
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700368 status_t err;
369 err = continueWrite(size);
370 if (err == NO_ERROR) {
371 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700372 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700373 }
374 return err;
375}
376
377void Parcel::setDataPosition(size_t pos) const
378{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700379 if (pos > INT32_MAX) {
380 // don't accept size_t values which may have come from an
381 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700382 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700383 }
384
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700385 mDataPos = pos;
386 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800387 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700388}
389
390status_t Parcel::setDataCapacity(size_t size)
391{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700392 if (size > 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
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700398 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700399 return NO_ERROR;
400}
401
402status_t Parcel::setData(const uint8_t* buffer, size_t len)
403{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700404 if (len > INT32_MAX) {
405 // don't accept size_t values which may have come from an
406 // inadvertent conversion from a negative int.
407 return BAD_VALUE;
408 }
409
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700410 status_t err = restartWrite(len);
411 if (err == NO_ERROR) {
412 memcpy(const_cast<uint8_t*>(data()), buffer, len);
413 mDataSize = len;
414 mFdsKnown = false;
415 }
416 return err;
417}
418
Andreas Huber51faf462011-04-13 10:21:56 -0700419status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700420{
Steven Moreland67753c32021-04-02 18:45:19 +0000421 if (parcel->isForRpc() != isForRpc()) {
422 ALOGE("Cannot append Parcel of one format to another.");
423 return BAD_TYPE;
424 }
425
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700426 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700427 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800428 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700429 size_t size = parcel->mObjectsSize;
430 int startPos = mDataPos;
431 int firstIndex = -1, lastIndex = -2;
432
433 if (len == 0) {
434 return NO_ERROR;
435 }
436
Nick Kralevichb6b14232015-04-02 09:36:02 -0700437 if (len > INT32_MAX) {
438 // don't accept size_t values which may have come from an
439 // inadvertent conversion from a negative int.
440 return BAD_VALUE;
441 }
442
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700443 // range checks against the source parcel size
444 if ((offset > parcel->mDataSize)
445 || (len > parcel->mDataSize)
446 || (offset + len > parcel->mDataSize)) {
447 return BAD_VALUE;
448 }
449
450 // Count objects in range
451 for (int i = 0; i < (int) size; i++) {
452 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700453 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700454 if (firstIndex == -1) {
455 firstIndex = i;
456 }
457 lastIndex = i;
458 }
459 }
460 int numObjects = lastIndex - firstIndex + 1;
461
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700462 if ((mDataSize+len) > mDataCapacity) {
463 // grow data
464 err = growData(len);
465 if (err != NO_ERROR) {
466 return err;
467 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700468 }
469
470 // append data
471 memcpy(mData + mDataPos, data + offset, len);
472 mDataPos += len;
473 mDataSize += len;
474
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400475 err = NO_ERROR;
476
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700477 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200478 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700479 // grow objects
480 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +0100481 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
482 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700483 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +0100484 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800485 binder_size_t *objects =
486 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700487 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700488 return NO_MEMORY;
489 }
490 mObjects = objects;
491 mObjectsCapacity = newSize;
492 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700493
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700494 // append and acquire objects
495 int idx = mObjectsSize;
496 for (int i = firstIndex; i <= lastIndex; i++) {
497 size_t off = objects[i] - offset + startPos;
498 mObjects[idx++] = off;
499 mObjectsSize++;
500
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700501 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700502 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700503 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700504
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700505 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700506 // If this is a file descriptor, we need to dup it so the
507 // new Parcel now owns its own fd, and can declare that we
508 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800509 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800510 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700511 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400512 if (!mAllowFds) {
513 err = FDS_NOT_ALLOWED;
514 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700515 }
516 }
517 }
518
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400519 return err;
520}
521
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700522int Parcel::compareData(const Parcel& other) {
523 size_t size = dataSize();
524 if (size != other.dataSize()) {
525 return size < other.dataSize() ? -1 : 1;
526 }
527 return memcmp(data(), other.data(), size);
528}
529
Jeff Brown13b16042014-11-11 16:44:25 -0800530bool Parcel::allowFds() const
531{
532 return mAllowFds;
533}
534
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700535bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400536{
537 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700538 if (!allowFds) {
539 mAllowFds = false;
540 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400541 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700542}
543
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700544void Parcel::restoreAllowFds(bool lastValue)
545{
546 mAllowFds = lastValue;
547}
548
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700549bool Parcel::hasFileDescriptors() const
550{
551 if (!mFdsKnown) {
552 scanForFds();
553 }
554 return mHasFds;
555}
556
Steven Morelandf183fdd2020-10-27 00:12:12 +0000557void Parcel::markSensitive() const
558{
559 mDeallocZero = true;
560}
561
Steven Moreland5553ac42020-11-11 02:14:45 +0000562void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000563 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
564
Steven Moreland5553ac42020-11-11 02:14:45 +0000565 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
566 markForRpc(binder->remoteBinder()->getPrivateAccessorForId().rpcConnection());
567 }
568}
569
570void Parcel::markForRpc(const sp<RpcConnection>& connection) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000571 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
572 "format must be set before data is written OR on IPC data");
573
Steven Moreland5553ac42020-11-11 02:14:45 +0000574 LOG_ALWAYS_FATAL_IF(connection == nullptr, "markForRpc requires connection");
575 mConnection = connection;
576}
577
578bool Parcel::isForRpc() const {
579 return mConnection != nullptr;
580}
581
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000582void Parcel::updateWorkSourceRequestHeaderPosition() const {
583 // Only update the request headers once. We only want to point
584 // to the first headers read/written.
585 if (!mRequestHeaderPresent) {
586 mWorkSourceRequestHeaderPosition = dataPosition();
587 mRequestHeaderPresent = true;
588 }
589}
590
Jooyung Han1b228f42020-01-30 13:41:12 +0900591#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700592constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
593#else
594constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
595#endif
596
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700597// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700598status_t Parcel::writeInterfaceToken(const String16& interface)
599{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000600 return writeInterfaceToken(interface.string(), interface.size());
601}
602
603status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000604 if (CC_LIKELY(!isForRpc())) {
605 const IPCThreadState* threadState = IPCThreadState::self();
606 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
607 updateWorkSourceRequestHeaderPosition();
608 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
609 : IPCThreadState::kUnsetWorkSource);
610 writeInt32(kHeader);
611 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000612
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700613 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000614 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700615}
616
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000617bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
618{
619 if (!mRequestHeaderPresent) {
620 return false;
621 }
622
623 const size_t initialPosition = dataPosition();
624 setDataPosition(mWorkSourceRequestHeaderPosition);
625 status_t err = writeInt32(uid);
626 setDataPosition(initialPosition);
627 return err == NO_ERROR;
628}
629
Steven Morelandf1b1e492019-05-06 15:05:13 -0700630uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000631{
632 if (!mRequestHeaderPresent) {
633 return IPCThreadState::kUnsetWorkSource;
634 }
635
636 const size_t initialPosition = dataPosition();
637 setDataPosition(mWorkSourceRequestHeaderPosition);
638 uid_t uid = readInt32();
639 setDataPosition(initialPosition);
640 return uid;
641}
642
Mathias Agopian83c04462009-05-22 19:00:22 -0700643bool Parcel::checkInterface(IBinder* binder) const
644{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700645 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700646}
647
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700648bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700649 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700650{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700651 return enforceInterface(interface.string(), interface.size(), threadState);
652}
653
654bool Parcel::enforceInterface(const char16_t* interface,
655 size_t len,
656 IPCThreadState* threadState) const
657{
Steven Moreland3af936a2021-03-26 03:05:38 +0000658 if (CC_LIKELY(!isForRpc())) {
659 // StrictModePolicy.
660 int32_t strictPolicy = readInt32();
661 if (threadState == nullptr) {
662 threadState = IPCThreadState::self();
663 }
664 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
665 // For one-way calls, the callee is running entirely
666 // disconnected from the caller, so disable StrictMode entirely.
667 // Not only does disk/network usage not impact the caller, but
668 // there's no way to communicate back violations anyway.
669 threadState->setStrictModePolicy(0);
670 } else {
671 threadState->setStrictModePolicy(strictPolicy);
672 }
673 // WorkSource.
674 updateWorkSourceRequestHeaderPosition();
675 int32_t workSource = readInt32();
676 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
677 // vendor header
678 int32_t header = readInt32();
679 if (header != kHeader) {
680 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
681 header);
682 return false;
683 }
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700684 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000685
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100686 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700687 size_t parcel_interface_len;
688 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
689 if (len == parcel_interface_len &&
690 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700691 return true;
692 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700693 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700694 String8(interface, len).string(),
695 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700696 return false;
697 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700698}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700699
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700700size_t Parcel::objectsCount() const
701{
702 return mObjectsSize;
703}
704
705status_t Parcel::errorCheck() const
706{
707 return mError;
708}
709
710void Parcel::setError(status_t err)
711{
712 mError = err;
713}
714
715status_t Parcel::finishWrite(size_t len)
716{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700717 if (len > INT32_MAX) {
718 // don't accept size_t values which may have come from an
719 // inadvertent conversion from a negative int.
720 return BAD_VALUE;
721 }
722
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700723 //printf("Finish write of %d\n", len);
724 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700725 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700726 if (mDataPos > mDataSize) {
727 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700728 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700729 }
730 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
731 return NO_ERROR;
732}
733
734status_t Parcel::writeUnpadded(const void* data, size_t len)
735{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700736 if (len > INT32_MAX) {
737 // don't accept size_t values which may have come from an
738 // inadvertent conversion from a negative int.
739 return BAD_VALUE;
740 }
741
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700742 size_t end = mDataPos + len;
743 if (end < mDataPos) {
744 // integer overflow
745 return BAD_VALUE;
746 }
747
748 if (end <= mDataCapacity) {
749restart_write:
750 memcpy(mData+mDataPos, data, len);
751 return finishWrite(len);
752 }
753
754 status_t err = growData(len);
755 if (err == NO_ERROR) goto restart_write;
756 return err;
757}
758
759status_t Parcel::write(const void* data, size_t len)
760{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700761 if (len > INT32_MAX) {
762 // don't accept size_t values which may have come from an
763 // inadvertent conversion from a negative int.
764 return BAD_VALUE;
765 }
766
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700767 void* const d = writeInplace(len);
768 if (d) {
769 memcpy(d, data, len);
770 return NO_ERROR;
771 }
772 return mError;
773}
774
775void* Parcel::writeInplace(size_t len)
776{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700777 if (len > INT32_MAX) {
778 // don't accept size_t values which may have come from an
779 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700780 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700781 }
782
783 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700784
785 // sanity check for integer overflow
786 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700787 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700788 }
789
790 if ((mDataPos+padded) <= mDataCapacity) {
791restart_write:
792 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
793 uint8_t* const data = mData+mDataPos;
794
795 // Need to pad at end?
796 if (padded != len) {
797#if BYTE_ORDER == BIG_ENDIAN
798 static const uint32_t mask[4] = {
799 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
800 };
801#endif
802#if BYTE_ORDER == LITTLE_ENDIAN
803 static const uint32_t mask[4] = {
804 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
805 };
806#endif
807 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
808 // *reinterpret_cast<void**>(data+padded-4));
809 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
810 }
811
812 finishWrite(padded);
813 return data;
814 }
815
816 status_t err = growData(padded);
817 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700818 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700819}
820
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800821status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
822 const uint8_t* strData = (uint8_t*)str.data();
823 const size_t strLen= str.length();
824 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100825 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800826 return BAD_VALUE;
827 }
828
829 status_t err = writeInt32(utf16Len);
830 if (err) {
831 return err;
832 }
833
834 // Allocate enough bytes to hold our converted string and its terminating NULL.
835 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
836 if (!dst) {
837 return NO_MEMORY;
838 }
839
Sergio Girof4607432016-07-21 14:46:35 +0100840 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800841
842 return NO_ERROR;
843}
844
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900845
Andy Hung49198cf2020-11-18 11:02:39 -0800846status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
847status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800848
Andy Hung49198cf2020-11-18 11:02:39 -0800849status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
850status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700851
Andy Hung49198cf2020-11-18 11:02:39 -0800852status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
853status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
854status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
855status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
856status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
857status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
858status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
859status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
860status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
861status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
862status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
863status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
864status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
865status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
866status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
867status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
868status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
869status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
870status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
871status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
872status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
873status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
874status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
875status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
876status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
877status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
878status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700879
Andy Hung49198cf2020-11-18 11:02:39 -0800880status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800881status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800882 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900883status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800884 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800885status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800886 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900887status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800888 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
889status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800890
Andy Hung49198cf2020-11-18 11:02:39 -0800891status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
892status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
893status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
894
895status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
896status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
897status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
898
899status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
900
901status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
902status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
903
904status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
905status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
906
907status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
908status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
909status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
910status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
911status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
912status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
913status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
914status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
915status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
916status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
917status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
918status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
919status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
920status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
921status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
922status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
923status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
924status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
925status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
926status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
927status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
928status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
929status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
930status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
931status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
932status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
933status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
934
935status_t Parcel::readString16Vector(
936 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
937status_t Parcel::readString16Vector(
938 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
939status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
940status_t Parcel::readUtf8VectorFromUtf16Vector(
941 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
942status_t Parcel::readUtf8VectorFromUtf16Vector(
943 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
944status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
945
946status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
947status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
948status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
949
950status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
951status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
952status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
953
954status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800955
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700956status_t Parcel::writeInt32(int32_t val)
957{
Andreas Huber84a6d042009-08-17 13:33:27 -0700958 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700959}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800960
961status_t Parcel::writeUint32(uint32_t val)
962{
963 return writeAligned(val);
964}
965
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700966status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700967 if (len > INT32_MAX) {
968 // don't accept size_t values which may have come from an
969 // inadvertent conversion from a negative int.
970 return BAD_VALUE;
971 }
972
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700973 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700974 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700975 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700976 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700977 if (ret == NO_ERROR) {
978 ret = write(val, len * sizeof(*val));
979 }
980 return ret;
981}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700982status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700983 if (len > INT32_MAX) {
984 // don't accept size_t values which may have come from an
985 // inadvertent conversion from a negative int.
986 return BAD_VALUE;
987 }
988
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700989 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700990 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700991 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700992 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700993 if (ret == NO_ERROR) {
994 ret = write(val, len * sizeof(*val));
995 }
996 return ret;
997}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700998
Casey Dahlind6848f52015-10-15 15:44:59 -0700999status_t Parcel::writeBool(bool val)
1000{
1001 return writeInt32(int32_t(val));
1002}
1003
1004status_t Parcel::writeChar(char16_t val)
1005{
1006 return writeInt32(int32_t(val));
1007}
1008
1009status_t Parcel::writeByte(int8_t val)
1010{
1011 return writeInt32(int32_t(val));
1012}
1013
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001014status_t Parcel::writeInt64(int64_t val)
1015{
Andreas Huber84a6d042009-08-17 13:33:27 -07001016 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001017}
1018
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001019status_t Parcel::writeUint64(uint64_t val)
1020{
1021 return writeAligned(val);
1022}
1023
Serban Constantinescuf683e012013-11-05 16:53:55 +00001024status_t Parcel::writePointer(uintptr_t val)
1025{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001026 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001027}
1028
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001029status_t Parcel::writeFloat(float val)
1030{
Andreas Huber84a6d042009-08-17 13:33:27 -07001031 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001032}
1033
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001034#if defined(__mips__) && defined(__mips_hard_float)
1035
1036status_t Parcel::writeDouble(double val)
1037{
1038 union {
1039 double d;
1040 unsigned long long ll;
1041 } u;
1042 u.d = val;
1043 return writeAligned(u.ll);
1044}
1045
1046#else
1047
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001048status_t Parcel::writeDouble(double val)
1049{
Andreas Huber84a6d042009-08-17 13:33:27 -07001050 return writeAligned(val);
1051}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001052
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001053#endif
1054
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001055status_t Parcel::writeCString(const char* str)
1056{
1057 return write(str, strlen(str)+1);
1058}
1059
1060status_t Parcel::writeString8(const String8& str)
1061{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001062 return writeString8(str.string(), str.size());
1063}
1064
1065status_t Parcel::writeString8(const char* str, size_t len)
1066{
1067 if (str == nullptr) return writeInt32(-1);
1068
Jeff Sharkey18220902020-11-05 08:36:20 -07001069 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001070 status_t err = writeInt32(len);
1071 if (err == NO_ERROR) {
1072 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1073 if (data) {
1074 memcpy(data, str, len);
1075 *reinterpret_cast<char*>(data+len) = 0;
1076 return NO_ERROR;
1077 }
1078 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001079 }
1080 return err;
1081}
1082
1083status_t Parcel::writeString16(const String16& str)
1084{
1085 return writeString16(str.string(), str.size());
1086}
1087
1088status_t Parcel::writeString16(const char16_t* str, size_t len)
1089{
Yi Kong91635562018-06-07 14:38:36 -07001090 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001091
Jeff Sharkey18220902020-11-05 08:36:20 -07001092 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001093 status_t err = writeInt32(len);
1094 if (err == NO_ERROR) {
1095 len *= sizeof(char16_t);
1096 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1097 if (data) {
1098 memcpy(data, str, len);
1099 *reinterpret_cast<char16_t*>(data+len) = 0;
1100 return NO_ERROR;
1101 }
1102 err = mError;
1103 }
1104 return err;
1105}
1106
1107status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1108{
Steven Morelanda86a3562019-08-01 23:28:34 +00001109 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001110}
1111
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001112
Casey Dahlinb9872622015-11-25 15:09:45 -08001113status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1114 if (!parcelable) {
1115 return writeInt32(0);
1116 }
1117
1118 return writeParcelable(*parcelable);
1119}
1120
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001121status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001122{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001123 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001124 return BAD_TYPE;
1125
1126 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001127 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001128 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001130 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001131 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001133 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1134 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135
1136 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001137 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138 return err;
1139 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001140 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001141 return err;
1142}
1143
Jeff Brown93ff1f92011-11-04 19:01:44 -07001144status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001145{
Steven Moreland5553ac42020-11-11 02:14:45 +00001146 if (isForRpc()) {
1147 ALOGE("Cannot write file descriptor to remote binder.");
1148 return BAD_TYPE;
1149 }
1150
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001151 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001152 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001153 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001154 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001155 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001156 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001157 return writeObject(obj, true);
1158}
1159
1160status_t Parcel::writeDupFileDescriptor(int fd)
1161{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001162 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001163 if (dupFd < 0) {
1164 return -errno;
1165 }
1166 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001167 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001168 close(dupFd);
1169 }
1170 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001171}
1172
Dianne Hackborn1941a402016-08-29 12:30:43 -07001173status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1174{
1175 writeInt32(0);
1176 return writeFileDescriptor(fd, takeOwnership);
1177}
1178
Ryo Hashimotobf551892018-05-31 16:58:35 +09001179status_t Parcel::writeDupParcelFileDescriptor(int fd)
1180{
1181 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1182 if (dupFd < 0) {
1183 return -errno;
1184 }
1185 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1186 if (err != OK) {
1187 close(dupFd);
1188 }
1189 return err;
1190}
1191
Christopher Wiley2cf19952016-04-11 11:09:37 -07001192status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001193 return writeDupFileDescriptor(fd.get());
1194}
1195
Jeff Brown13b16042014-11-11 16:44:25 -08001196status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001197{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001198 if (len > INT32_MAX) {
1199 // don't accept size_t values which may have come from an
1200 // inadvertent conversion from a negative int.
1201 return BAD_VALUE;
1202 }
1203
Jeff Brown13b16042014-11-11 16:44:25 -08001204 status_t status;
1205 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001206 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001207 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001208 if (status) return status;
1209
1210 void* ptr = writeInplace(len);
1211 if (!ptr) return NO_MEMORY;
1212
Jeff Brown13b16042014-11-11 16:44:25 -08001213 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001214 return NO_ERROR;
1215 }
1216
Steve Block6807e592011-10-20 11:56:00 +01001217 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001218 int fd = ashmem_create_region("Parcel Blob", len);
1219 if (fd < 0) return NO_MEMORY;
1220
1221 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1222 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001223 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001224 } else {
Yi Kong91635562018-06-07 14:38:36 -07001225 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001226 if (ptr == MAP_FAILED) {
1227 status = -errno;
1228 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001229 if (!mutableCopy) {
1230 result = ashmem_set_prot_region(fd, PROT_READ);
1231 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001232 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001233 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001234 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001235 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001236 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001237 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001238 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001239 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001240 return NO_ERROR;
1241 }
1242 }
1243 }
1244 }
1245 ::munmap(ptr, len);
1246 }
1247 ::close(fd);
1248 return status;
1249}
1250
Jeff Brown13b16042014-11-11 16:44:25 -08001251status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1252{
1253 // Must match up with what's done in writeBlob.
1254 if (!mAllowFds) return FDS_NOT_ALLOWED;
1255 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1256 if (status) return status;
1257 return writeDupFileDescriptor(fd);
1258}
1259
Mathias Agopiane1424282013-07-29 21:24:40 -07001260status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001261{
1262 status_t err;
1263
1264 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001265 const size_t len = val.getFlattenedSize();
1266 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001267
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001268 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001269 // don't accept size_t values which may have come from an
1270 // inadvertent conversion from a negative int.
1271 return BAD_VALUE;
1272 }
1273
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001274 err = this->writeInt32(len);
1275 if (err) return err;
1276
1277 err = this->writeInt32(fd_count);
1278 if (err) return err;
1279
1280 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001281 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001282 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001283 return BAD_VALUE;
1284
Yi Kong91635562018-06-07 14:38:36 -07001285 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001286 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001287 fds = new (std::nothrow) int[fd_count];
1288 if (fds == nullptr) {
1289 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1290 return BAD_VALUE;
1291 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001292 }
1293
1294 err = val.flatten(buf, len, fds, fd_count);
1295 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1296 err = this->writeDupFileDescriptor( fds[i] );
1297 }
1298
1299 if (fd_count) {
1300 delete [] fds;
1301 }
1302
1303 return err;
1304}
1305
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001306status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1307{
1308 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1309 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1310 if (enoughData && enoughObjects) {
1311restart_write:
1312 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001313
Christopher Tate98e67d32015-06-03 18:44:15 -07001314 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001315 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001316 if (!mAllowFds) {
1317 // fail before modifying our object index
1318 return FDS_NOT_ALLOWED;
1319 }
1320 mHasFds = mFdsKnown = true;
1321 }
1322
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001323 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001324 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001325 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001326 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001327 mObjectsSize++;
1328 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001329
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001330 return finishWrite(sizeof(flat_binder_object));
1331 }
1332
1333 if (!enoughData) {
1334 const status_t err = growData(sizeof(val));
1335 if (err != NO_ERROR) return err;
1336 }
1337 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001338 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1339 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001340 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001341 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001342 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001343 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001344 mObjects = objects;
1345 mObjectsCapacity = newSize;
1346 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001347
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001348 goto restart_write;
1349}
1350
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001351status_t Parcel::writeNoException()
1352{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001353 binder::Status status;
1354 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001355}
1356
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001357status_t Parcel::validateReadData(size_t upperBound) const
1358{
1359 // Don't allow non-object reads on object data
1360 if (mObjectsSorted || mObjectsSize <= 1) {
1361data_sorted:
1362 // Expect to check only against the next object
1363 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1364 // For some reason the current read position is greater than the next object
1365 // hint. Iterate until we find the right object
1366 size_t nextObject = mNextObjectHint;
1367 do {
1368 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1369 // Requested info overlaps with an object
1370 ALOGE("Attempt to read from protected data in Parcel %p", this);
1371 return PERMISSION_DENIED;
1372 }
1373 nextObject++;
1374 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1375 mNextObjectHint = nextObject;
1376 }
1377 return NO_ERROR;
1378 }
1379 // Quickly determine if mObjects is sorted.
1380 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1381 binder_size_t* prevObj = currObj;
1382 while (currObj > mObjects) {
1383 prevObj--;
1384 if(*prevObj > *currObj) {
1385 goto data_unsorted;
1386 }
1387 currObj--;
1388 }
1389 mObjectsSorted = true;
1390 goto data_sorted;
1391
1392data_unsorted:
1393 // Insertion Sort mObjects
1394 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1395 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1396 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1397 binder_size_t temp = *iter0;
1398 binder_size_t* iter1 = iter0 - 1;
1399 while (iter1 >= mObjects && *iter1 > temp) {
1400 *(iter1 + 1) = *iter1;
1401 iter1--;
1402 }
1403 *(iter1 + 1) = temp;
1404 }
1405 mNextObjectHint = 0;
1406 mObjectsSorted = true;
1407 goto data_sorted;
1408}
1409
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001410status_t Parcel::read(void* outData, size_t len) const
1411{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001412 if (len > INT32_MAX) {
1413 // don't accept size_t values which may have come from an
1414 // inadvertent conversion from a negative int.
1415 return BAD_VALUE;
1416 }
1417
1418 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1419 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001420 if (mObjectsSize > 0) {
1421 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001422 if(err != NO_ERROR) {
1423 // Still increment the data position by the expected length
1424 mDataPos += pad_size(len);
1425 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1426 return err;
1427 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001428 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001429 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001430 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001431 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001432 return NO_ERROR;
1433 }
1434 return NOT_ENOUGH_DATA;
1435}
1436
1437const void* Parcel::readInplace(size_t len) const
1438{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001439 if (len > INT32_MAX) {
1440 // don't accept size_t values which may have come from an
1441 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001442 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001443 }
1444
1445 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1446 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001447 if (mObjectsSize > 0) {
1448 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001449 if(err != NO_ERROR) {
1450 // Still increment the data position by the expected length
1451 mDataPos += pad_size(len);
1452 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001453 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001454 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001455 }
1456
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001457 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001458 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001459 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001460 return data;
1461 }
Yi Kong91635562018-06-07 14:38:36 -07001462 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001463}
1464
Andreas Huber84a6d042009-08-17 13:33:27 -07001465template<class T>
1466status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001467 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001468
1469 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001470 if (mObjectsSize > 0) {
1471 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001472 if(err != NO_ERROR) {
1473 // Still increment the data position by the expected length
1474 mDataPos += sizeof(T);
1475 return err;
1476 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001477 }
1478
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001479 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001480 mDataPos += sizeof(T);
1481 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001482 return NO_ERROR;
1483 } else {
1484 return NOT_ENOUGH_DATA;
1485 }
1486}
1487
Andreas Huber84a6d042009-08-17 13:33:27 -07001488template<class T>
1489T Parcel::readAligned() const {
1490 T result;
1491 if (readAligned(&result) != NO_ERROR) {
1492 result = 0;
1493 }
1494
1495 return result;
1496}
1497
1498template<class T>
1499status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001500 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001501
1502 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1503restart_write:
1504 *reinterpret_cast<T*>(mData+mDataPos) = val;
1505 return finishWrite(sizeof(val));
1506 }
1507
1508 status_t err = growData(sizeof(val));
1509 if (err == NO_ERROR) goto restart_write;
1510 return err;
1511}
1512
1513status_t Parcel::readInt32(int32_t *pArg) const
1514{
1515 return readAligned(pArg);
1516}
1517
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001518int32_t Parcel::readInt32() const
1519{
Andreas Huber84a6d042009-08-17 13:33:27 -07001520 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001521}
1522
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001523status_t Parcel::readUint32(uint32_t *pArg) const
1524{
1525 return readAligned(pArg);
1526}
1527
1528uint32_t Parcel::readUint32() const
1529{
1530 return readAligned<uint32_t>();
1531}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001532
1533status_t Parcel::readInt64(int64_t *pArg) const
1534{
Andreas Huber84a6d042009-08-17 13:33:27 -07001535 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001536}
1537
1538
1539int64_t Parcel::readInt64() const
1540{
Andreas Huber84a6d042009-08-17 13:33:27 -07001541 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001542}
1543
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001544status_t Parcel::readUint64(uint64_t *pArg) const
1545{
1546 return readAligned(pArg);
1547}
1548
1549uint64_t Parcel::readUint64() const
1550{
1551 return readAligned<uint64_t>();
1552}
1553
Serban Constantinescuf683e012013-11-05 16:53:55 +00001554status_t Parcel::readPointer(uintptr_t *pArg) const
1555{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001556 status_t ret;
1557 binder_uintptr_t ptr;
1558 ret = readAligned(&ptr);
1559 if (!ret)
1560 *pArg = ptr;
1561 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001562}
1563
1564uintptr_t Parcel::readPointer() const
1565{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001566 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001567}
1568
1569
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001570status_t Parcel::readFloat(float *pArg) const
1571{
Andreas Huber84a6d042009-08-17 13:33:27 -07001572 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001573}
1574
1575
1576float Parcel::readFloat() const
1577{
Andreas Huber84a6d042009-08-17 13:33:27 -07001578 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001579}
1580
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001581#if defined(__mips__) && defined(__mips_hard_float)
1582
1583status_t Parcel::readDouble(double *pArg) const
1584{
1585 union {
1586 double d;
1587 unsigned long long ll;
1588 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001589 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001590 status_t status;
1591 status = readAligned(&u.ll);
1592 *pArg = u.d;
1593 return status;
1594}
1595
1596double Parcel::readDouble() const
1597{
1598 union {
1599 double d;
1600 unsigned long long ll;
1601 } u;
1602 u.ll = readAligned<unsigned long long>();
1603 return u.d;
1604}
1605
1606#else
1607
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001608status_t Parcel::readDouble(double *pArg) const
1609{
Andreas Huber84a6d042009-08-17 13:33:27 -07001610 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001611}
1612
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001613double Parcel::readDouble() const
1614{
Andreas Huber84a6d042009-08-17 13:33:27 -07001615 return readAligned<double>();
1616}
1617
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001618#endif
1619
Casey Dahlind6848f52015-10-15 15:44:59 -07001620status_t Parcel::readBool(bool *pArg) const
1621{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001622 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001623 status_t ret = readInt32(&tmp);
1624 *pArg = (tmp != 0);
1625 return ret;
1626}
1627
1628bool Parcel::readBool() const
1629{
1630 return readInt32() != 0;
1631}
1632
1633status_t Parcel::readChar(char16_t *pArg) const
1634{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001635 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001636 status_t ret = readInt32(&tmp);
1637 *pArg = char16_t(tmp);
1638 return ret;
1639}
1640
1641char16_t Parcel::readChar() const
1642{
1643 return char16_t(readInt32());
1644}
1645
1646status_t Parcel::readByte(int8_t *pArg) const
1647{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001648 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001649 status_t ret = readInt32(&tmp);
1650 *pArg = int8_t(tmp);
1651 return ret;
1652}
1653
1654int8_t Parcel::readByte() const
1655{
1656 return int8_t(readInt32());
1657}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001658
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001659status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1660 size_t utf16Size = 0;
1661 const char16_t* src = readString16Inplace(&utf16Size);
1662 if (!src) {
1663 return UNEXPECTED_NULL;
1664 }
1665
1666 // Save ourselves the trouble, we're done.
1667 if (utf16Size == 0u) {
1668 str->clear();
1669 return NO_ERROR;
1670 }
1671
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001672 // Allow for closing '\0'
1673 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1674 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001675 return BAD_VALUE;
1676 }
1677 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001678 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001679 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001680 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1681 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001682 return NO_ERROR;
1683}
1684
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001685const char* Parcel::readCString() const
1686{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001687 if (mDataPos < mDataSize) {
1688 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001689 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1690 // is the string's trailing NUL within the parcel's valid bounds?
1691 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1692 if (eos) {
1693 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001694 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001695 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001696 return str;
1697 }
1698 }
Yi Kong91635562018-06-07 14:38:36 -07001699 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001700}
1701
1702String8 Parcel::readString8() const
1703{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001704 size_t len;
1705 const char* str = readString8Inplace(&len);
1706 if (str) return String8(str, len);
1707 ALOGE("Reading a NULL string not supported here.");
1708 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07001709}
1710
1711status_t Parcel::readString8(String8* pArg) const
1712{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001713 size_t len;
1714 const char* str = readString8Inplace(&len);
1715 if (str) {
1716 pArg->setTo(str, len);
1717 return 0;
1718 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07001719 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001720 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07001721 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001722}
1723
1724const char* Parcel::readString8Inplace(size_t* outLen) const
1725{
1726 int32_t size = readInt32();
1727 // watch for potential int overflow from size+1
1728 if (size >= 0 && size < INT32_MAX) {
1729 *outLen = size;
1730 const char* str = (const char*)readInplace(size+1);
1731 if (str != nullptr) {
Steven Moreland61d0f842020-12-04 21:13:03 +00001732 if (str[size] == '\0') {
1733 return str;
1734 }
1735 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001736 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001737 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001738 *outLen = 0;
1739 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001740}
1741
1742String16 Parcel::readString16() const
1743{
1744 size_t len;
1745 const char16_t* str = readString16Inplace(&len);
1746 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001747 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001748 return String16();
1749}
1750
Casey Dahlinb9872622015-11-25 15:09:45 -08001751
Casey Dahlin451ff582015-10-19 18:12:18 -07001752status_t Parcel::readString16(String16* pArg) const
1753{
1754 size_t len;
1755 const char16_t* str = readString16Inplace(&len);
1756 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001757 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001758 return 0;
1759 } else {
1760 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001761 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001762 }
1763}
1764
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001765const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1766{
1767 int32_t size = readInt32();
1768 // watch for potential int overflow from size+1
1769 if (size >= 0 && size < INT32_MAX) {
1770 *outLen = size;
1771 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001772 if (str != nullptr) {
Steven Moreland61d0f842020-12-04 21:13:03 +00001773 if (str[size] == u'\0') {
1774 return str;
1775 }
1776 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001777 }
1778 }
1779 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001780 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001781}
1782
Casey Dahlinf0c13772015-10-27 18:33:56 -07001783status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1784{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001785 status_t status = readNullableStrongBinder(val);
1786 if (status == OK && !val->get()) {
1787 status = UNEXPECTED_NULL;
1788 }
1789 return status;
1790}
1791
1792status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1793{
Steven Morelanda86a3562019-08-01 23:28:34 +00001794 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001795}
1796
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001797sp<IBinder> Parcel::readStrongBinder() const
1798{
1799 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001800 // Note that a lot of code in Android reads binders by hand with this
1801 // method, and that code has historically been ok with getting nullptr
1802 // back (while ignoring error codes).
1803 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001804 return val;
1805}
1806
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001807int32_t Parcel::readExceptionCode() const
1808{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001809 binder::Status status;
1810 status.readFromParcel(*this);
1811 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001812}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001813
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001814native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001815{
1816 int numFds, numInts;
1817 status_t err;
1818 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001819 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001820 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001821 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001822
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001823 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001824 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001825 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001826 }
1827
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001828 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001829 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001830 if (h->data[i] < 0) {
1831 for (int j = 0; j < i; j++) {
1832 close(h->data[j]);
1833 }
1834 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001835 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001836 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001837 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001838 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001839 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001840 native_handle_close(h);
1841 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001842 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001843 }
1844 return h;
1845}
1846
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001847int Parcel::readFileDescriptor() const
1848{
1849 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001850
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001851 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001852 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001853 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001854
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001855 return BAD_TYPE;
1856}
1857
Dianne Hackborn1941a402016-08-29 12:30:43 -07001858int Parcel::readParcelFileDescriptor() const
1859{
1860 int32_t hasComm = readInt32();
1861 int fd = readFileDescriptor();
1862 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001863 // detach (owned by the binder driver)
1864 int comm = readFileDescriptor();
1865
1866 // warning: this must be kept in sync with:
1867 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1868 enum ParcelFileDescriptorStatus {
1869 DETACHED = 2,
1870 };
1871
1872#if BYTE_ORDER == BIG_ENDIAN
1873 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
1874#endif
1875#if BYTE_ORDER == LITTLE_ENDIAN
1876 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
1877#endif
1878
1879 ssize_t written = TEMP_FAILURE_RETRY(
1880 ::write(comm, &message, sizeof(message)));
1881
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08001882 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001883 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
1884 written, strerror(errno));
1885 return BAD_TYPE;
1886 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07001887 }
1888 return fd;
1889}
1890
Christopher Wiley2cf19952016-04-11 11:09:37 -07001891status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08001892{
1893 int got = readFileDescriptor();
1894
1895 if (got == BAD_TYPE) {
1896 return BAD_TYPE;
1897 }
1898
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001899 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08001900
1901 if (val->get() < 0) {
1902 return BAD_VALUE;
1903 }
1904
1905 return OK;
1906}
1907
Ryo Hashimotobf551892018-05-31 16:58:35 +09001908status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
1909{
1910 int got = readParcelFileDescriptor();
1911
1912 if (got == BAD_TYPE) {
1913 return BAD_TYPE;
1914 }
1915
1916 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
1917
1918 if (val->get() < 0) {
1919 return BAD_VALUE;
1920 }
1921
1922 return OK;
1923}
Casey Dahlin06673e32015-11-23 13:24:23 -08001924
Jeff Brown5707dbf2011-09-23 21:17:56 -07001925status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1926{
Jeff Brown13b16042014-11-11 16:44:25 -08001927 int32_t blobType;
1928 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001929 if (status) return status;
1930
Jeff Brown13b16042014-11-11 16:44:25 -08001931 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001932 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001933 const void* ptr = readInplace(len);
1934 if (!ptr) return BAD_VALUE;
1935
Jeff Brown13b16042014-11-11 16:44:25 -08001936 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001937 return NO_ERROR;
1938 }
1939
Steve Block6807e592011-10-20 11:56:00 +01001940 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001941 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001942 int fd = readFileDescriptor();
1943 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1944
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001945 if (!ashmem_valid(fd)) {
1946 ALOGE("invalid fd");
1947 return BAD_VALUE;
1948 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001949 int size = ashmem_get_size_region(fd);
1950 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001951 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001952 return BAD_VALUE;
1953 }
Yi Kong91635562018-06-07 14:38:36 -07001954 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08001955 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001956 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001957
Jeff Brown13b16042014-11-11 16:44:25 -08001958 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001959 return NO_ERROR;
1960}
1961
Mathias Agopiane1424282013-07-29 21:24:40 -07001962status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001963{
1964 // size
1965 const size_t len = this->readInt32();
1966 const size_t fd_count = this->readInt32();
1967
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001968 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001969 // don't accept size_t values which may have come from an
1970 // inadvertent conversion from a negative int.
1971 return BAD_VALUE;
1972 }
1973
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001974 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001975 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07001976 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001977 return BAD_VALUE;
1978
Yi Kong91635562018-06-07 14:38:36 -07001979 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001980 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001981 fds = new (std::nothrow) int[fd_count];
1982 if (fds == nullptr) {
1983 ALOGE("read: failed to allocate requested %zu fds", fd_count);
1984 return BAD_VALUE;
1985 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001986 }
1987
1988 status_t err = NO_ERROR;
1989 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07001990 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001991 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001992 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001993 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 -07001994 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
1995 // Close all the file descriptors that were dup-ed.
1996 for (size_t j=0; j<i ;j++) {
1997 close(fds[j]);
1998 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001999 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002000 }
2001
2002 if (err == NO_ERROR) {
2003 err = val.unflatten(buf, len, fds, fd_count);
2004 }
2005
2006 if (fd_count) {
2007 delete [] fds;
2008 }
2009
2010 return err;
2011}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002012const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2013{
2014 const size_t DPOS = mDataPos;
2015 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2016 const flat_binder_object* obj
2017 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2018 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002019 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002020 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002021 // the object list, so we don't want to check for it when
2022 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002023 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002024 return obj;
2025 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002026
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002027 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002028 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002029 const size_t N = mObjectsSize;
2030 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002031
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002032 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002033 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002034 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002035
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002036 // Start at the current hint position, looking for an object at
2037 // the current data position.
2038 if (opos < N) {
2039 while (opos < (N-1) && OBJS[opos] < DPOS) {
2040 opos++;
2041 }
2042 } else {
2043 opos = N-1;
2044 }
2045 if (OBJS[opos] == DPOS) {
2046 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002047 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002048 this, DPOS, opos);
2049 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002050 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002051 return obj;
2052 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002053
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002054 // Look backwards for it...
2055 while (opos > 0 && OBJS[opos] > DPOS) {
2056 opos--;
2057 }
2058 if (OBJS[opos] == DPOS) {
2059 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002060 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002061 this, DPOS, opos);
2062 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002063 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002064 return obj;
2065 }
2066 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002067 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 -07002068 this, DPOS);
2069 }
Yi Kong91635562018-06-07 14:38:36 -07002070 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002071}
2072
2073void Parcel::closeFileDescriptors()
2074{
2075 size_t i = mObjectsSize;
2076 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002077 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002078 }
2079 while (i > 0) {
2080 i--;
2081 const flat_binder_object* flat
2082 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002083 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002084 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002085 close(flat->handle);
2086 }
2087 }
2088}
2089
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002090uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002091{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002092 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002093}
2094
2095size_t Parcel::ipcDataSize() const
2096{
2097 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2098}
2099
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002100uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002101{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002102 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002103}
2104
2105size_t Parcel::ipcObjectsCount() const
2106{
2107 return mObjectsSize;
2108}
2109
2110void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Steven Moreland161fe122020-11-12 23:16:47 +00002111 const binder_size_t* objects, size_t objectsCount, release_func relFunc)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002112{
Steven Moreland438cce82021-04-02 18:04:08 +00002113 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2114 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2115
Steven Morelandceed9bb2020-12-17 01:01:06 +00002116 freeData();
2117
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002118 mData = const_cast<uint8_t*>(data);
2119 mDataSize = mDataCapacity = dataSize;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002120 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002121 mObjectsSize = mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002122 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002123
2124 binder_size_t minOffset = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002125 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002126 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002127 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002128 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002129 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002130 mObjectsSize = 0;
2131 break;
2132 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002133 const flat_binder_object* flat
2134 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2135 uint32_t type = flat->hdr.type;
2136 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2137 type == BINDER_TYPE_FD)) {
2138 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2139 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2140 // recover gracefully by clearing out the objects, and releasing the objects we do
2141 // know about.
2142 android_errorWriteLog(0x534e4554, "135930648");
2143 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2144 __func__, type, (uint64_t)offset);
2145 releaseObjects();
2146 mObjectsSize = 0;
2147 break;
2148 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002149 minOffset = offset + sizeof(flat_binder_object);
2150 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002151 scanForFds();
2152}
2153
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002154void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002155{
2156 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002157
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002158 if (errorCheck() != NO_ERROR) {
2159 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002160 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002161 } else if (dataSize() > 0) {
2162 const uint8_t* DATA = data();
2163 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002164 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002165 const size_t N = objectsCount();
2166 for (size_t i=0; i<N; i++) {
2167 const flat_binder_object* flat
2168 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2169 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002170 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002171 << " = " << flat->binder;
2172 }
2173 } else {
2174 to << "NULL";
2175 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002176
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002177 to << ")";
2178}
2179
2180void Parcel::releaseObjects()
2181{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002182 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002183 if (i == 0) {
2184 return;
2185 }
2186 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002187 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002188 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002189 while (i > 0) {
2190 i--;
2191 const flat_binder_object* flat
2192 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002193 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002194 }
2195}
2196
2197void Parcel::acquireObjects()
2198{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002199 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002200 if (i == 0) {
2201 return;
2202 }
2203 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002204 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002205 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002206 while (i > 0) {
2207 i--;
2208 const flat_binder_object* flat
2209 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002210 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002211 }
2212}
2213
2214void Parcel::freeData()
2215{
2216 freeDataNoInit();
2217 initState();
2218}
2219
2220void Parcel::freeDataNoInit()
2221{
2222 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002223 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002224 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002225 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002226 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002227 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002228 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002229 if (mData) {
2230 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002231 gParcelGlobalAllocSize -= mDataCapacity;
2232 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002233 if (mDeallocZero) {
2234 zeroMemory(mData, mDataSize);
2235 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002236 free(mData);
2237 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002238 if (mObjects) free(mObjects);
2239 }
2240}
2241
2242status_t Parcel::growData(size_t len)
2243{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002244 if (len > INT32_MAX) {
2245 // don't accept size_t values which may have come from an
2246 // inadvertent conversion from a negative int.
2247 return BAD_VALUE;
2248 }
2249
Martijn Coenen93fe5182020-01-22 10:46:25 +01002250 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2251 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002252 size_t newSize = ((mDataSize+len)*3)/2;
2253 return (newSize <= mDataSize)
2254 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002255 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002256}
2257
Steven Morelandf183fdd2020-10-27 00:12:12 +00002258static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2259 if (!zero) {
2260 return (uint8_t*)realloc(data, newCapacity);
2261 }
2262 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2263 if (!newData) {
2264 return nullptr;
2265 }
2266
2267 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2268 zeroMemory(data, oldCapacity);
2269 free(data);
2270 return newData;
2271}
2272
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002273status_t Parcel::restartWrite(size_t desired)
2274{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002275 if (desired > INT32_MAX) {
2276 // don't accept size_t values which may have come from an
2277 // inadvertent conversion from a negative int.
2278 return BAD_VALUE;
2279 }
2280
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002281 if (mOwner) {
2282 freeData();
2283 return continueWrite(desired);
2284 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002285
Steven Morelandf183fdd2020-10-27 00:12:12 +00002286 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002287 if (!data && desired > mDataCapacity) {
2288 mError = NO_MEMORY;
2289 return NO_MEMORY;
2290 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002291
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002292 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002293
Devin Moore4a0a55e2020-06-04 13:23:10 -07002294 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002295 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002296 if (mDataCapacity > desired) {
2297 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2298 } else {
2299 gParcelGlobalAllocSize += (desired - mDataCapacity);
2300 }
2301
Colin Cross83ec65e2015-12-08 17:15:50 -08002302 if (!mData) {
2303 gParcelGlobalAllocCount++;
2304 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002305 mData = data;
2306 mDataCapacity = desired;
2307 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002308
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002309 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002310 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2311 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2312
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002313 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002314 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002315 mObjectsSize = mObjectsCapacity = 0;
2316 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002317 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002318 mHasFds = false;
2319 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002320 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002321
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002322 return NO_ERROR;
2323}
2324
2325status_t Parcel::continueWrite(size_t desired)
2326{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002327 if (desired > INT32_MAX) {
2328 // don't accept size_t values which may have come from an
2329 // inadvertent conversion from a negative int.
2330 return BAD_VALUE;
2331 }
2332
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002333 // If shrinking, first adjust for any objects that appear
2334 // after the new data size.
2335 size_t objectsSize = mObjectsSize;
2336 if (desired < mDataSize) {
2337 if (desired == 0) {
2338 objectsSize = 0;
2339 } else {
2340 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002341 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002342 break;
2343 objectsSize--;
2344 }
2345 }
2346 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002347
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002348 if (mOwner) {
2349 // If the size is going to zero, just release the owner's data.
2350 if (desired == 0) {
2351 freeData();
2352 return NO_ERROR;
2353 }
2354
2355 // If there is a different owner, we need to take
2356 // posession.
2357 uint8_t* data = (uint8_t*)malloc(desired);
2358 if (!data) {
2359 mError = NO_MEMORY;
2360 return NO_MEMORY;
2361 }
Yi Kong91635562018-06-07 14:38:36 -07002362 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002363
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002364 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002365 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002366 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002367 free(data);
2368
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002369 mError = NO_MEMORY;
2370 return NO_MEMORY;
2371 }
2372
2373 // Little hack to only acquire references on objects
2374 // we will be keeping.
2375 size_t oldObjectsSize = mObjectsSize;
2376 mObjectsSize = objectsSize;
2377 acquireObjects();
2378 mObjectsSize = oldObjectsSize;
2379 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002380
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002381 if (mData) {
2382 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2383 }
2384 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002385 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002386 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002387 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002388 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
Yi Kong91635562018-06-07 14:38:36 -07002389 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002390
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002391 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002392 gParcelGlobalAllocSize += desired;
2393 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002394
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002395 mData = data;
2396 mObjects = objects;
2397 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002398 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002399 mDataCapacity = desired;
2400 mObjectsSize = mObjectsCapacity = objectsSize;
2401 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002402 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002403
2404 } else if (mData) {
2405 if (objectsSize < mObjectsSize) {
2406 // Need to release refs on any objects we are dropping.
2407 const sp<ProcessState> proc(ProcessState::self());
2408 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2409 const flat_binder_object* flat
2410 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002411 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002412 // will need to rescan because we may have lopped off the only FDs
2413 mFdsKnown = false;
2414 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002415 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002416 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002417
2418 if (objectsSize == 0) {
2419 free(mObjects);
2420 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002421 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002422 } else {
2423 binder_size_t* objects =
2424 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2425 if (objects) {
2426 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002427 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002428 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002429 }
2430 mObjectsSize = objectsSize;
2431 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002432 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002433 }
2434
2435 // We own the data, so we can just do a realloc().
2436 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002437 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002438 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002439 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2440 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002441 gParcelGlobalAllocSize += desired;
2442 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002443 mData = data;
2444 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002445 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002446 mError = NO_MEMORY;
2447 return NO_MEMORY;
2448 }
2449 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002450 if (mDataSize > desired) {
2451 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002452 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002453 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002454 if (mDataPos > desired) {
2455 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002456 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002457 }
2458 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002459
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002460 } else {
2461 // This is the first data. Easy!
2462 uint8_t* data = (uint8_t*)malloc(desired);
2463 if (!data) {
2464 mError = NO_MEMORY;
2465 return NO_MEMORY;
2466 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002467
Yi Kong91635562018-06-07 14:38:36 -07002468 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002469 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002470 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002471 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002472
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002473 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002474 gParcelGlobalAllocSize += desired;
2475 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002476
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002477 mData = data;
2478 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002479 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2480 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002481 mDataCapacity = desired;
2482 }
2483
2484 return NO_ERROR;
2485}
2486
2487void Parcel::initState()
2488{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002489 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002490 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002491 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002492 mDataSize = 0;
2493 mDataCapacity = 0;
2494 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002495 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2496 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Steven Moreland5553ac42020-11-11 02:14:45 +00002497 mConnection = nullptr;
Yi Kong91635562018-06-07 14:38:36 -07002498 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002499 mObjectsSize = 0;
2500 mObjectsCapacity = 0;
2501 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002502 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002503 mHasFds = false;
2504 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002505 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002506 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07002507 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002508 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002509 mWorkSourceRequestHeaderPosition = 0;
2510 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002511
2512 // racing multiple init leads only to multiple identical write
2513 if (gMaxFds == 0) {
2514 struct rlimit result;
2515 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2516 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002517 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002518 } else {
2519 ALOGW("Unable to getrlimit: %s", strerror(errno));
2520 gMaxFds = 1024;
2521 }
2522 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002523}
2524
2525void Parcel::scanForFds() const
2526{
2527 bool hasFds = false;
2528 for (size_t i=0; i<mObjectsSize; i++) {
2529 const flat_binder_object* flat
2530 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002531 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002532 hasFds = true;
2533 break;
2534 }
2535 }
2536 mHasFds = hasFds;
2537 mFdsKnown = true;
2538}
2539
Dan Sandleraa5c2342015-04-10 10:08:45 -04002540size_t Parcel::getBlobAshmemSize() const
2541{
Adrian Roos6bb31142015-10-22 16:46:12 -07002542 // This used to return the size of all blobs that were written to ashmem, now we're returning
2543 // the ashmem currently referenced by this Parcel, which should be equivalent.
2544 // TODO: Remove method once ABI can be changed.
2545 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002546}
2547
Adrian Rooscbf37262015-10-22 16:12:53 -07002548size_t Parcel::getOpenAshmemSize() const
2549{
2550 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002551}
2552
2553// --- Parcel::Blob ---
2554
2555Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002556 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002557}
2558
2559Parcel::Blob::~Blob() {
2560 release();
2561}
2562
2563void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002564 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002565 ::munmap(mData, mSize);
2566 }
2567 clear();
2568}
2569
Jeff Brown13b16042014-11-11 16:44:25 -08002570void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2571 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002572 mData = data;
2573 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002574 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002575}
2576
2577void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002578 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002579 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002580 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002581 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002582}
2583
Steven Moreland61ff8492019-09-26 16:05:45 -07002584} // namespace android