blob: e67dd7b5e27c055a75bcbe9ea4f6723e85b943ed [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
Andrei Homescu1519b982022-06-09 02:04:44 +000090// Maximum number of file descriptors per Parcel.
91constexpr size_t kMaxFds = 1024;
Christopher Tatee4e0ae82016-03-24 16:03:44 -070092
Jeff Brown13b16042014-11-11 16:44:25 -080093// Maximum size of a blob to transfer in-place.
94static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
95
96enum {
97 BLOB_INPLACE = 0,
98 BLOB_ASHMEM_IMMUTABLE = 1,
99 BLOB_ASHMEM_MUTABLE = 2,
100};
101
Steven Morelandc673f1f2021-10-07 18:23:35 -0700102static void acquire_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
103 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700104 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700105 case BINDER_TYPE_BINDER:
106 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000107 LOG_REFS("Parcel %p acquiring reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800108 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 }
110 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700111 case BINDER_TYPE_HANDLE: {
112 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700113 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700114 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
115 b->incStrong(who);
116 }
117 return;
118 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700119 case BINDER_TYPE_FD: {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700120 return;
121 }
122 }
123
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700124 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700125}
126
Steven Morelandc673f1f2021-10-07 18:23:35 -0700127static void release_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
128 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700129 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700130 case BINDER_TYPE_BINDER:
131 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000132 LOG_REFS("Parcel %p releasing reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800133 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700134 }
135 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136 case BINDER_TYPE_HANDLE: {
137 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700138 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
140 b->decStrong(who);
141 }
142 return;
143 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800145 if (obj.cookie != 0) { // owned
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800146 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700147 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700148 return;
149 }
150 }
151
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700152 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700153}
154
Frederick Mayled3c595a2022-05-09 23:02:54 +0000155Parcel::RpcFields::RpcFields(const sp<RpcSession>& session) : mSession(session) {
156 LOG_ALWAYS_FATAL_IF(mSession == nullptr);
157}
158
Steven Moreland34b48cb2020-12-01 22:45:38 +0000159status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700161 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland16a41062021-07-23 13:35:25 -0700162 int16_t rep = internal::Stability::getRepr(binder.get());
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000163 return writeInt32(rep);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700164}
165
Steven Morelanda86a3562019-08-01 23:28:34 +0000166status_t Parcel::finishUnflattenBinder(
167 const sp<IBinder>& binder, sp<IBinder>* out) const
168{
169 int32_t stability;
170 status_t status = readInt32(&stability);
171 if (status != OK) return status;
172
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000173 status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability),
174 true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000175 if (status != OK) return status;
176
177 *out = binder;
178 return OK;
179}
180
Steven Morelandbf1915b2020-07-16 22:43:02 +0000181static constexpr inline int schedPolicyMask(int policy, int priority) {
182 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
183}
184
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500185status_t Parcel::flattenBinder(const sp<IBinder>& binder) {
186 BBinder* local = nullptr;
187 if (binder) local = binder->localBinder();
188 if (local) local->setParceled();
189
Frederick Mayled3c595a2022-05-09 23:02:54 +0000190 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000191 if (binder) {
192 status_t status = writeInt32(1); // non-null
193 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700194 uint64_t address;
Steven Morelanda5036f02021-06-08 02:26:57 +0000195 // TODO(b/167966510): need to undo this if the Parcel is not sent
Frederick Mayled3c595a2022-05-09 23:02:54 +0000196 status = rpcFields->mSession->state()->onBinderLeaving(rpcFields->mSession, binder,
197 &address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000198 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700199 status = writeUint64(address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000200 if (status != OK) return status;
201 } else {
202 status_t status = writeInt32(0); // null
203 if (status != OK) return status;
204 }
205 return finishFlattenBinder(binder);
206 }
207
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700208 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700209
Steven Morelandbf1915b2020-07-16 22:43:02 +0000210 int schedBits = 0;
211 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
212 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700213 }
214
Yi Kong91635562018-06-07 14:38:36 -0700215 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700216 if (!local) {
217 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700218 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000219 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000220 } else {
221 if (proxy->isRpcBinder()) {
Steven Morelanda9231112021-09-22 10:08:14 -0700222 ALOGE("Sending a socket binder over kernel binder is prohibited");
Steven Moreland5553ac42020-11-11 02:14:45 +0000223 return INVALID_OPERATION;
224 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700225 }
Steven Moreland99157622021-09-13 16:27:34 -0700226 const int32_t handle = proxy ? proxy->getPrivateAccessor().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700227 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800228 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
Steven Moreland49c27532022-03-01 10:21:07 +0000229 obj.flags = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800231 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700232 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000233 int policy = local->getMinSchedulerPolicy();
234 int priority = local->getMinSchedulerPriority();
235
236 if (policy != 0 || priority != 0) {
237 // override value, since it is set explicitly
238 schedBits = schedPolicyMask(policy, priority);
239 }
Steven Moreland49c27532022-03-01 10:21:07 +0000240 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Steven Morelandf0212002018-12-26 13:59:23 -0800241 if (local->isRequestingSid()) {
242 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
243 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000244 if (local->isInheritRt()) {
245 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
246 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700247 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800248 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
249 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700250 }
251 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700252 obj.hdr.type = BINDER_TYPE_BINDER;
Steven Moreland49c27532022-03-01 10:21:07 +0000253 obj.flags = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800254 obj.binder = 0;
255 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700256 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700257
Steven Morelandbf1915b2020-07-16 22:43:02 +0000258 obj.flags |= schedBits;
259
Steven Moreland34b48cb2020-12-01 22:45:38 +0000260 status_t status = writeObject(obj, false);
261 if (status != OK) return status;
262
263 return finishFlattenBinder(binder);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700264}
265
Steven Morelanda86a3562019-08-01 23:28:34 +0000266status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700267{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000268 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5623d1a2021-09-10 15:45:34 -0700269 int32_t isPresent;
270 status_t status = readInt32(&isPresent);
Steven Moreland5553ac42020-11-11 02:14:45 +0000271 if (status != OK) return status;
272
273 sp<IBinder> binder;
274
Steven Moreland5623d1a2021-09-10 15:45:34 -0700275 if (isPresent & 1) {
276 uint64_t addr;
277 if (status_t status = readUint64(&addr); status != OK) return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000278 if (status_t status =
279 rpcFields->mSession->state()->onBinderEntering(rpcFields->mSession, addr,
280 &binder);
Steven Moreland7227c8a2021-06-02 00:24:32 +0000281 status != OK)
282 return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000283 if (status_t status =
284 rpcFields->mSession->state()->flushExcessBinderRefs(rpcFields->mSession,
285 addr, binder);
Steven Morelandd8083312021-09-22 13:37:10 -0700286 status != OK)
287 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000288 }
289
290 return finishUnflattenBinder(binder, out);
291 }
292
Steven Morelanda86a3562019-08-01 23:28:34 +0000293 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700294
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700295 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700296 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000297 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000298 sp<IBinder> binder =
299 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000300 return finishUnflattenBinder(binder, out);
301 }
302 case BINDER_TYPE_HANDLE: {
303 sp<IBinder> binder =
304 ProcessState::self()->getStrongProxyForHandle(flat->handle);
305 return finishUnflattenBinder(binder, out);
306 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700307 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700308 }
309 return BAD_TYPE;
310}
311
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700312// ---------------------------------------------------------------------------
313
314Parcel::Parcel()
315{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800316 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700317 initState();
318}
319
320Parcel::~Parcel()
321{
322 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800323 LOG_ALLOC("Parcel %p: destroyed", this);
324}
325
326size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600327 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800328}
329
330size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600331 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700332}
333
334const uint8_t* Parcel::data() const
335{
336 return mData;
337}
338
339size_t Parcel::dataSize() const
340{
341 return (mDataSize > mDataPos ? mDataSize : mDataPos);
342}
343
344size_t Parcel::dataAvail() const
345{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700346 size_t result = dataSize() - dataPosition();
347 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700348 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700349 }
350 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700351}
352
353size_t Parcel::dataPosition() const
354{
355 return mDataPos;
356}
357
358size_t Parcel::dataCapacity() const
359{
360 return mDataCapacity;
361}
362
363status_t Parcel::setDataSize(size_t size)
364{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700365 if (size > INT32_MAX) {
366 // don't accept size_t values which may have come from an
367 // inadvertent conversion from a negative int.
368 return BAD_VALUE;
369 }
370
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700371 status_t err;
372 err = continueWrite(size);
373 if (err == NO_ERROR) {
374 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700375 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700376 }
377 return err;
378}
379
380void Parcel::setDataPosition(size_t pos) const
381{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700382 if (pos > INT32_MAX) {
383 // don't accept size_t values which may have come from an
384 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700385 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700386 }
387
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700388 mDataPos = pos;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000389 if (const auto* kernelFields = maybeKernelFields()) {
390 kernelFields->mNextObjectHint = 0;
391 kernelFields->mObjectsSorted = false;
392 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700393}
394
395status_t Parcel::setDataCapacity(size_t size)
396{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700397 if (size > INT32_MAX) {
398 // don't accept size_t values which may have come from an
399 // inadvertent conversion from a negative int.
400 return BAD_VALUE;
401 }
402
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700403 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700404 return NO_ERROR;
405}
406
407status_t Parcel::setData(const uint8_t* buffer, size_t len)
408{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700409 if (len > INT32_MAX) {
410 // don't accept size_t values which may have come from an
411 // inadvertent conversion from a negative int.
412 return BAD_VALUE;
413 }
414
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700415 status_t err = restartWrite(len);
416 if (err == NO_ERROR) {
417 memcpy(const_cast<uint8_t*>(data()), buffer, len);
418 mDataSize = len;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000419 if (auto* kernelFields = maybeKernelFields()) {
420 kernelFields->mFdsKnown = false;
421 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700422 }
423 return err;
424}
425
Frederick Mayled3c595a2022-05-09 23:02:54 +0000426status_t Parcel::appendFrom(const Parcel* parcel, size_t offset, size_t len) {
427 if (isForRpc() != parcel->isForRpc()) {
Steven Moreland2034eff2021-10-13 11:24:35 -0700428 ALOGE("Cannot append Parcel from one context to another. They may be different formats, "
429 "and objects are specific to a context.");
Steven Moreland67753c32021-04-02 18:45:19 +0000430 return BAD_TYPE;
431 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000432 if (isForRpc() && maybeRpcFields()->mSession != parcel->maybeRpcFields()->mSession) {
433 ALOGE("Cannot append Parcels from different sessions");
434 return BAD_TYPE;
435 }
Steven Moreland67753c32021-04-02 18:45:19 +0000436
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437 status_t err;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000438 const uint8_t* data = parcel->mData;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700439 int startPos = mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700440
441 if (len == 0) {
442 return NO_ERROR;
443 }
444
Nick Kralevichb6b14232015-04-02 09:36:02 -0700445 if (len > INT32_MAX) {
446 // don't accept size_t values which may have come from an
447 // inadvertent conversion from a negative int.
448 return BAD_VALUE;
449 }
450
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700451 // range checks against the source parcel size
452 if ((offset > parcel->mDataSize)
453 || (len > parcel->mDataSize)
454 || (offset + len > parcel->mDataSize)) {
455 return BAD_VALUE;
456 }
457
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700458 if ((mDataSize+len) > mDataCapacity) {
459 // grow data
460 err = growData(len);
461 if (err != NO_ERROR) {
462 return err;
463 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700464 }
465
466 // append data
467 memcpy(mData + mDataPos, data + offset, len);
468 mDataPos += len;
469 mDataSize += len;
470
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400471 err = NO_ERROR;
472
Frederick Mayled3c595a2022-05-09 23:02:54 +0000473 if (auto* kernelFields = maybeKernelFields()) {
474 auto* otherKernelFields = parcel->maybeKernelFields();
475 LOG_ALWAYS_FATAL_IF(otherKernelFields == nullptr);
476
477 const binder_size_t* objects = otherKernelFields->mObjects;
478 size_t size = otherKernelFields->mObjectsSize;
479 // Count objects in range
480 int firstIndex = -1, lastIndex = -2;
481 for (int i = 0; i < (int)size; i++) {
482 size_t off = objects[i];
483 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
484 if (firstIndex == -1) {
485 firstIndex = i;
486 }
487 lastIndex = i;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700488 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700489 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000490 int numObjects = lastIndex - firstIndex + 1;
491 if (numObjects > 0) {
492 const sp<ProcessState> proc(ProcessState::self());
493 // grow objects
494 if (kernelFields->mObjectsCapacity < kernelFields->mObjectsSize + numObjects) {
495 if ((size_t)numObjects > SIZE_MAX - kernelFields->mObjectsSize)
496 return NO_MEMORY; // overflow
497 if (kernelFields->mObjectsSize + numObjects > SIZE_MAX / 3)
498 return NO_MEMORY; // overflow
499 size_t newSize = ((kernelFields->mObjectsSize + numObjects) * 3) / 2;
500 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
501 binder_size_t* objects = (binder_size_t*)realloc(kernelFields->mObjects,
502 newSize * sizeof(binder_size_t));
503 if (objects == (binder_size_t*)nullptr) {
504 return NO_MEMORY;
505 }
506 kernelFields->mObjects = objects;
507 kernelFields->mObjectsCapacity = newSize;
508 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700509
Frederick Mayled3c595a2022-05-09 23:02:54 +0000510 // append and acquire objects
511 int idx = kernelFields->mObjectsSize;
512 for (int i = firstIndex; i <= lastIndex; i++) {
513 size_t off = objects[i] - offset + startPos;
514 kernelFields->mObjects[idx++] = off;
515 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700516
Frederick Mayled3c595a2022-05-09 23:02:54 +0000517 flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(mData + off);
518 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700519
Frederick Mayled3c595a2022-05-09 23:02:54 +0000520 if (flat->hdr.type == BINDER_TYPE_FD) {
521 // If this is a file descriptor, we need to dup it so the
522 // new Parcel now owns its own fd, and can declare that we
523 // officially know we have fds.
524 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
525 flat->cookie = 1;
526 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
527 if (!mAllowFds) {
528 err = FDS_NOT_ALLOWED;
529 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400530 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700531 }
532 }
533 }
534
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400535 return err;
536}
537
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700538int Parcel::compareData(const Parcel& other) {
539 size_t size = dataSize();
540 if (size != other.dataSize()) {
541 return size < other.dataSize() ? -1 : 1;
542 }
543 return memcmp(data(), other.data(), size);
544}
545
Bernardo Rufino897b1652021-10-08 10:30:20 +0100546status_t Parcel::compareDataInRange(size_t thisOffset, const Parcel& other, size_t otherOffset,
547 size_t len, int* result) const {
548 if (len > INT32_MAX || thisOffset > INT32_MAX || otherOffset > INT32_MAX) {
549 // Don't accept size_t values which may have come from an inadvertent conversion from a
550 // negative int.
551 return BAD_VALUE;
552 }
553 size_t thisLimit;
554 if (__builtin_add_overflow(thisOffset, len, &thisLimit) || thisLimit > mDataSize) {
555 return BAD_VALUE;
556 }
557 size_t otherLimit;
558 if (__builtin_add_overflow(otherOffset, len, &otherLimit) || otherLimit > other.mDataSize) {
559 return BAD_VALUE;
560 }
561 *result = memcmp(data() + thisOffset, other.data() + otherOffset, len);
562 return NO_ERROR;
563}
564
Jeff Brown13b16042014-11-11 16:44:25 -0800565bool Parcel::allowFds() const
566{
567 return mAllowFds;
568}
569
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700570bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400571{
572 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700573 if (!allowFds) {
574 mAllowFds = false;
575 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400576 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700577}
578
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700579void Parcel::restoreAllowFds(bool lastValue)
580{
581 mAllowFds = lastValue;
582}
583
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700584bool Parcel::hasFileDescriptors() const
585{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000586 if (const auto* rpcFields = maybeRpcFields()) {
587 return false;
588 }
589 auto* kernelFields = maybeKernelFields();
590 if (!kernelFields->mFdsKnown) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700591 scanForFds();
592 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000593 return kernelFields->mHasFds;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700594}
595
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000596std::vector<sp<IBinder>> Parcel::debugReadAllStrongBinders() const {
597 std::vector<sp<IBinder>> ret;
598
Frederick Mayled3c595a2022-05-09 23:02:54 +0000599 const auto* kernelFields = maybeKernelFields();
600 if (kernelFields == nullptr) {
601 return ret;
602 }
603
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000604 size_t initPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000605 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
606 binder_size_t offset = kernelFields->mObjects[i];
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000607 const flat_binder_object* flat =
608 reinterpret_cast<const flat_binder_object*>(mData + offset);
609 if (flat->hdr.type != BINDER_TYPE_BINDER) continue;
610
611 setDataPosition(offset);
612
613 sp<IBinder> binder = readStrongBinder();
614 if (binder != nullptr) ret.push_back(binder);
615 }
616
617 setDataPosition(initPosition);
618 return ret;
619}
620
621std::vector<int> Parcel::debugReadAllFileDescriptors() const {
622 std::vector<int> ret;
623
Frederick Mayled3c595a2022-05-09 23:02:54 +0000624 const auto* kernelFields = maybeKernelFields();
625 if (kernelFields == nullptr) {
626 return ret;
627 }
628
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000629 size_t initPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000630 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
631 binder_size_t offset = kernelFields->mObjects[i];
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000632 const flat_binder_object* flat =
633 reinterpret_cast<const flat_binder_object*>(mData + offset);
634 if (flat->hdr.type != BINDER_TYPE_FD) continue;
635
636 setDataPosition(offset);
637
638 int fd = readFileDescriptor();
639 LOG_ALWAYS_FATAL_IF(fd == -1);
640 ret.push_back(fd);
641 }
642
643 setDataPosition(initPosition);
644 return ret;
645}
646
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100647status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000648 const auto* kernelFields = maybeKernelFields();
649 if (kernelFields == nullptr) {
650 return BAD_TYPE;
651 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100652 if (len > INT32_MAX || offset > INT32_MAX) {
653 // Don't accept size_t values which may have come from an inadvertent conversion from a
654 // negative int.
655 return BAD_VALUE;
656 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100657 size_t limit;
658 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100659 return BAD_VALUE;
660 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100661 *result = false;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000662 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
663 size_t pos = kernelFields->mObjects[i];
Bernardo Rufino22092af2021-10-07 14:09:24 +0100664 if (pos < offset) continue;
665 if (pos + sizeof(flat_binder_object) > offset + len) {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000666 if (kernelFields->mObjectsSorted) {
667 break;
668 } else {
669 continue;
670 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100671 }
672 const flat_binder_object* flat = reinterpret_cast<const flat_binder_object*>(mData + pos);
673 if (flat->hdr.type == BINDER_TYPE_FD) {
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100674 *result = true;
675 break;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100676 }
677 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100678 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100679}
680
Steven Morelandf183fdd2020-10-27 00:12:12 +0000681void Parcel::markSensitive() const
682{
683 mDeallocZero = true;
684}
685
Steven Moreland5553ac42020-11-11 02:14:45 +0000686void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000687 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
688
Steven Moreland5553ac42020-11-11 02:14:45 +0000689 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700690 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000691 }
692}
693
Steven Morelandc9939062021-05-05 17:57:41 +0000694void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000695 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
696 "format must be set before data is written OR on IPC data");
697
Frederick Mayled3c595a2022-05-09 23:02:54 +0000698 mVariantFields.emplace<RpcFields>(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000699}
700
701bool Parcel::isForRpc() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000702 return std::holds_alternative<RpcFields>(mVariantFields);
Steven Moreland5553ac42020-11-11 02:14:45 +0000703}
704
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000705void Parcel::updateWorkSourceRequestHeaderPosition() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000706 auto* kernelFields = maybeKernelFields();
707 if (kernelFields == nullptr) {
708 return;
709 }
710
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000711 // Only update the request headers once. We only want to point
712 // to the first headers read/written.
Frederick Mayled3c595a2022-05-09 23:02:54 +0000713 if (!kernelFields->mRequestHeaderPresent) {
714 kernelFields->mWorkSourceRequestHeaderPosition = dataPosition();
715 kernelFields->mRequestHeaderPresent = true;
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000716 }
717}
718
Steven Morelandb6c7e222021-02-18 19:20:14 +0000719#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700720constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
Yifan Hong70786532021-10-20 21:41:18 -0700721#elif defined(__ANDROID_RECOVERY__)
722constexpr int32_t kHeader = B_PACK_CHARS('R', 'E', 'C', 'O');
Steven Morelandd70160f2019-07-23 10:20:38 -0700723#else
724constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
725#endif
726
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700727// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700728status_t Parcel::writeInterfaceToken(const String16& interface)
729{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000730 return writeInterfaceToken(interface.string(), interface.size());
731}
732
733status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000734 if (auto* kernelFields = maybeKernelFields()) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000735 const IPCThreadState* threadState = IPCThreadState::self();
736 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
737 updateWorkSourceRequestHeaderPosition();
738 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
739 : IPCThreadState::kUnsetWorkSource);
740 writeInt32(kHeader);
741 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000742
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700743 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000744 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700745}
746
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000747bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
748{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000749 auto* kernelFields = maybeKernelFields();
750 if (kernelFields == nullptr) {
751 return false;
752 }
753 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000754 return false;
755 }
756
757 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000758 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000759 status_t err = writeInt32(uid);
760 setDataPosition(initialPosition);
761 return err == NO_ERROR;
762}
763
Steven Morelandf1b1e492019-05-06 15:05:13 -0700764uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000765{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000766 auto* kernelFields = maybeKernelFields();
767 if (kernelFields == nullptr) {
768 return false;
769 }
770 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000771 return IPCThreadState::kUnsetWorkSource;
772 }
773
774 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000775 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000776 uid_t uid = readInt32();
777 setDataPosition(initialPosition);
778 return uid;
779}
780
Mathias Agopian83c04462009-05-22 19:00:22 -0700781bool Parcel::checkInterface(IBinder* binder) const
782{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700783 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700784}
785
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700786bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700787 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700788{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700789 return enforceInterface(interface.string(), interface.size(), threadState);
790}
791
792bool Parcel::enforceInterface(const char16_t* interface,
793 size_t len,
794 IPCThreadState* threadState) const
795{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000796 if (auto* kernelFields = maybeKernelFields()) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000797 // StrictModePolicy.
798 int32_t strictPolicy = readInt32();
799 if (threadState == nullptr) {
800 threadState = IPCThreadState::self();
801 }
802 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
803 // For one-way calls, the callee is running entirely
804 // disconnected from the caller, so disable StrictMode entirely.
805 // Not only does disk/network usage not impact the caller, but
806 // there's no way to communicate back violations anyway.
807 threadState->setStrictModePolicy(0);
808 } else {
809 threadState->setStrictModePolicy(strictPolicy);
810 }
811 // WorkSource.
812 updateWorkSourceRequestHeaderPosition();
813 int32_t workSource = readInt32();
814 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
815 // vendor header
816 int32_t header = readInt32();
817 if (header != kHeader) {
818 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
819 header);
820 return false;
821 }
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700822 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000823
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100824 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700825 size_t parcel_interface_len;
826 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
827 if (len == parcel_interface_len &&
828 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700829 return true;
830 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700831 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700832 String8(interface, len).string(),
833 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700834 return false;
835 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700836}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700837
Jooyung Hand23f9502021-12-23 14:39:57 +0900838binder::Status Parcel::enforceNoDataAvail() const {
839 const auto n = dataAvail();
840 if (n == 0) {
841 return binder::Status::ok();
842 }
843 return binder::Status::
844 fromExceptionCode(binder::Status::Exception::EX_BAD_PARCELABLE,
845 String8::format("Parcel data not fully consumed, unread size: %zu",
846 n));
847}
848
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700849size_t Parcel::objectsCount() const
850{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000851 if (const auto* kernelFields = maybeKernelFields()) {
852 return kernelFields->mObjectsSize;
853 }
854 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700855}
856
857status_t Parcel::errorCheck() const
858{
859 return mError;
860}
861
862void Parcel::setError(status_t err)
863{
864 mError = err;
865}
866
867status_t Parcel::finishWrite(size_t len)
868{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700869 if (len > INT32_MAX) {
870 // don't accept size_t values which may have come from an
871 // inadvertent conversion from a negative int.
872 return BAD_VALUE;
873 }
874
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700875 //printf("Finish write of %d\n", len);
876 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700877 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700878 if (mDataPos > mDataSize) {
879 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700880 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700881 }
882 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
883 return NO_ERROR;
884}
885
886status_t Parcel::writeUnpadded(const void* data, size_t len)
887{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700888 if (len > INT32_MAX) {
889 // don't accept size_t values which may have come from an
890 // inadvertent conversion from a negative int.
891 return BAD_VALUE;
892 }
893
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700894 size_t end = mDataPos + len;
895 if (end < mDataPos) {
896 // integer overflow
897 return BAD_VALUE;
898 }
899
900 if (end <= mDataCapacity) {
901restart_write:
902 memcpy(mData+mDataPos, data, len);
903 return finishWrite(len);
904 }
905
906 status_t err = growData(len);
907 if (err == NO_ERROR) goto restart_write;
908 return err;
909}
910
911status_t Parcel::write(const void* data, size_t len)
912{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700913 if (len > INT32_MAX) {
914 // don't accept size_t values which may have come from an
915 // inadvertent conversion from a negative int.
916 return BAD_VALUE;
917 }
918
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700919 void* const d = writeInplace(len);
920 if (d) {
921 memcpy(d, data, len);
922 return NO_ERROR;
923 }
924 return mError;
925}
926
927void* Parcel::writeInplace(size_t len)
928{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700929 if (len > INT32_MAX) {
930 // don't accept size_t values which may have come from an
931 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700932 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700933 }
934
935 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700936
Khalid Ramadanb3d817b2021-11-18 07:02:50 +0000937 // check for integer overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700938 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700939 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700940 }
941
942 if ((mDataPos+padded) <= mDataCapacity) {
943restart_write:
944 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
945 uint8_t* const data = mData+mDataPos;
946
947 // Need to pad at end?
948 if (padded != len) {
949#if BYTE_ORDER == BIG_ENDIAN
950 static const uint32_t mask[4] = {
951 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
952 };
953#endif
954#if BYTE_ORDER == LITTLE_ENDIAN
955 static const uint32_t mask[4] = {
956 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
957 };
958#endif
959 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
960 // *reinterpret_cast<void**>(data+padded-4));
961 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
962 }
963
964 finishWrite(padded);
965 return data;
966 }
967
968 status_t err = growData(padded);
969 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700970 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700971}
972
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800973status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
974 const uint8_t* strData = (uint8_t*)str.data();
975 const size_t strLen= str.length();
976 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100977 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800978 return BAD_VALUE;
979 }
980
981 status_t err = writeInt32(utf16Len);
982 if (err) {
983 return err;
984 }
985
986 // Allocate enough bytes to hold our converted string and its terminating NULL.
987 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
988 if (!dst) {
989 return NO_MEMORY;
990 }
991
Sergio Girof4607432016-07-21 14:46:35 +0100992 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800993
994 return NO_ERROR;
995}
996
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900997
Andy Hung49198cf2020-11-18 11:02:39 -0800998status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
999status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001000
Andy Hung49198cf2020-11-18 11:02:39 -08001001status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
1002status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001003
Andy Hung49198cf2020-11-18 11:02:39 -08001004status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
1005status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
1006status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
1007status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
1008status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
1009status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
1010status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
1011status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
1012status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
1013status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
1014status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
1015status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
1016status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
1017status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
1018status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
1019status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
1020status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
1021status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
1022status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
1023status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
1024status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
1025status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
1026status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
1027status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
1028status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
1029status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
1030status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001031
Andy Hung49198cf2020-11-18 11:02:39 -08001032status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -08001033status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001034 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001035status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001036 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001037status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001038 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001039status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001040 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
1041status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001042
Andy Hung49198cf2020-11-18 11:02:39 -08001043status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
1044status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
1045status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
1046
1047status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
1048status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1049status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1050
1051status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
1052
1053status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
1054status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
1055
1056status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
1057status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
1058
1059status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
1060status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
1061status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
1062status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
1063status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
1064status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
1065status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
1066status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
1067status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
1068status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
1069status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
1070status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
1071status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
1072status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
1073status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
1074status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
1075status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
1076status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
1077status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
1078status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
1079status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
1080status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
1081status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
1082status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
1083status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
1084status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
1085status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
1086
1087status_t Parcel::readString16Vector(
1088 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
1089status_t Parcel::readString16Vector(
1090 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
1091status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
1092status_t Parcel::readUtf8VectorFromUtf16Vector(
1093 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
1094status_t Parcel::readUtf8VectorFromUtf16Vector(
1095 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
1096status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
1097
1098status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
1099status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
1100status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
1101
1102status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1103status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1104status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
1105
1106status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001107
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001108status_t Parcel::writeInt32(int32_t val)
1109{
Andreas Huber84a6d042009-08-17 13:33:27 -07001110 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001111}
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001112
1113status_t Parcel::writeUint32(uint32_t val)
1114{
1115 return writeAligned(val);
1116}
1117
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001118status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001119 if (len > INT32_MAX) {
1120 // don't accept size_t values which may have come from an
1121 // inadvertent conversion from a negative int.
1122 return BAD_VALUE;
1123 }
1124
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001125 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001126 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001127 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001128 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001129 if (ret == NO_ERROR) {
1130 ret = write(val, len * sizeof(*val));
1131 }
1132 return ret;
1133}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001134status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001135 if (len > INT32_MAX) {
1136 // don't accept size_t values which may have come from an
1137 // inadvertent conversion from a negative int.
1138 return BAD_VALUE;
1139 }
1140
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001141 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001142 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001143 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001144 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001145 if (ret == NO_ERROR) {
1146 ret = write(val, len * sizeof(*val));
1147 }
1148 return ret;
1149}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001150
Casey Dahlind6848f52015-10-15 15:44:59 -07001151status_t Parcel::writeBool(bool val)
1152{
1153 return writeInt32(int32_t(val));
1154}
1155
1156status_t Parcel::writeChar(char16_t val)
1157{
1158 return writeInt32(int32_t(val));
1159}
1160
1161status_t Parcel::writeByte(int8_t val)
1162{
1163 return writeInt32(int32_t(val));
1164}
1165
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001166status_t Parcel::writeInt64(int64_t val)
1167{
Andreas Huber84a6d042009-08-17 13:33:27 -07001168 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001169}
1170
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001171status_t Parcel::writeUint64(uint64_t val)
1172{
1173 return writeAligned(val);
1174}
1175
Serban Constantinescuf683e012013-11-05 16:53:55 +00001176status_t Parcel::writePointer(uintptr_t val)
1177{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001178 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001179}
1180
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001181status_t Parcel::writeFloat(float val)
1182{
Andreas Huber84a6d042009-08-17 13:33:27 -07001183 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001184}
1185
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001186#if defined(__mips__) && defined(__mips_hard_float)
1187
1188status_t Parcel::writeDouble(double val)
1189{
1190 union {
1191 double d;
1192 unsigned long long ll;
1193 } u;
1194 u.d = val;
1195 return writeAligned(u.ll);
1196}
1197
1198#else
1199
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001200status_t Parcel::writeDouble(double val)
1201{
Andreas Huber84a6d042009-08-17 13:33:27 -07001202 return writeAligned(val);
1203}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001204
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001205#endif
1206
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001207status_t Parcel::writeCString(const char* str)
1208{
1209 return write(str, strlen(str)+1);
1210}
1211
1212status_t Parcel::writeString8(const String8& str)
1213{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001214 return writeString8(str.string(), str.size());
1215}
1216
1217status_t Parcel::writeString8(const char* str, size_t len)
1218{
1219 if (str == nullptr) return writeInt32(-1);
1220
Jeff Sharkey18220902020-11-05 08:36:20 -07001221 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001222 status_t err = writeInt32(len);
1223 if (err == NO_ERROR) {
1224 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1225 if (data) {
1226 memcpy(data, str, len);
1227 *reinterpret_cast<char*>(data+len) = 0;
1228 return NO_ERROR;
1229 }
1230 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001231 }
1232 return err;
1233}
1234
1235status_t Parcel::writeString16(const String16& str)
1236{
1237 return writeString16(str.string(), str.size());
1238}
1239
1240status_t Parcel::writeString16(const char16_t* str, size_t len)
1241{
Yi Kong91635562018-06-07 14:38:36 -07001242 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001243
Jeff Sharkey18220902020-11-05 08:36:20 -07001244 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001245 status_t err = writeInt32(len);
1246 if (err == NO_ERROR) {
1247 len *= sizeof(char16_t);
1248 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1249 if (data) {
1250 memcpy(data, str, len);
1251 *reinterpret_cast<char16_t*>(data+len) = 0;
1252 return NO_ERROR;
1253 }
1254 err = mError;
1255 }
1256 return err;
1257}
1258
1259status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1260{
Steven Morelanda86a3562019-08-01 23:28:34 +00001261 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001262}
1263
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001264
Casey Dahlinb9872622015-11-25 15:09:45 -08001265status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1266 if (!parcelable) {
1267 return writeInt32(0);
1268 }
1269
1270 return writeParcelable(*parcelable);
1271}
1272
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001273status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001274{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001275 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001276 return BAD_TYPE;
1277
1278 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001279 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001280 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001282 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001283 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001285 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1286 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287
1288 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001289 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 return err;
1291 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001292 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001293 return err;
1294}
1295
Jeff Brown93ff1f92011-11-04 19:01:44 -07001296status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001297{
Steven Moreland5553ac42020-11-11 02:14:45 +00001298 if (isForRpc()) {
1299 ALOGE("Cannot write file descriptor to remote binder.");
1300 return BAD_TYPE;
1301 }
1302
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001303 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001304 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001305 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001306 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001307 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001308 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001309 return writeObject(obj, true);
1310}
1311
1312status_t Parcel::writeDupFileDescriptor(int fd)
1313{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001314 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001315 if (dupFd < 0) {
1316 return -errno;
1317 }
1318 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001319 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001320 close(dupFd);
1321 }
1322 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001323}
1324
Dianne Hackborn1941a402016-08-29 12:30:43 -07001325status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1326{
1327 writeInt32(0);
1328 return writeFileDescriptor(fd, takeOwnership);
1329}
1330
Ryo Hashimotobf551892018-05-31 16:58:35 +09001331status_t Parcel::writeDupParcelFileDescriptor(int fd)
1332{
1333 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1334 if (dupFd < 0) {
1335 return -errno;
1336 }
1337 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1338 if (err != OK) {
1339 close(dupFd);
1340 }
1341 return err;
1342}
1343
Christopher Wiley2cf19952016-04-11 11:09:37 -07001344status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001345 return writeDupFileDescriptor(fd.get());
1346}
1347
Jeff Brown13b16042014-11-11 16:44:25 -08001348status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001349{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001350 if (len > INT32_MAX) {
1351 // don't accept size_t values which may have come from an
1352 // inadvertent conversion from a negative int.
1353 return BAD_VALUE;
1354 }
1355
Jeff Brown13b16042014-11-11 16:44:25 -08001356 status_t status;
1357 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001358 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001359 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001360 if (status) return status;
1361
1362 void* ptr = writeInplace(len);
1363 if (!ptr) return NO_MEMORY;
1364
Jeff Brown13b16042014-11-11 16:44:25 -08001365 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001366 return NO_ERROR;
1367 }
1368
Steve Block6807e592011-10-20 11:56:00 +01001369 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001370 int fd = ashmem_create_region("Parcel Blob", len);
1371 if (fd < 0) return NO_MEMORY;
1372
1373 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1374 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001375 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001376 } else {
Yi Kong91635562018-06-07 14:38:36 -07001377 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001378 if (ptr == MAP_FAILED) {
1379 status = -errno;
1380 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001381 if (!mutableCopy) {
1382 result = ashmem_set_prot_region(fd, PROT_READ);
1383 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001384 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001385 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001386 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001387 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001388 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001389 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001390 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001391 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001392 return NO_ERROR;
1393 }
1394 }
1395 }
1396 }
1397 ::munmap(ptr, len);
1398 }
1399 ::close(fd);
1400 return status;
1401}
1402
Jeff Brown13b16042014-11-11 16:44:25 -08001403status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1404{
1405 // Must match up with what's done in writeBlob.
1406 if (!mAllowFds) return FDS_NOT_ALLOWED;
1407 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1408 if (status) return status;
1409 return writeDupFileDescriptor(fd);
1410}
1411
Mathias Agopiane1424282013-07-29 21:24:40 -07001412status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001413{
1414 status_t err;
1415
1416 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001417 const size_t len = val.getFlattenedSize();
1418 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001419
Andrei Homescu1519b982022-06-09 02:04:44 +00001420 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001421 // don't accept size_t values which may have come from an
1422 // inadvertent conversion from a negative int.
1423 return BAD_VALUE;
1424 }
1425
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001426 err = this->writeInt32(len);
1427 if (err) return err;
1428
1429 err = this->writeInt32(fd_count);
1430 if (err) return err;
1431
1432 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001433 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001434 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001435 return BAD_VALUE;
1436
Yi Kong91635562018-06-07 14:38:36 -07001437 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001438 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001439 fds = new (std::nothrow) int[fd_count];
1440 if (fds == nullptr) {
1441 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1442 return BAD_VALUE;
1443 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001444 }
1445
1446 err = val.flatten(buf, len, fds, fd_count);
1447 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1448 err = this->writeDupFileDescriptor( fds[i] );
1449 }
1450
1451 if (fd_count) {
1452 delete [] fds;
1453 }
1454
1455 return err;
1456}
1457
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001458status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1459{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001460 auto* kernelFields = maybeKernelFields();
1461 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr, "Can't write flat_binder_object to RPC Parcel");
1462
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001463 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001464 const bool enoughObjects = kernelFields->mObjectsSize < kernelFields->mObjectsCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001465 if (enoughData && enoughObjects) {
1466restart_write:
1467 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001468
Christopher Tate98e67d32015-06-03 18:44:15 -07001469 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001470 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001471 if (!mAllowFds) {
1472 // fail before modifying our object index
1473 return FDS_NOT_ALLOWED;
1474 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001475 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
Christopher Tate98e67d32015-06-03 18:44:15 -07001476 }
1477
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001478 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001479 if (nullMetaData || val.binder != 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001480 kernelFields->mObjects[kernelFields->mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001481 acquire_object(ProcessState::self(), val, this);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001482 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001483 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001484
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001485 return finishWrite(sizeof(flat_binder_object));
1486 }
1487
1488 if (!enoughData) {
1489 const status_t err = growData(sizeof(val));
1490 if (err != NO_ERROR) return err;
1491 }
1492 if (!enoughObjects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001493 if (kernelFields->mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1494 if ((kernelFields->mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
1495 size_t newSize = ((kernelFields->mObjectsSize + 2) * 3) / 2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001496 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Frederick Mayled3c595a2022-05-09 23:02:54 +00001497 binder_size_t* objects =
1498 (binder_size_t*)realloc(kernelFields->mObjects, newSize * sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001499 if (objects == nullptr) return NO_MEMORY;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001500 kernelFields->mObjects = objects;
1501 kernelFields->mObjectsCapacity = newSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001502 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001503
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001504 goto restart_write;
1505}
1506
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001507status_t Parcel::writeNoException()
1508{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001509 binder::Status status;
1510 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001511}
1512
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001513status_t Parcel::validateReadData(size_t upperBound) const
1514{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001515 const auto* kernelFields = maybeKernelFields();
1516 if (kernelFields == nullptr) {
1517 // Can't validate RPC Parcel reads because the location of binder
1518 // objects is unknown.
1519 return OK;
1520 }
1521
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001522 // Don't allow non-object reads on object data
Frederick Mayled3c595a2022-05-09 23:02:54 +00001523 if (kernelFields->mObjectsSorted || kernelFields->mObjectsSize <= 1) {
1524 data_sorted:
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001525 // Expect to check only against the next object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001526 if (kernelFields->mNextObjectHint < kernelFields->mObjectsSize &&
1527 upperBound > kernelFields->mObjects[kernelFields->mNextObjectHint]) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001528 // For some reason the current read position is greater than the next object
1529 // hint. Iterate until we find the right object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001530 size_t nextObject = kernelFields->mNextObjectHint;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001531 do {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001532 if (mDataPos < kernelFields->mObjects[nextObject] + sizeof(flat_binder_object)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001533 // Requested info overlaps with an object
1534 ALOGE("Attempt to read from protected data in Parcel %p", this);
1535 return PERMISSION_DENIED;
1536 }
1537 nextObject++;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001538 } while (nextObject < kernelFields->mObjectsSize &&
1539 upperBound > kernelFields->mObjects[nextObject]);
1540 kernelFields->mNextObjectHint = nextObject;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001541 }
1542 return NO_ERROR;
1543 }
1544 // Quickly determine if mObjects is sorted.
Frederick Mayled3c595a2022-05-09 23:02:54 +00001545 binder_size_t* currObj = kernelFields->mObjects + kernelFields->mObjectsSize - 1;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001546 binder_size_t* prevObj = currObj;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001547 while (currObj > kernelFields->mObjects) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001548 prevObj--;
1549 if(*prevObj > *currObj) {
1550 goto data_unsorted;
1551 }
1552 currObj--;
1553 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001554 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001555 goto data_sorted;
1556
1557data_unsorted:
1558 // Insertion Sort mObjects
1559 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1560 // switch to std::sort(mObjects, mObjects + mObjectsSize);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001561 for (binder_size_t* iter0 = kernelFields->mObjects + 1;
1562 iter0 < kernelFields->mObjects + kernelFields->mObjectsSize; iter0++) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001563 binder_size_t temp = *iter0;
1564 binder_size_t* iter1 = iter0 - 1;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001565 while (iter1 >= kernelFields->mObjects && *iter1 > temp) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001566 *(iter1 + 1) = *iter1;
1567 iter1--;
1568 }
1569 *(iter1 + 1) = temp;
1570 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001571 kernelFields->mNextObjectHint = 0;
1572 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001573 goto data_sorted;
1574}
1575
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001576status_t Parcel::read(void* outData, size_t len) const
1577{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001578 if (len > INT32_MAX) {
1579 // don't accept size_t values which may have come from an
1580 // inadvertent conversion from a negative int.
1581 return BAD_VALUE;
1582 }
1583
1584 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1585 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001586 const auto* kernelFields = maybeKernelFields();
1587 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001588 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001589 if(err != NO_ERROR) {
1590 // Still increment the data position by the expected length
1591 mDataPos += pad_size(len);
1592 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1593 return err;
1594 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001595 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001596 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001597 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001598 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001599 return NO_ERROR;
1600 }
1601 return NOT_ENOUGH_DATA;
1602}
1603
1604const void* Parcel::readInplace(size_t len) const
1605{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001606 if (len > INT32_MAX) {
1607 // don't accept size_t values which may have come from an
1608 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001609 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001610 }
1611
1612 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1613 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001614 const auto* kernelFields = maybeKernelFields();
1615 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001616 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001617 if(err != NO_ERROR) {
1618 // Still increment the data position by the expected length
1619 mDataPos += pad_size(len);
1620 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001621 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001622 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001623 }
1624
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001625 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001626 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001627 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001628 return data;
1629 }
Yi Kong91635562018-06-07 14:38:36 -07001630 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001631}
1632
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001633status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1634 if (status_t status = readInt32(size); status != OK) return status;
1635 if (*size < 0) return OK; // may be null, client to handle
1636
1637 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1638
1639 // approximation, can't know max element size (e.g. if it makes heap
1640 // allocations)
1641 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1642 int32_t allocationSize;
1643 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1644
1645 // High limit of 1MB since something this big could never be returned. Could
1646 // probably scope this down, but might impact very specific usecases.
1647 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1648
1649 if (allocationSize >= kMaxAllocationSize) {
1650 return NO_MEMORY;
1651 }
1652
1653 return OK;
1654}
1655
Andreas Huber84a6d042009-08-17 13:33:27 -07001656template<class T>
1657status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001658 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001659 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001660
1661 if ((mDataPos+sizeof(T)) <= mDataSize) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001662 const auto* kernelFields = maybeKernelFields();
1663 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001664 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001665 if(err != NO_ERROR) {
1666 // Still increment the data position by the expected length
1667 mDataPos += sizeof(T);
1668 return err;
1669 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001670 }
1671
Andrei Homescue33238e2022-03-29 22:50:00 +00001672 memcpy(pArg, mData + mDataPos, sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001673 mDataPos += sizeof(T);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001674 return NO_ERROR;
1675 } else {
1676 return NOT_ENOUGH_DATA;
1677 }
1678}
1679
Andreas Huber84a6d042009-08-17 13:33:27 -07001680template<class T>
1681T Parcel::readAligned() const {
1682 T result;
1683 if (readAligned(&result) != NO_ERROR) {
1684 result = 0;
1685 }
1686
1687 return result;
1688}
1689
1690template<class T>
1691status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001692 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001693 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001694
1695 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1696restart_write:
Andrei Homescue33238e2022-03-29 22:50:00 +00001697 memcpy(mData + mDataPos, &val, sizeof(val));
Andreas Huber84a6d042009-08-17 13:33:27 -07001698 return finishWrite(sizeof(val));
1699 }
1700
1701 status_t err = growData(sizeof(val));
1702 if (err == NO_ERROR) goto restart_write;
1703 return err;
1704}
1705
1706status_t Parcel::readInt32(int32_t *pArg) const
1707{
1708 return readAligned(pArg);
1709}
1710
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001711int32_t Parcel::readInt32() const
1712{
Andreas Huber84a6d042009-08-17 13:33:27 -07001713 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001714}
1715
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001716status_t Parcel::readUint32(uint32_t *pArg) const
1717{
1718 return readAligned(pArg);
1719}
1720
1721uint32_t Parcel::readUint32() const
1722{
1723 return readAligned<uint32_t>();
1724}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001725
1726status_t Parcel::readInt64(int64_t *pArg) const
1727{
Andreas Huber84a6d042009-08-17 13:33:27 -07001728 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001729}
1730
1731
1732int64_t Parcel::readInt64() const
1733{
Andreas Huber84a6d042009-08-17 13:33:27 -07001734 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001735}
1736
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001737status_t Parcel::readUint64(uint64_t *pArg) const
1738{
1739 return readAligned(pArg);
1740}
1741
1742uint64_t Parcel::readUint64() const
1743{
1744 return readAligned<uint64_t>();
1745}
1746
Serban Constantinescuf683e012013-11-05 16:53:55 +00001747status_t Parcel::readPointer(uintptr_t *pArg) const
1748{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001749 status_t ret;
1750 binder_uintptr_t ptr;
1751 ret = readAligned(&ptr);
1752 if (!ret)
1753 *pArg = ptr;
1754 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001755}
1756
1757uintptr_t Parcel::readPointer() const
1758{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001759 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001760}
1761
1762
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001763status_t Parcel::readFloat(float *pArg) const
1764{
Andreas Huber84a6d042009-08-17 13:33:27 -07001765 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001766}
1767
1768
1769float Parcel::readFloat() const
1770{
Andreas Huber84a6d042009-08-17 13:33:27 -07001771 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001772}
1773
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001774#if defined(__mips__) && defined(__mips_hard_float)
1775
1776status_t Parcel::readDouble(double *pArg) const
1777{
1778 union {
1779 double d;
1780 unsigned long long ll;
1781 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001782 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001783 status_t status;
1784 status = readAligned(&u.ll);
1785 *pArg = u.d;
1786 return status;
1787}
1788
1789double Parcel::readDouble() const
1790{
1791 union {
1792 double d;
1793 unsigned long long ll;
1794 } u;
1795 u.ll = readAligned<unsigned long long>();
1796 return u.d;
1797}
1798
1799#else
1800
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001801status_t Parcel::readDouble(double *pArg) const
1802{
Andreas Huber84a6d042009-08-17 13:33:27 -07001803 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001804}
1805
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001806double Parcel::readDouble() const
1807{
Andreas Huber84a6d042009-08-17 13:33:27 -07001808 return readAligned<double>();
1809}
1810
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001811#endif
1812
Casey Dahlind6848f52015-10-15 15:44:59 -07001813status_t Parcel::readBool(bool *pArg) const
1814{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001815 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001816 status_t ret = readInt32(&tmp);
1817 *pArg = (tmp != 0);
1818 return ret;
1819}
1820
1821bool Parcel::readBool() const
1822{
1823 return readInt32() != 0;
1824}
1825
1826status_t Parcel::readChar(char16_t *pArg) const
1827{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001828 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001829 status_t ret = readInt32(&tmp);
1830 *pArg = char16_t(tmp);
1831 return ret;
1832}
1833
1834char16_t Parcel::readChar() const
1835{
1836 return char16_t(readInt32());
1837}
1838
1839status_t Parcel::readByte(int8_t *pArg) const
1840{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001841 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001842 status_t ret = readInt32(&tmp);
1843 *pArg = int8_t(tmp);
1844 return ret;
1845}
1846
1847int8_t Parcel::readByte() const
1848{
1849 return int8_t(readInt32());
1850}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001851
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001852status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1853 size_t utf16Size = 0;
1854 const char16_t* src = readString16Inplace(&utf16Size);
1855 if (!src) {
1856 return UNEXPECTED_NULL;
1857 }
1858
1859 // Save ourselves the trouble, we're done.
1860 if (utf16Size == 0u) {
1861 str->clear();
1862 return NO_ERROR;
1863 }
1864
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001865 // Allow for closing '\0'
1866 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1867 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001868 return BAD_VALUE;
1869 }
1870 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001871 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001872 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001873 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1874 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001875 return NO_ERROR;
1876}
1877
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001878const char* Parcel::readCString() const
1879{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001880 if (mDataPos < mDataSize) {
1881 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001882 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1883 // is the string's trailing NUL within the parcel's valid bounds?
1884 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1885 if (eos) {
1886 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001887 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001888 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001889 return str;
1890 }
1891 }
Yi Kong91635562018-06-07 14:38:36 -07001892 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001893}
1894
1895String8 Parcel::readString8() const
1896{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001897 size_t len;
1898 const char* str = readString8Inplace(&len);
1899 if (str) return String8(str, len);
1900 ALOGE("Reading a NULL string not supported here.");
1901 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07001902}
1903
1904status_t Parcel::readString8(String8* pArg) const
1905{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001906 size_t len;
1907 const char* str = readString8Inplace(&len);
1908 if (str) {
1909 pArg->setTo(str, len);
1910 return 0;
1911 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07001912 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001913 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07001914 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001915}
1916
1917const char* Parcel::readString8Inplace(size_t* outLen) const
1918{
1919 int32_t size = readInt32();
1920 // watch for potential int overflow from size+1
1921 if (size >= 0 && size < INT32_MAX) {
1922 *outLen = size;
1923 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00001924 if (str != nullptr) {
1925 if (str[size] == '\0') {
1926 return str;
1927 }
1928 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001929 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001930 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001931 *outLen = 0;
1932 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001933}
1934
1935String16 Parcel::readString16() const
1936{
1937 size_t len;
1938 const char16_t* str = readString16Inplace(&len);
1939 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001940 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001941 return String16();
1942}
1943
Casey Dahlinb9872622015-11-25 15:09:45 -08001944
Casey Dahlin451ff582015-10-19 18:12:18 -07001945status_t Parcel::readString16(String16* pArg) const
1946{
1947 size_t len;
1948 const char16_t* str = readString16Inplace(&len);
1949 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001950 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001951 return 0;
1952 } else {
1953 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001954 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001955 }
1956}
1957
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001958const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1959{
1960 int32_t size = readInt32();
1961 // watch for potential int overflow from size+1
1962 if (size >= 0 && size < INT32_MAX) {
1963 *outLen = size;
1964 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00001965 if (str != nullptr) {
1966 if (str[size] == u'\0') {
1967 return str;
1968 }
1969 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001970 }
1971 }
1972 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001973 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001974}
1975
Casey Dahlinf0c13772015-10-27 18:33:56 -07001976status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1977{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001978 status_t status = readNullableStrongBinder(val);
1979 if (status == OK && !val->get()) {
Steven Morelanddce98242022-03-11 00:26:10 +00001980 ALOGW("Expecting binder but got null!");
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001981 status = UNEXPECTED_NULL;
1982 }
1983 return status;
1984}
1985
1986status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1987{
Steven Morelanda86a3562019-08-01 23:28:34 +00001988 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001989}
1990
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001991sp<IBinder> Parcel::readStrongBinder() const
1992{
1993 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001994 // Note that a lot of code in Android reads binders by hand with this
1995 // method, and that code has historically been ok with getting nullptr
1996 // back (while ignoring error codes).
1997 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001998 return val;
1999}
2000
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002001int32_t Parcel::readExceptionCode() const
2002{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002003 binder::Status status;
2004 status.readFromParcel(*this);
2005 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002006}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002007
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002008native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002009{
2010 int numFds, numInts;
2011 status_t err;
2012 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002013 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002014 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002015 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002016
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002017 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002018 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002019 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002020 }
2021
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002022 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002023 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002024 if (h->data[i] < 0) {
2025 for (int j = 0; j < i; j++) {
2026 close(h->data[j]);
2027 }
2028 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002029 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002030 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002031 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002032 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002033 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002034 native_handle_close(h);
2035 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002036 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002037 }
2038 return h;
2039}
2040
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002041int Parcel::readFileDescriptor() const
2042{
2043 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002044
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002045 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002046 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002047 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002048
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002049 return BAD_TYPE;
2050}
2051
Dianne Hackborn1941a402016-08-29 12:30:43 -07002052int Parcel::readParcelFileDescriptor() const
2053{
2054 int32_t hasComm = readInt32();
2055 int fd = readFileDescriptor();
2056 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002057 // detach (owned by the binder driver)
2058 int comm = readFileDescriptor();
2059
2060 // warning: this must be kept in sync with:
2061 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2062 enum ParcelFileDescriptorStatus {
2063 DETACHED = 2,
2064 };
2065
2066#if BYTE_ORDER == BIG_ENDIAN
2067 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2068#endif
2069#if BYTE_ORDER == LITTLE_ENDIAN
2070 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2071#endif
2072
2073 ssize_t written = TEMP_FAILURE_RETRY(
2074 ::write(comm, &message, sizeof(message)));
2075
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08002076 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002077 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2078 written, strerror(errno));
2079 return BAD_TYPE;
2080 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002081 }
2082 return fd;
2083}
2084
Christopher Wiley2cf19952016-04-11 11:09:37 -07002085status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002086{
2087 int got = readFileDescriptor();
2088
2089 if (got == BAD_TYPE) {
2090 return BAD_TYPE;
2091 }
2092
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002093 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002094
2095 if (val->get() < 0) {
2096 return BAD_VALUE;
2097 }
2098
2099 return OK;
2100}
2101
Ryo Hashimotobf551892018-05-31 16:58:35 +09002102status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2103{
2104 int got = readParcelFileDescriptor();
2105
2106 if (got == BAD_TYPE) {
2107 return BAD_TYPE;
2108 }
2109
2110 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2111
2112 if (val->get() < 0) {
2113 return BAD_VALUE;
2114 }
2115
2116 return OK;
2117}
Casey Dahlin06673e32015-11-23 13:24:23 -08002118
Jeff Brown5707dbf2011-09-23 21:17:56 -07002119status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2120{
Jeff Brown13b16042014-11-11 16:44:25 -08002121 int32_t blobType;
2122 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002123 if (status) return status;
2124
Jeff Brown13b16042014-11-11 16:44:25 -08002125 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002126 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002127 const void* ptr = readInplace(len);
2128 if (!ptr) return BAD_VALUE;
2129
Jeff Brown13b16042014-11-11 16:44:25 -08002130 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002131 return NO_ERROR;
2132 }
2133
Steve Block6807e592011-10-20 11:56:00 +01002134 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002135 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002136 int fd = readFileDescriptor();
2137 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2138
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002139 if (!ashmem_valid(fd)) {
2140 ALOGE("invalid fd");
2141 return BAD_VALUE;
2142 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002143 int size = ashmem_get_size_region(fd);
2144 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002145 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002146 return BAD_VALUE;
2147 }
Yi Kong91635562018-06-07 14:38:36 -07002148 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002149 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002150 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002151
Jeff Brown13b16042014-11-11 16:44:25 -08002152 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002153 return NO_ERROR;
2154}
2155
Mathias Agopiane1424282013-07-29 21:24:40 -07002156status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002157{
2158 // size
2159 const size_t len = this->readInt32();
2160 const size_t fd_count = this->readInt32();
2161
Andrei Homescu1519b982022-06-09 02:04:44 +00002162 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002163 // don't accept size_t values which may have come from an
2164 // inadvertent conversion from a negative int.
2165 return BAD_VALUE;
2166 }
2167
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002168 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002169 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002170 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002171 return BAD_VALUE;
2172
Yi Kong91635562018-06-07 14:38:36 -07002173 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002174 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002175 fds = new (std::nothrow) int[fd_count];
2176 if (fds == nullptr) {
2177 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2178 return BAD_VALUE;
2179 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002180 }
2181
2182 status_t err = NO_ERROR;
2183 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002184 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002185 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002186 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002187 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 -07002188 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2189 // Close all the file descriptors that were dup-ed.
2190 for (size_t j=0; j<i ;j++) {
2191 close(fds[j]);
2192 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002193 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002194 }
2195
2196 if (err == NO_ERROR) {
2197 err = val.unflatten(buf, len, fds, fd_count);
2198 }
2199
2200 if (fd_count) {
2201 delete [] fds;
2202 }
2203
2204 return err;
2205}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002206const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2207{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002208 const auto* kernelFields = maybeKernelFields();
2209 if (kernelFields == nullptr) {
2210 return nullptr;
2211 }
2212
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002213 const size_t DPOS = mDataPos;
2214 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2215 const flat_binder_object* obj
2216 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2217 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002218 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002219 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002220 // the object list, so we don't want to check for it when
2221 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002222 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002223 return obj;
2224 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002225
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002226 // Ensure that this object is valid...
Frederick Mayled3c595a2022-05-09 23:02:54 +00002227 binder_size_t* const OBJS = kernelFields->mObjects;
2228 const size_t N = kernelFields->mObjectsSize;
2229 size_t opos = kernelFields->mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002230
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002232 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002233 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002234
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002235 // Start at the current hint position, looking for an object at
2236 // the current data position.
2237 if (opos < N) {
2238 while (opos < (N-1) && OBJS[opos] < DPOS) {
2239 opos++;
2240 }
2241 } else {
2242 opos = N-1;
2243 }
2244 if (OBJS[opos] == DPOS) {
2245 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002246 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002247 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002248 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002249 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002250 return obj;
2251 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002252
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002253 // Look backwards for it...
2254 while (opos > 0 && OBJS[opos] > DPOS) {
2255 opos--;
2256 }
2257 if (OBJS[opos] == DPOS) {
2258 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002259 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002260 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002261 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002262 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002263 return obj;
2264 }
2265 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002266 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 -07002267 this, DPOS);
2268 }
Yi Kong91635562018-06-07 14:38:36 -07002269 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002270}
2271
Frederick Mayled3c595a2022-05-09 23:02:54 +00002272void Parcel::closeFileDescriptors() {
2273 auto* kernelFields = maybeKernelFields();
2274 if (kernelFields == nullptr) {
2275 return;
2276 }
2277 size_t i = kernelFields->mObjectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002278 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002279 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002280 }
2281 while (i > 0) {
2282 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002283 const flat_binder_object* flat =
2284 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002285 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002286 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002287 close(flat->handle);
2288 }
2289 }
2290}
2291
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002292uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002293{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002294 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002295}
2296
2297size_t Parcel::ipcDataSize() const
2298{
2299 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2300}
2301
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002302uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002303{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002304 if (const auto* kernelFields = maybeKernelFields()) {
2305 return reinterpret_cast<uintptr_t>(kernelFields->mObjects);
2306 }
2307 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002308}
2309
2310size_t Parcel::ipcObjectsCount() const
2311{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002312 if (const auto* kernelFields = maybeKernelFields()) {
2313 return kernelFields->mObjectsSize;
2314 }
2315 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002316}
2317
Frederick Mayled3c595a2022-05-09 23:02:54 +00002318void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, const binder_size_t* objects,
2319 size_t objectsCount, release_func relFunc) {
Steven Moreland438cce82021-04-02 18:04:08 +00002320 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2321 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2322
Steven Morelandceed9bb2020-12-17 01:01:06 +00002323 freeData();
2324
Frederick Mayled3c595a2022-05-09 23:02:54 +00002325 auto* kernelFields = maybeKernelFields();
2326 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr); // guaranteed by freeData.
2327
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002328 mData = const_cast<uint8_t*>(data);
2329 mDataSize = mDataCapacity = dataSize;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002330 kernelFields->mObjects = const_cast<binder_size_t*>(objects);
2331 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002332 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002333
2334 binder_size_t minOffset = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002335 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
2336 binder_size_t offset = kernelFields->mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002337 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002338 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002339 __func__, (uint64_t)offset, (uint64_t)minOffset);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002340 kernelFields->mObjectsSize = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002341 break;
2342 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002343 const flat_binder_object* flat
2344 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2345 uint32_t type = flat->hdr.type;
2346 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2347 type == BINDER_TYPE_FD)) {
2348 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2349 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
Steven Morelandf2e0a952021-11-01 18:17:23 -07002350 // recover gracefully by clearing out the objects.
Martijn Coenen82c75312019-07-24 15:18:30 +02002351 android_errorWriteLog(0x534e4554, "135930648");
Steven Morelandf2e0a952021-11-01 18:17:23 -07002352 android_errorWriteLog(0x534e4554, "203847542");
Martijn Coenen82c75312019-07-24 15:18:30 +02002353 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2354 __func__, type, (uint64_t)offset);
Steven Morelandf2e0a952021-11-01 18:17:23 -07002355
2356 // WARNING: callers of ipcSetDataReference need to make sure they
2357 // don't rely on mObjectsSize in their release_func.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002358 kernelFields->mObjectsSize = 0;
Martijn Coenen82c75312019-07-24 15:18:30 +02002359 break;
2360 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002361 minOffset = offset + sizeof(flat_binder_object);
2362 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002363 scanForFds();
2364}
2365
Frederick Mayled3c595a2022-05-09 23:02:54 +00002366void Parcel::rpcSetDataReference(const sp<RpcSession>& session, const uint8_t* data,
2367 size_t dataSize, release_func relFunc) {
2368 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2369 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2370
2371 LOG_ALWAYS_FATAL_IF(session == nullptr);
2372
2373 freeData();
2374 markForRpc(session);
2375
2376 mData = const_cast<uint8_t*>(data);
2377 mDataSize = mDataCapacity = dataSize;
2378 mOwner = relFunc;
2379}
2380
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002381void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002382{
2383 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002384
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002385 if (errorCheck() != NO_ERROR) {
2386 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002387 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002388 } else if (dataSize() > 0) {
2389 const uint8_t* DATA = data();
2390 to << indent << HexDump(DATA, dataSize()) << dedent;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002391 if (const auto* kernelFields = maybeKernelFields()) {
2392 const binder_size_t* OBJS = kernelFields->mObjects;
2393 const size_t N = objectsCount();
2394 for (size_t i = 0; i < N; i++) {
2395 const flat_binder_object* flat =
2396 reinterpret_cast<const flat_binder_object*>(DATA + OBJS[i]);
2397 to << endl
2398 << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2399 << TypeCode(flat->hdr.type & 0x7f7f7f00) << " = " << flat->binder;
2400 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002401 }
2402 } else {
2403 to << "NULL";
2404 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002405
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002406 to << ")";
2407}
2408
2409void Parcel::releaseObjects()
2410{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002411 auto* kernelFields = maybeKernelFields();
2412 if (kernelFields == nullptr) {
2413 return;
2414 }
2415
2416 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002417 if (i == 0) {
2418 return;
2419 }
2420 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002422 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002423 while (i > 0) {
2424 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002425 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002426 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427 }
2428}
2429
2430void Parcel::acquireObjects()
2431{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002432 auto* kernelFields = maybeKernelFields();
2433 if (kernelFields == nullptr) {
2434 return;
2435 }
2436
2437 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002438 if (i == 0) {
2439 return;
2440 }
2441 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002442 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002443 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002444 while (i > 0) {
2445 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002446 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002447 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002448 }
2449}
2450
2451void Parcel::freeData()
2452{
2453 freeDataNoInit();
2454 initState();
2455}
2456
2457void Parcel::freeDataNoInit()
2458{
2459 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002460 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002461 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002462 auto* kernelFields = maybeKernelFields();
2463 mOwner(this, mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
2464 kernelFields ? kernelFields->mObjectsSize : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002465 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002466 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002467 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002468 if (mData) {
2469 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002470 gParcelGlobalAllocSize -= mDataCapacity;
2471 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002472 if (mDeallocZero) {
2473 zeroMemory(mData, mDataSize);
2474 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002475 free(mData);
2476 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002477 auto* kernelFields = maybeKernelFields();
2478 if (kernelFields && kernelFields->mObjects) free(kernelFields->mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479 }
2480}
2481
2482status_t Parcel::growData(size_t len)
2483{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002484 if (len > INT32_MAX) {
2485 // don't accept size_t values which may have come from an
2486 // inadvertent conversion from a negative int.
2487 return BAD_VALUE;
2488 }
2489
Martijn Coenen93fe5182020-01-22 10:46:25 +01002490 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2491 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002492 size_t newSize = ((mDataSize+len)*3)/2;
2493 return (newSize <= mDataSize)
2494 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002495 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002496}
2497
Steven Morelandf183fdd2020-10-27 00:12:12 +00002498static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2499 if (!zero) {
2500 return (uint8_t*)realloc(data, newCapacity);
2501 }
2502 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2503 if (!newData) {
2504 return nullptr;
2505 }
2506
2507 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2508 zeroMemory(data, oldCapacity);
2509 free(data);
2510 return newData;
2511}
2512
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513status_t Parcel::restartWrite(size_t desired)
2514{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002515 if (desired > INT32_MAX) {
2516 // don't accept size_t values which may have come from an
2517 // inadvertent conversion from a negative int.
2518 return BAD_VALUE;
2519 }
2520
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002521 if (mOwner) {
2522 freeData();
2523 return continueWrite(desired);
2524 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002525
Steven Morelandf183fdd2020-10-27 00:12:12 +00002526 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002527 if (!data && desired > mDataCapacity) {
2528 mError = NO_MEMORY;
2529 return NO_MEMORY;
2530 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002531
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002532 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002533
Devin Moore4a0a55e2020-06-04 13:23:10 -07002534 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002535 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002536 if (mDataCapacity > desired) {
2537 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2538 } else {
2539 gParcelGlobalAllocSize += (desired - mDataCapacity);
2540 }
2541
Colin Cross83ec65e2015-12-08 17:15:50 -08002542 if (!mData) {
2543 gParcelGlobalAllocCount++;
2544 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002545 mData = data;
2546 mDataCapacity = desired;
2547 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002548
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002549 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002550 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2551 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2552
Frederick Mayled3c595a2022-05-09 23:02:54 +00002553 if (auto* kernelFields = maybeKernelFields()) {
2554 free(kernelFields->mObjects);
2555 kernelFields->mObjects = nullptr;
2556 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = 0;
2557 kernelFields->mNextObjectHint = 0;
2558 kernelFields->mObjectsSorted = false;
2559 kernelFields->mHasFds = false;
2560 kernelFields->mFdsKnown = true;
2561 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002562 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002563
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002564 return NO_ERROR;
2565}
2566
2567status_t Parcel::continueWrite(size_t desired)
2568{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002569 if (desired > INT32_MAX) {
2570 // don't accept size_t values which may have come from an
2571 // inadvertent conversion from a negative int.
2572 return BAD_VALUE;
2573 }
2574
Frederick Mayled3c595a2022-05-09 23:02:54 +00002575 auto* kernelFields = maybeKernelFields();
2576
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002577 // If shrinking, first adjust for any objects that appear
2578 // after the new data size.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002579 size_t objectsSize = kernelFields ? kernelFields->mObjectsSize : 0;
2580 if (kernelFields && desired < mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002581 if (desired == 0) {
2582 objectsSize = 0;
2583 } else {
2584 while (objectsSize > 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002585 if (kernelFields->mObjects[objectsSize - 1] < desired) break;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002586 objectsSize--;
2587 }
2588 }
2589 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002590
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002591 if (mOwner) {
2592 // If the size is going to zero, just release the owner's data.
2593 if (desired == 0) {
2594 freeData();
2595 return NO_ERROR;
2596 }
2597
2598 // If there is a different owner, we need to take
2599 // posession.
2600 uint8_t* data = (uint8_t*)malloc(desired);
2601 if (!data) {
2602 mError = NO_MEMORY;
2603 return NO_MEMORY;
2604 }
Yi Kong91635562018-06-07 14:38:36 -07002605 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002606
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002607 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002608 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002609 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002610 free(data);
2611
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002612 mError = NO_MEMORY;
2613 return NO_MEMORY;
2614 }
2615
2616 // Little hack to only acquire references on objects
2617 // we will be keeping.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002618 size_t oldObjectsSize = kernelFields->mObjectsSize;
2619 kernelFields->mObjectsSize = objectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002620 acquireObjects();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002621 kernelFields->mObjectsSize = oldObjectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002622 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002623
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002624 if (mData) {
2625 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2626 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002627 if (objects && kernelFields && kernelFields->mObjects) {
2628 memcpy(objects, kernelFields->mObjects, objectsSize * sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002629 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002630 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002631 mOwner(this, mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
2632 kernelFields ? kernelFields->mObjectsSize : 0);
Yi Kong91635562018-06-07 14:38:36 -07002633 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002634
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002635 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002636 gParcelGlobalAllocSize += desired;
2637 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002638
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002639 mData = data;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002640 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002641 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002642 mDataCapacity = desired;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002643 if (kernelFields) {
2644 kernelFields->mObjects = objects;
2645 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsSize;
2646 kernelFields->mNextObjectHint = 0;
2647 kernelFields->mObjectsSorted = false;
2648 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002649
2650 } else if (mData) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002651 if (kernelFields && objectsSize < kernelFields->mObjectsSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002652 // Need to release refs on any objects we are dropping.
2653 const sp<ProcessState> proc(ProcessState::self());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002654 for (size_t i = objectsSize; i < kernelFields->mObjectsSize; i++) {
2655 const flat_binder_object* flat =
2656 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002657 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002658 // will need to rescan because we may have lopped off the only FDs
Frederick Mayled3c595a2022-05-09 23:02:54 +00002659 kernelFields->mFdsKnown = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002660 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07002661 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002662 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002663
2664 if (objectsSize == 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002665 free(kernelFields->mObjects);
2666 kernelFields->mObjects = nullptr;
2667 kernelFields->mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002668 } else {
2669 binder_size_t* objects =
Frederick Mayled3c595a2022-05-09 23:02:54 +00002670 (binder_size_t*)realloc(kernelFields->mObjects,
2671 objectsSize * sizeof(binder_size_t));
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002672 if (objects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002673 kernelFields->mObjects = objects;
2674 kernelFields->mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002675 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002676 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002677 kernelFields->mObjectsSize = objectsSize;
2678 kernelFields->mNextObjectHint = 0;
2679 kernelFields->mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002680 }
2681
2682 // We own the data, so we can just do a realloc().
2683 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002684 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002685 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002686 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2687 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002688 gParcelGlobalAllocSize += desired;
2689 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002690 mData = data;
2691 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002692 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002693 mError = NO_MEMORY;
2694 return NO_MEMORY;
2695 }
2696 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002697 if (mDataSize > desired) {
2698 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002699 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002700 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002701 if (mDataPos > desired) {
2702 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002703 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002704 }
2705 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002706
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002707 } else {
2708 // This is the first data. Easy!
2709 uint8_t* data = (uint8_t*)malloc(desired);
2710 if (!data) {
2711 mError = NO_MEMORY;
2712 return NO_MEMORY;
2713 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002714
Frederick Mayled3c595a2022-05-09 23:02:54 +00002715 if (!(mDataCapacity == 0 &&
2716 (kernelFields == nullptr ||
2717 (kernelFields->mObjects == nullptr && kernelFields->mObjectsCapacity == 0)))) {
2718 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity,
2719 kernelFields ? kernelFields->mObjects : nullptr,
2720 kernelFields ? kernelFields->mObjectsCapacity : 0, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002721 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002722
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002723 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002724 gParcelGlobalAllocSize += desired;
2725 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002726
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002727 mData = data;
2728 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002729 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2730 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002731 mDataCapacity = desired;
2732 }
2733
2734 return NO_ERROR;
2735}
2736
2737void Parcel::initState()
2738{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002739 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002740 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002741 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002742 mDataSize = 0;
2743 mDataCapacity = 0;
2744 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002745 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2746 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002747 mVariantFields.emplace<KernelFields>();
Steven Moreland6e5a7752019-08-05 20:30:14 -07002748 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002749 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07002750 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002751}
2752
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01002753void Parcel::scanForFds() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002754 auto* kernelFields = maybeKernelFields();
2755 if (kernelFields == nullptr) {
2756 return;
2757 }
2758 status_t status = hasFileDescriptorsInRange(0, dataSize(), &kernelFields->mHasFds);
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01002759 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002760 kernelFields->mFdsKnown = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002761}
2762
Dan Sandleraa5c2342015-04-10 10:08:45 -04002763size_t Parcel::getBlobAshmemSize() const
2764{
Adrian Roos6bb31142015-10-22 16:46:12 -07002765 // This used to return the size of all blobs that were written to ashmem, now we're returning
2766 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07002767 // TODO(b/202029388): Remove method once ABI can be changed.
2768 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04002769}
2770
Adrian Rooscbf37262015-10-22 16:12:53 -07002771size_t Parcel::getOpenAshmemSize() const
2772{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002773 auto* kernelFields = maybeKernelFields();
2774 if (kernelFields == nullptr) {
2775 return 0;
2776 }
2777
Steven Morelandc673f1f2021-10-07 18:23:35 -07002778 size_t openAshmemSize = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002779 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
Steven Morelandc673f1f2021-10-07 18:23:35 -07002780 const flat_binder_object* flat =
Frederick Mayled3c595a2022-05-09 23:02:54 +00002781 reinterpret_cast<const flat_binder_object*>(mData + kernelFields->mObjects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002782
2783 // cookie is compared against zero for historical reasons
2784 // > obj.cookie = takeOwnership ? 1 : 0;
2785 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
2786 int size = ashmem_get_size_region(flat->handle);
2787 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
2788 ALOGE("Overflow when computing ashmem size.");
2789 return SIZE_MAX;
2790 }
2791 }
2792 }
2793 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002794}
2795
2796// --- Parcel::Blob ---
2797
2798Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002799 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002800}
2801
2802Parcel::Blob::~Blob() {
2803 release();
2804}
2805
2806void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002807 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002808 ::munmap(mData, mSize);
2809 }
2810 clear();
2811}
2812
Jeff Brown13b16042014-11-11 16:44:25 -08002813void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2814 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002815 mData = data;
2816 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002817 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002818}
2819
2820void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002821 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002822 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002823 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002824 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002825}
2826
Steven Moreland61ff8492019-09-26 16:05:45 -07002827} // namespace android