blob: 038ce381e25c9f091302eea9ec692d902467463a [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()
Andrei Homescuf7f2c172022-03-08 22:52:49 +000066#define PAD_SIZE_UNSAFE(s) (((s) + 3) & ~3UL)
Nick Kralevichb6b14232015-04-02 09:36:02 -070067
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 Morelandc673f1f2021-10-07 18:23:35 -0700101static void acquire_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
102 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700103 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104 case BINDER_TYPE_BINDER:
105 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000106 LOG_REFS("Parcel %p acquiring reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800107 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108 }
109 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700110 case BINDER_TYPE_HANDLE: {
111 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700112 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700113 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
114 b->incStrong(who);
115 }
116 return;
117 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700118 case BINDER_TYPE_FD: {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700119 return;
120 }
121 }
122
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700123 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700124}
125
Steven Morelandc673f1f2021-10-07 18:23:35 -0700126static void release_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
127 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700128 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700129 case BINDER_TYPE_BINDER:
130 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000131 LOG_REFS("Parcel %p releasing reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800132 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700133 }
134 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700135 case BINDER_TYPE_HANDLE: {
136 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700137 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700138 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
139 b->decStrong(who);
140 }
141 return;
142 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700143 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800144 if (obj.cookie != 0) { // owned
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800145 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700146 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 return;
148 }
149 }
150
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700151 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700152}
153
Frederick Mayled3c595a2022-05-09 23:02:54 +0000154Parcel::RpcFields::RpcFields(const sp<RpcSession>& session) : mSession(session) {
155 LOG_ALWAYS_FATAL_IF(mSession == nullptr);
156}
157
Steven Moreland34b48cb2020-12-01 22:45:38 +0000158status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700159{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700160 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland16a41062021-07-23 13:35:25 -0700161 int16_t rep = internal::Stability::getRepr(binder.get());
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000162 return writeInt32(rep);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700163}
164
Steven Morelanda86a3562019-08-01 23:28:34 +0000165status_t Parcel::finishUnflattenBinder(
166 const sp<IBinder>& binder, sp<IBinder>* out) const
167{
168 int32_t stability;
169 status_t status = readInt32(&stability);
170 if (status != OK) return status;
171
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000172 status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability),
173 true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000174 if (status != OK) return status;
175
176 *out = binder;
177 return OK;
178}
179
Steven Morelandbf1915b2020-07-16 22:43:02 +0000180static constexpr inline int schedPolicyMask(int policy, int priority) {
181 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
182}
183
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500184status_t Parcel::flattenBinder(const sp<IBinder>& binder) {
185 BBinder* local = nullptr;
186 if (binder) local = binder->localBinder();
187 if (local) local->setParceled();
188
Frederick Mayled3c595a2022-05-09 23:02:54 +0000189 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000190 if (binder) {
191 status_t status = writeInt32(1); // non-null
192 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700193 uint64_t address;
Steven Morelanda5036f02021-06-08 02:26:57 +0000194 // TODO(b/167966510): need to undo this if the Parcel is not sent
Frederick Mayled3c595a2022-05-09 23:02:54 +0000195 status = rpcFields->mSession->state()->onBinderLeaving(rpcFields->mSession, binder,
196 &address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000197 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700198 status = writeUint64(address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000199 if (status != OK) return status;
200 } else {
201 status_t status = writeInt32(0); // null
202 if (status != OK) return status;
203 }
204 return finishFlattenBinder(binder);
205 }
206
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700207 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700208
Steven Morelandbf1915b2020-07-16 22:43:02 +0000209 int schedBits = 0;
210 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
211 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700212 }
213
Yi Kong91635562018-06-07 14:38:36 -0700214 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700215 if (!local) {
216 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700217 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000218 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000219 } else {
220 if (proxy->isRpcBinder()) {
Steven Morelanda9231112021-09-22 10:08:14 -0700221 ALOGE("Sending a socket binder over kernel binder is prohibited");
Steven Moreland5553ac42020-11-11 02:14:45 +0000222 return INVALID_OPERATION;
223 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700224 }
Steven Moreland99157622021-09-13 16:27:34 -0700225 const int32_t handle = proxy ? proxy->getPrivateAccessor().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700226 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800227 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
Steven Moreland49c27532022-03-01 10:21:07 +0000228 obj.flags = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800230 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000232 int policy = local->getMinSchedulerPolicy();
233 int priority = local->getMinSchedulerPriority();
234
235 if (policy != 0 || priority != 0) {
236 // override value, since it is set explicitly
237 schedBits = schedPolicyMask(policy, priority);
238 }
Steven Moreland49c27532022-03-01 10:21:07 +0000239 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Steven Morelandf0212002018-12-26 13:59:23 -0800240 if (local->isRequestingSid()) {
241 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
242 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000243 if (local->isInheritRt()) {
244 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
245 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700246 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800247 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
248 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700249 }
250 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700251 obj.hdr.type = BINDER_TYPE_BINDER;
Steven Moreland49c27532022-03-01 10:21:07 +0000252 obj.flags = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800253 obj.binder = 0;
254 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700256
Steven Morelandbf1915b2020-07-16 22:43:02 +0000257 obj.flags |= schedBits;
258
Steven Moreland34b48cb2020-12-01 22:45:38 +0000259 status_t status = writeObject(obj, false);
260 if (status != OK) return status;
261
262 return finishFlattenBinder(binder);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700263}
264
Steven Morelanda86a3562019-08-01 23:28:34 +0000265status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700266{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000267 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5623d1a2021-09-10 15:45:34 -0700268 int32_t isPresent;
269 status_t status = readInt32(&isPresent);
Steven Moreland5553ac42020-11-11 02:14:45 +0000270 if (status != OK) return status;
271
272 sp<IBinder> binder;
273
Steven Moreland5623d1a2021-09-10 15:45:34 -0700274 if (isPresent & 1) {
275 uint64_t addr;
276 if (status_t status = readUint64(&addr); status != OK) return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000277 if (status_t status =
278 rpcFields->mSession->state()->onBinderEntering(rpcFields->mSession, addr,
279 &binder);
Steven Moreland7227c8a2021-06-02 00:24:32 +0000280 status != OK)
281 return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000282 if (status_t status =
283 rpcFields->mSession->state()->flushExcessBinderRefs(rpcFields->mSession,
284 addr, binder);
Steven Morelandd8083312021-09-22 13:37:10 -0700285 status != OK)
286 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000287 }
288
289 return finishUnflattenBinder(binder, out);
290 }
291
Steven Morelanda86a3562019-08-01 23:28:34 +0000292 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700293
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700294 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700295 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000296 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000297 sp<IBinder> binder =
298 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000299 return finishUnflattenBinder(binder, out);
300 }
301 case BINDER_TYPE_HANDLE: {
302 sp<IBinder> binder =
303 ProcessState::self()->getStrongProxyForHandle(flat->handle);
304 return finishUnflattenBinder(binder, out);
305 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700306 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700307 }
308 return BAD_TYPE;
309}
310
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700311// ---------------------------------------------------------------------------
312
313Parcel::Parcel()
314{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800315 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700316 initState();
317}
318
319Parcel::~Parcel()
320{
321 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800322 LOG_ALLOC("Parcel %p: destroyed", this);
323}
324
325size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600326 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800327}
328
329size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600330 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700331}
332
333const uint8_t* Parcel::data() const
334{
335 return mData;
336}
337
338size_t Parcel::dataSize() const
339{
340 return (mDataSize > mDataPos ? mDataSize : mDataPos);
341}
342
343size_t Parcel::dataAvail() const
344{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700345 size_t result = dataSize() - dataPosition();
346 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700347 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700348 }
349 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700350}
351
352size_t Parcel::dataPosition() const
353{
354 return mDataPos;
355}
356
357size_t Parcel::dataCapacity() const
358{
359 return mDataCapacity;
360}
361
362status_t Parcel::setDataSize(size_t size)
363{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700364 if (size > INT32_MAX) {
365 // don't accept size_t values which may have come from an
366 // inadvertent conversion from a negative int.
367 return BAD_VALUE;
368 }
369
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700370 status_t err;
371 err = continueWrite(size);
372 if (err == NO_ERROR) {
373 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700374 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700375 }
376 return err;
377}
378
379void Parcel::setDataPosition(size_t pos) const
380{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700381 if (pos > INT32_MAX) {
382 // don't accept size_t values which may have come from an
383 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700384 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700385 }
386
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700387 mDataPos = pos;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000388 if (const auto* kernelFields = maybeKernelFields()) {
389 kernelFields->mNextObjectHint = 0;
390 kernelFields->mObjectsSorted = false;
391 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700392}
393
394status_t Parcel::setDataCapacity(size_t size)
395{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700396 if (size > INT32_MAX) {
397 // don't accept size_t values which may have come from an
398 // inadvertent conversion from a negative int.
399 return BAD_VALUE;
400 }
401
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700402 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700403 return NO_ERROR;
404}
405
406status_t Parcel::setData(const uint8_t* buffer, size_t len)
407{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700408 if (len > INT32_MAX) {
409 // don't accept size_t values which may have come from an
410 // inadvertent conversion from a negative int.
411 return BAD_VALUE;
412 }
413
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700414 status_t err = restartWrite(len);
415 if (err == NO_ERROR) {
416 memcpy(const_cast<uint8_t*>(data()), buffer, len);
417 mDataSize = len;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000418 if (auto* kernelFields = maybeKernelFields()) {
419 kernelFields->mFdsKnown = false;
420 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700421 }
422 return err;
423}
424
Frederick Mayled3c595a2022-05-09 23:02:54 +0000425status_t Parcel::appendFrom(const Parcel* parcel, size_t offset, size_t len) {
426 if (isForRpc() != parcel->isForRpc()) {
Steven Moreland2034eff2021-10-13 11:24:35 -0700427 ALOGE("Cannot append Parcel from one context to another. They may be different formats, "
428 "and objects are specific to a context.");
Steven Moreland67753c32021-04-02 18:45:19 +0000429 return BAD_TYPE;
430 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000431 if (isForRpc() && maybeRpcFields()->mSession != parcel->maybeRpcFields()->mSession) {
432 ALOGE("Cannot append Parcels from different sessions");
433 return BAD_TYPE;
434 }
Steven Moreland67753c32021-04-02 18:45:19 +0000435
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700436 status_t err;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000437 const uint8_t* data = parcel->mData;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700438 int startPos = mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700439
440 if (len == 0) {
441 return NO_ERROR;
442 }
443
Nick Kralevichb6b14232015-04-02 09:36:02 -0700444 if (len > INT32_MAX) {
445 // don't accept size_t values which may have come from an
446 // inadvertent conversion from a negative int.
447 return BAD_VALUE;
448 }
449
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700450 // range checks against the source parcel size
451 if ((offset > parcel->mDataSize)
452 || (len > parcel->mDataSize)
453 || (offset + len > parcel->mDataSize)) {
454 return BAD_VALUE;
455 }
456
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700457 if ((mDataSize+len) > mDataCapacity) {
458 // grow data
459 err = growData(len);
460 if (err != NO_ERROR) {
461 return err;
462 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700463 }
464
465 // append data
466 memcpy(mData + mDataPos, data + offset, len);
467 mDataPos += len;
468 mDataSize += len;
469
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400470 err = NO_ERROR;
471
Frederick Mayled3c595a2022-05-09 23:02:54 +0000472 if (auto* kernelFields = maybeKernelFields()) {
473 auto* otherKernelFields = parcel->maybeKernelFields();
474 LOG_ALWAYS_FATAL_IF(otherKernelFields == nullptr);
475
476 const binder_size_t* objects = otherKernelFields->mObjects;
477 size_t size = otherKernelFields->mObjectsSize;
478 // Count objects in range
479 int firstIndex = -1, lastIndex = -2;
480 for (int i = 0; i < (int)size; i++) {
481 size_t off = objects[i];
482 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
483 if (firstIndex == -1) {
484 firstIndex = i;
485 }
486 lastIndex = i;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700487 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700488 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000489 int numObjects = lastIndex - firstIndex + 1;
490 if (numObjects > 0) {
491 const sp<ProcessState> proc(ProcessState::self());
492 // grow objects
493 if (kernelFields->mObjectsCapacity < kernelFields->mObjectsSize + numObjects) {
494 if ((size_t)numObjects > SIZE_MAX - kernelFields->mObjectsSize)
495 return NO_MEMORY; // overflow
496 if (kernelFields->mObjectsSize + numObjects > SIZE_MAX / 3)
497 return NO_MEMORY; // overflow
498 size_t newSize = ((kernelFields->mObjectsSize + numObjects) * 3) / 2;
499 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
500 binder_size_t* objects = (binder_size_t*)realloc(kernelFields->mObjects,
501 newSize * sizeof(binder_size_t));
502 if (objects == (binder_size_t*)nullptr) {
503 return NO_MEMORY;
504 }
505 kernelFields->mObjects = objects;
506 kernelFields->mObjectsCapacity = newSize;
507 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700508
Frederick Mayled3c595a2022-05-09 23:02:54 +0000509 // append and acquire objects
510 int idx = kernelFields->mObjectsSize;
511 for (int i = firstIndex; i <= lastIndex; i++) {
512 size_t off = objects[i] - offset + startPos;
513 kernelFields->mObjects[idx++] = off;
514 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700515
Frederick Mayled3c595a2022-05-09 23:02:54 +0000516 flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(mData + off);
517 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700518
Frederick Mayled3c595a2022-05-09 23:02:54 +0000519 if (flat->hdr.type == BINDER_TYPE_FD) {
520 // If this is a file descriptor, we need to dup it so the
521 // new Parcel now owns its own fd, and can declare that we
522 // officially know we have fds.
523 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
524 flat->cookie = 1;
525 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
526 if (!mAllowFds) {
527 err = FDS_NOT_ALLOWED;
528 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400529 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700530 }
531 }
532 }
533
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400534 return err;
535}
536
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700537int Parcel::compareData(const Parcel& other) {
538 size_t size = dataSize();
539 if (size != other.dataSize()) {
540 return size < other.dataSize() ? -1 : 1;
541 }
542 return memcmp(data(), other.data(), size);
543}
544
Bernardo Rufino897b1652021-10-08 10:30:20 +0100545status_t Parcel::compareDataInRange(size_t thisOffset, const Parcel& other, size_t otherOffset,
546 size_t len, int* result) const {
547 if (len > INT32_MAX || thisOffset > INT32_MAX || otherOffset > INT32_MAX) {
548 // Don't accept size_t values which may have come from an inadvertent conversion from a
549 // negative int.
550 return BAD_VALUE;
551 }
552 size_t thisLimit;
553 if (__builtin_add_overflow(thisOffset, len, &thisLimit) || thisLimit > mDataSize) {
554 return BAD_VALUE;
555 }
556 size_t otherLimit;
557 if (__builtin_add_overflow(otherOffset, len, &otherLimit) || otherLimit > other.mDataSize) {
558 return BAD_VALUE;
559 }
560 *result = memcmp(data() + thisOffset, other.data() + otherOffset, len);
561 return NO_ERROR;
562}
563
Jeff Brown13b16042014-11-11 16:44:25 -0800564bool Parcel::allowFds() const
565{
566 return mAllowFds;
567}
568
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700569bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400570{
571 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700572 if (!allowFds) {
573 mAllowFds = false;
574 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400575 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700576}
577
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700578void Parcel::restoreAllowFds(bool lastValue)
579{
580 mAllowFds = lastValue;
581}
582
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700583bool Parcel::hasFileDescriptors() const
584{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000585 if (const auto* rpcFields = maybeRpcFields()) {
586 return false;
587 }
588 auto* kernelFields = maybeKernelFields();
589 if (!kernelFields->mFdsKnown) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700590 scanForFds();
591 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000592 return kernelFields->mHasFds;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700593}
594
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000595std::vector<sp<IBinder>> Parcel::debugReadAllStrongBinders() const {
596 std::vector<sp<IBinder>> ret;
597
Frederick Mayled3c595a2022-05-09 23:02:54 +0000598 const auto* kernelFields = maybeKernelFields();
599 if (kernelFields == nullptr) {
600 return ret;
601 }
602
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000603 size_t initPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000604 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
605 binder_size_t offset = kernelFields->mObjects[i];
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000606 const flat_binder_object* flat =
607 reinterpret_cast<const flat_binder_object*>(mData + offset);
608 if (flat->hdr.type != BINDER_TYPE_BINDER) continue;
609
610 setDataPosition(offset);
611
612 sp<IBinder> binder = readStrongBinder();
613 if (binder != nullptr) ret.push_back(binder);
614 }
615
616 setDataPosition(initPosition);
617 return ret;
618}
619
620std::vector<int> Parcel::debugReadAllFileDescriptors() const {
621 std::vector<int> ret;
622
Frederick Mayled3c595a2022-05-09 23:02:54 +0000623 const auto* kernelFields = maybeKernelFields();
624 if (kernelFields == nullptr) {
625 return ret;
626 }
627
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000628 size_t initPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000629 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
630 binder_size_t offset = kernelFields->mObjects[i];
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000631 const flat_binder_object* flat =
632 reinterpret_cast<const flat_binder_object*>(mData + offset);
633 if (flat->hdr.type != BINDER_TYPE_FD) continue;
634
635 setDataPosition(offset);
636
637 int fd = readFileDescriptor();
638 LOG_ALWAYS_FATAL_IF(fd == -1);
639 ret.push_back(fd);
640 }
641
642 setDataPosition(initPosition);
643 return ret;
644}
645
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100646status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000647 const auto* kernelFields = maybeKernelFields();
648 if (kernelFields == nullptr) {
649 return BAD_TYPE;
650 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100651 if (len > INT32_MAX || offset > INT32_MAX) {
652 // Don't accept size_t values which may have come from an inadvertent conversion from a
653 // negative int.
654 return BAD_VALUE;
655 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100656 size_t limit;
657 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100658 return BAD_VALUE;
659 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100660 *result = false;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000661 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
662 size_t pos = kernelFields->mObjects[i];
Bernardo Rufino22092af2021-10-07 14:09:24 +0100663 if (pos < offset) continue;
664 if (pos + sizeof(flat_binder_object) > offset + len) {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000665 if (kernelFields->mObjectsSorted) {
666 break;
667 } else {
668 continue;
669 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100670 }
671 const flat_binder_object* flat = reinterpret_cast<const flat_binder_object*>(mData + pos);
672 if (flat->hdr.type == BINDER_TYPE_FD) {
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100673 *result = true;
674 break;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100675 }
676 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100677 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100678}
679
Steven Morelandf183fdd2020-10-27 00:12:12 +0000680void Parcel::markSensitive() const
681{
682 mDeallocZero = true;
683}
684
Steven Moreland5553ac42020-11-11 02:14:45 +0000685void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000686 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
687
Steven Moreland5553ac42020-11-11 02:14:45 +0000688 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700689 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000690 }
691}
692
Steven Morelandc9939062021-05-05 17:57:41 +0000693void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000694 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
695 "format must be set before data is written OR on IPC data");
696
Frederick Mayled3c595a2022-05-09 23:02:54 +0000697 mVariantFields.emplace<RpcFields>(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000698}
699
700bool Parcel::isForRpc() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000701 return std::holds_alternative<RpcFields>(mVariantFields);
Steven Moreland5553ac42020-11-11 02:14:45 +0000702}
703
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000704void Parcel::updateWorkSourceRequestHeaderPosition() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000705 auto* kernelFields = maybeKernelFields();
706 if (kernelFields == nullptr) {
707 return;
708 }
709
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000710 // Only update the request headers once. We only want to point
711 // to the first headers read/written.
Frederick Mayled3c595a2022-05-09 23:02:54 +0000712 if (!kernelFields->mRequestHeaderPresent) {
713 kernelFields->mWorkSourceRequestHeaderPosition = dataPosition();
714 kernelFields->mRequestHeaderPresent = true;
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000715 }
716}
717
Steven Morelandb6c7e222021-02-18 19:20:14 +0000718#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700719constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
Yifan Hong70786532021-10-20 21:41:18 -0700720#elif defined(__ANDROID_RECOVERY__)
721constexpr int32_t kHeader = B_PACK_CHARS('R', 'E', 'C', 'O');
Steven Morelandd70160f2019-07-23 10:20:38 -0700722#else
723constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
724#endif
725
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700726// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700727status_t Parcel::writeInterfaceToken(const String16& interface)
728{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000729 return writeInterfaceToken(interface.string(), interface.size());
730}
731
732status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000733 if (auto* kernelFields = maybeKernelFields()) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000734 const IPCThreadState* threadState = IPCThreadState::self();
735 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
736 updateWorkSourceRequestHeaderPosition();
737 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
738 : IPCThreadState::kUnsetWorkSource);
739 writeInt32(kHeader);
740 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000741
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700742 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000743 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700744}
745
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000746bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
747{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000748 auto* kernelFields = maybeKernelFields();
749 if (kernelFields == nullptr) {
750 return false;
751 }
752 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000753 return false;
754 }
755
756 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000757 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000758 status_t err = writeInt32(uid);
759 setDataPosition(initialPosition);
760 return err == NO_ERROR;
761}
762
Steven Morelandf1b1e492019-05-06 15:05:13 -0700763uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000764{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000765 auto* kernelFields = maybeKernelFields();
766 if (kernelFields == nullptr) {
767 return false;
768 }
769 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000770 return IPCThreadState::kUnsetWorkSource;
771 }
772
773 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000774 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000775 uid_t uid = readInt32();
776 setDataPosition(initialPosition);
777 return uid;
778}
779
Mathias Agopian83c04462009-05-22 19:00:22 -0700780bool Parcel::checkInterface(IBinder* binder) const
781{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700782 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700783}
784
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700785bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700786 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700787{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700788 return enforceInterface(interface.string(), interface.size(), threadState);
789}
790
791bool Parcel::enforceInterface(const char16_t* interface,
792 size_t len,
793 IPCThreadState* threadState) const
794{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000795 if (auto* kernelFields = maybeKernelFields()) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000796 // StrictModePolicy.
797 int32_t strictPolicy = readInt32();
798 if (threadState == nullptr) {
799 threadState = IPCThreadState::self();
800 }
801 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
802 // For one-way calls, the callee is running entirely
803 // disconnected from the caller, so disable StrictMode entirely.
804 // Not only does disk/network usage not impact the caller, but
805 // there's no way to communicate back violations anyway.
806 threadState->setStrictModePolicy(0);
807 } else {
808 threadState->setStrictModePolicy(strictPolicy);
809 }
810 // WorkSource.
811 updateWorkSourceRequestHeaderPosition();
812 int32_t workSource = readInt32();
813 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
814 // vendor header
815 int32_t header = readInt32();
816 if (header != kHeader) {
817 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
818 header);
819 return false;
820 }
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700821 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000822
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100823 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700824 size_t parcel_interface_len;
825 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
826 if (len == parcel_interface_len &&
827 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700828 return true;
829 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700830 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700831 String8(interface, len).string(),
832 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700833 return false;
834 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700835}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700836
Jooyung Hand23f9502021-12-23 14:39:57 +0900837binder::Status Parcel::enforceNoDataAvail() const {
838 const auto n = dataAvail();
839 if (n == 0) {
840 return binder::Status::ok();
841 }
842 return binder::Status::
843 fromExceptionCode(binder::Status::Exception::EX_BAD_PARCELABLE,
844 String8::format("Parcel data not fully consumed, unread size: %zu",
845 n));
846}
847
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700848size_t Parcel::objectsCount() const
849{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000850 if (const auto* kernelFields = maybeKernelFields()) {
851 return kernelFields->mObjectsSize;
852 }
853 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700854}
855
856status_t Parcel::errorCheck() const
857{
858 return mError;
859}
860
861void Parcel::setError(status_t err)
862{
863 mError = err;
864}
865
866status_t Parcel::finishWrite(size_t len)
867{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700868 if (len > INT32_MAX) {
869 // don't accept size_t values which may have come from an
870 // inadvertent conversion from a negative int.
871 return BAD_VALUE;
872 }
873
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700874 //printf("Finish write of %d\n", len);
875 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700876 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700877 if (mDataPos > mDataSize) {
878 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700879 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700880 }
881 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
882 return NO_ERROR;
883}
884
885status_t Parcel::writeUnpadded(const void* data, size_t len)
886{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700887 if (len > INT32_MAX) {
888 // don't accept size_t values which may have come from an
889 // inadvertent conversion from a negative int.
890 return BAD_VALUE;
891 }
892
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700893 size_t end = mDataPos + len;
894 if (end < mDataPos) {
895 // integer overflow
896 return BAD_VALUE;
897 }
898
899 if (end <= mDataCapacity) {
900restart_write:
901 memcpy(mData+mDataPos, data, len);
902 return finishWrite(len);
903 }
904
905 status_t err = growData(len);
906 if (err == NO_ERROR) goto restart_write;
907 return err;
908}
909
910status_t Parcel::write(const void* data, size_t len)
911{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700912 if (len > INT32_MAX) {
913 // don't accept size_t values which may have come from an
914 // inadvertent conversion from a negative int.
915 return BAD_VALUE;
916 }
917
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700918 void* const d = writeInplace(len);
919 if (d) {
920 memcpy(d, data, len);
921 return NO_ERROR;
922 }
923 return mError;
924}
925
926void* Parcel::writeInplace(size_t len)
927{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700928 if (len > INT32_MAX) {
929 // don't accept size_t values which may have come from an
930 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700931 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700932 }
933
934 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700935
Khalid Ramadanb3d817b2021-11-18 07:02:50 +0000936 // check for integer overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700937 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700938 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700939 }
940
941 if ((mDataPos+padded) <= mDataCapacity) {
942restart_write:
943 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
944 uint8_t* const data = mData+mDataPos;
945
946 // Need to pad at end?
947 if (padded != len) {
948#if BYTE_ORDER == BIG_ENDIAN
949 static const uint32_t mask[4] = {
950 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
951 };
952#endif
953#if BYTE_ORDER == LITTLE_ENDIAN
954 static const uint32_t mask[4] = {
955 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
956 };
957#endif
958 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
959 // *reinterpret_cast<void**>(data+padded-4));
960 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
961 }
962
963 finishWrite(padded);
964 return data;
965 }
966
967 status_t err = growData(padded);
968 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700969 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700970}
971
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800972status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
973 const uint8_t* strData = (uint8_t*)str.data();
974 const size_t strLen= str.length();
975 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100976 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800977 return BAD_VALUE;
978 }
979
980 status_t err = writeInt32(utf16Len);
981 if (err) {
982 return err;
983 }
984
985 // Allocate enough bytes to hold our converted string and its terminating NULL.
986 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
987 if (!dst) {
988 return NO_MEMORY;
989 }
990
Sergio Girof4607432016-07-21 14:46:35 +0100991 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800992
993 return NO_ERROR;
994}
995
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900996
Andy Hung49198cf2020-11-18 11:02:39 -0800997status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
998status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800999
Andy Hung49198cf2020-11-18 11:02:39 -08001000status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
1001status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001002
Andy Hung49198cf2020-11-18 11:02:39 -08001003status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
1004status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
1005status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
1006status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
1007status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
1008status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
1009status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
1010status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
1011status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
1012status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
1013status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
1014status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
1015status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
1016status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
1017status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
1018status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
1019status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
1020status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
1021status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
1022status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
1023status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
1024status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
1025status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
1026status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
1027status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
1028status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
1029status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001030
Andy Hung49198cf2020-11-18 11:02:39 -08001031status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -08001032status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001033 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001034status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001035 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001036status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001037 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001038status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001039 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
1040status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001041
Andy Hung49198cf2020-11-18 11:02:39 -08001042status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
1043status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
1044status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
1045
1046status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
1047status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1048status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1049
1050status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
1051
1052status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
1053status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
1054
1055status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
1056status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
1057
1058status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
1059status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
1060status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
1061status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
1062status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
1063status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
1064status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
1065status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
1066status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
1067status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
1068status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
1069status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
1070status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
1071status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
1072status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
1073status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
1074status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
1075status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
1076status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
1077status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
1078status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
1079status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
1080status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
1081status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
1082status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
1083status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
1084status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
1085
1086status_t Parcel::readString16Vector(
1087 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
1088status_t Parcel::readString16Vector(
1089 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
1090status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
1091status_t Parcel::readUtf8VectorFromUtf16Vector(
1092 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
1093status_t Parcel::readUtf8VectorFromUtf16Vector(
1094 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
1095status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
1096
1097status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
1098status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
1099status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
1100
1101status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1102status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1103status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
1104
1105status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001106
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001107status_t Parcel::writeInt32(int32_t val)
1108{
Andreas Huber84a6d042009-08-17 13:33:27 -07001109 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001110}
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001111
1112status_t Parcel::writeUint32(uint32_t val)
1113{
1114 return writeAligned(val);
1115}
1116
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001117status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001118 if (len > INT32_MAX) {
1119 // don't accept size_t values which may have come from an
1120 // inadvertent conversion from a negative int.
1121 return BAD_VALUE;
1122 }
1123
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001124 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001125 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001126 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001127 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001128 if (ret == NO_ERROR) {
1129 ret = write(val, len * sizeof(*val));
1130 }
1131 return ret;
1132}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001133status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001134 if (len > INT32_MAX) {
1135 // don't accept size_t values which may have come from an
1136 // inadvertent conversion from a negative int.
1137 return BAD_VALUE;
1138 }
1139
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001140 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001141 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001142 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001143 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001144 if (ret == NO_ERROR) {
1145 ret = write(val, len * sizeof(*val));
1146 }
1147 return ret;
1148}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001149
Casey Dahlind6848f52015-10-15 15:44:59 -07001150status_t Parcel::writeBool(bool val)
1151{
1152 return writeInt32(int32_t(val));
1153}
1154
1155status_t Parcel::writeChar(char16_t val)
1156{
1157 return writeInt32(int32_t(val));
1158}
1159
1160status_t Parcel::writeByte(int8_t val)
1161{
1162 return writeInt32(int32_t(val));
1163}
1164
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001165status_t Parcel::writeInt64(int64_t val)
1166{
Andreas Huber84a6d042009-08-17 13:33:27 -07001167 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001168}
1169
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001170status_t Parcel::writeUint64(uint64_t val)
1171{
1172 return writeAligned(val);
1173}
1174
Serban Constantinescuf683e012013-11-05 16:53:55 +00001175status_t Parcel::writePointer(uintptr_t val)
1176{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001177 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001178}
1179
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001180status_t Parcel::writeFloat(float val)
1181{
Andreas Huber84a6d042009-08-17 13:33:27 -07001182 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001183}
1184
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001185#if defined(__mips__) && defined(__mips_hard_float)
1186
1187status_t Parcel::writeDouble(double val)
1188{
1189 union {
1190 double d;
1191 unsigned long long ll;
1192 } u;
1193 u.d = val;
1194 return writeAligned(u.ll);
1195}
1196
1197#else
1198
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001199status_t Parcel::writeDouble(double val)
1200{
Andreas Huber84a6d042009-08-17 13:33:27 -07001201 return writeAligned(val);
1202}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001203
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001204#endif
1205
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001206status_t Parcel::writeCString(const char* str)
1207{
1208 return write(str, strlen(str)+1);
1209}
1210
1211status_t Parcel::writeString8(const String8& str)
1212{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001213 return writeString8(str.string(), str.size());
1214}
1215
1216status_t Parcel::writeString8(const char* str, size_t len)
1217{
1218 if (str == nullptr) return writeInt32(-1);
1219
Jeff Sharkey18220902020-11-05 08:36:20 -07001220 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001221 status_t err = writeInt32(len);
1222 if (err == NO_ERROR) {
1223 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1224 if (data) {
1225 memcpy(data, str, len);
1226 *reinterpret_cast<char*>(data+len) = 0;
1227 return NO_ERROR;
1228 }
1229 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001230 }
1231 return err;
1232}
1233
1234status_t Parcel::writeString16(const String16& str)
1235{
1236 return writeString16(str.string(), str.size());
1237}
1238
1239status_t Parcel::writeString16(const char16_t* str, size_t len)
1240{
Yi Kong91635562018-06-07 14:38:36 -07001241 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001242
Jeff Sharkey18220902020-11-05 08:36:20 -07001243 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001244 status_t err = writeInt32(len);
1245 if (err == NO_ERROR) {
1246 len *= sizeof(char16_t);
1247 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1248 if (data) {
1249 memcpy(data, str, len);
1250 *reinterpret_cast<char16_t*>(data+len) = 0;
1251 return NO_ERROR;
1252 }
1253 err = mError;
1254 }
1255 return err;
1256}
1257
1258status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1259{
Steven Morelanda86a3562019-08-01 23:28:34 +00001260 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001261}
1262
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001263
Casey Dahlinb9872622015-11-25 15:09:45 -08001264status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1265 if (!parcelable) {
1266 return writeInt32(0);
1267 }
1268
1269 return writeParcelable(*parcelable);
1270}
1271
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001272status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001273{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001274 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001275 return BAD_TYPE;
1276
1277 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001278 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001279 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001281 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001282 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001284 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1285 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001286
1287 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001288 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 return err;
1290 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001291 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001292 return err;
1293}
1294
Jeff Brown93ff1f92011-11-04 19:01:44 -07001295status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001296{
Steven Moreland5553ac42020-11-11 02:14:45 +00001297 if (isForRpc()) {
1298 ALOGE("Cannot write file descriptor to remote binder.");
1299 return BAD_TYPE;
1300 }
1301
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001302 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001303 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001304 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001305 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001306 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001307 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001308 return writeObject(obj, true);
1309}
1310
1311status_t Parcel::writeDupFileDescriptor(int fd)
1312{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001313 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001314 if (dupFd < 0) {
1315 return -errno;
1316 }
1317 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001318 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001319 close(dupFd);
1320 }
1321 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001322}
1323
Dianne Hackborn1941a402016-08-29 12:30:43 -07001324status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1325{
1326 writeInt32(0);
1327 return writeFileDescriptor(fd, takeOwnership);
1328}
1329
Ryo Hashimotobf551892018-05-31 16:58:35 +09001330status_t Parcel::writeDupParcelFileDescriptor(int fd)
1331{
1332 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1333 if (dupFd < 0) {
1334 return -errno;
1335 }
1336 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1337 if (err != OK) {
1338 close(dupFd);
1339 }
1340 return err;
1341}
1342
Christopher Wiley2cf19952016-04-11 11:09:37 -07001343status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001344 return writeDupFileDescriptor(fd.get());
1345}
1346
Jeff Brown13b16042014-11-11 16:44:25 -08001347status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001348{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001349 if (len > INT32_MAX) {
1350 // don't accept size_t values which may have come from an
1351 // inadvertent conversion from a negative int.
1352 return BAD_VALUE;
1353 }
1354
Jeff Brown13b16042014-11-11 16:44:25 -08001355 status_t status;
1356 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001357 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001358 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001359 if (status) return status;
1360
1361 void* ptr = writeInplace(len);
1362 if (!ptr) return NO_MEMORY;
1363
Jeff Brown13b16042014-11-11 16:44:25 -08001364 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001365 return NO_ERROR;
1366 }
1367
Steve Block6807e592011-10-20 11:56:00 +01001368 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001369 int fd = ashmem_create_region("Parcel Blob", len);
1370 if (fd < 0) return NO_MEMORY;
1371
1372 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1373 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001374 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001375 } else {
Yi Kong91635562018-06-07 14:38:36 -07001376 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001377 if (ptr == MAP_FAILED) {
1378 status = -errno;
1379 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001380 if (!mutableCopy) {
1381 result = ashmem_set_prot_region(fd, PROT_READ);
1382 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001383 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001384 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001385 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001386 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001387 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001388 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001389 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001390 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001391 return NO_ERROR;
1392 }
1393 }
1394 }
1395 }
1396 ::munmap(ptr, len);
1397 }
1398 ::close(fd);
1399 return status;
1400}
1401
Jeff Brown13b16042014-11-11 16:44:25 -08001402status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1403{
1404 // Must match up with what's done in writeBlob.
1405 if (!mAllowFds) return FDS_NOT_ALLOWED;
1406 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1407 if (status) return status;
1408 return writeDupFileDescriptor(fd);
1409}
1410
Mathias Agopiane1424282013-07-29 21:24:40 -07001411status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001412{
1413 status_t err;
1414
1415 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001416 const size_t len = val.getFlattenedSize();
1417 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001418
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001419 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001420 // don't accept size_t values which may have come from an
1421 // inadvertent conversion from a negative int.
1422 return BAD_VALUE;
1423 }
1424
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001425 err = this->writeInt32(len);
1426 if (err) return err;
1427
1428 err = this->writeInt32(fd_count);
1429 if (err) return err;
1430
1431 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001432 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001433 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001434 return BAD_VALUE;
1435
Yi Kong91635562018-06-07 14:38:36 -07001436 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001437 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001438 fds = new (std::nothrow) int[fd_count];
1439 if (fds == nullptr) {
1440 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1441 return BAD_VALUE;
1442 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001443 }
1444
1445 err = val.flatten(buf, len, fds, fd_count);
1446 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1447 err = this->writeDupFileDescriptor( fds[i] );
1448 }
1449
1450 if (fd_count) {
1451 delete [] fds;
1452 }
1453
1454 return err;
1455}
1456
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001457status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1458{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001459 auto* kernelFields = maybeKernelFields();
1460 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr, "Can't write flat_binder_object to RPC Parcel");
1461
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001462 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001463 const bool enoughObjects = kernelFields->mObjectsSize < kernelFields->mObjectsCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001464 if (enoughData && enoughObjects) {
1465restart_write:
1466 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001467
Christopher Tate98e67d32015-06-03 18:44:15 -07001468 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001469 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001470 if (!mAllowFds) {
1471 // fail before modifying our object index
1472 return FDS_NOT_ALLOWED;
1473 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001474 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
Christopher Tate98e67d32015-06-03 18:44:15 -07001475 }
1476
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001477 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001478 if (nullMetaData || val.binder != 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001479 kernelFields->mObjects[kernelFields->mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001480 acquire_object(ProcessState::self(), val, this);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001481 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001482 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001483
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001484 return finishWrite(sizeof(flat_binder_object));
1485 }
1486
1487 if (!enoughData) {
1488 const status_t err = growData(sizeof(val));
1489 if (err != NO_ERROR) return err;
1490 }
1491 if (!enoughObjects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001492 if (kernelFields->mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1493 if ((kernelFields->mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
1494 size_t newSize = ((kernelFields->mObjectsSize + 2) * 3) / 2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001495 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Frederick Mayled3c595a2022-05-09 23:02:54 +00001496 binder_size_t* objects =
1497 (binder_size_t*)realloc(kernelFields->mObjects, newSize * sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001498 if (objects == nullptr) return NO_MEMORY;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001499 kernelFields->mObjects = objects;
1500 kernelFields->mObjectsCapacity = newSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001501 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001502
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001503 goto restart_write;
1504}
1505
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001506status_t Parcel::writeNoException()
1507{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001508 binder::Status status;
1509 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001510}
1511
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001512status_t Parcel::validateReadData(size_t upperBound) const
1513{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001514 const auto* kernelFields = maybeKernelFields();
1515 if (kernelFields == nullptr) {
1516 // Can't validate RPC Parcel reads because the location of binder
1517 // objects is unknown.
1518 return OK;
1519 }
1520
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001521 // Don't allow non-object reads on object data
Frederick Mayled3c595a2022-05-09 23:02:54 +00001522 if (kernelFields->mObjectsSorted || kernelFields->mObjectsSize <= 1) {
1523 data_sorted:
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001524 // Expect to check only against the next object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001525 if (kernelFields->mNextObjectHint < kernelFields->mObjectsSize &&
1526 upperBound > kernelFields->mObjects[kernelFields->mNextObjectHint]) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001527 // For some reason the current read position is greater than the next object
1528 // hint. Iterate until we find the right object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001529 size_t nextObject = kernelFields->mNextObjectHint;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001530 do {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001531 if (mDataPos < kernelFields->mObjects[nextObject] + sizeof(flat_binder_object)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001532 // Requested info overlaps with an object
1533 ALOGE("Attempt to read from protected data in Parcel %p", this);
1534 return PERMISSION_DENIED;
1535 }
1536 nextObject++;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001537 } while (nextObject < kernelFields->mObjectsSize &&
1538 upperBound > kernelFields->mObjects[nextObject]);
1539 kernelFields->mNextObjectHint = nextObject;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001540 }
1541 return NO_ERROR;
1542 }
1543 // Quickly determine if mObjects is sorted.
Frederick Mayled3c595a2022-05-09 23:02:54 +00001544 binder_size_t* currObj = kernelFields->mObjects + kernelFields->mObjectsSize - 1;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001545 binder_size_t* prevObj = currObj;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001546 while (currObj > kernelFields->mObjects) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001547 prevObj--;
1548 if(*prevObj > *currObj) {
1549 goto data_unsorted;
1550 }
1551 currObj--;
1552 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001553 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001554 goto data_sorted;
1555
1556data_unsorted:
1557 // Insertion Sort mObjects
1558 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1559 // switch to std::sort(mObjects, mObjects + mObjectsSize);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001560 for (binder_size_t* iter0 = kernelFields->mObjects + 1;
1561 iter0 < kernelFields->mObjects + kernelFields->mObjectsSize; iter0++) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001562 binder_size_t temp = *iter0;
1563 binder_size_t* iter1 = iter0 - 1;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001564 while (iter1 >= kernelFields->mObjects && *iter1 > temp) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001565 *(iter1 + 1) = *iter1;
1566 iter1--;
1567 }
1568 *(iter1 + 1) = temp;
1569 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001570 kernelFields->mNextObjectHint = 0;
1571 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001572 goto data_sorted;
1573}
1574
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001575status_t Parcel::read(void* outData, size_t len) const
1576{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001577 if (len > INT32_MAX) {
1578 // don't accept size_t values which may have come from an
1579 // inadvertent conversion from a negative int.
1580 return BAD_VALUE;
1581 }
1582
1583 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1584 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001585 const auto* kernelFields = maybeKernelFields();
1586 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001587 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001588 if(err != NO_ERROR) {
1589 // Still increment the data position by the expected length
1590 mDataPos += pad_size(len);
1591 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1592 return err;
1593 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001594 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001595 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001596 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001597 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001598 return NO_ERROR;
1599 }
1600 return NOT_ENOUGH_DATA;
1601}
1602
1603const void* Parcel::readInplace(size_t len) const
1604{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001605 if (len > INT32_MAX) {
1606 // don't accept size_t values which may have come from an
1607 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001608 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001609 }
1610
1611 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1612 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001613 const auto* kernelFields = maybeKernelFields();
1614 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001615 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001616 if(err != NO_ERROR) {
1617 // Still increment the data position by the expected length
1618 mDataPos += pad_size(len);
1619 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001620 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001621 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001622 }
1623
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001624 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001625 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001626 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001627 return data;
1628 }
Yi Kong91635562018-06-07 14:38:36 -07001629 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001630}
1631
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001632status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1633 if (status_t status = readInt32(size); status != OK) return status;
1634 if (*size < 0) return OK; // may be null, client to handle
1635
1636 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1637
1638 // approximation, can't know max element size (e.g. if it makes heap
1639 // allocations)
1640 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1641 int32_t allocationSize;
1642 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1643
1644 // High limit of 1MB since something this big could never be returned. Could
1645 // probably scope this down, but might impact very specific usecases.
1646 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1647
1648 if (allocationSize >= kMaxAllocationSize) {
1649 return NO_MEMORY;
1650 }
1651
1652 return OK;
1653}
1654
Andreas Huber84a6d042009-08-17 13:33:27 -07001655template<class T>
1656status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001657 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001658 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001659
1660 if ((mDataPos+sizeof(T)) <= mDataSize) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001661 const auto* kernelFields = maybeKernelFields();
1662 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001663 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001664 if(err != NO_ERROR) {
1665 // Still increment the data position by the expected length
1666 mDataPos += sizeof(T);
1667 return err;
1668 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001669 }
1670
Andrei Homescue33238e2022-03-29 22:50:00 +00001671 memcpy(pArg, mData + mDataPos, sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001672 mDataPos += sizeof(T);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001673 return NO_ERROR;
1674 } else {
1675 return NOT_ENOUGH_DATA;
1676 }
1677}
1678
Andreas Huber84a6d042009-08-17 13:33:27 -07001679template<class T>
1680T Parcel::readAligned() const {
1681 T result;
1682 if (readAligned(&result) != NO_ERROR) {
1683 result = 0;
1684 }
1685
1686 return result;
1687}
1688
1689template<class T>
1690status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001691 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001692 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001693
1694 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1695restart_write:
Andrei Homescue33238e2022-03-29 22:50:00 +00001696 memcpy(mData + mDataPos, &val, sizeof(val));
Andreas Huber84a6d042009-08-17 13:33:27 -07001697 return finishWrite(sizeof(val));
1698 }
1699
1700 status_t err = growData(sizeof(val));
1701 if (err == NO_ERROR) goto restart_write;
1702 return err;
1703}
1704
1705status_t Parcel::readInt32(int32_t *pArg) const
1706{
1707 return readAligned(pArg);
1708}
1709
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001710int32_t Parcel::readInt32() const
1711{
Andreas Huber84a6d042009-08-17 13:33:27 -07001712 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001713}
1714
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001715status_t Parcel::readUint32(uint32_t *pArg) const
1716{
1717 return readAligned(pArg);
1718}
1719
1720uint32_t Parcel::readUint32() const
1721{
1722 return readAligned<uint32_t>();
1723}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001724
1725status_t Parcel::readInt64(int64_t *pArg) const
1726{
Andreas Huber84a6d042009-08-17 13:33:27 -07001727 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001728}
1729
1730
1731int64_t Parcel::readInt64() const
1732{
Andreas Huber84a6d042009-08-17 13:33:27 -07001733 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001734}
1735
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001736status_t Parcel::readUint64(uint64_t *pArg) const
1737{
1738 return readAligned(pArg);
1739}
1740
1741uint64_t Parcel::readUint64() const
1742{
1743 return readAligned<uint64_t>();
1744}
1745
Serban Constantinescuf683e012013-11-05 16:53:55 +00001746status_t Parcel::readPointer(uintptr_t *pArg) const
1747{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001748 status_t ret;
1749 binder_uintptr_t ptr;
1750 ret = readAligned(&ptr);
1751 if (!ret)
1752 *pArg = ptr;
1753 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001754}
1755
1756uintptr_t Parcel::readPointer() const
1757{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001758 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001759}
1760
1761
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001762status_t Parcel::readFloat(float *pArg) const
1763{
Andreas Huber84a6d042009-08-17 13:33:27 -07001764 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001765}
1766
1767
1768float Parcel::readFloat() const
1769{
Andreas Huber84a6d042009-08-17 13:33:27 -07001770 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001771}
1772
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001773#if defined(__mips__) && defined(__mips_hard_float)
1774
1775status_t Parcel::readDouble(double *pArg) const
1776{
1777 union {
1778 double d;
1779 unsigned long long ll;
1780 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001781 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001782 status_t status;
1783 status = readAligned(&u.ll);
1784 *pArg = u.d;
1785 return status;
1786}
1787
1788double Parcel::readDouble() const
1789{
1790 union {
1791 double d;
1792 unsigned long long ll;
1793 } u;
1794 u.ll = readAligned<unsigned long long>();
1795 return u.d;
1796}
1797
1798#else
1799
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001800status_t Parcel::readDouble(double *pArg) const
1801{
Andreas Huber84a6d042009-08-17 13:33:27 -07001802 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001803}
1804
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001805double Parcel::readDouble() const
1806{
Andreas Huber84a6d042009-08-17 13:33:27 -07001807 return readAligned<double>();
1808}
1809
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001810#endif
1811
Casey Dahlind6848f52015-10-15 15:44:59 -07001812status_t Parcel::readBool(bool *pArg) const
1813{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001814 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001815 status_t ret = readInt32(&tmp);
1816 *pArg = (tmp != 0);
1817 return ret;
1818}
1819
1820bool Parcel::readBool() const
1821{
1822 return readInt32() != 0;
1823}
1824
1825status_t Parcel::readChar(char16_t *pArg) const
1826{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001827 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001828 status_t ret = readInt32(&tmp);
1829 *pArg = char16_t(tmp);
1830 return ret;
1831}
1832
1833char16_t Parcel::readChar() const
1834{
1835 return char16_t(readInt32());
1836}
1837
1838status_t Parcel::readByte(int8_t *pArg) const
1839{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001840 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001841 status_t ret = readInt32(&tmp);
1842 *pArg = int8_t(tmp);
1843 return ret;
1844}
1845
1846int8_t Parcel::readByte() const
1847{
1848 return int8_t(readInt32());
1849}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001850
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001851status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1852 size_t utf16Size = 0;
1853 const char16_t* src = readString16Inplace(&utf16Size);
1854 if (!src) {
1855 return UNEXPECTED_NULL;
1856 }
1857
1858 // Save ourselves the trouble, we're done.
1859 if (utf16Size == 0u) {
1860 str->clear();
1861 return NO_ERROR;
1862 }
1863
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001864 // Allow for closing '\0'
1865 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1866 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001867 return BAD_VALUE;
1868 }
1869 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001870 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001871 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001872 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1873 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001874 return NO_ERROR;
1875}
1876
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001877const char* Parcel::readCString() const
1878{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001879 if (mDataPos < mDataSize) {
1880 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001881 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1882 // is the string's trailing NUL within the parcel's valid bounds?
1883 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1884 if (eos) {
1885 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001886 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001887 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001888 return str;
1889 }
1890 }
Yi Kong91635562018-06-07 14:38:36 -07001891 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001892}
1893
1894String8 Parcel::readString8() const
1895{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001896 size_t len;
1897 const char* str = readString8Inplace(&len);
1898 if (str) return String8(str, len);
1899 ALOGE("Reading a NULL string not supported here.");
1900 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07001901}
1902
1903status_t Parcel::readString8(String8* pArg) const
1904{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001905 size_t len;
1906 const char* str = readString8Inplace(&len);
1907 if (str) {
1908 pArg->setTo(str, len);
1909 return 0;
1910 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07001911 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001912 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07001913 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001914}
1915
1916const char* Parcel::readString8Inplace(size_t* outLen) const
1917{
1918 int32_t size = readInt32();
1919 // watch for potential int overflow from size+1
1920 if (size >= 0 && size < INT32_MAX) {
1921 *outLen = size;
1922 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00001923 if (str != nullptr) {
1924 if (str[size] == '\0') {
1925 return str;
1926 }
1927 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001928 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001929 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001930 *outLen = 0;
1931 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001932}
1933
1934String16 Parcel::readString16() const
1935{
1936 size_t len;
1937 const char16_t* str = readString16Inplace(&len);
1938 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001939 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001940 return String16();
1941}
1942
Casey Dahlinb9872622015-11-25 15:09:45 -08001943
Casey Dahlin451ff582015-10-19 18:12:18 -07001944status_t Parcel::readString16(String16* pArg) const
1945{
1946 size_t len;
1947 const char16_t* str = readString16Inplace(&len);
1948 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001949 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001950 return 0;
1951 } else {
1952 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001953 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001954 }
1955}
1956
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001957const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1958{
1959 int32_t size = readInt32();
1960 // watch for potential int overflow from size+1
1961 if (size >= 0 && size < INT32_MAX) {
1962 *outLen = size;
1963 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00001964 if (str != nullptr) {
1965 if (str[size] == u'\0') {
1966 return str;
1967 }
1968 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001969 }
1970 }
1971 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001972 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001973}
1974
Casey Dahlinf0c13772015-10-27 18:33:56 -07001975status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1976{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001977 status_t status = readNullableStrongBinder(val);
1978 if (status == OK && !val->get()) {
Steven Morelanddce98242022-03-11 00:26:10 +00001979 ALOGW("Expecting binder but got null!");
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001980 status = UNEXPECTED_NULL;
1981 }
1982 return status;
1983}
1984
1985status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1986{
Steven Morelanda86a3562019-08-01 23:28:34 +00001987 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001988}
1989
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001990sp<IBinder> Parcel::readStrongBinder() const
1991{
1992 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001993 // Note that a lot of code in Android reads binders by hand with this
1994 // method, and that code has historically been ok with getting nullptr
1995 // back (while ignoring error codes).
1996 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001997 return val;
1998}
1999
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002000int32_t Parcel::readExceptionCode() const
2001{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002002 binder::Status status;
2003 status.readFromParcel(*this);
2004 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002005}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002006
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002007native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002008{
2009 int numFds, numInts;
2010 status_t err;
2011 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002012 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002013 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002014 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002015
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002016 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002017 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002018 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002019 }
2020
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002021 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002022 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002023 if (h->data[i] < 0) {
2024 for (int j = 0; j < i; j++) {
2025 close(h->data[j]);
2026 }
2027 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002028 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002029 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002030 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002031 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002032 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002033 native_handle_close(h);
2034 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002035 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002036 }
2037 return h;
2038}
2039
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002040int Parcel::readFileDescriptor() const
2041{
2042 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002043
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002044 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002045 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002046 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002047
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002048 return BAD_TYPE;
2049}
2050
Dianne Hackborn1941a402016-08-29 12:30:43 -07002051int Parcel::readParcelFileDescriptor() const
2052{
2053 int32_t hasComm = readInt32();
2054 int fd = readFileDescriptor();
2055 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002056 // detach (owned by the binder driver)
2057 int comm = readFileDescriptor();
2058
2059 // warning: this must be kept in sync with:
2060 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2061 enum ParcelFileDescriptorStatus {
2062 DETACHED = 2,
2063 };
2064
2065#if BYTE_ORDER == BIG_ENDIAN
2066 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2067#endif
2068#if BYTE_ORDER == LITTLE_ENDIAN
2069 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2070#endif
2071
2072 ssize_t written = TEMP_FAILURE_RETRY(
2073 ::write(comm, &message, sizeof(message)));
2074
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08002075 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002076 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2077 written, strerror(errno));
2078 return BAD_TYPE;
2079 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002080 }
2081 return fd;
2082}
2083
Christopher Wiley2cf19952016-04-11 11:09:37 -07002084status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002085{
2086 int got = readFileDescriptor();
2087
2088 if (got == BAD_TYPE) {
2089 return BAD_TYPE;
2090 }
2091
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002092 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002093
2094 if (val->get() < 0) {
2095 return BAD_VALUE;
2096 }
2097
2098 return OK;
2099}
2100
Ryo Hashimotobf551892018-05-31 16:58:35 +09002101status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2102{
2103 int got = readParcelFileDescriptor();
2104
2105 if (got == BAD_TYPE) {
2106 return BAD_TYPE;
2107 }
2108
2109 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2110
2111 if (val->get() < 0) {
2112 return BAD_VALUE;
2113 }
2114
2115 return OK;
2116}
Casey Dahlin06673e32015-11-23 13:24:23 -08002117
Jeff Brown5707dbf2011-09-23 21:17:56 -07002118status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2119{
Jeff Brown13b16042014-11-11 16:44:25 -08002120 int32_t blobType;
2121 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002122 if (status) return status;
2123
Jeff Brown13b16042014-11-11 16:44:25 -08002124 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002125 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002126 const void* ptr = readInplace(len);
2127 if (!ptr) return BAD_VALUE;
2128
Jeff Brown13b16042014-11-11 16:44:25 -08002129 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002130 return NO_ERROR;
2131 }
2132
Steve Block6807e592011-10-20 11:56:00 +01002133 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002134 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002135 int fd = readFileDescriptor();
2136 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2137
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002138 if (!ashmem_valid(fd)) {
2139 ALOGE("invalid fd");
2140 return BAD_VALUE;
2141 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002142 int size = ashmem_get_size_region(fd);
2143 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002144 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002145 return BAD_VALUE;
2146 }
Yi Kong91635562018-06-07 14:38:36 -07002147 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002148 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002149 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002150
Jeff Brown13b16042014-11-11 16:44:25 -08002151 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002152 return NO_ERROR;
2153}
2154
Mathias Agopiane1424282013-07-29 21:24:40 -07002155status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002156{
2157 // size
2158 const size_t len = this->readInt32();
2159 const size_t fd_count = this->readInt32();
2160
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002161 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002162 // don't accept size_t values which may have come from an
2163 // inadvertent conversion from a negative int.
2164 return BAD_VALUE;
2165 }
2166
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002167 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002168 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002169 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002170 return BAD_VALUE;
2171
Yi Kong91635562018-06-07 14:38:36 -07002172 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002173 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002174 fds = new (std::nothrow) int[fd_count];
2175 if (fds == nullptr) {
2176 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2177 return BAD_VALUE;
2178 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002179 }
2180
2181 status_t err = NO_ERROR;
2182 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002183 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002184 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002185 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002186 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 -07002187 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2188 // Close all the file descriptors that were dup-ed.
2189 for (size_t j=0; j<i ;j++) {
2190 close(fds[j]);
2191 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002192 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002193 }
2194
2195 if (err == NO_ERROR) {
2196 err = val.unflatten(buf, len, fds, fd_count);
2197 }
2198
2199 if (fd_count) {
2200 delete [] fds;
2201 }
2202
2203 return err;
2204}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002205const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2206{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002207 const auto* kernelFields = maybeKernelFields();
2208 if (kernelFields == nullptr) {
2209 return nullptr;
2210 }
2211
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002212 const size_t DPOS = mDataPos;
2213 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2214 const flat_binder_object* obj
2215 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2216 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002217 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002218 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002219 // the object list, so we don't want to check for it when
2220 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002221 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002222 return obj;
2223 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002224
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002225 // Ensure that this object is valid...
Frederick Mayled3c595a2022-05-09 23:02:54 +00002226 binder_size_t* const OBJS = kernelFields->mObjects;
2227 const size_t N = kernelFields->mObjectsSize;
2228 size_t opos = kernelFields->mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002229
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002230 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002231 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002232 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002233
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002234 // Start at the current hint position, looking for an object at
2235 // the current data position.
2236 if (opos < N) {
2237 while (opos < (N-1) && OBJS[opos] < DPOS) {
2238 opos++;
2239 }
2240 } else {
2241 opos = N-1;
2242 }
2243 if (OBJS[opos] == DPOS) {
2244 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002245 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002246 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002247 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002248 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002249 return obj;
2250 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002251
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002252 // Look backwards for it...
2253 while (opos > 0 && OBJS[opos] > DPOS) {
2254 opos--;
2255 }
2256 if (OBJS[opos] == DPOS) {
2257 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002258 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002259 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002260 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002261 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002262 return obj;
2263 }
2264 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002265 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 -07002266 this, DPOS);
2267 }
Yi Kong91635562018-06-07 14:38:36 -07002268 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002269}
2270
Frederick Mayled3c595a2022-05-09 23:02:54 +00002271void Parcel::closeFileDescriptors() {
2272 auto* kernelFields = maybeKernelFields();
2273 if (kernelFields == nullptr) {
2274 return;
2275 }
2276 size_t i = kernelFields->mObjectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002277 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002278 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002279 }
2280 while (i > 0) {
2281 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002282 const flat_binder_object* flat =
2283 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002284 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002285 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002286 close(flat->handle);
2287 }
2288 }
2289}
2290
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002291uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002292{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002293 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002294}
2295
2296size_t Parcel::ipcDataSize() const
2297{
2298 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2299}
2300
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002301uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002302{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002303 if (const auto* kernelFields = maybeKernelFields()) {
2304 return reinterpret_cast<uintptr_t>(kernelFields->mObjects);
2305 }
2306 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002307}
2308
2309size_t Parcel::ipcObjectsCount() const
2310{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002311 if (const auto* kernelFields = maybeKernelFields()) {
2312 return kernelFields->mObjectsSize;
2313 }
2314 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002315}
2316
Frederick Mayled3c595a2022-05-09 23:02:54 +00002317void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, const binder_size_t* objects,
2318 size_t objectsCount, release_func relFunc) {
Steven Moreland438cce82021-04-02 18:04:08 +00002319 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2320 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2321
Steven Morelandceed9bb2020-12-17 01:01:06 +00002322 freeData();
2323
Frederick Mayled3c595a2022-05-09 23:02:54 +00002324 auto* kernelFields = maybeKernelFields();
2325 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr); // guaranteed by freeData.
2326
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002327 mData = const_cast<uint8_t*>(data);
2328 mDataSize = mDataCapacity = dataSize;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002329 kernelFields->mObjects = const_cast<binder_size_t*>(objects);
2330 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002331 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002332
2333 binder_size_t minOffset = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002334 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
2335 binder_size_t offset = kernelFields->mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002336 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002337 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002338 __func__, (uint64_t)offset, (uint64_t)minOffset);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002339 kernelFields->mObjectsSize = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002340 break;
2341 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002342 const flat_binder_object* flat
2343 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2344 uint32_t type = flat->hdr.type;
2345 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2346 type == BINDER_TYPE_FD)) {
2347 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2348 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
Steven Morelandf2e0a952021-11-01 18:17:23 -07002349 // recover gracefully by clearing out the objects.
Martijn Coenen82c75312019-07-24 15:18:30 +02002350 android_errorWriteLog(0x534e4554, "135930648");
Steven Morelandf2e0a952021-11-01 18:17:23 -07002351 android_errorWriteLog(0x534e4554, "203847542");
Martijn Coenen82c75312019-07-24 15:18:30 +02002352 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2353 __func__, type, (uint64_t)offset);
Steven Morelandf2e0a952021-11-01 18:17:23 -07002354
2355 // WARNING: callers of ipcSetDataReference need to make sure they
2356 // don't rely on mObjectsSize in their release_func.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002357 kernelFields->mObjectsSize = 0;
Martijn Coenen82c75312019-07-24 15:18:30 +02002358 break;
2359 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002360 minOffset = offset + sizeof(flat_binder_object);
2361 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002362 scanForFds();
2363}
2364
Frederick Mayled3c595a2022-05-09 23:02:54 +00002365void Parcel::rpcSetDataReference(const sp<RpcSession>& session, const uint8_t* data,
2366 size_t dataSize, release_func relFunc) {
2367 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2368 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2369
2370 LOG_ALWAYS_FATAL_IF(session == nullptr);
2371
2372 freeData();
2373 markForRpc(session);
2374
2375 mData = const_cast<uint8_t*>(data);
2376 mDataSize = mDataCapacity = dataSize;
2377 mOwner = relFunc;
2378}
2379
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002380void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002381{
2382 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002383
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002384 if (errorCheck() != NO_ERROR) {
2385 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002386 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002387 } else if (dataSize() > 0) {
2388 const uint8_t* DATA = data();
2389 to << indent << HexDump(DATA, dataSize()) << dedent;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002390 if (const auto* kernelFields = maybeKernelFields()) {
2391 const binder_size_t* OBJS = kernelFields->mObjects;
2392 const size_t N = objectsCount();
2393 for (size_t i = 0; i < N; i++) {
2394 const flat_binder_object* flat =
2395 reinterpret_cast<const flat_binder_object*>(DATA + OBJS[i]);
2396 to << endl
2397 << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2398 << TypeCode(flat->hdr.type & 0x7f7f7f00) << " = " << flat->binder;
2399 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002400 }
2401 } else {
2402 to << "NULL";
2403 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002404
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002405 to << ")";
2406}
2407
2408void Parcel::releaseObjects()
2409{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002410 auto* kernelFields = maybeKernelFields();
2411 if (kernelFields == nullptr) {
2412 return;
2413 }
2414
2415 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002416 if (i == 0) {
2417 return;
2418 }
2419 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002420 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002421 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002422 while (i > 0) {
2423 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002424 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002425 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002426 }
2427}
2428
2429void Parcel::acquireObjects()
2430{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002431 auto* kernelFields = maybeKernelFields();
2432 if (kernelFields == nullptr) {
2433 return;
2434 }
2435
2436 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002437 if (i == 0) {
2438 return;
2439 }
2440 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002441 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002442 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002443 while (i > 0) {
2444 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002445 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002446 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002447 }
2448}
2449
2450void Parcel::freeData()
2451{
2452 freeDataNoInit();
2453 initState();
2454}
2455
2456void Parcel::freeDataNoInit()
2457{
2458 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002459 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002460 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002461 auto* kernelFields = maybeKernelFields();
2462 mOwner(this, mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
2463 kernelFields ? kernelFields->mObjectsSize : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002464 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002465 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002466 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002467 if (mData) {
2468 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002469 gParcelGlobalAllocSize -= mDataCapacity;
2470 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002471 if (mDeallocZero) {
2472 zeroMemory(mData, mDataSize);
2473 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002474 free(mData);
2475 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002476 auto* kernelFields = maybeKernelFields();
2477 if (kernelFields && kernelFields->mObjects) free(kernelFields->mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002478 }
2479}
2480
2481status_t Parcel::growData(size_t len)
2482{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002483 if (len > INT32_MAX) {
2484 // don't accept size_t values which may have come from an
2485 // inadvertent conversion from a negative int.
2486 return BAD_VALUE;
2487 }
2488
Martijn Coenen93fe5182020-01-22 10:46:25 +01002489 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2490 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002491 size_t newSize = ((mDataSize+len)*3)/2;
2492 return (newSize <= mDataSize)
2493 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002494 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002495}
2496
Steven Morelandf183fdd2020-10-27 00:12:12 +00002497static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2498 if (!zero) {
2499 return (uint8_t*)realloc(data, newCapacity);
2500 }
2501 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2502 if (!newData) {
2503 return nullptr;
2504 }
2505
2506 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2507 zeroMemory(data, oldCapacity);
2508 free(data);
2509 return newData;
2510}
2511
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002512status_t Parcel::restartWrite(size_t desired)
2513{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002514 if (desired > INT32_MAX) {
2515 // don't accept size_t values which may have come from an
2516 // inadvertent conversion from a negative int.
2517 return BAD_VALUE;
2518 }
2519
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002520 if (mOwner) {
2521 freeData();
2522 return continueWrite(desired);
2523 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002524
Steven Morelandf183fdd2020-10-27 00:12:12 +00002525 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002526 if (!data && desired > mDataCapacity) {
2527 mError = NO_MEMORY;
2528 return NO_MEMORY;
2529 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002530
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002531 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002532
Devin Moore4a0a55e2020-06-04 13:23:10 -07002533 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002534 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002535 if (mDataCapacity > desired) {
2536 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2537 } else {
2538 gParcelGlobalAllocSize += (desired - mDataCapacity);
2539 }
2540
Colin Cross83ec65e2015-12-08 17:15:50 -08002541 if (!mData) {
2542 gParcelGlobalAllocCount++;
2543 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544 mData = data;
2545 mDataCapacity = desired;
2546 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002547
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002548 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002549 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2550 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2551
Frederick Mayled3c595a2022-05-09 23:02:54 +00002552 if (auto* kernelFields = maybeKernelFields()) {
2553 free(kernelFields->mObjects);
2554 kernelFields->mObjects = nullptr;
2555 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = 0;
2556 kernelFields->mNextObjectHint = 0;
2557 kernelFields->mObjectsSorted = false;
2558 kernelFields->mHasFds = false;
2559 kernelFields->mFdsKnown = true;
2560 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002561 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002562
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002563 return NO_ERROR;
2564}
2565
2566status_t Parcel::continueWrite(size_t desired)
2567{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002568 if (desired > INT32_MAX) {
2569 // don't accept size_t values which may have come from an
2570 // inadvertent conversion from a negative int.
2571 return BAD_VALUE;
2572 }
2573
Frederick Mayled3c595a2022-05-09 23:02:54 +00002574 auto* kernelFields = maybeKernelFields();
2575
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002576 // If shrinking, first adjust for any objects that appear
2577 // after the new data size.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002578 size_t objectsSize = kernelFields ? kernelFields->mObjectsSize : 0;
2579 if (kernelFields && desired < mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002580 if (desired == 0) {
2581 objectsSize = 0;
2582 } else {
2583 while (objectsSize > 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002584 if (kernelFields->mObjects[objectsSize - 1] < desired) break;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002585 objectsSize--;
2586 }
2587 }
2588 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002589
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002590 if (mOwner) {
2591 // If the size is going to zero, just release the owner's data.
2592 if (desired == 0) {
2593 freeData();
2594 return NO_ERROR;
2595 }
2596
2597 // If there is a different owner, we need to take
2598 // posession.
2599 uint8_t* data = (uint8_t*)malloc(desired);
2600 if (!data) {
2601 mError = NO_MEMORY;
2602 return NO_MEMORY;
2603 }
Yi Kong91635562018-06-07 14:38:36 -07002604 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002605
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002606 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002607 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002608 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002609 free(data);
2610
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002611 mError = NO_MEMORY;
2612 return NO_MEMORY;
2613 }
2614
2615 // Little hack to only acquire references on objects
2616 // we will be keeping.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002617 size_t oldObjectsSize = kernelFields->mObjectsSize;
2618 kernelFields->mObjectsSize = objectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002619 acquireObjects();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002620 kernelFields->mObjectsSize = oldObjectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002621 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002622
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002623 if (mData) {
2624 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2625 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002626 if (objects && kernelFields && kernelFields->mObjects) {
2627 memcpy(objects, kernelFields->mObjects, objectsSize * sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002628 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002629 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002630 mOwner(this, mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
2631 kernelFields ? kernelFields->mObjectsSize : 0);
Yi Kong91635562018-06-07 14:38:36 -07002632 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002633
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002634 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002635 gParcelGlobalAllocSize += desired;
2636 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002637
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002638 mData = data;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002639 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002640 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002641 mDataCapacity = desired;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002642 if (kernelFields) {
2643 kernelFields->mObjects = objects;
2644 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsSize;
2645 kernelFields->mNextObjectHint = 0;
2646 kernelFields->mObjectsSorted = false;
2647 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002648
2649 } else if (mData) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002650 if (kernelFields && objectsSize < kernelFields->mObjectsSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002651 // Need to release refs on any objects we are dropping.
2652 const sp<ProcessState> proc(ProcessState::self());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002653 for (size_t i = objectsSize; i < kernelFields->mObjectsSize; i++) {
2654 const flat_binder_object* flat =
2655 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002656 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002657 // will need to rescan because we may have lopped off the only FDs
Frederick Mayled3c595a2022-05-09 23:02:54 +00002658 kernelFields->mFdsKnown = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002659 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07002660 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002661 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002662
2663 if (objectsSize == 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002664 free(kernelFields->mObjects);
2665 kernelFields->mObjects = nullptr;
2666 kernelFields->mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002667 } else {
2668 binder_size_t* objects =
Frederick Mayled3c595a2022-05-09 23:02:54 +00002669 (binder_size_t*)realloc(kernelFields->mObjects,
2670 objectsSize * sizeof(binder_size_t));
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002671 if (objects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002672 kernelFields->mObjects = objects;
2673 kernelFields->mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002674 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002675 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002676 kernelFields->mObjectsSize = objectsSize;
2677 kernelFields->mNextObjectHint = 0;
2678 kernelFields->mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002679 }
2680
2681 // We own the data, so we can just do a realloc().
2682 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002683 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002684 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002685 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2686 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002687 gParcelGlobalAllocSize += desired;
2688 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002689 mData = data;
2690 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002691 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002692 mError = NO_MEMORY;
2693 return NO_MEMORY;
2694 }
2695 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002696 if (mDataSize > desired) {
2697 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002698 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002699 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002700 if (mDataPos > desired) {
2701 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002702 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002703 }
2704 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002705
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002706 } else {
2707 // This is the first data. Easy!
2708 uint8_t* data = (uint8_t*)malloc(desired);
2709 if (!data) {
2710 mError = NO_MEMORY;
2711 return NO_MEMORY;
2712 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002713
Frederick Mayled3c595a2022-05-09 23:02:54 +00002714 if (!(mDataCapacity == 0 &&
2715 (kernelFields == nullptr ||
2716 (kernelFields->mObjects == nullptr && kernelFields->mObjectsCapacity == 0)))) {
2717 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity,
2718 kernelFields ? kernelFields->mObjects : nullptr,
2719 kernelFields ? kernelFields->mObjectsCapacity : 0, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002720 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002721
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002722 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002723 gParcelGlobalAllocSize += desired;
2724 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002725
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002726 mData = data;
2727 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002728 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2729 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002730 mDataCapacity = desired;
2731 }
2732
2733 return NO_ERROR;
2734}
2735
2736void Parcel::initState()
2737{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002738 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002739 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002740 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002741 mDataSize = 0;
2742 mDataCapacity = 0;
2743 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002744 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2745 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002746 mVariantFields.emplace<KernelFields>();
Steven Moreland6e5a7752019-08-05 20:30:14 -07002747 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002748 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07002749 mOwner = nullptr;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002750
2751 // racing multiple init leads only to multiple identical write
2752 if (gMaxFds == 0) {
2753 struct rlimit result;
2754 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2755 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002756 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002757 } else {
2758 ALOGW("Unable to getrlimit: %s", strerror(errno));
2759 gMaxFds = 1024;
2760 }
2761 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002762}
2763
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01002764void Parcel::scanForFds() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002765 auto* kernelFields = maybeKernelFields();
2766 if (kernelFields == nullptr) {
2767 return;
2768 }
2769 status_t status = hasFileDescriptorsInRange(0, dataSize(), &kernelFields->mHasFds);
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01002770 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002771 kernelFields->mFdsKnown = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002772}
2773
Dan Sandleraa5c2342015-04-10 10:08:45 -04002774size_t Parcel::getBlobAshmemSize() const
2775{
Adrian Roos6bb31142015-10-22 16:46:12 -07002776 // This used to return the size of all blobs that were written to ashmem, now we're returning
2777 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07002778 // TODO(b/202029388): Remove method once ABI can be changed.
2779 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04002780}
2781
Adrian Rooscbf37262015-10-22 16:12:53 -07002782size_t Parcel::getOpenAshmemSize() const
2783{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002784 auto* kernelFields = maybeKernelFields();
2785 if (kernelFields == nullptr) {
2786 return 0;
2787 }
2788
Steven Morelandc673f1f2021-10-07 18:23:35 -07002789 size_t openAshmemSize = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002790 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
Steven Morelandc673f1f2021-10-07 18:23:35 -07002791 const flat_binder_object* flat =
Frederick Mayled3c595a2022-05-09 23:02:54 +00002792 reinterpret_cast<const flat_binder_object*>(mData + kernelFields->mObjects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002793
2794 // cookie is compared against zero for historical reasons
2795 // > obj.cookie = takeOwnership ? 1 : 0;
2796 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
2797 int size = ashmem_get_size_region(flat->handle);
2798 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
2799 ALOGE("Overflow when computing ashmem size.");
2800 return SIZE_MAX;
2801 }
2802 }
2803 }
2804 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002805}
2806
2807// --- Parcel::Blob ---
2808
2809Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002810 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002811}
2812
2813Parcel::Blob::~Blob() {
2814 release();
2815}
2816
2817void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002818 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002819 ::munmap(mData, mSize);
2820 }
2821 clear();
2822}
2823
Jeff Brown13b16042014-11-11 16:44:25 -08002824void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2825 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002826 mData = data;
2827 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002828 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002829}
2830
2831void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002832 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002833 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002834 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002835 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002836}
2837
Steven Moreland61ff8492019-09-26 16:05:45 -07002838} // namespace android