blob: 9f95167f868b8c529e5c57d7aabe0fb9e1b7266d [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
Steven Moreland5553ac42020-11-11 02:14:45 +000051#include "RpcState.h"
Steven Morelanda4853cd2019-07-12 15:44:37 -070052#include "Static.h"
Steven Morelandf183fdd2020-10-27 00:12:12 +000053#include "Utils.h"
Steven Moreland6ba5a252021-05-04 22:49:00 +000054#include "binder_module.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
Steven Moreland90c1f9a2021-05-03 18:27:24 +000081#ifdef __LP64__
82static_assert(sizeof(Parcel) == 120);
83#else
84static_assert(sizeof(Parcel) == 60);
85#endif
Steven Moreland7b102262019-08-01 15:48:43 -070086
Jeff Sharkey8994c182020-09-11 12:07:10 -060087static std::atomic<size_t> gParcelGlobalAllocCount;
88static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -080089
Christopher Tatee4e0ae82016-03-24 16:03:44 -070090static size_t gMaxFds = 0;
91
Jeff Brown13b16042014-11-11 16:44:25 -080092// Maximum size of a blob to transfer in-place.
93static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
94
95enum {
96 BLOB_INPLACE = 0,
97 BLOB_ASHMEM_IMMUTABLE = 1,
98 BLOB_ASHMEM_MUTABLE = 2,
99};
100
Steven Morelandb1c81202019-04-05 18:49:55 -0700101static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700102 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700103{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700104 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700105 case BINDER_TYPE_BINDER:
106 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000107 LOG_REFS("Parcel %p acquiring reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800108 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 }
110 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700111 case BINDER_TYPE_HANDLE: {
112 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700113 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700114 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
115 b->incStrong(who);
116 }
117 return;
118 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700119 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000120 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700121 // If we own an ashmem fd, keep track of how much memory it refers to.
122 int size = ashmem_get_size_region(obj.handle);
123 if (size > 0) {
124 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700125 }
126 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700127 return;
128 }
129 }
130
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700131 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700132}
133
Adrian Roos6bb31142015-10-22 16:46:12 -0700134static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700135 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700137 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700138 case BINDER_TYPE_BINDER:
139 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000140 LOG_REFS("Parcel %p releasing reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800141 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142 }
143 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144 case BINDER_TYPE_HANDLE: {
145 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700146 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
148 b->decStrong(who);
149 }
150 return;
151 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700152 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800153 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000154 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700155 int size = ashmem_get_size_region(obj.handle);
156 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800157 // ashmem size might have changed since last time it was accounted for, e.g.
158 // in acquire_object(). Value of *outAshmemSize is not critical since we are
159 // releasing the object anyway. Check for integer overflow condition.
160 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700161 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700162 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800163
164 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700165 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700166 return;
167 }
168 }
169
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700170 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700171}
172
Steven Moreland34b48cb2020-12-01 22:45:38 +0000173status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700174{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700175 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland16a41062021-07-23 13:35:25 -0700176 int16_t rep = internal::Stability::getRepr(binder.get());
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000177 return writeInt32(rep);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700178}
179
Steven Morelanda86a3562019-08-01 23:28:34 +0000180status_t Parcel::finishUnflattenBinder(
181 const sp<IBinder>& binder, sp<IBinder>* out) const
182{
183 int32_t stability;
184 status_t status = readInt32(&stability);
185 if (status != OK) return status;
186
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000187 status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability),
188 true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000189 if (status != OK) return status;
190
191 *out = binder;
192 return OK;
193}
194
Steven Morelandbf1915b2020-07-16 22:43:02 +0000195static constexpr inline int schedPolicyMask(int policy, int priority) {
196 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
197}
198
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500199status_t Parcel::flattenBinder(const sp<IBinder>& binder) {
200 BBinder* local = nullptr;
201 if (binder) local = binder->localBinder();
202 if (local) local->setParceled();
203
Steven Moreland5553ac42020-11-11 02:14:45 +0000204 if (isForRpc()) {
205 if (binder) {
206 status_t status = writeInt32(1); // non-null
207 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700208 uint64_t address;
Steven Morelanda5036f02021-06-08 02:26:57 +0000209 // TODO(b/167966510): need to undo this if the Parcel is not sent
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000210 status = mSession->state()->onBinderLeaving(mSession, binder, &address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000211 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700212 status = writeUint64(address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000213 if (status != OK) return status;
214 } else {
215 status_t status = writeInt32(0); // null
216 if (status != OK) return status;
217 }
218 return finishFlattenBinder(binder);
219 }
220
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700221 flat_binder_object obj;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000222 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700223
Steven Morelandbf1915b2020-07-16 22:43:02 +0000224 int schedBits = 0;
225 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
226 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700227 }
228
Yi Kong91635562018-06-07 14:38:36 -0700229 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230 if (!local) {
231 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700232 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000233 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000234 } else {
235 if (proxy->isRpcBinder()) {
236 ALOGE("Sending a socket binder over RPC is prohibited");
237 return INVALID_OPERATION;
238 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700239 }
Steven Moreland99157622021-09-13 16:27:34 -0700240 const int32_t handle = proxy ? proxy->getPrivateAccessor().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700241 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800242 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800244 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700245 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000246 int policy = local->getMinSchedulerPolicy();
247 int priority = local->getMinSchedulerPriority();
248
249 if (policy != 0 || priority != 0) {
250 // override value, since it is set explicitly
251 schedBits = schedPolicyMask(policy, priority);
252 }
Steven Morelandf0212002018-12-26 13:59:23 -0800253 if (local->isRequestingSid()) {
254 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
255 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000256 if (local->isInheritRt()) {
257 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
258 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700259 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800260 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
261 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700262 }
263 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700264 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800265 obj.binder = 0;
266 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700267 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700268
Steven Morelandbf1915b2020-07-16 22:43:02 +0000269 obj.flags |= schedBits;
270
Steven Moreland34b48cb2020-12-01 22:45:38 +0000271 status_t status = writeObject(obj, false);
272 if (status != OK) return status;
273
274 return finishFlattenBinder(binder);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700275}
276
Steven Morelanda86a3562019-08-01 23:28:34 +0000277status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700278{
Steven Moreland5553ac42020-11-11 02:14:45 +0000279 if (isForRpc()) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000280 LOG_ALWAYS_FATAL_IF(mSession == nullptr, "RpcSession required to read from remote parcel");
Steven Moreland5553ac42020-11-11 02:14:45 +0000281
Steven Moreland5623d1a2021-09-10 15:45:34 -0700282 int32_t isPresent;
283 status_t status = readInt32(&isPresent);
Steven Moreland5553ac42020-11-11 02:14:45 +0000284 if (status != OK) return status;
285
286 sp<IBinder> binder;
287
Steven Moreland5623d1a2021-09-10 15:45:34 -0700288 if (isPresent & 1) {
289 uint64_t addr;
290 if (status_t status = readUint64(&addr); status != OK) return status;
Steven Moreland7227c8a2021-06-02 00:24:32 +0000291 if (status_t status = mSession->state()->onBinderEntering(mSession, addr, &binder);
292 status != OK)
293 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000294 }
295
296 return finishUnflattenBinder(binder, out);
297 }
298
Steven Morelanda86a3562019-08-01 23:28:34 +0000299 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700300
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700301 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700302 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000303 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000304 sp<IBinder> binder =
305 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000306 return finishUnflattenBinder(binder, out);
307 }
308 case BINDER_TYPE_HANDLE: {
309 sp<IBinder> binder =
310 ProcessState::self()->getStrongProxyForHandle(flat->handle);
311 return finishUnflattenBinder(binder, out);
312 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700313 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700314 }
315 return BAD_TYPE;
316}
317
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700318// ---------------------------------------------------------------------------
319
320Parcel::Parcel()
321{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800322 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700323 initState();
324}
325
326Parcel::~Parcel()
327{
328 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800329 LOG_ALLOC("Parcel %p: destroyed", this);
330}
331
332size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600333 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800334}
335
336size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600337 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700338}
339
340const uint8_t* Parcel::data() const
341{
342 return mData;
343}
344
345size_t Parcel::dataSize() const
346{
347 return (mDataSize > mDataPos ? mDataSize : mDataPos);
348}
349
350size_t Parcel::dataAvail() const
351{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700352 size_t result = dataSize() - dataPosition();
353 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700354 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700355 }
356 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700357}
358
359size_t Parcel::dataPosition() const
360{
361 return mDataPos;
362}
363
364size_t Parcel::dataCapacity() const
365{
366 return mDataCapacity;
367}
368
369status_t Parcel::setDataSize(size_t size)
370{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700371 if (size > INT32_MAX) {
372 // don't accept size_t values which may have come from an
373 // inadvertent conversion from a negative int.
374 return BAD_VALUE;
375 }
376
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700377 status_t err;
378 err = continueWrite(size);
379 if (err == NO_ERROR) {
380 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700381 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700382 }
383 return err;
384}
385
386void Parcel::setDataPosition(size_t pos) const
387{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700388 if (pos > INT32_MAX) {
389 // don't accept size_t values which may have come from an
390 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700391 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700392 }
393
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700394 mDataPos = pos;
395 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800396 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700397}
398
399status_t Parcel::setDataCapacity(size_t size)
400{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700401 if (size > INT32_MAX) {
402 // don't accept size_t values which may have come from an
403 // inadvertent conversion from a negative int.
404 return BAD_VALUE;
405 }
406
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700407 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700408 return NO_ERROR;
409}
410
411status_t Parcel::setData(const uint8_t* buffer, size_t len)
412{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700413 if (len > INT32_MAX) {
414 // don't accept size_t values which may have come from an
415 // inadvertent conversion from a negative int.
416 return BAD_VALUE;
417 }
418
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700419 status_t err = restartWrite(len);
420 if (err == NO_ERROR) {
421 memcpy(const_cast<uint8_t*>(data()), buffer, len);
422 mDataSize = len;
423 mFdsKnown = false;
424 }
425 return err;
426}
427
Andreas Huber51faf462011-04-13 10:21:56 -0700428status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700429{
Steven Moreland67753c32021-04-02 18:45:19 +0000430 if (parcel->isForRpc() != isForRpc()) {
431 ALOGE("Cannot append Parcel of one format to another.");
432 return BAD_TYPE;
433 }
434
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700435 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700436 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800437 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700438 size_t size = parcel->mObjectsSize;
439 int startPos = mDataPos;
440 int firstIndex = -1, lastIndex = -2;
441
442 if (len == 0) {
443 return NO_ERROR;
444 }
445
Nick Kralevichb6b14232015-04-02 09:36:02 -0700446 if (len > INT32_MAX) {
447 // don't accept size_t values which may have come from an
448 // inadvertent conversion from a negative int.
449 return BAD_VALUE;
450 }
451
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700452 // range checks against the source parcel size
453 if ((offset > parcel->mDataSize)
454 || (len > parcel->mDataSize)
455 || (offset + len > parcel->mDataSize)) {
456 return BAD_VALUE;
457 }
458
459 // Count objects in range
460 for (int i = 0; i < (int) size; i++) {
461 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700462 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700463 if (firstIndex == -1) {
464 firstIndex = i;
465 }
466 lastIndex = i;
467 }
468 }
469 int numObjects = lastIndex - firstIndex + 1;
470
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700471 if ((mDataSize+len) > mDataCapacity) {
472 // grow data
473 err = growData(len);
474 if (err != NO_ERROR) {
475 return err;
476 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700477 }
478
479 // append data
480 memcpy(mData + mDataPos, data + offset, len);
481 mDataPos += len;
482 mDataSize += len;
483
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400484 err = NO_ERROR;
485
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700486 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200487 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700488 // grow objects
489 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +0100490 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
491 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700492 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +0100493 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800494 binder_size_t *objects =
495 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700496 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700497 return NO_MEMORY;
498 }
499 mObjects = objects;
500 mObjectsCapacity = newSize;
501 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700502
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700503 // append and acquire objects
504 int idx = mObjectsSize;
505 for (int i = firstIndex; i <= lastIndex; i++) {
506 size_t off = objects[i] - offset + startPos;
507 mObjects[idx++] = off;
508 mObjectsSize++;
509
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700510 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700511 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700512 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700513
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700514 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700515 // If this is a file descriptor, we need to dup it so the
516 // new Parcel now owns its own fd, and can declare that we
517 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800518 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800519 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700520 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400521 if (!mAllowFds) {
522 err = FDS_NOT_ALLOWED;
523 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700524 }
525 }
526 }
527
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400528 return err;
529}
530
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700531int Parcel::compareData(const Parcel& other) {
532 size_t size = dataSize();
533 if (size != other.dataSize()) {
534 return size < other.dataSize() ? -1 : 1;
535 }
536 return memcmp(data(), other.data(), size);
537}
538
Jeff Brown13b16042014-11-11 16:44:25 -0800539bool Parcel::allowFds() const
540{
541 return mAllowFds;
542}
543
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700544bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400545{
546 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700547 if (!allowFds) {
548 mAllowFds = false;
549 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400550 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700551}
552
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700553void Parcel::restoreAllowFds(bool lastValue)
554{
555 mAllowFds = lastValue;
556}
557
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700558bool Parcel::hasFileDescriptors() const
559{
560 if (!mFdsKnown) {
561 scanForFds();
562 }
563 return mHasFds;
564}
565
Steven Morelandf183fdd2020-10-27 00:12:12 +0000566void Parcel::markSensitive() const
567{
568 mDeallocZero = true;
569}
570
Steven Moreland5553ac42020-11-11 02:14:45 +0000571void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000572 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
573
Steven Moreland5553ac42020-11-11 02:14:45 +0000574 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700575 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000576 }
577}
578
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000579void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000580 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
581 "format must be set before data is written OR on IPC data");
582
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000583 LOG_ALWAYS_FATAL_IF(session == nullptr, "markForRpc requires session");
584 mSession = session;
Steven Moreland5553ac42020-11-11 02:14:45 +0000585}
586
587bool Parcel::isForRpc() const {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000588 return mSession != nullptr;
Steven Moreland5553ac42020-11-11 02:14:45 +0000589}
590
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000591void Parcel::updateWorkSourceRequestHeaderPosition() const {
592 // Only update the request headers once. We only want to point
593 // to the first headers read/written.
594 if (!mRequestHeaderPresent) {
595 mWorkSourceRequestHeaderPosition = dataPosition();
596 mRequestHeaderPresent = true;
597 }
598}
599
Jooyung Han1b228f42020-01-30 13:41:12 +0900600#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700601constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
602#else
603constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
604#endif
605
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700606// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700607status_t Parcel::writeInterfaceToken(const String16& interface)
608{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000609 return writeInterfaceToken(interface.string(), interface.size());
610}
611
612status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000613 if (CC_LIKELY(!isForRpc())) {
614 const IPCThreadState* threadState = IPCThreadState::self();
615 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
616 updateWorkSourceRequestHeaderPosition();
617 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
618 : IPCThreadState::kUnsetWorkSource);
619 writeInt32(kHeader);
620 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000621
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700622 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000623 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700624}
625
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000626bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
627{
628 if (!mRequestHeaderPresent) {
629 return false;
630 }
631
632 const size_t initialPosition = dataPosition();
633 setDataPosition(mWorkSourceRequestHeaderPosition);
634 status_t err = writeInt32(uid);
635 setDataPosition(initialPosition);
636 return err == NO_ERROR;
637}
638
Steven Morelandf1b1e492019-05-06 15:05:13 -0700639uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000640{
641 if (!mRequestHeaderPresent) {
642 return IPCThreadState::kUnsetWorkSource;
643 }
644
645 const size_t initialPosition = dataPosition();
646 setDataPosition(mWorkSourceRequestHeaderPosition);
647 uid_t uid = readInt32();
648 setDataPosition(initialPosition);
649 return uid;
650}
651
Mathias Agopian83c04462009-05-22 19:00:22 -0700652bool Parcel::checkInterface(IBinder* binder) const
653{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700654 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700655}
656
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700657bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700658 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700659{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700660 return enforceInterface(interface.string(), interface.size(), threadState);
661}
662
663bool Parcel::enforceInterface(const char16_t* interface,
664 size_t len,
665 IPCThreadState* threadState) const
666{
Steven Moreland3af936a2021-03-26 03:05:38 +0000667 if (CC_LIKELY(!isForRpc())) {
668 // StrictModePolicy.
669 int32_t strictPolicy = readInt32();
670 if (threadState == nullptr) {
671 threadState = IPCThreadState::self();
672 }
673 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
674 // For one-way calls, the callee is running entirely
675 // disconnected from the caller, so disable StrictMode entirely.
676 // Not only does disk/network usage not impact the caller, but
677 // there's no way to communicate back violations anyway.
678 threadState->setStrictModePolicy(0);
679 } else {
680 threadState->setStrictModePolicy(strictPolicy);
681 }
682 // WorkSource.
683 updateWorkSourceRequestHeaderPosition();
684 int32_t workSource = readInt32();
685 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
686 // vendor header
687 int32_t header = readInt32();
688 if (header != kHeader) {
689 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
690 header);
691 return false;
692 }
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700693 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000694
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100695 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700696 size_t parcel_interface_len;
697 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
698 if (len == parcel_interface_len &&
699 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700700 return true;
701 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700702 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700703 String8(interface, len).string(),
704 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700705 return false;
706 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700707}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700708
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700709size_t Parcel::objectsCount() const
710{
711 return mObjectsSize;
712}
713
714status_t Parcel::errorCheck() const
715{
716 return mError;
717}
718
719void Parcel::setError(status_t err)
720{
721 mError = err;
722}
723
724status_t Parcel::finishWrite(size_t len)
725{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700726 if (len > INT32_MAX) {
727 // don't accept size_t values which may have come from an
728 // inadvertent conversion from a negative int.
729 return BAD_VALUE;
730 }
731
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700732 //printf("Finish write of %d\n", len);
733 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700734 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700735 if (mDataPos > mDataSize) {
736 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700737 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700738 }
739 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
740 return NO_ERROR;
741}
742
743status_t Parcel::writeUnpadded(const void* data, size_t len)
744{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700745 if (len > INT32_MAX) {
746 // don't accept size_t values which may have come from an
747 // inadvertent conversion from a negative int.
748 return BAD_VALUE;
749 }
750
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700751 size_t end = mDataPos + len;
752 if (end < mDataPos) {
753 // integer overflow
754 return BAD_VALUE;
755 }
756
757 if (end <= mDataCapacity) {
758restart_write:
759 memcpy(mData+mDataPos, data, len);
760 return finishWrite(len);
761 }
762
763 status_t err = growData(len);
764 if (err == NO_ERROR) goto restart_write;
765 return err;
766}
767
768status_t Parcel::write(const void* data, size_t len)
769{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700770 if (len > INT32_MAX) {
771 // don't accept size_t values which may have come from an
772 // inadvertent conversion from a negative int.
773 return BAD_VALUE;
774 }
775
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700776 void* const d = writeInplace(len);
777 if (d) {
778 memcpy(d, data, len);
779 return NO_ERROR;
780 }
781 return mError;
782}
783
784void* Parcel::writeInplace(size_t len)
785{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700786 if (len > INT32_MAX) {
787 // don't accept size_t values which may have come from an
788 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700789 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700790 }
791
792 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700793
794 // sanity check for integer overflow
795 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700796 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700797 }
798
799 if ((mDataPos+padded) <= mDataCapacity) {
800restart_write:
801 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
802 uint8_t* const data = mData+mDataPos;
803
804 // Need to pad at end?
805 if (padded != len) {
806#if BYTE_ORDER == BIG_ENDIAN
807 static const uint32_t mask[4] = {
808 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
809 };
810#endif
811#if BYTE_ORDER == LITTLE_ENDIAN
812 static const uint32_t mask[4] = {
813 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
814 };
815#endif
816 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
817 // *reinterpret_cast<void**>(data+padded-4));
818 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
819 }
820
821 finishWrite(padded);
822 return data;
823 }
824
825 status_t err = growData(padded);
826 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700827 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700828}
829
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800830status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
831 const uint8_t* strData = (uint8_t*)str.data();
832 const size_t strLen= str.length();
833 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100834 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800835 return BAD_VALUE;
836 }
837
838 status_t err = writeInt32(utf16Len);
839 if (err) {
840 return err;
841 }
842
843 // Allocate enough bytes to hold our converted string and its terminating NULL.
844 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
845 if (!dst) {
846 return NO_MEMORY;
847 }
848
Sergio Girof4607432016-07-21 14:46:35 +0100849 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800850
851 return NO_ERROR;
852}
853
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900854
Andy Hung49198cf2020-11-18 11:02:39 -0800855status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
856status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800857
Andy Hung49198cf2020-11-18 11:02:39 -0800858status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
859status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700860
Andy Hung49198cf2020-11-18 11:02:39 -0800861status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
862status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
863status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
864status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
865status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
866status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
867status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
868status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
869status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
870status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
871status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
872status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
873status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
874status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
875status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
876status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
877status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
878status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
879status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
880status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
881status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
882status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
883status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
884status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
885status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
886status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
887status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700888
Andy Hung49198cf2020-11-18 11:02:39 -0800889status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800890status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800891 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900892status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800893 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800894status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800895 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900896status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800897 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
898status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800899
Andy Hung49198cf2020-11-18 11:02:39 -0800900status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
901status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
902status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
903
904status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
905status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
906status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
907
908status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
909
910status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
911status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
912
913status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
914status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
915
916status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
917status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
918status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
919status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
920status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
921status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
922status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
923status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
924status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
925status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
926status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
927status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
928status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
929status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
930status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
931status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
932status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
933status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
934status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
935status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
936status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
937status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
938status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
939status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
940status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
941status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
942status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
943
944status_t Parcel::readString16Vector(
945 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
946status_t Parcel::readString16Vector(
947 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
948status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
949status_t Parcel::readUtf8VectorFromUtf16Vector(
950 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
951status_t Parcel::readUtf8VectorFromUtf16Vector(
952 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
953status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
954
955status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
956status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
957status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
958
959status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
960status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
961status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
962
963status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800964
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700965status_t Parcel::writeInt32(int32_t val)
966{
Andreas Huber84a6d042009-08-17 13:33:27 -0700967 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700968}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800969
970status_t Parcel::writeUint32(uint32_t val)
971{
972 return writeAligned(val);
973}
974
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700975status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700976 if (len > INT32_MAX) {
977 // don't accept size_t values which may have come from an
978 // inadvertent conversion from a negative int.
979 return BAD_VALUE;
980 }
981
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700982 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700983 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700984 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700985 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700986 if (ret == NO_ERROR) {
987 ret = write(val, len * sizeof(*val));
988 }
989 return ret;
990}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700991status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700992 if (len > INT32_MAX) {
993 // don't accept size_t values which may have come from an
994 // inadvertent conversion from a negative int.
995 return BAD_VALUE;
996 }
997
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700998 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700999 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001000 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001001 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001002 if (ret == NO_ERROR) {
1003 ret = write(val, len * sizeof(*val));
1004 }
1005 return ret;
1006}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001007
Casey Dahlind6848f52015-10-15 15:44:59 -07001008status_t Parcel::writeBool(bool val)
1009{
1010 return writeInt32(int32_t(val));
1011}
1012
1013status_t Parcel::writeChar(char16_t val)
1014{
1015 return writeInt32(int32_t(val));
1016}
1017
1018status_t Parcel::writeByte(int8_t val)
1019{
1020 return writeInt32(int32_t(val));
1021}
1022
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001023status_t Parcel::writeInt64(int64_t val)
1024{
Andreas Huber84a6d042009-08-17 13:33:27 -07001025 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001026}
1027
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001028status_t Parcel::writeUint64(uint64_t val)
1029{
1030 return writeAligned(val);
1031}
1032
Serban Constantinescuf683e012013-11-05 16:53:55 +00001033status_t Parcel::writePointer(uintptr_t val)
1034{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001035 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001036}
1037
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001038status_t Parcel::writeFloat(float val)
1039{
Andreas Huber84a6d042009-08-17 13:33:27 -07001040 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001041}
1042
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001043#if defined(__mips__) && defined(__mips_hard_float)
1044
1045status_t Parcel::writeDouble(double val)
1046{
1047 union {
1048 double d;
1049 unsigned long long ll;
1050 } u;
1051 u.d = val;
1052 return writeAligned(u.ll);
1053}
1054
1055#else
1056
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001057status_t Parcel::writeDouble(double val)
1058{
Andreas Huber84a6d042009-08-17 13:33:27 -07001059 return writeAligned(val);
1060}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001061
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001062#endif
1063
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001064status_t Parcel::writeCString(const char* str)
1065{
1066 return write(str, strlen(str)+1);
1067}
1068
1069status_t Parcel::writeString8(const String8& str)
1070{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001071 return writeString8(str.string(), str.size());
1072}
1073
1074status_t Parcel::writeString8(const char* str, size_t len)
1075{
1076 if (str == nullptr) return writeInt32(-1);
1077
Jeff Sharkey18220902020-11-05 08:36:20 -07001078 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001079 status_t err = writeInt32(len);
1080 if (err == NO_ERROR) {
1081 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1082 if (data) {
1083 memcpy(data, str, len);
1084 *reinterpret_cast<char*>(data+len) = 0;
1085 return NO_ERROR;
1086 }
1087 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001088 }
1089 return err;
1090}
1091
1092status_t Parcel::writeString16(const String16& str)
1093{
1094 return writeString16(str.string(), str.size());
1095}
1096
1097status_t Parcel::writeString16(const char16_t* str, size_t len)
1098{
Yi Kong91635562018-06-07 14:38:36 -07001099 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001100
Jeff Sharkey18220902020-11-05 08:36:20 -07001101 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001102 status_t err = writeInt32(len);
1103 if (err == NO_ERROR) {
1104 len *= sizeof(char16_t);
1105 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1106 if (data) {
1107 memcpy(data, str, len);
1108 *reinterpret_cast<char16_t*>(data+len) = 0;
1109 return NO_ERROR;
1110 }
1111 err = mError;
1112 }
1113 return err;
1114}
1115
1116status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1117{
Steven Morelanda86a3562019-08-01 23:28:34 +00001118 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001119}
1120
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001121
Casey Dahlinb9872622015-11-25 15:09:45 -08001122status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1123 if (!parcelable) {
1124 return writeInt32(0);
1125 }
1126
1127 return writeParcelable(*parcelable);
1128}
1129
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001130status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001131{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001132 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001133 return BAD_TYPE;
1134
1135 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001136 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001137 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001139 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001140 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001142 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1143 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001144
1145 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001146 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147 return err;
1148 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001149 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001150 return err;
1151}
1152
Jeff Brown93ff1f92011-11-04 19:01:44 -07001153status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001154{
Steven Moreland5553ac42020-11-11 02:14:45 +00001155 if (isForRpc()) {
1156 ALOGE("Cannot write file descriptor to remote binder.");
1157 return BAD_TYPE;
1158 }
1159
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001160 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001161 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001162 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001163 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001164 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001165 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001166 return writeObject(obj, true);
1167}
1168
1169status_t Parcel::writeDupFileDescriptor(int fd)
1170{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001171 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001172 if (dupFd < 0) {
1173 return -errno;
1174 }
1175 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001176 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001177 close(dupFd);
1178 }
1179 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001180}
1181
Dianne Hackborn1941a402016-08-29 12:30:43 -07001182status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1183{
1184 writeInt32(0);
1185 return writeFileDescriptor(fd, takeOwnership);
1186}
1187
Ryo Hashimotobf551892018-05-31 16:58:35 +09001188status_t Parcel::writeDupParcelFileDescriptor(int fd)
1189{
1190 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1191 if (dupFd < 0) {
1192 return -errno;
1193 }
1194 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1195 if (err != OK) {
1196 close(dupFd);
1197 }
1198 return err;
1199}
1200
Christopher Wiley2cf19952016-04-11 11:09:37 -07001201status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001202 return writeDupFileDescriptor(fd.get());
1203}
1204
Jeff Brown13b16042014-11-11 16:44:25 -08001205status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001206{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001207 if (len > INT32_MAX) {
1208 // don't accept size_t values which may have come from an
1209 // inadvertent conversion from a negative int.
1210 return BAD_VALUE;
1211 }
1212
Jeff Brown13b16042014-11-11 16:44:25 -08001213 status_t status;
1214 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001215 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001216 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001217 if (status) return status;
1218
1219 void* ptr = writeInplace(len);
1220 if (!ptr) return NO_MEMORY;
1221
Jeff Brown13b16042014-11-11 16:44:25 -08001222 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001223 return NO_ERROR;
1224 }
1225
Steve Block6807e592011-10-20 11:56:00 +01001226 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001227 int fd = ashmem_create_region("Parcel Blob", len);
1228 if (fd < 0) return NO_MEMORY;
1229
1230 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1231 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001232 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001233 } else {
Yi Kong91635562018-06-07 14:38:36 -07001234 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001235 if (ptr == MAP_FAILED) {
1236 status = -errno;
1237 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001238 if (!mutableCopy) {
1239 result = ashmem_set_prot_region(fd, PROT_READ);
1240 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001241 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001242 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001243 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001244 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001245 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001246 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001247 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001248 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001249 return NO_ERROR;
1250 }
1251 }
1252 }
1253 }
1254 ::munmap(ptr, len);
1255 }
1256 ::close(fd);
1257 return status;
1258}
1259
Jeff Brown13b16042014-11-11 16:44:25 -08001260status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1261{
1262 // Must match up with what's done in writeBlob.
1263 if (!mAllowFds) return FDS_NOT_ALLOWED;
1264 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1265 if (status) return status;
1266 return writeDupFileDescriptor(fd);
1267}
1268
Mathias Agopiane1424282013-07-29 21:24:40 -07001269status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001270{
1271 status_t err;
1272
1273 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001274 const size_t len = val.getFlattenedSize();
1275 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001276
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001277 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001278 // don't accept size_t values which may have come from an
1279 // inadvertent conversion from a negative int.
1280 return BAD_VALUE;
1281 }
1282
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001283 err = this->writeInt32(len);
1284 if (err) return err;
1285
1286 err = this->writeInt32(fd_count);
1287 if (err) return err;
1288
1289 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001290 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001291 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001292 return BAD_VALUE;
1293
Yi Kong91635562018-06-07 14:38:36 -07001294 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001295 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001296 fds = new (std::nothrow) int[fd_count];
1297 if (fds == nullptr) {
1298 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1299 return BAD_VALUE;
1300 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001301 }
1302
1303 err = val.flatten(buf, len, fds, fd_count);
1304 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1305 err = this->writeDupFileDescriptor( fds[i] );
1306 }
1307
1308 if (fd_count) {
1309 delete [] fds;
1310 }
1311
1312 return err;
1313}
1314
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001315status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1316{
1317 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1318 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1319 if (enoughData && enoughObjects) {
1320restart_write:
1321 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001322
Christopher Tate98e67d32015-06-03 18:44:15 -07001323 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001324 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001325 if (!mAllowFds) {
1326 // fail before modifying our object index
1327 return FDS_NOT_ALLOWED;
1328 }
1329 mHasFds = mFdsKnown = true;
1330 }
1331
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001332 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001333 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001334 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001335 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001336 mObjectsSize++;
1337 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001338
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001339 return finishWrite(sizeof(flat_binder_object));
1340 }
1341
1342 if (!enoughData) {
1343 const status_t err = growData(sizeof(val));
1344 if (err != NO_ERROR) return err;
1345 }
1346 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001347 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1348 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001349 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001350 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001351 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001352 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001353 mObjects = objects;
1354 mObjectsCapacity = newSize;
1355 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001356
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001357 goto restart_write;
1358}
1359
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001360status_t Parcel::writeNoException()
1361{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001362 binder::Status status;
1363 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001364}
1365
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001366status_t Parcel::validateReadData(size_t upperBound) const
1367{
1368 // Don't allow non-object reads on object data
1369 if (mObjectsSorted || mObjectsSize <= 1) {
1370data_sorted:
1371 // Expect to check only against the next object
1372 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1373 // For some reason the current read position is greater than the next object
1374 // hint. Iterate until we find the right object
1375 size_t nextObject = mNextObjectHint;
1376 do {
1377 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1378 // Requested info overlaps with an object
1379 ALOGE("Attempt to read from protected data in Parcel %p", this);
1380 return PERMISSION_DENIED;
1381 }
1382 nextObject++;
1383 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1384 mNextObjectHint = nextObject;
1385 }
1386 return NO_ERROR;
1387 }
1388 // Quickly determine if mObjects is sorted.
1389 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1390 binder_size_t* prevObj = currObj;
1391 while (currObj > mObjects) {
1392 prevObj--;
1393 if(*prevObj > *currObj) {
1394 goto data_unsorted;
1395 }
1396 currObj--;
1397 }
1398 mObjectsSorted = true;
1399 goto data_sorted;
1400
1401data_unsorted:
1402 // Insertion Sort mObjects
1403 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1404 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1405 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1406 binder_size_t temp = *iter0;
1407 binder_size_t* iter1 = iter0 - 1;
1408 while (iter1 >= mObjects && *iter1 > temp) {
1409 *(iter1 + 1) = *iter1;
1410 iter1--;
1411 }
1412 *(iter1 + 1) = temp;
1413 }
1414 mNextObjectHint = 0;
1415 mObjectsSorted = true;
1416 goto data_sorted;
1417}
1418
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001419status_t Parcel::read(void* outData, size_t len) const
1420{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001421 if (len > INT32_MAX) {
1422 // don't accept size_t values which may have come from an
1423 // inadvertent conversion from a negative int.
1424 return BAD_VALUE;
1425 }
1426
1427 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1428 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001429 if (mObjectsSize > 0) {
1430 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001431 if(err != NO_ERROR) {
1432 // Still increment the data position by the expected length
1433 mDataPos += pad_size(len);
1434 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1435 return err;
1436 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001437 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001438 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001439 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001440 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001441 return NO_ERROR;
1442 }
1443 return NOT_ENOUGH_DATA;
1444}
1445
1446const void* Parcel::readInplace(size_t len) const
1447{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001448 if (len > INT32_MAX) {
1449 // don't accept size_t values which may have come from an
1450 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001451 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001452 }
1453
1454 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1455 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001456 if (mObjectsSize > 0) {
1457 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001458 if(err != NO_ERROR) {
1459 // Still increment the data position by the expected length
1460 mDataPos += pad_size(len);
1461 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001462 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001463 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001464 }
1465
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001466 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001467 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001468 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001469 return data;
1470 }
Yi Kong91635562018-06-07 14:38:36 -07001471 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001472}
1473
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001474status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1475 if (status_t status = readInt32(size); status != OK) return status;
1476 if (*size < 0) return OK; // may be null, client to handle
1477
1478 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1479
1480 // approximation, can't know max element size (e.g. if it makes heap
1481 // allocations)
1482 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1483 int32_t allocationSize;
1484 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1485
1486 // High limit of 1MB since something this big could never be returned. Could
1487 // probably scope this down, but might impact very specific usecases.
1488 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1489
1490 if (allocationSize >= kMaxAllocationSize) {
1491 return NO_MEMORY;
1492 }
1493
1494 return OK;
1495}
1496
Andreas Huber84a6d042009-08-17 13:33:27 -07001497template<class T>
1498status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001499 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001500
1501 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001502 if (mObjectsSize > 0) {
1503 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001504 if(err != NO_ERROR) {
1505 // Still increment the data position by the expected length
1506 mDataPos += sizeof(T);
1507 return err;
1508 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001509 }
1510
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001511 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001512 mDataPos += sizeof(T);
1513 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001514 return NO_ERROR;
1515 } else {
1516 return NOT_ENOUGH_DATA;
1517 }
1518}
1519
Andreas Huber84a6d042009-08-17 13:33:27 -07001520template<class T>
1521T Parcel::readAligned() const {
1522 T result;
1523 if (readAligned(&result) != NO_ERROR) {
1524 result = 0;
1525 }
1526
1527 return result;
1528}
1529
1530template<class T>
1531status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001532 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001533
1534 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1535restart_write:
1536 *reinterpret_cast<T*>(mData+mDataPos) = val;
1537 return finishWrite(sizeof(val));
1538 }
1539
1540 status_t err = growData(sizeof(val));
1541 if (err == NO_ERROR) goto restart_write;
1542 return err;
1543}
1544
1545status_t Parcel::readInt32(int32_t *pArg) const
1546{
1547 return readAligned(pArg);
1548}
1549
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001550int32_t Parcel::readInt32() const
1551{
Andreas Huber84a6d042009-08-17 13:33:27 -07001552 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001553}
1554
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001555status_t Parcel::readUint32(uint32_t *pArg) const
1556{
1557 return readAligned(pArg);
1558}
1559
1560uint32_t Parcel::readUint32() const
1561{
1562 return readAligned<uint32_t>();
1563}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001564
1565status_t Parcel::readInt64(int64_t *pArg) const
1566{
Andreas Huber84a6d042009-08-17 13:33:27 -07001567 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001568}
1569
1570
1571int64_t Parcel::readInt64() const
1572{
Andreas Huber84a6d042009-08-17 13:33:27 -07001573 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001574}
1575
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001576status_t Parcel::readUint64(uint64_t *pArg) const
1577{
1578 return readAligned(pArg);
1579}
1580
1581uint64_t Parcel::readUint64() const
1582{
1583 return readAligned<uint64_t>();
1584}
1585
Serban Constantinescuf683e012013-11-05 16:53:55 +00001586status_t Parcel::readPointer(uintptr_t *pArg) const
1587{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001588 status_t ret;
1589 binder_uintptr_t ptr;
1590 ret = readAligned(&ptr);
1591 if (!ret)
1592 *pArg = ptr;
1593 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001594}
1595
1596uintptr_t Parcel::readPointer() const
1597{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001598 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001599}
1600
1601
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001602status_t Parcel::readFloat(float *pArg) const
1603{
Andreas Huber84a6d042009-08-17 13:33:27 -07001604 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001605}
1606
1607
1608float Parcel::readFloat() const
1609{
Andreas Huber84a6d042009-08-17 13:33:27 -07001610 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001611}
1612
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001613#if defined(__mips__) && defined(__mips_hard_float)
1614
1615status_t Parcel::readDouble(double *pArg) const
1616{
1617 union {
1618 double d;
1619 unsigned long long ll;
1620 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001621 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001622 status_t status;
1623 status = readAligned(&u.ll);
1624 *pArg = u.d;
1625 return status;
1626}
1627
1628double Parcel::readDouble() const
1629{
1630 union {
1631 double d;
1632 unsigned long long ll;
1633 } u;
1634 u.ll = readAligned<unsigned long long>();
1635 return u.d;
1636}
1637
1638#else
1639
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001640status_t Parcel::readDouble(double *pArg) const
1641{
Andreas Huber84a6d042009-08-17 13:33:27 -07001642 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001643}
1644
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001645double Parcel::readDouble() const
1646{
Andreas Huber84a6d042009-08-17 13:33:27 -07001647 return readAligned<double>();
1648}
1649
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001650#endif
1651
Casey Dahlind6848f52015-10-15 15:44:59 -07001652status_t Parcel::readBool(bool *pArg) const
1653{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001654 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001655 status_t ret = readInt32(&tmp);
1656 *pArg = (tmp != 0);
1657 return ret;
1658}
1659
1660bool Parcel::readBool() const
1661{
1662 return readInt32() != 0;
1663}
1664
1665status_t Parcel::readChar(char16_t *pArg) const
1666{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001667 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001668 status_t ret = readInt32(&tmp);
1669 *pArg = char16_t(tmp);
1670 return ret;
1671}
1672
1673char16_t Parcel::readChar() const
1674{
1675 return char16_t(readInt32());
1676}
1677
1678status_t Parcel::readByte(int8_t *pArg) const
1679{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001680 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001681 status_t ret = readInt32(&tmp);
1682 *pArg = int8_t(tmp);
1683 return ret;
1684}
1685
1686int8_t Parcel::readByte() const
1687{
1688 return int8_t(readInt32());
1689}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001690
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001691status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1692 size_t utf16Size = 0;
1693 const char16_t* src = readString16Inplace(&utf16Size);
1694 if (!src) {
1695 return UNEXPECTED_NULL;
1696 }
1697
1698 // Save ourselves the trouble, we're done.
1699 if (utf16Size == 0u) {
1700 str->clear();
1701 return NO_ERROR;
1702 }
1703
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001704 // Allow for closing '\0'
1705 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1706 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001707 return BAD_VALUE;
1708 }
1709 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001710 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001711 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001712 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1713 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001714 return NO_ERROR;
1715}
1716
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001717const char* Parcel::readCString() const
1718{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001719 if (mDataPos < mDataSize) {
1720 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001721 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1722 // is the string's trailing NUL within the parcel's valid bounds?
1723 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1724 if (eos) {
1725 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001726 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001727 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001728 return str;
1729 }
1730 }
Yi Kong91635562018-06-07 14:38:36 -07001731 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001732}
1733
1734String8 Parcel::readString8() const
1735{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001736 size_t len;
1737 const char* str = readString8Inplace(&len);
1738 if (str) return String8(str, len);
1739 ALOGE("Reading a NULL string not supported here.");
1740 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07001741}
1742
1743status_t Parcel::readString8(String8* pArg) const
1744{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001745 size_t len;
1746 const char* str = readString8Inplace(&len);
1747 if (str) {
1748 pArg->setTo(str, len);
1749 return 0;
1750 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07001751 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001752 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07001753 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001754}
1755
1756const char* Parcel::readString8Inplace(size_t* outLen) const
1757{
1758 int32_t size = readInt32();
1759 // watch for potential int overflow from size+1
1760 if (size >= 0 && size < INT32_MAX) {
1761 *outLen = size;
1762 const char* str = (const char*)readInplace(size+1);
1763 if (str != nullptr) {
Steven Moreland61d0f842020-12-04 21:13:03 +00001764 if (str[size] == '\0') {
1765 return str;
1766 }
1767 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001768 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001769 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001770 *outLen = 0;
1771 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001772}
1773
1774String16 Parcel::readString16() const
1775{
1776 size_t len;
1777 const char16_t* str = readString16Inplace(&len);
1778 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001779 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001780 return String16();
1781}
1782
Casey Dahlinb9872622015-11-25 15:09:45 -08001783
Casey Dahlin451ff582015-10-19 18:12:18 -07001784status_t Parcel::readString16(String16* pArg) const
1785{
1786 size_t len;
1787 const char16_t* str = readString16Inplace(&len);
1788 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001789 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001790 return 0;
1791 } else {
1792 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001793 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001794 }
1795}
1796
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001797const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1798{
1799 int32_t size = readInt32();
1800 // watch for potential int overflow from size+1
1801 if (size >= 0 && size < INT32_MAX) {
1802 *outLen = size;
1803 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001804 if (str != nullptr) {
Steven Moreland61d0f842020-12-04 21:13:03 +00001805 if (str[size] == u'\0') {
1806 return str;
1807 }
1808 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001809 }
1810 }
1811 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001812 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001813}
1814
Casey Dahlinf0c13772015-10-27 18:33:56 -07001815status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1816{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001817 status_t status = readNullableStrongBinder(val);
1818 if (status == OK && !val->get()) {
1819 status = UNEXPECTED_NULL;
1820 }
1821 return status;
1822}
1823
1824status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1825{
Steven Morelanda86a3562019-08-01 23:28:34 +00001826 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001827}
1828
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001829sp<IBinder> Parcel::readStrongBinder() const
1830{
1831 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001832 // Note that a lot of code in Android reads binders by hand with this
1833 // method, and that code has historically been ok with getting nullptr
1834 // back (while ignoring error codes).
1835 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001836 return val;
1837}
1838
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001839int32_t Parcel::readExceptionCode() const
1840{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001841 binder::Status status;
1842 status.readFromParcel(*this);
1843 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001844}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001845
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001846native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001847{
1848 int numFds, numInts;
1849 status_t err;
1850 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001851 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001852 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001853 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001854
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001855 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001856 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001857 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001858 }
1859
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001860 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001861 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001862 if (h->data[i] < 0) {
1863 for (int j = 0; j < i; j++) {
1864 close(h->data[j]);
1865 }
1866 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001867 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001868 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001869 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001870 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001871 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001872 native_handle_close(h);
1873 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001874 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001875 }
1876 return h;
1877}
1878
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001879int Parcel::readFileDescriptor() const
1880{
1881 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001882
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001883 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001884 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001885 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001886
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001887 return BAD_TYPE;
1888}
1889
Dianne Hackborn1941a402016-08-29 12:30:43 -07001890int Parcel::readParcelFileDescriptor() const
1891{
1892 int32_t hasComm = readInt32();
1893 int fd = readFileDescriptor();
1894 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001895 // detach (owned by the binder driver)
1896 int comm = readFileDescriptor();
1897
1898 // warning: this must be kept in sync with:
1899 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1900 enum ParcelFileDescriptorStatus {
1901 DETACHED = 2,
1902 };
1903
1904#if BYTE_ORDER == BIG_ENDIAN
1905 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
1906#endif
1907#if BYTE_ORDER == LITTLE_ENDIAN
1908 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
1909#endif
1910
1911 ssize_t written = TEMP_FAILURE_RETRY(
1912 ::write(comm, &message, sizeof(message)));
1913
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08001914 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001915 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
1916 written, strerror(errno));
1917 return BAD_TYPE;
1918 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07001919 }
1920 return fd;
1921}
1922
Christopher Wiley2cf19952016-04-11 11:09:37 -07001923status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08001924{
1925 int got = readFileDescriptor();
1926
1927 if (got == BAD_TYPE) {
1928 return BAD_TYPE;
1929 }
1930
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001931 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08001932
1933 if (val->get() < 0) {
1934 return BAD_VALUE;
1935 }
1936
1937 return OK;
1938}
1939
Ryo Hashimotobf551892018-05-31 16:58:35 +09001940status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
1941{
1942 int got = readParcelFileDescriptor();
1943
1944 if (got == BAD_TYPE) {
1945 return BAD_TYPE;
1946 }
1947
1948 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
1949
1950 if (val->get() < 0) {
1951 return BAD_VALUE;
1952 }
1953
1954 return OK;
1955}
Casey Dahlin06673e32015-11-23 13:24:23 -08001956
Jeff Brown5707dbf2011-09-23 21:17:56 -07001957status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1958{
Jeff Brown13b16042014-11-11 16:44:25 -08001959 int32_t blobType;
1960 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001961 if (status) return status;
1962
Jeff Brown13b16042014-11-11 16:44:25 -08001963 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001964 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001965 const void* ptr = readInplace(len);
1966 if (!ptr) return BAD_VALUE;
1967
Jeff Brown13b16042014-11-11 16:44:25 -08001968 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001969 return NO_ERROR;
1970 }
1971
Steve Block6807e592011-10-20 11:56:00 +01001972 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001973 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001974 int fd = readFileDescriptor();
1975 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1976
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001977 if (!ashmem_valid(fd)) {
1978 ALOGE("invalid fd");
1979 return BAD_VALUE;
1980 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001981 int size = ashmem_get_size_region(fd);
1982 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001983 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001984 return BAD_VALUE;
1985 }
Yi Kong91635562018-06-07 14:38:36 -07001986 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08001987 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001988 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001989
Jeff Brown13b16042014-11-11 16:44:25 -08001990 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001991 return NO_ERROR;
1992}
1993
Mathias Agopiane1424282013-07-29 21:24:40 -07001994status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001995{
1996 // size
1997 const size_t len = this->readInt32();
1998 const size_t fd_count = this->readInt32();
1999
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002000 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002001 // don't accept size_t values which may have come from an
2002 // inadvertent conversion from a negative int.
2003 return BAD_VALUE;
2004 }
2005
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002006 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002007 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002008 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002009 return BAD_VALUE;
2010
Yi Kong91635562018-06-07 14:38:36 -07002011 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002012 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002013 fds = new (std::nothrow) int[fd_count];
2014 if (fds == nullptr) {
2015 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2016 return BAD_VALUE;
2017 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002018 }
2019
2020 status_t err = NO_ERROR;
2021 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002022 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002023 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002024 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002025 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 -07002026 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2027 // Close all the file descriptors that were dup-ed.
2028 for (size_t j=0; j<i ;j++) {
2029 close(fds[j]);
2030 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002031 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002032 }
2033
2034 if (err == NO_ERROR) {
2035 err = val.unflatten(buf, len, fds, fd_count);
2036 }
2037
2038 if (fd_count) {
2039 delete [] fds;
2040 }
2041
2042 return err;
2043}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002044const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2045{
2046 const size_t DPOS = mDataPos;
2047 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2048 const flat_binder_object* obj
2049 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2050 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002051 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002052 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002053 // the object list, so we don't want to check for it when
2054 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002055 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002056 return obj;
2057 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002058
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002059 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002060 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002061 const size_t N = mObjectsSize;
2062 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002063
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002064 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002065 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002066 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002067
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002068 // Start at the current hint position, looking for an object at
2069 // the current data position.
2070 if (opos < N) {
2071 while (opos < (N-1) && OBJS[opos] < DPOS) {
2072 opos++;
2073 }
2074 } else {
2075 opos = N-1;
2076 }
2077 if (OBJS[opos] == DPOS) {
2078 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002079 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002080 this, DPOS, opos);
2081 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002082 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002083 return obj;
2084 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002085
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002086 // Look backwards for it...
2087 while (opos > 0 && OBJS[opos] > DPOS) {
2088 opos--;
2089 }
2090 if (OBJS[opos] == DPOS) {
2091 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002092 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002093 this, DPOS, opos);
2094 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002095 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002096 return obj;
2097 }
2098 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002099 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 -07002100 this, DPOS);
2101 }
Yi Kong91635562018-06-07 14:38:36 -07002102 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002103}
2104
2105void Parcel::closeFileDescriptors()
2106{
2107 size_t i = mObjectsSize;
2108 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002109 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002110 }
2111 while (i > 0) {
2112 i--;
2113 const flat_binder_object* flat
2114 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002115 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002116 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002117 close(flat->handle);
2118 }
2119 }
2120}
2121
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002122uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002123{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002124 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002125}
2126
2127size_t Parcel::ipcDataSize() const
2128{
2129 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2130}
2131
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002132uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002133{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002134 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002135}
2136
2137size_t Parcel::ipcObjectsCount() const
2138{
2139 return mObjectsSize;
2140}
2141
2142void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Steven Moreland161fe122020-11-12 23:16:47 +00002143 const binder_size_t* objects, size_t objectsCount, release_func relFunc)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002144{
Steven Moreland438cce82021-04-02 18:04:08 +00002145 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2146 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2147
Steven Morelandceed9bb2020-12-17 01:01:06 +00002148 freeData();
2149
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002150 mData = const_cast<uint8_t*>(data);
2151 mDataSize = mDataCapacity = dataSize;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002152 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002153 mObjectsSize = mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002154 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002155
2156 binder_size_t minOffset = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002157 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002158 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002159 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002160 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002161 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002162 mObjectsSize = 0;
2163 break;
2164 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002165 const flat_binder_object* flat
2166 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2167 uint32_t type = flat->hdr.type;
2168 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2169 type == BINDER_TYPE_FD)) {
2170 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2171 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2172 // recover gracefully by clearing out the objects, and releasing the objects we do
2173 // know about.
2174 android_errorWriteLog(0x534e4554, "135930648");
2175 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2176 __func__, type, (uint64_t)offset);
2177 releaseObjects();
2178 mObjectsSize = 0;
2179 break;
2180 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002181 minOffset = offset + sizeof(flat_binder_object);
2182 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002183 scanForFds();
2184}
2185
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002186void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002187{
2188 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002189
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002190 if (errorCheck() != NO_ERROR) {
2191 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002192 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002193 } else if (dataSize() > 0) {
2194 const uint8_t* DATA = data();
2195 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002196 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002197 const size_t N = objectsCount();
2198 for (size_t i=0; i<N; i++) {
2199 const flat_binder_object* flat
2200 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2201 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002202 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002203 << " = " << flat->binder;
2204 }
2205 } else {
2206 to << "NULL";
2207 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002208
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002209 to << ")";
2210}
2211
2212void Parcel::releaseObjects()
2213{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002214 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002215 if (i == 0) {
2216 return;
2217 }
2218 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002219 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002220 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002221 while (i > 0) {
2222 i--;
2223 const flat_binder_object* flat
2224 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002225 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002226 }
2227}
2228
2229void Parcel::acquireObjects()
2230{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002232 if (i == 0) {
2233 return;
2234 }
2235 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002236 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002237 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002238 while (i > 0) {
2239 i--;
2240 const flat_binder_object* flat
2241 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002242 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002243 }
2244}
2245
2246void Parcel::freeData()
2247{
2248 freeDataNoInit();
2249 initState();
2250}
2251
2252void Parcel::freeDataNoInit()
2253{
2254 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002255 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002256 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002257 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002258 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002259 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002260 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002261 if (mData) {
2262 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002263 gParcelGlobalAllocSize -= mDataCapacity;
2264 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002265 if (mDeallocZero) {
2266 zeroMemory(mData, mDataSize);
2267 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002268 free(mData);
2269 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002270 if (mObjects) free(mObjects);
2271 }
2272}
2273
2274status_t Parcel::growData(size_t len)
2275{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002276 if (len > INT32_MAX) {
2277 // don't accept size_t values which may have come from an
2278 // inadvertent conversion from a negative int.
2279 return BAD_VALUE;
2280 }
2281
Martijn Coenen93fe5182020-01-22 10:46:25 +01002282 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2283 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002284 size_t newSize = ((mDataSize+len)*3)/2;
2285 return (newSize <= mDataSize)
2286 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002287 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002288}
2289
Steven Morelandf183fdd2020-10-27 00:12:12 +00002290static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2291 if (!zero) {
2292 return (uint8_t*)realloc(data, newCapacity);
2293 }
2294 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2295 if (!newData) {
2296 return nullptr;
2297 }
2298
2299 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2300 zeroMemory(data, oldCapacity);
2301 free(data);
2302 return newData;
2303}
2304
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002305status_t Parcel::restartWrite(size_t desired)
2306{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002307 if (desired > INT32_MAX) {
2308 // don't accept size_t values which may have come from an
2309 // inadvertent conversion from a negative int.
2310 return BAD_VALUE;
2311 }
2312
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002313 if (mOwner) {
2314 freeData();
2315 return continueWrite(desired);
2316 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002317
Steven Morelandf183fdd2020-10-27 00:12:12 +00002318 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002319 if (!data && desired > mDataCapacity) {
2320 mError = NO_MEMORY;
2321 return NO_MEMORY;
2322 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002323
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002324 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002325
Devin Moore4a0a55e2020-06-04 13:23:10 -07002326 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002327 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002328 if (mDataCapacity > desired) {
2329 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2330 } else {
2331 gParcelGlobalAllocSize += (desired - mDataCapacity);
2332 }
2333
Colin Cross83ec65e2015-12-08 17:15:50 -08002334 if (!mData) {
2335 gParcelGlobalAllocCount++;
2336 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002337 mData = data;
2338 mDataCapacity = desired;
2339 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002340
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002341 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002342 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2343 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2344
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002345 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002346 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002347 mObjectsSize = mObjectsCapacity = 0;
2348 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002349 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002350 mHasFds = false;
2351 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002352 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002353
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002354 return NO_ERROR;
2355}
2356
2357status_t Parcel::continueWrite(size_t desired)
2358{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002359 if (desired > INT32_MAX) {
2360 // don't accept size_t values which may have come from an
2361 // inadvertent conversion from a negative int.
2362 return BAD_VALUE;
2363 }
2364
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002365 // If shrinking, first adjust for any objects that appear
2366 // after the new data size.
2367 size_t objectsSize = mObjectsSize;
2368 if (desired < mDataSize) {
2369 if (desired == 0) {
2370 objectsSize = 0;
2371 } else {
2372 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002373 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002374 break;
2375 objectsSize--;
2376 }
2377 }
2378 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002379
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002380 if (mOwner) {
2381 // If the size is going to zero, just release the owner's data.
2382 if (desired == 0) {
2383 freeData();
2384 return NO_ERROR;
2385 }
2386
2387 // If there is a different owner, we need to take
2388 // posession.
2389 uint8_t* data = (uint8_t*)malloc(desired);
2390 if (!data) {
2391 mError = NO_MEMORY;
2392 return NO_MEMORY;
2393 }
Yi Kong91635562018-06-07 14:38:36 -07002394 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002395
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002396 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002397 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002398 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002399 free(data);
2400
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002401 mError = NO_MEMORY;
2402 return NO_MEMORY;
2403 }
2404
2405 // Little hack to only acquire references on objects
2406 // we will be keeping.
2407 size_t oldObjectsSize = mObjectsSize;
2408 mObjectsSize = objectsSize;
2409 acquireObjects();
2410 mObjectsSize = oldObjectsSize;
2411 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002412
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002413 if (mData) {
2414 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2415 }
2416 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002417 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002418 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002419 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002420 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
Yi Kong91635562018-06-07 14:38:36 -07002421 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002422
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002423 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002424 gParcelGlobalAllocSize += desired;
2425 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002426
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427 mData = data;
2428 mObjects = objects;
2429 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002430 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002431 mDataCapacity = desired;
2432 mObjectsSize = mObjectsCapacity = objectsSize;
2433 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002434 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435
2436 } else if (mData) {
2437 if (objectsSize < mObjectsSize) {
2438 // Need to release refs on any objects we are dropping.
2439 const sp<ProcessState> proc(ProcessState::self());
2440 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2441 const flat_binder_object* flat
2442 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002443 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002444 // will need to rescan because we may have lopped off the only FDs
2445 mFdsKnown = false;
2446 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002447 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002448 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002449
2450 if (objectsSize == 0) {
2451 free(mObjects);
2452 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002453 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002454 } else {
2455 binder_size_t* objects =
2456 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2457 if (objects) {
2458 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002459 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002460 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002461 }
2462 mObjectsSize = objectsSize;
2463 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002464 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002465 }
2466
2467 // We own the data, so we can just do a realloc().
2468 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002469 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002470 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002471 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2472 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002473 gParcelGlobalAllocSize += desired;
2474 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002475 mData = data;
2476 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002477 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002478 mError = NO_MEMORY;
2479 return NO_MEMORY;
2480 }
2481 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002482 if (mDataSize > desired) {
2483 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002484 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002485 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002486 if (mDataPos > desired) {
2487 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002488 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002489 }
2490 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002491
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002492 } else {
2493 // This is the first data. Easy!
2494 uint8_t* data = (uint8_t*)malloc(desired);
2495 if (!data) {
2496 mError = NO_MEMORY;
2497 return NO_MEMORY;
2498 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002499
Yi Kong91635562018-06-07 14:38:36 -07002500 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002501 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002502 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002503 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002504
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002505 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002506 gParcelGlobalAllocSize += desired;
2507 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002508
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002509 mData = data;
2510 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002511 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2512 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513 mDataCapacity = desired;
2514 }
2515
2516 return NO_ERROR;
2517}
2518
2519void Parcel::initState()
2520{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002521 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002522 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002523 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002524 mDataSize = 0;
2525 mDataCapacity = 0;
2526 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002527 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2528 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Steven Morelandbdb53ab2021-05-05 17:57:41 +00002529 mSession = nullptr;
Yi Kong91635562018-06-07 14:38:36 -07002530 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002531 mObjectsSize = 0;
2532 mObjectsCapacity = 0;
2533 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002534 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002535 mHasFds = false;
2536 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002537 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002538 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07002539 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002540 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002541 mWorkSourceRequestHeaderPosition = 0;
2542 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002543
2544 // racing multiple init leads only to multiple identical write
2545 if (gMaxFds == 0) {
2546 struct rlimit result;
2547 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2548 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002549 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002550 } else {
2551 ALOGW("Unable to getrlimit: %s", strerror(errno));
2552 gMaxFds = 1024;
2553 }
2554 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002555}
2556
2557void Parcel::scanForFds() const
2558{
2559 bool hasFds = false;
2560 for (size_t i=0; i<mObjectsSize; i++) {
2561 const flat_binder_object* flat
2562 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002563 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002564 hasFds = true;
2565 break;
2566 }
2567 }
2568 mHasFds = hasFds;
2569 mFdsKnown = true;
2570}
2571
Dan Sandleraa5c2342015-04-10 10:08:45 -04002572size_t Parcel::getBlobAshmemSize() const
2573{
Adrian Roos6bb31142015-10-22 16:46:12 -07002574 // This used to return the size of all blobs that were written to ashmem, now we're returning
2575 // the ashmem currently referenced by this Parcel, which should be equivalent.
2576 // TODO: Remove method once ABI can be changed.
2577 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002578}
2579
Adrian Rooscbf37262015-10-22 16:12:53 -07002580size_t Parcel::getOpenAshmemSize() const
2581{
2582 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002583}
2584
2585// --- Parcel::Blob ---
2586
2587Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002588 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002589}
2590
2591Parcel::Blob::~Blob() {
2592 release();
2593}
2594
2595void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002596 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002597 ::munmap(mData, mSize);
2598 }
2599 clear();
2600}
2601
Jeff Brown13b16042014-11-11 16:44:25 -08002602void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2603 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002604 mData = data;
2605 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002606 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002607}
2608
2609void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002610 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002611 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002612 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002613 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002614}
2615
Steven Moreland61ff8492019-09-26 16:05:45 -07002616} // namespace android