blob: 232a70c8944c7d2d15a83bf4ede4c3dcbcde65f1 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Steven Morelandbf1915b2020-07-16 22:43:02 +000023#include <linux/sched.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080024#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080029#include <sys/stat.h>
30#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070031#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080032#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070033
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/Binder.h>
35#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080036#include <binder/IPCThreadState.h>
37#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070038#include <binder/ProcessState.h>
Steven Moreland6e5a7752019-08-05 20:30:14 -070039#include <binder/Stability.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080040#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041#include <binder/TextOutput.h>
42
Mark Salyzynabed7f72016-01-27 08:02:48 -080043#include <cutils/ashmem.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000044#include <cutils/compiler.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080045#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#include <utils/Log.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String16.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000048#include <utils/String8.h>
49#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070050
Steven Moreland5553ac42020-11-11 02:14:45 +000051#include "RpcState.h"
Steven Morelanda4853cd2019-07-12 15:44:37 -070052#include "Static.h"
Steven Morelandf183fdd2020-10-27 00:12:12 +000053#include "Utils.h"
Steven Moreland6ba5a252021-05-04 22:49:00 +000054#include "binder_module.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070055
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070056#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080057//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080058#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080059//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070060
61// ---------------------------------------------------------------------------
62
Nick Kralevichb6b14232015-04-02 09:36:02 -070063// This macro should never be used at runtime, as a too large value
64// of s could cause an integer overflow. Instead, you should always
65// use the wrapper function pad_size()
66#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
67
68static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070069 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070070 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070071 }
72 return PAD_SIZE_UNSAFE(s);
73}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070074
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070075// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060076#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070077
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070078namespace android {
79
Steven Moreland7b102262019-08-01 15:48:43 -070080// many things compile this into prebuilts on the stack
Steven Moreland90c1f9a2021-05-03 18:27:24 +000081#ifdef __LP64__
82static_assert(sizeof(Parcel) == 120);
83#else
84static_assert(sizeof(Parcel) == 60);
85#endif
Steven Moreland7b102262019-08-01 15:48:43 -070086
Jeff Sharkey8994c182020-09-11 12:07:10 -060087static std::atomic<size_t> gParcelGlobalAllocCount;
88static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -080089
Christopher Tatee4e0ae82016-03-24 16:03:44 -070090static size_t gMaxFds = 0;
91
Jeff Brown13b16042014-11-11 16:44:25 -080092// Maximum size of a blob to transfer in-place.
93static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
94
95enum {
96 BLOB_INPLACE = 0,
97 BLOB_ASHMEM_IMMUTABLE = 1,
98 BLOB_ASHMEM_MUTABLE = 2,
99};
100
Steven Morelandb1c81202019-04-05 18:49:55 -0700101static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700102 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700103{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700104 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700105 case BINDER_TYPE_BINDER:
106 if (obj.binder) {
107 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800108 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 }
110 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700111 case BINDER_TYPE_HANDLE: {
112 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700113 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700114 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
115 b->incStrong(who);
116 }
117 return;
118 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700119 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000120 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700121 // If we own an ashmem fd, keep track of how much memory it refers to.
122 int size = ashmem_get_size_region(obj.handle);
123 if (size > 0) {
124 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700125 }
126 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700127 return;
128 }
129 }
130
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700131 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700132}
133
Adrian Roos6bb31142015-10-22 16:46:12 -0700134static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700135 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700137 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700138 case BINDER_TYPE_BINDER:
139 if (obj.binder) {
140 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800141 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142 }
143 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144 case BINDER_TYPE_HANDLE: {
145 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700146 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
148 b->decStrong(who);
149 }
150 return;
151 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700152 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800153 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000154 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700155 int size = ashmem_get_size_region(obj.handle);
156 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800157 // ashmem size might have changed since last time it was accounted for, e.g.
158 // in acquire_object(). Value of *outAshmemSize is not critical since we are
159 // releasing the object anyway. Check for integer overflow condition.
160 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700161 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700162 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800163
164 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700165 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700166 return;
167 }
168 }
169
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700170 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700171}
172
Steven Moreland34b48cb2020-12-01 22:45:38 +0000173status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700174{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700175 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000176 int16_t rep = internal::Stability::getCategory(binder.get()).repr();
177 return writeInt32(rep);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700178}
179
Steven Morelanda86a3562019-08-01 23:28:34 +0000180status_t Parcel::finishUnflattenBinder(
181 const sp<IBinder>& binder, sp<IBinder>* out) const
182{
183 int32_t stability;
184 status_t status = readInt32(&stability);
185 if (status != OK) return status;
186
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000187 status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability),
188 true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000189 if (status != OK) return status;
190
191 *out = binder;
192 return OK;
193}
194
Steven Morelandbf1915b2020-07-16 22:43:02 +0000195static constexpr inline int schedPolicyMask(int policy, int priority) {
196 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
197}
198
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500199status_t Parcel::flattenBinder(const sp<IBinder>& binder) {
200 BBinder* local = nullptr;
201 if (binder) local = binder->localBinder();
202 if (local) local->setParceled();
203
Steven Moreland5553ac42020-11-11 02:14:45 +0000204 if (isForRpc()) {
205 if (binder) {
206 status_t status = writeInt32(1); // non-null
207 if (status != OK) return status;
208 RpcAddress address = RpcAddress::zero();
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000209 status = mSession->state()->onBinderLeaving(mSession, binder, &address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000210 if (status != OK) return status;
211 status = address.writeToParcel(this);
212 if (status != OK) return status;
213 } else {
214 status_t status = writeInt32(0); // null
215 if (status != OK) return status;
216 }
217 return finishFlattenBinder(binder);
218 }
219
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700220 flat_binder_object obj;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000221 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700222
Steven Morelandbf1915b2020-07-16 22:43:02 +0000223 int schedBits = 0;
224 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
225 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700226 }
227
Yi Kong91635562018-06-07 14:38:36 -0700228 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229 if (!local) {
230 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700231 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000232 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000233 } else {
234 if (proxy->isRpcBinder()) {
235 ALOGE("Sending a socket binder over RPC is prohibited");
236 return INVALID_OPERATION;
237 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000239 const int32_t handle = proxy ? proxy->getPrivateAccessorForId().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700240 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800241 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700242 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800243 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700244 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000245 int policy = local->getMinSchedulerPolicy();
246 int priority = local->getMinSchedulerPriority();
247
248 if (policy != 0 || priority != 0) {
249 // override value, since it is set explicitly
250 schedBits = schedPolicyMask(policy, priority);
251 }
Steven Morelandf0212002018-12-26 13:59:23 -0800252 if (local->isRequestingSid()) {
253 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
254 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000255 if (local->isInheritRt()) {
256 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
257 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700258 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800259 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
260 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261 }
262 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700263 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800264 obj.binder = 0;
265 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700266 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700267
Steven Morelandbf1915b2020-07-16 22:43:02 +0000268 obj.flags |= schedBits;
269
Steven Moreland34b48cb2020-12-01 22:45:38 +0000270 status_t status = writeObject(obj, false);
271 if (status != OK) return status;
272
273 return finishFlattenBinder(binder);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700274}
275
Steven Morelanda86a3562019-08-01 23:28:34 +0000276status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700277{
Steven Moreland5553ac42020-11-11 02:14:45 +0000278 if (isForRpc()) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000279 LOG_ALWAYS_FATAL_IF(mSession == nullptr, "RpcSession required to read from remote parcel");
Steven Moreland5553ac42020-11-11 02:14:45 +0000280
281 int32_t isNull;
282 status_t status = readInt32(&isNull);
283 if (status != OK) return status;
284
285 sp<IBinder> binder;
286
287 if (isNull & 1) {
288 auto addr = RpcAddress::zero();
Steven Moreland7227c8a2021-06-02 00:24:32 +0000289 if (status_t status = addr.readFromParcel(*this); status != OK) return status;
290 if (status_t status = mSession->state()->onBinderEntering(mSession, addr, &binder);
291 status != OK)
292 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000293 }
294
295 return finishUnflattenBinder(binder, out);
296 }
297
Steven Morelanda86a3562019-08-01 23:28:34 +0000298 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700299
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700300 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700301 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000302 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000303 sp<IBinder> binder =
304 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000305 return finishUnflattenBinder(binder, out);
306 }
307 case BINDER_TYPE_HANDLE: {
308 sp<IBinder> binder =
309 ProcessState::self()->getStrongProxyForHandle(flat->handle);
310 return finishUnflattenBinder(binder, out);
311 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700312 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700313 }
314 return BAD_TYPE;
315}
316
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700317// ---------------------------------------------------------------------------
318
319Parcel::Parcel()
320{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800321 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700322 initState();
323}
324
325Parcel::~Parcel()
326{
327 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800328 LOG_ALLOC("Parcel %p: destroyed", this);
329}
330
331size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600332 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800333}
334
335size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600336 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700337}
338
339const uint8_t* Parcel::data() const
340{
341 return mData;
342}
343
344size_t Parcel::dataSize() const
345{
346 return (mDataSize > mDataPos ? mDataSize : mDataPos);
347}
348
349size_t Parcel::dataAvail() const
350{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700351 size_t result = dataSize() - dataPosition();
352 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700353 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700354 }
355 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700356}
357
358size_t Parcel::dataPosition() const
359{
360 return mDataPos;
361}
362
363size_t Parcel::dataCapacity() const
364{
365 return mDataCapacity;
366}
367
368status_t Parcel::setDataSize(size_t size)
369{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700370 if (size > INT32_MAX) {
371 // don't accept size_t values which may have come from an
372 // inadvertent conversion from a negative int.
373 return BAD_VALUE;
374 }
375
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700376 status_t err;
377 err = continueWrite(size);
378 if (err == NO_ERROR) {
379 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700380 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700381 }
382 return err;
383}
384
385void Parcel::setDataPosition(size_t pos) const
386{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700387 if (pos > INT32_MAX) {
388 // don't accept size_t values which may have come from an
389 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700390 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700391 }
392
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700393 mDataPos = pos;
394 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800395 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700396}
397
398status_t Parcel::setDataCapacity(size_t size)
399{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700400 if (size > INT32_MAX) {
401 // don't accept size_t values which may have come from an
402 // inadvertent conversion from a negative int.
403 return BAD_VALUE;
404 }
405
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700406 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700407 return NO_ERROR;
408}
409
410status_t Parcel::setData(const uint8_t* buffer, size_t len)
411{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700412 if (len > INT32_MAX) {
413 // don't accept size_t values which may have come from an
414 // inadvertent conversion from a negative int.
415 return BAD_VALUE;
416 }
417
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700418 status_t err = restartWrite(len);
419 if (err == NO_ERROR) {
420 memcpy(const_cast<uint8_t*>(data()), buffer, len);
421 mDataSize = len;
422 mFdsKnown = false;
423 }
424 return err;
425}
426
Andreas Huber51faf462011-04-13 10:21:56 -0700427status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700428{
Steven Moreland67753c32021-04-02 18:45:19 +0000429 if (parcel->isForRpc() != isForRpc()) {
430 ALOGE("Cannot append Parcel of one format to another.");
431 return BAD_TYPE;
432 }
433
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700435 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800436 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437 size_t size = parcel->mObjectsSize;
438 int startPos = mDataPos;
439 int firstIndex = -1, lastIndex = -2;
440
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
458 // Count objects in range
459 for (int i = 0; i < (int) size; i++) {
460 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700461 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700462 if (firstIndex == -1) {
463 firstIndex = i;
464 }
465 lastIndex = i;
466 }
467 }
468 int numObjects = lastIndex - firstIndex + 1;
469
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700470 if ((mDataSize+len) > mDataCapacity) {
471 // grow data
472 err = growData(len);
473 if (err != NO_ERROR) {
474 return err;
475 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700476 }
477
478 // append data
479 memcpy(mData + mDataPos, data + offset, len);
480 mDataPos += len;
481 mDataSize += len;
482
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400483 err = NO_ERROR;
484
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700485 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200486 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700487 // grow objects
488 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +0100489 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
490 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700491 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +0100492 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800493 binder_size_t *objects =
494 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700495 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700496 return NO_MEMORY;
497 }
498 mObjects = objects;
499 mObjectsCapacity = newSize;
500 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700501
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700502 // append and acquire objects
503 int idx = mObjectsSize;
504 for (int i = firstIndex; i <= lastIndex; i++) {
505 size_t off = objects[i] - offset + startPos;
506 mObjects[idx++] = off;
507 mObjectsSize++;
508
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700509 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700510 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700511 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700512
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700513 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700514 // If this is a file descriptor, we need to dup it so the
515 // new Parcel now owns its own fd, and can declare that we
516 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800517 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800518 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700519 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400520 if (!mAllowFds) {
521 err = FDS_NOT_ALLOWED;
522 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700523 }
524 }
525 }
526
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400527 return err;
528}
529
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700530int Parcel::compareData(const Parcel& other) {
531 size_t size = dataSize();
532 if (size != other.dataSize()) {
533 return size < other.dataSize() ? -1 : 1;
534 }
535 return memcmp(data(), other.data(), size);
536}
537
Jeff Brown13b16042014-11-11 16:44:25 -0800538bool Parcel::allowFds() const
539{
540 return mAllowFds;
541}
542
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700543bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400544{
545 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700546 if (!allowFds) {
547 mAllowFds = false;
548 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400549 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700550}
551
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700552void Parcel::restoreAllowFds(bool lastValue)
553{
554 mAllowFds = lastValue;
555}
556
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700557bool Parcel::hasFileDescriptors() const
558{
559 if (!mFdsKnown) {
560 scanForFds();
561 }
562 return mHasFds;
563}
564
Steven Morelandf183fdd2020-10-27 00:12:12 +0000565void Parcel::markSensitive() const
566{
567 mDeallocZero = true;
568}
569
Steven Moreland5553ac42020-11-11 02:14:45 +0000570void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000571 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
572
Steven Moreland5553ac42020-11-11 02:14:45 +0000573 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000574 markForRpc(binder->remoteBinder()->getPrivateAccessorForId().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000575 }
576}
577
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000578void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000579 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
580 "format must be set before data is written OR on IPC data");
581
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000582 LOG_ALWAYS_FATAL_IF(session == nullptr, "markForRpc requires session");
583 mSession = session;
Steven Moreland5553ac42020-11-11 02:14:45 +0000584}
585
586bool Parcel::isForRpc() const {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000587 return mSession != nullptr;
Steven Moreland5553ac42020-11-11 02:14:45 +0000588}
589
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000590void Parcel::updateWorkSourceRequestHeaderPosition() const {
591 // Only update the request headers once. We only want to point
592 // to the first headers read/written.
593 if (!mRequestHeaderPresent) {
594 mWorkSourceRequestHeaderPosition = dataPosition();
595 mRequestHeaderPresent = true;
596 }
597}
598
Jooyung Han1b228f42020-01-30 13:41:12 +0900599#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700600constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
601#else
602constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
603#endif
604
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700605// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700606status_t Parcel::writeInterfaceToken(const String16& interface)
607{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000608 return writeInterfaceToken(interface.string(), interface.size());
609}
610
611status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000612 if (CC_LIKELY(!isForRpc())) {
613 const IPCThreadState* threadState = IPCThreadState::self();
614 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
615 updateWorkSourceRequestHeaderPosition();
616 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
617 : IPCThreadState::kUnsetWorkSource);
618 writeInt32(kHeader);
619 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000620
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700621 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000622 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700623}
624
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000625bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
626{
627 if (!mRequestHeaderPresent) {
628 return false;
629 }
630
631 const size_t initialPosition = dataPosition();
632 setDataPosition(mWorkSourceRequestHeaderPosition);
633 status_t err = writeInt32(uid);
634 setDataPosition(initialPosition);
635 return err == NO_ERROR;
636}
637
Steven Morelandf1b1e492019-05-06 15:05:13 -0700638uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000639{
640 if (!mRequestHeaderPresent) {
641 return IPCThreadState::kUnsetWorkSource;
642 }
643
644 const size_t initialPosition = dataPosition();
645 setDataPosition(mWorkSourceRequestHeaderPosition);
646 uid_t uid = readInt32();
647 setDataPosition(initialPosition);
648 return uid;
649}
650
Mathias Agopian83c04462009-05-22 19:00:22 -0700651bool Parcel::checkInterface(IBinder* binder) const
652{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700653 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700654}
655
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700656bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700657 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700658{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700659 return enforceInterface(interface.string(), interface.size(), threadState);
660}
661
662bool Parcel::enforceInterface(const char16_t* interface,
663 size_t len,
664 IPCThreadState* threadState) const
665{
Steven Moreland3af936a2021-03-26 03:05:38 +0000666 if (CC_LIKELY(!isForRpc())) {
667 // StrictModePolicy.
668 int32_t strictPolicy = readInt32();
669 if (threadState == nullptr) {
670 threadState = IPCThreadState::self();
671 }
672 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
673 // For one-way calls, the callee is running entirely
674 // disconnected from the caller, so disable StrictMode entirely.
675 // Not only does disk/network usage not impact the caller, but
676 // there's no way to communicate back violations anyway.
677 threadState->setStrictModePolicy(0);
678 } else {
679 threadState->setStrictModePolicy(strictPolicy);
680 }
681 // WorkSource.
682 updateWorkSourceRequestHeaderPosition();
683 int32_t workSource = readInt32();
684 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
685 // vendor header
686 int32_t header = readInt32();
687 if (header != kHeader) {
688 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
689 header);
690 return false;
691 }
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700692 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000693
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100694 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700695 size_t parcel_interface_len;
696 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
697 if (len == parcel_interface_len &&
698 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700699 return true;
700 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700701 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700702 String8(interface, len).string(),
703 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700704 return false;
705 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700706}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700707
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700708size_t Parcel::objectsCount() const
709{
710 return mObjectsSize;
711}
712
713status_t Parcel::errorCheck() const
714{
715 return mError;
716}
717
718void Parcel::setError(status_t err)
719{
720 mError = err;
721}
722
723status_t Parcel::finishWrite(size_t len)
724{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700725 if (len > INT32_MAX) {
726 // don't accept size_t values which may have come from an
727 // inadvertent conversion from a negative int.
728 return BAD_VALUE;
729 }
730
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700731 //printf("Finish write of %d\n", len);
732 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700733 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700734 if (mDataPos > mDataSize) {
735 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700736 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700737 }
738 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
739 return NO_ERROR;
740}
741
742status_t Parcel::writeUnpadded(const void* data, size_t len)
743{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700744 if (len > INT32_MAX) {
745 // don't accept size_t values which may have come from an
746 // inadvertent conversion from a negative int.
747 return BAD_VALUE;
748 }
749
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700750 size_t end = mDataPos + len;
751 if (end < mDataPos) {
752 // integer overflow
753 return BAD_VALUE;
754 }
755
756 if (end <= mDataCapacity) {
757restart_write:
758 memcpy(mData+mDataPos, data, len);
759 return finishWrite(len);
760 }
761
762 status_t err = growData(len);
763 if (err == NO_ERROR) goto restart_write;
764 return err;
765}
766
767status_t Parcel::write(const void* data, size_t len)
768{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700769 if (len > INT32_MAX) {
770 // don't accept size_t values which may have come from an
771 // inadvertent conversion from a negative int.
772 return BAD_VALUE;
773 }
774
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700775 void* const d = writeInplace(len);
776 if (d) {
777 memcpy(d, data, len);
778 return NO_ERROR;
779 }
780 return mError;
781}
782
783void* Parcel::writeInplace(size_t len)
784{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700785 if (len > INT32_MAX) {
786 // don't accept size_t values which may have come from an
787 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700788 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700789 }
790
791 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700792
793 // sanity check for integer overflow
794 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700795 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700796 }
797
798 if ((mDataPos+padded) <= mDataCapacity) {
799restart_write:
800 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
801 uint8_t* const data = mData+mDataPos;
802
803 // Need to pad at end?
804 if (padded != len) {
805#if BYTE_ORDER == BIG_ENDIAN
806 static const uint32_t mask[4] = {
807 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
808 };
809#endif
810#if BYTE_ORDER == LITTLE_ENDIAN
811 static const uint32_t mask[4] = {
812 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
813 };
814#endif
815 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
816 // *reinterpret_cast<void**>(data+padded-4));
817 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
818 }
819
820 finishWrite(padded);
821 return data;
822 }
823
824 status_t err = growData(padded);
825 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700826 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700827}
828
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800829status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
830 const uint8_t* strData = (uint8_t*)str.data();
831 const size_t strLen= str.length();
832 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100833 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800834 return BAD_VALUE;
835 }
836
837 status_t err = writeInt32(utf16Len);
838 if (err) {
839 return err;
840 }
841
842 // Allocate enough bytes to hold our converted string and its terminating NULL.
843 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
844 if (!dst) {
845 return NO_MEMORY;
846 }
847
Sergio Girof4607432016-07-21 14:46:35 +0100848 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800849
850 return NO_ERROR;
851}
852
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900853
Andy Hung49198cf2020-11-18 11:02:39 -0800854status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
855status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800856
Andy Hung49198cf2020-11-18 11:02:39 -0800857status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
858status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700859
Andy Hung49198cf2020-11-18 11:02:39 -0800860status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
861status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
862status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
863status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
864status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
865status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
866status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
867status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
868status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
869status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
870status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
871status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
872status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
873status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
874status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
875status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
876status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
877status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
878status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
879status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
880status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
881status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
882status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
883status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
884status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
885status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
886status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700887
Andy Hung49198cf2020-11-18 11:02:39 -0800888status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800889status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800890 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900891status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800892 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800893status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800894 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900895status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800896 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
897status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800898
Andy Hung49198cf2020-11-18 11:02:39 -0800899status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
900status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
901status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
902
903status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
904status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
905status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
906
907status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
908
909status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
910status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
911
912status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
913status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
914
915status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
916status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
917status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
918status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
919status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
920status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
921status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
922status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
923status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
924status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
925status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
926status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
927status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
928status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
929status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
930status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
931status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
932status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
933status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
934status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
935status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
936status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
937status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
938status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
939status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
940status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
941status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
942
943status_t Parcel::readString16Vector(
944 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
945status_t Parcel::readString16Vector(
946 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
947status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
948status_t Parcel::readUtf8VectorFromUtf16Vector(
949 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
950status_t Parcel::readUtf8VectorFromUtf16Vector(
951 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
952status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
953
954status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
955status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
956status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
957
958status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
959status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
960status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
961
962status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800963
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700964status_t Parcel::writeInt32(int32_t val)
965{
Andreas Huber84a6d042009-08-17 13:33:27 -0700966 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700967}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800968
969status_t Parcel::writeUint32(uint32_t val)
970{
971 return writeAligned(val);
972}
973
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700974status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700975 if (len > INT32_MAX) {
976 // don't accept size_t values which may have come from an
977 // inadvertent conversion from a negative int.
978 return BAD_VALUE;
979 }
980
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700981 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700982 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700983 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700984 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700985 if (ret == NO_ERROR) {
986 ret = write(val, len * sizeof(*val));
987 }
988 return ret;
989}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700990status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700991 if (len > INT32_MAX) {
992 // don't accept size_t values which may have come from an
993 // inadvertent conversion from a negative int.
994 return BAD_VALUE;
995 }
996
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700997 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700998 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700999 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001000 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001001 if (ret == NO_ERROR) {
1002 ret = write(val, len * sizeof(*val));
1003 }
1004 return ret;
1005}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001006
Casey Dahlind6848f52015-10-15 15:44:59 -07001007status_t Parcel::writeBool(bool val)
1008{
1009 return writeInt32(int32_t(val));
1010}
1011
1012status_t Parcel::writeChar(char16_t val)
1013{
1014 return writeInt32(int32_t(val));
1015}
1016
1017status_t Parcel::writeByte(int8_t val)
1018{
1019 return writeInt32(int32_t(val));
1020}
1021
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001022status_t Parcel::writeInt64(int64_t val)
1023{
Andreas Huber84a6d042009-08-17 13:33:27 -07001024 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001025}
1026
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001027status_t Parcel::writeUint64(uint64_t val)
1028{
1029 return writeAligned(val);
1030}
1031
Serban Constantinescuf683e012013-11-05 16:53:55 +00001032status_t Parcel::writePointer(uintptr_t val)
1033{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001034 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001035}
1036
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001037status_t Parcel::writeFloat(float val)
1038{
Andreas Huber84a6d042009-08-17 13:33:27 -07001039 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001040}
1041
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001042#if defined(__mips__) && defined(__mips_hard_float)
1043
1044status_t Parcel::writeDouble(double val)
1045{
1046 union {
1047 double d;
1048 unsigned long long ll;
1049 } u;
1050 u.d = val;
1051 return writeAligned(u.ll);
1052}
1053
1054#else
1055
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001056status_t Parcel::writeDouble(double val)
1057{
Andreas Huber84a6d042009-08-17 13:33:27 -07001058 return writeAligned(val);
1059}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001060
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001061#endif
1062
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001063status_t Parcel::writeCString(const char* str)
1064{
1065 return write(str, strlen(str)+1);
1066}
1067
1068status_t Parcel::writeString8(const String8& str)
1069{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001070 return writeString8(str.string(), str.size());
1071}
1072
1073status_t Parcel::writeString8(const char* str, size_t len)
1074{
1075 if (str == nullptr) return writeInt32(-1);
1076
Jeff Sharkey18220902020-11-05 08:36:20 -07001077 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001078 status_t err = writeInt32(len);
1079 if (err == NO_ERROR) {
1080 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1081 if (data) {
1082 memcpy(data, str, len);
1083 *reinterpret_cast<char*>(data+len) = 0;
1084 return NO_ERROR;
1085 }
1086 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001087 }
1088 return err;
1089}
1090
1091status_t Parcel::writeString16(const String16& str)
1092{
1093 return writeString16(str.string(), str.size());
1094}
1095
1096status_t Parcel::writeString16(const char16_t* str, size_t len)
1097{
Yi Kong91635562018-06-07 14:38:36 -07001098 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001099
Jeff Sharkey18220902020-11-05 08:36:20 -07001100 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001101 status_t err = writeInt32(len);
1102 if (err == NO_ERROR) {
1103 len *= sizeof(char16_t);
1104 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1105 if (data) {
1106 memcpy(data, str, len);
1107 *reinterpret_cast<char16_t*>(data+len) = 0;
1108 return NO_ERROR;
1109 }
1110 err = mError;
1111 }
1112 return err;
1113}
1114
1115status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1116{
Steven Morelanda86a3562019-08-01 23:28:34 +00001117 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001118}
1119
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001120
Casey Dahlinb9872622015-11-25 15:09:45 -08001121status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1122 if (!parcelable) {
1123 return writeInt32(0);
1124 }
1125
1126 return writeParcelable(*parcelable);
1127}
1128
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001129status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001130{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001131 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001132 return BAD_TYPE;
1133
1134 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001135 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001136 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001138 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001139 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001141 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1142 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143
1144 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001145 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146 return err;
1147 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001148 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001149 return err;
1150}
1151
Jeff Brown93ff1f92011-11-04 19:01:44 -07001152status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001153{
Steven Moreland5553ac42020-11-11 02:14:45 +00001154 if (isForRpc()) {
1155 ALOGE("Cannot write file descriptor to remote binder.");
1156 return BAD_TYPE;
1157 }
1158
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001159 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001160 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001161 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001162 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001163 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001164 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001165 return writeObject(obj, true);
1166}
1167
1168status_t Parcel::writeDupFileDescriptor(int fd)
1169{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001170 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001171 if (dupFd < 0) {
1172 return -errno;
1173 }
1174 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001175 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001176 close(dupFd);
1177 }
1178 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001179}
1180
Dianne Hackborn1941a402016-08-29 12:30:43 -07001181status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1182{
1183 writeInt32(0);
1184 return writeFileDescriptor(fd, takeOwnership);
1185}
1186
Ryo Hashimotobf551892018-05-31 16:58:35 +09001187status_t Parcel::writeDupParcelFileDescriptor(int fd)
1188{
1189 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1190 if (dupFd < 0) {
1191 return -errno;
1192 }
1193 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1194 if (err != OK) {
1195 close(dupFd);
1196 }
1197 return err;
1198}
1199
Christopher Wiley2cf19952016-04-11 11:09:37 -07001200status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001201 return writeDupFileDescriptor(fd.get());
1202}
1203
Jeff Brown13b16042014-11-11 16:44:25 -08001204status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001205{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001206 if (len > INT32_MAX) {
1207 // don't accept size_t values which may have come from an
1208 // inadvertent conversion from a negative int.
1209 return BAD_VALUE;
1210 }
1211
Jeff Brown13b16042014-11-11 16:44:25 -08001212 status_t status;
1213 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001214 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001215 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001216 if (status) return status;
1217
1218 void* ptr = writeInplace(len);
1219 if (!ptr) return NO_MEMORY;
1220
Jeff Brown13b16042014-11-11 16:44:25 -08001221 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001222 return NO_ERROR;
1223 }
1224
Steve Block6807e592011-10-20 11:56:00 +01001225 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001226 int fd = ashmem_create_region("Parcel Blob", len);
1227 if (fd < 0) return NO_MEMORY;
1228
1229 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1230 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001231 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001232 } else {
Yi Kong91635562018-06-07 14:38:36 -07001233 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001234 if (ptr == MAP_FAILED) {
1235 status = -errno;
1236 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001237 if (!mutableCopy) {
1238 result = ashmem_set_prot_region(fd, PROT_READ);
1239 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001240 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001241 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001242 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001243 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001244 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001245 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001246 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001247 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001248 return NO_ERROR;
1249 }
1250 }
1251 }
1252 }
1253 ::munmap(ptr, len);
1254 }
1255 ::close(fd);
1256 return status;
1257}
1258
Jeff Brown13b16042014-11-11 16:44:25 -08001259status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1260{
1261 // Must match up with what's done in writeBlob.
1262 if (!mAllowFds) return FDS_NOT_ALLOWED;
1263 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1264 if (status) return status;
1265 return writeDupFileDescriptor(fd);
1266}
1267
Mathias Agopiane1424282013-07-29 21:24:40 -07001268status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001269{
1270 status_t err;
1271
1272 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001273 const size_t len = val.getFlattenedSize();
1274 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001275
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001276 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001277 // don't accept size_t values which may have come from an
1278 // inadvertent conversion from a negative int.
1279 return BAD_VALUE;
1280 }
1281
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001282 err = this->writeInt32(len);
1283 if (err) return err;
1284
1285 err = this->writeInt32(fd_count);
1286 if (err) return err;
1287
1288 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001289 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001290 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001291 return BAD_VALUE;
1292
Yi Kong91635562018-06-07 14:38:36 -07001293 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001294 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001295 fds = new (std::nothrow) int[fd_count];
1296 if (fds == nullptr) {
1297 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1298 return BAD_VALUE;
1299 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001300 }
1301
1302 err = val.flatten(buf, len, fds, fd_count);
1303 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1304 err = this->writeDupFileDescriptor( fds[i] );
1305 }
1306
1307 if (fd_count) {
1308 delete [] fds;
1309 }
1310
1311 return err;
1312}
1313
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001314status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1315{
1316 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1317 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1318 if (enoughData && enoughObjects) {
1319restart_write:
1320 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001321
Christopher Tate98e67d32015-06-03 18:44:15 -07001322 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001323 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001324 if (!mAllowFds) {
1325 // fail before modifying our object index
1326 return FDS_NOT_ALLOWED;
1327 }
1328 mHasFds = mFdsKnown = true;
1329 }
1330
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001331 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001332 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001333 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001334 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001335 mObjectsSize++;
1336 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001337
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001338 return finishWrite(sizeof(flat_binder_object));
1339 }
1340
1341 if (!enoughData) {
1342 const status_t err = growData(sizeof(val));
1343 if (err != NO_ERROR) return err;
1344 }
1345 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001346 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1347 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001348 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001349 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001350 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001351 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001352 mObjects = objects;
1353 mObjectsCapacity = newSize;
1354 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001355
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001356 goto restart_write;
1357}
1358
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001359status_t Parcel::writeNoException()
1360{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001361 binder::Status status;
1362 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001363}
1364
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001365status_t Parcel::validateReadData(size_t upperBound) const
1366{
1367 // Don't allow non-object reads on object data
1368 if (mObjectsSorted || mObjectsSize <= 1) {
1369data_sorted:
1370 // Expect to check only against the next object
1371 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1372 // For some reason the current read position is greater than the next object
1373 // hint. Iterate until we find the right object
1374 size_t nextObject = mNextObjectHint;
1375 do {
1376 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1377 // Requested info overlaps with an object
1378 ALOGE("Attempt to read from protected data in Parcel %p", this);
1379 return PERMISSION_DENIED;
1380 }
1381 nextObject++;
1382 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1383 mNextObjectHint = nextObject;
1384 }
1385 return NO_ERROR;
1386 }
1387 // Quickly determine if mObjects is sorted.
1388 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1389 binder_size_t* prevObj = currObj;
1390 while (currObj > mObjects) {
1391 prevObj--;
1392 if(*prevObj > *currObj) {
1393 goto data_unsorted;
1394 }
1395 currObj--;
1396 }
1397 mObjectsSorted = true;
1398 goto data_sorted;
1399
1400data_unsorted:
1401 // Insertion Sort mObjects
1402 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1403 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1404 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1405 binder_size_t temp = *iter0;
1406 binder_size_t* iter1 = iter0 - 1;
1407 while (iter1 >= mObjects && *iter1 > temp) {
1408 *(iter1 + 1) = *iter1;
1409 iter1--;
1410 }
1411 *(iter1 + 1) = temp;
1412 }
1413 mNextObjectHint = 0;
1414 mObjectsSorted = true;
1415 goto data_sorted;
1416}
1417
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001418status_t Parcel::read(void* outData, size_t len) const
1419{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001420 if (len > INT32_MAX) {
1421 // 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
1426 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1427 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001428 if (mObjectsSize > 0) {
1429 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001430 if(err != NO_ERROR) {
1431 // Still increment the data position by the expected length
1432 mDataPos += pad_size(len);
1433 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1434 return err;
1435 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001436 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001437 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001438 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001439 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001440 return NO_ERROR;
1441 }
1442 return NOT_ENOUGH_DATA;
1443}
1444
1445const void* Parcel::readInplace(size_t len) const
1446{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001447 if (len > INT32_MAX) {
1448 // don't accept size_t values which may have come from an
1449 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001450 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001451 }
1452
1453 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1454 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001455 if (mObjectsSize > 0) {
1456 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001457 if(err != NO_ERROR) {
1458 // Still increment the data position by the expected length
1459 mDataPos += pad_size(len);
1460 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001461 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001462 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001463 }
1464
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001465 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001466 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001467 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001468 return data;
1469 }
Yi Kong91635562018-06-07 14:38:36 -07001470 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001471}
1472
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001473status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1474 if (status_t status = readInt32(size); status != OK) return status;
1475 if (*size < 0) return OK; // may be null, client to handle
1476
1477 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1478
1479 // approximation, can't know max element size (e.g. if it makes heap
1480 // allocations)
1481 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1482 int32_t allocationSize;
1483 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1484
1485 // High limit of 1MB since something this big could never be returned. Could
1486 // probably scope this down, but might impact very specific usecases.
1487 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1488
1489 if (allocationSize >= kMaxAllocationSize) {
1490 return NO_MEMORY;
1491 }
1492
1493 return OK;
1494}
1495
Andreas Huber84a6d042009-08-17 13:33:27 -07001496template<class T>
1497status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001498 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001499
1500 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001501 if (mObjectsSize > 0) {
1502 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001503 if(err != NO_ERROR) {
1504 // Still increment the data position by the expected length
1505 mDataPos += sizeof(T);
1506 return err;
1507 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001508 }
1509
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001510 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001511 mDataPos += sizeof(T);
1512 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001513 return NO_ERROR;
1514 } else {
1515 return NOT_ENOUGH_DATA;
1516 }
1517}
1518
Andreas Huber84a6d042009-08-17 13:33:27 -07001519template<class T>
1520T Parcel::readAligned() const {
1521 T result;
1522 if (readAligned(&result) != NO_ERROR) {
1523 result = 0;
1524 }
1525
1526 return result;
1527}
1528
1529template<class T>
1530status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001531 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001532
1533 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1534restart_write:
1535 *reinterpret_cast<T*>(mData+mDataPos) = val;
1536 return finishWrite(sizeof(val));
1537 }
1538
1539 status_t err = growData(sizeof(val));
1540 if (err == NO_ERROR) goto restart_write;
1541 return err;
1542}
1543
1544status_t Parcel::readInt32(int32_t *pArg) const
1545{
1546 return readAligned(pArg);
1547}
1548
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001549int32_t Parcel::readInt32() const
1550{
Andreas Huber84a6d042009-08-17 13:33:27 -07001551 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001552}
1553
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001554status_t Parcel::readUint32(uint32_t *pArg) const
1555{
1556 return readAligned(pArg);
1557}
1558
1559uint32_t Parcel::readUint32() const
1560{
1561 return readAligned<uint32_t>();
1562}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001563
1564status_t Parcel::readInt64(int64_t *pArg) const
1565{
Andreas Huber84a6d042009-08-17 13:33:27 -07001566 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001567}
1568
1569
1570int64_t Parcel::readInt64() const
1571{
Andreas Huber84a6d042009-08-17 13:33:27 -07001572 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001573}
1574
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001575status_t Parcel::readUint64(uint64_t *pArg) const
1576{
1577 return readAligned(pArg);
1578}
1579
1580uint64_t Parcel::readUint64() const
1581{
1582 return readAligned<uint64_t>();
1583}
1584
Serban Constantinescuf683e012013-11-05 16:53:55 +00001585status_t Parcel::readPointer(uintptr_t *pArg) const
1586{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001587 status_t ret;
1588 binder_uintptr_t ptr;
1589 ret = readAligned(&ptr);
1590 if (!ret)
1591 *pArg = ptr;
1592 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001593}
1594
1595uintptr_t Parcel::readPointer() const
1596{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001597 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001598}
1599
1600
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001601status_t Parcel::readFloat(float *pArg) const
1602{
Andreas Huber84a6d042009-08-17 13:33:27 -07001603 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001604}
1605
1606
1607float Parcel::readFloat() const
1608{
Andreas Huber84a6d042009-08-17 13:33:27 -07001609 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001610}
1611
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001612#if defined(__mips__) && defined(__mips_hard_float)
1613
1614status_t Parcel::readDouble(double *pArg) const
1615{
1616 union {
1617 double d;
1618 unsigned long long ll;
1619 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001620 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001621 status_t status;
1622 status = readAligned(&u.ll);
1623 *pArg = u.d;
1624 return status;
1625}
1626
1627double Parcel::readDouble() const
1628{
1629 union {
1630 double d;
1631 unsigned long long ll;
1632 } u;
1633 u.ll = readAligned<unsigned long long>();
1634 return u.d;
1635}
1636
1637#else
1638
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001639status_t Parcel::readDouble(double *pArg) const
1640{
Andreas Huber84a6d042009-08-17 13:33:27 -07001641 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001642}
1643
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001644double Parcel::readDouble() const
1645{
Andreas Huber84a6d042009-08-17 13:33:27 -07001646 return readAligned<double>();
1647}
1648
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001649#endif
1650
Casey Dahlind6848f52015-10-15 15:44:59 -07001651status_t Parcel::readBool(bool *pArg) const
1652{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001653 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001654 status_t ret = readInt32(&tmp);
1655 *pArg = (tmp != 0);
1656 return ret;
1657}
1658
1659bool Parcel::readBool() const
1660{
1661 return readInt32() != 0;
1662}
1663
1664status_t Parcel::readChar(char16_t *pArg) const
1665{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001666 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001667 status_t ret = readInt32(&tmp);
1668 *pArg = char16_t(tmp);
1669 return ret;
1670}
1671
1672char16_t Parcel::readChar() const
1673{
1674 return char16_t(readInt32());
1675}
1676
1677status_t Parcel::readByte(int8_t *pArg) const
1678{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001679 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001680 status_t ret = readInt32(&tmp);
1681 *pArg = int8_t(tmp);
1682 return ret;
1683}
1684
1685int8_t Parcel::readByte() const
1686{
1687 return int8_t(readInt32());
1688}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001689
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001690status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1691 size_t utf16Size = 0;
1692 const char16_t* src = readString16Inplace(&utf16Size);
1693 if (!src) {
1694 return UNEXPECTED_NULL;
1695 }
1696
1697 // Save ourselves the trouble, we're done.
1698 if (utf16Size == 0u) {
1699 str->clear();
1700 return NO_ERROR;
1701 }
1702
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001703 // Allow for closing '\0'
1704 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1705 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001706 return BAD_VALUE;
1707 }
1708 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001709 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001710 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001711 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1712 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001713 return NO_ERROR;
1714}
1715
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001716const char* Parcel::readCString() const
1717{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001718 if (mDataPos < mDataSize) {
1719 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001720 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1721 // is the string's trailing NUL within the parcel's valid bounds?
1722 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1723 if (eos) {
1724 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001725 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001726 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001727 return str;
1728 }
1729 }
Yi Kong91635562018-06-07 14:38:36 -07001730 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001731}
1732
1733String8 Parcel::readString8() const
1734{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001735 size_t len;
1736 const char* str = readString8Inplace(&len);
1737 if (str) return String8(str, len);
1738 ALOGE("Reading a NULL string not supported here.");
1739 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07001740}
1741
1742status_t Parcel::readString8(String8* pArg) const
1743{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001744 size_t len;
1745 const char* str = readString8Inplace(&len);
1746 if (str) {
1747 pArg->setTo(str, len);
1748 return 0;
1749 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07001750 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001751 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07001752 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001753}
1754
1755const char* Parcel::readString8Inplace(size_t* outLen) const
1756{
1757 int32_t size = readInt32();
1758 // watch for potential int overflow from size+1
1759 if (size >= 0 && size < INT32_MAX) {
1760 *outLen = size;
1761 const char* str = (const char*)readInplace(size+1);
1762 if (str != nullptr) {
Steven Moreland61d0f842020-12-04 21:13:03 +00001763 if (str[size] == '\0') {
1764 return str;
1765 }
1766 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001767 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001768 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001769 *outLen = 0;
1770 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001771}
1772
1773String16 Parcel::readString16() const
1774{
1775 size_t len;
1776 const char16_t* str = readString16Inplace(&len);
1777 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001778 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001779 return String16();
1780}
1781
Casey Dahlinb9872622015-11-25 15:09:45 -08001782
Casey Dahlin451ff582015-10-19 18:12:18 -07001783status_t Parcel::readString16(String16* pArg) const
1784{
1785 size_t len;
1786 const char16_t* str = readString16Inplace(&len);
1787 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001788 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001789 return 0;
1790 } else {
1791 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001792 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001793 }
1794}
1795
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001796const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1797{
1798 int32_t size = readInt32();
1799 // watch for potential int overflow from size+1
1800 if (size >= 0 && size < INT32_MAX) {
1801 *outLen = size;
1802 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001803 if (str != nullptr) {
Steven Moreland61d0f842020-12-04 21:13:03 +00001804 if (str[size] == u'\0') {
1805 return str;
1806 }
1807 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001808 }
1809 }
1810 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001811 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001812}
1813
Casey Dahlinf0c13772015-10-27 18:33:56 -07001814status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1815{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001816 status_t status = readNullableStrongBinder(val);
1817 if (status == OK && !val->get()) {
1818 status = UNEXPECTED_NULL;
1819 }
1820 return status;
1821}
1822
1823status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1824{
Steven Morelanda86a3562019-08-01 23:28:34 +00001825 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001826}
1827
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001828sp<IBinder> Parcel::readStrongBinder() const
1829{
1830 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001831 // Note that a lot of code in Android reads binders by hand with this
1832 // method, and that code has historically been ok with getting nullptr
1833 // back (while ignoring error codes).
1834 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001835 return val;
1836}
1837
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001838int32_t Parcel::readExceptionCode() const
1839{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001840 binder::Status status;
1841 status.readFromParcel(*this);
1842 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001843}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001844
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001845native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001846{
1847 int numFds, numInts;
1848 status_t err;
1849 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001850 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001851 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001852 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001853
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001854 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001855 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001856 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001857 }
1858
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001859 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001860 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001861 if (h->data[i] < 0) {
1862 for (int j = 0; j < i; j++) {
1863 close(h->data[j]);
1864 }
1865 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001866 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001867 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001868 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001869 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001870 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001871 native_handle_close(h);
1872 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001873 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001874 }
1875 return h;
1876}
1877
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001878int Parcel::readFileDescriptor() const
1879{
1880 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001881
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001882 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001883 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001884 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001885
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001886 return BAD_TYPE;
1887}
1888
Dianne Hackborn1941a402016-08-29 12:30:43 -07001889int Parcel::readParcelFileDescriptor() const
1890{
1891 int32_t hasComm = readInt32();
1892 int fd = readFileDescriptor();
1893 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001894 // detach (owned by the binder driver)
1895 int comm = readFileDescriptor();
1896
1897 // warning: this must be kept in sync with:
1898 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1899 enum ParcelFileDescriptorStatus {
1900 DETACHED = 2,
1901 };
1902
1903#if BYTE_ORDER == BIG_ENDIAN
1904 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
1905#endif
1906#if BYTE_ORDER == LITTLE_ENDIAN
1907 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
1908#endif
1909
1910 ssize_t written = TEMP_FAILURE_RETRY(
1911 ::write(comm, &message, sizeof(message)));
1912
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08001913 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001914 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
1915 written, strerror(errno));
1916 return BAD_TYPE;
1917 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07001918 }
1919 return fd;
1920}
1921
Christopher Wiley2cf19952016-04-11 11:09:37 -07001922status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08001923{
1924 int got = readFileDescriptor();
1925
1926 if (got == BAD_TYPE) {
1927 return BAD_TYPE;
1928 }
1929
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001930 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08001931
1932 if (val->get() < 0) {
1933 return BAD_VALUE;
1934 }
1935
1936 return OK;
1937}
1938
Ryo Hashimotobf551892018-05-31 16:58:35 +09001939status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
1940{
1941 int got = readParcelFileDescriptor();
1942
1943 if (got == BAD_TYPE) {
1944 return BAD_TYPE;
1945 }
1946
1947 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
1948
1949 if (val->get() < 0) {
1950 return BAD_VALUE;
1951 }
1952
1953 return OK;
1954}
Casey Dahlin06673e32015-11-23 13:24:23 -08001955
Jeff Brown5707dbf2011-09-23 21:17:56 -07001956status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1957{
Jeff Brown13b16042014-11-11 16:44:25 -08001958 int32_t blobType;
1959 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001960 if (status) return status;
1961
Jeff Brown13b16042014-11-11 16:44:25 -08001962 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001963 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001964 const void* ptr = readInplace(len);
1965 if (!ptr) return BAD_VALUE;
1966
Jeff Brown13b16042014-11-11 16:44:25 -08001967 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001968 return NO_ERROR;
1969 }
1970
Steve Block6807e592011-10-20 11:56:00 +01001971 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001972 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001973 int fd = readFileDescriptor();
1974 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1975
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001976 if (!ashmem_valid(fd)) {
1977 ALOGE("invalid fd");
1978 return BAD_VALUE;
1979 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001980 int size = ashmem_get_size_region(fd);
1981 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001982 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001983 return BAD_VALUE;
1984 }
Yi Kong91635562018-06-07 14:38:36 -07001985 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08001986 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001987 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001988
Jeff Brown13b16042014-11-11 16:44:25 -08001989 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001990 return NO_ERROR;
1991}
1992
Mathias Agopiane1424282013-07-29 21:24:40 -07001993status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001994{
1995 // size
1996 const size_t len = this->readInt32();
1997 const size_t fd_count = this->readInt32();
1998
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001999 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002000 // don't accept size_t values which may have come from an
2001 // inadvertent conversion from a negative int.
2002 return BAD_VALUE;
2003 }
2004
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002005 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002006 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002007 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002008 return BAD_VALUE;
2009
Yi Kong91635562018-06-07 14:38:36 -07002010 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002011 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002012 fds = new (std::nothrow) int[fd_count];
2013 if (fds == nullptr) {
2014 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2015 return BAD_VALUE;
2016 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002017 }
2018
2019 status_t err = NO_ERROR;
2020 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002021 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002022 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002023 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002024 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 -07002025 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2026 // Close all the file descriptors that were dup-ed.
2027 for (size_t j=0; j<i ;j++) {
2028 close(fds[j]);
2029 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002030 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002031 }
2032
2033 if (err == NO_ERROR) {
2034 err = val.unflatten(buf, len, fds, fd_count);
2035 }
2036
2037 if (fd_count) {
2038 delete [] fds;
2039 }
2040
2041 return err;
2042}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002043const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2044{
2045 const size_t DPOS = mDataPos;
2046 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2047 const flat_binder_object* obj
2048 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2049 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002050 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002051 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002052 // the object list, so we don't want to check for it when
2053 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002054 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002055 return obj;
2056 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002057
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002058 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002059 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002060 const size_t N = mObjectsSize;
2061 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002062
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002063 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002064 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002065 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002066
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002067 // Start at the current hint position, looking for an object at
2068 // the current data position.
2069 if (opos < N) {
2070 while (opos < (N-1) && OBJS[opos] < DPOS) {
2071 opos++;
2072 }
2073 } else {
2074 opos = N-1;
2075 }
2076 if (OBJS[opos] == DPOS) {
2077 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002078 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002079 this, DPOS, opos);
2080 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002081 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002082 return obj;
2083 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002084
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002085 // Look backwards for it...
2086 while (opos > 0 && OBJS[opos] > DPOS) {
2087 opos--;
2088 }
2089 if (OBJS[opos] == DPOS) {
2090 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002091 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002092 this, DPOS, opos);
2093 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002094 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002095 return obj;
2096 }
2097 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002098 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 -07002099 this, DPOS);
2100 }
Yi Kong91635562018-06-07 14:38:36 -07002101 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002102}
2103
2104void Parcel::closeFileDescriptors()
2105{
2106 size_t i = mObjectsSize;
2107 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002108 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002109 }
2110 while (i > 0) {
2111 i--;
2112 const flat_binder_object* flat
2113 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002114 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002115 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002116 close(flat->handle);
2117 }
2118 }
2119}
2120
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002121uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002122{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002123 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002124}
2125
2126size_t Parcel::ipcDataSize() const
2127{
2128 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2129}
2130
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002131uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002132{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002133 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002134}
2135
2136size_t Parcel::ipcObjectsCount() const
2137{
2138 return mObjectsSize;
2139}
2140
2141void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Steven Moreland161fe122020-11-12 23:16:47 +00002142 const binder_size_t* objects, size_t objectsCount, release_func relFunc)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002143{
Steven Moreland438cce82021-04-02 18:04:08 +00002144 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2145 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2146
Steven Morelandceed9bb2020-12-17 01:01:06 +00002147 freeData();
2148
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002149 mData = const_cast<uint8_t*>(data);
2150 mDataSize = mDataCapacity = dataSize;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002151 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002152 mObjectsSize = mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002153 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002154
2155 binder_size_t minOffset = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002156 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002157 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002158 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002159 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002160 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002161 mObjectsSize = 0;
2162 break;
2163 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002164 const flat_binder_object* flat
2165 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2166 uint32_t type = flat->hdr.type;
2167 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2168 type == BINDER_TYPE_FD)) {
2169 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2170 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2171 // recover gracefully by clearing out the objects, and releasing the objects we do
2172 // know about.
2173 android_errorWriteLog(0x534e4554, "135930648");
2174 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2175 __func__, type, (uint64_t)offset);
2176 releaseObjects();
2177 mObjectsSize = 0;
2178 break;
2179 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002180 minOffset = offset + sizeof(flat_binder_object);
2181 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002182 scanForFds();
2183}
2184
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002185void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002186{
2187 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002188
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002189 if (errorCheck() != NO_ERROR) {
2190 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002191 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002192 } else if (dataSize() > 0) {
2193 const uint8_t* DATA = data();
2194 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002195 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002196 const size_t N = objectsCount();
2197 for (size_t i=0; i<N; i++) {
2198 const flat_binder_object* flat
2199 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2200 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002201 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002202 << " = " << flat->binder;
2203 }
2204 } else {
2205 to << "NULL";
2206 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002207
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002208 to << ")";
2209}
2210
2211void Parcel::releaseObjects()
2212{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002213 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002214 if (i == 0) {
2215 return;
2216 }
2217 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002218 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002219 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002220 while (i > 0) {
2221 i--;
2222 const flat_binder_object* flat
2223 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002224 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002225 }
2226}
2227
2228void Parcel::acquireObjects()
2229{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002230 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002231 if (i == 0) {
2232 return;
2233 }
2234 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002235 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002236 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002237 while (i > 0) {
2238 i--;
2239 const flat_binder_object* flat
2240 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002241 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002242 }
2243}
2244
2245void Parcel::freeData()
2246{
2247 freeDataNoInit();
2248 initState();
2249}
2250
2251void Parcel::freeDataNoInit()
2252{
2253 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002254 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002255 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002256 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002257 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002258 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002259 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002260 if (mData) {
2261 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002262 gParcelGlobalAllocSize -= mDataCapacity;
2263 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002264 if (mDeallocZero) {
2265 zeroMemory(mData, mDataSize);
2266 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002267 free(mData);
2268 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002269 if (mObjects) free(mObjects);
2270 }
2271}
2272
2273status_t Parcel::growData(size_t len)
2274{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002275 if (len > INT32_MAX) {
2276 // don't accept size_t values which may have come from an
2277 // inadvertent conversion from a negative int.
2278 return BAD_VALUE;
2279 }
2280
Martijn Coenen93fe5182020-01-22 10:46:25 +01002281 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2282 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002283 size_t newSize = ((mDataSize+len)*3)/2;
2284 return (newSize <= mDataSize)
2285 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002286 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002287}
2288
Steven Morelandf183fdd2020-10-27 00:12:12 +00002289static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2290 if (!zero) {
2291 return (uint8_t*)realloc(data, newCapacity);
2292 }
2293 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2294 if (!newData) {
2295 return nullptr;
2296 }
2297
2298 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2299 zeroMemory(data, oldCapacity);
2300 free(data);
2301 return newData;
2302}
2303
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002304status_t Parcel::restartWrite(size_t desired)
2305{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002306 if (desired > INT32_MAX) {
2307 // don't accept size_t values which may have come from an
2308 // inadvertent conversion from a negative int.
2309 return BAD_VALUE;
2310 }
2311
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002312 if (mOwner) {
2313 freeData();
2314 return continueWrite(desired);
2315 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002316
Steven Morelandf183fdd2020-10-27 00:12:12 +00002317 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002318 if (!data && desired > mDataCapacity) {
2319 mError = NO_MEMORY;
2320 return NO_MEMORY;
2321 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002322
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002323 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002324
Devin Moore4a0a55e2020-06-04 13:23:10 -07002325 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002326 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002327 if (mDataCapacity > desired) {
2328 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2329 } else {
2330 gParcelGlobalAllocSize += (desired - mDataCapacity);
2331 }
2332
Colin Cross83ec65e2015-12-08 17:15:50 -08002333 if (!mData) {
2334 gParcelGlobalAllocCount++;
2335 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002336 mData = data;
2337 mDataCapacity = desired;
2338 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002339
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002340 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002341 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2342 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2343
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002344 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002345 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002346 mObjectsSize = mObjectsCapacity = 0;
2347 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002348 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002349 mHasFds = false;
2350 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002351 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002352
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002353 return NO_ERROR;
2354}
2355
2356status_t Parcel::continueWrite(size_t desired)
2357{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002358 if (desired > INT32_MAX) {
2359 // don't accept size_t values which may have come from an
2360 // inadvertent conversion from a negative int.
2361 return BAD_VALUE;
2362 }
2363
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002364 // If shrinking, first adjust for any objects that appear
2365 // after the new data size.
2366 size_t objectsSize = mObjectsSize;
2367 if (desired < mDataSize) {
2368 if (desired == 0) {
2369 objectsSize = 0;
2370 } else {
2371 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002372 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002373 break;
2374 objectsSize--;
2375 }
2376 }
2377 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002378
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002379 if (mOwner) {
2380 // If the size is going to zero, just release the owner's data.
2381 if (desired == 0) {
2382 freeData();
2383 return NO_ERROR;
2384 }
2385
2386 // If there is a different owner, we need to take
2387 // posession.
2388 uint8_t* data = (uint8_t*)malloc(desired);
2389 if (!data) {
2390 mError = NO_MEMORY;
2391 return NO_MEMORY;
2392 }
Yi Kong91635562018-06-07 14:38:36 -07002393 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002394
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002395 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002396 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002397 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002398 free(data);
2399
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002400 mError = NO_MEMORY;
2401 return NO_MEMORY;
2402 }
2403
2404 // Little hack to only acquire references on objects
2405 // we will be keeping.
2406 size_t oldObjectsSize = mObjectsSize;
2407 mObjectsSize = objectsSize;
2408 acquireObjects();
2409 mObjectsSize = oldObjectsSize;
2410 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002411
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002412 if (mData) {
2413 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2414 }
2415 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002416 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002417 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002418 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002419 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
Yi Kong91635562018-06-07 14:38:36 -07002420 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002422 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002423 gParcelGlobalAllocSize += desired;
2424 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002425
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002426 mData = data;
2427 mObjects = objects;
2428 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002429 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002430 mDataCapacity = desired;
2431 mObjectsSize = mObjectsCapacity = objectsSize;
2432 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002433 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002434
2435 } else if (mData) {
2436 if (objectsSize < mObjectsSize) {
2437 // Need to release refs on any objects we are dropping.
2438 const sp<ProcessState> proc(ProcessState::self());
2439 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2440 const flat_binder_object* flat
2441 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002442 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002443 // will need to rescan because we may have lopped off the only FDs
2444 mFdsKnown = false;
2445 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002446 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002447 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002448
2449 if (objectsSize == 0) {
2450 free(mObjects);
2451 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002452 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002453 } else {
2454 binder_size_t* objects =
2455 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2456 if (objects) {
2457 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002458 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002459 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002460 }
2461 mObjectsSize = objectsSize;
2462 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002463 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002464 }
2465
2466 // We own the data, so we can just do a realloc().
2467 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002468 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002469 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002470 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2471 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002472 gParcelGlobalAllocSize += desired;
2473 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002474 mData = data;
2475 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002476 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002477 mError = NO_MEMORY;
2478 return NO_MEMORY;
2479 }
2480 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002481 if (mDataSize > desired) {
2482 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002483 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002484 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002485 if (mDataPos > desired) {
2486 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002487 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002488 }
2489 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002490
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002491 } else {
2492 // This is the first data. Easy!
2493 uint8_t* data = (uint8_t*)malloc(desired);
2494 if (!data) {
2495 mError = NO_MEMORY;
2496 return NO_MEMORY;
2497 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002498
Yi Kong91635562018-06-07 14:38:36 -07002499 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002500 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002501 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002502 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002503
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002504 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002505 gParcelGlobalAllocSize += desired;
2506 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002507
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508 mData = data;
2509 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002510 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2511 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002512 mDataCapacity = desired;
2513 }
2514
2515 return NO_ERROR;
2516}
2517
2518void Parcel::initState()
2519{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002520 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002521 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002522 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002523 mDataSize = 0;
2524 mDataCapacity = 0;
2525 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002526 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2527 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Steven Morelandbdb53ab2021-05-05 17:57:41 +00002528 mSession = nullptr;
Yi Kong91635562018-06-07 14:38:36 -07002529 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002530 mObjectsSize = 0;
2531 mObjectsCapacity = 0;
2532 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002533 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002534 mHasFds = false;
2535 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002536 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002537 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07002538 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002539 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002540 mWorkSourceRequestHeaderPosition = 0;
2541 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002542
2543 // racing multiple init leads only to multiple identical write
2544 if (gMaxFds == 0) {
2545 struct rlimit result;
2546 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2547 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002548 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002549 } else {
2550 ALOGW("Unable to getrlimit: %s", strerror(errno));
2551 gMaxFds = 1024;
2552 }
2553 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002554}
2555
2556void Parcel::scanForFds() const
2557{
2558 bool hasFds = false;
2559 for (size_t i=0; i<mObjectsSize; i++) {
2560 const flat_binder_object* flat
2561 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002562 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002563 hasFds = true;
2564 break;
2565 }
2566 }
2567 mHasFds = hasFds;
2568 mFdsKnown = true;
2569}
2570
Dan Sandleraa5c2342015-04-10 10:08:45 -04002571size_t Parcel::getBlobAshmemSize() const
2572{
Adrian Roos6bb31142015-10-22 16:46:12 -07002573 // This used to return the size of all blobs that were written to ashmem, now we're returning
2574 // the ashmem currently referenced by this Parcel, which should be equivalent.
2575 // TODO: Remove method once ABI can be changed.
2576 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002577}
2578
Adrian Rooscbf37262015-10-22 16:12:53 -07002579size_t Parcel::getOpenAshmemSize() const
2580{
2581 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002582}
2583
2584// --- Parcel::Blob ---
2585
2586Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002587 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002588}
2589
2590Parcel::Blob::~Blob() {
2591 release();
2592}
2593
2594void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002595 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002596 ::munmap(mData, mSize);
2597 }
2598 clear();
2599}
2600
Jeff Brown13b16042014-11-11 16:44:25 -08002601void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2602 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002603 mData = data;
2604 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002605 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002606}
2607
2608void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002609 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002610 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002611 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002612 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002613}
2614
Steven Moreland61ff8492019-09-26 16:05:45 -07002615} // namespace android