blob: 39d6a2ec4fd153069d18c0fa9f035b9f00181600 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Steven Morelandbf1915b2020-07-16 22:43:02 +000023#include <linux/sched.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080024#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080029#include <sys/stat.h>
30#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070031#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080032#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070033
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/Binder.h>
35#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080036#include <binder/IPCThreadState.h>
37#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070038#include <binder/ProcessState.h>
Steven Moreland6e5a7752019-08-05 20:30:14 -070039#include <binder/Stability.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080040#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041#include <binder/TextOutput.h>
42
Mark Salyzynabed7f72016-01-27 08:02:48 -080043#include <cutils/ashmem.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000044#include <cutils/compiler.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080045#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#include <utils/Log.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String16.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000048#include <utils/String8.h>
49#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070050
Mathias Agopian208059f2009-05-18 15:08:03 -070051#include <private/binder/binder_module.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000052#include "RpcState.h"
Steven Morelanda4853cd2019-07-12 15:44:37 -070053#include "Static.h"
Steven Morelandf183fdd2020-10-27 00:12:12 +000054#include "Utils.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070055
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070056#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080057//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080058#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080059//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070060
61// ---------------------------------------------------------------------------
62
Nick Kralevichb6b14232015-04-02 09:36:02 -070063// This macro should never be used at runtime, as a too large value
64// of s could cause an integer overflow. Instead, you should always
65// use the wrapper function pad_size()
66#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
67
68static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070069 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070070 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070071 }
72 return PAD_SIZE_UNSAFE(s);
73}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070074
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070075// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060076#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070077
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070078namespace android {
79
Steven Moreland7b102262019-08-01 15:48:43 -070080// many things compile this into prebuilts on the stack
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 Moreland89ddfc52020-11-13 02:39:26 +0000176 auto category = internal::Stability::getCategory(binder.get());
177 return writeInt32(category.repr());
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 Moreland89ddfc52020-11-13 02:39:26 +0000187 status = internal::Stability::setRepr(binder.get(), stability, true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000188 if (status != OK) return status;
189
190 *out = binder;
191 return OK;
192}
193
Steven Morelandbf1915b2020-07-16 22:43:02 +0000194static constexpr inline int schedPolicyMask(int policy, int priority) {
195 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
196}
197
Steven Morelanda86a3562019-08-01 23:28:34 +0000198status_t Parcel::flattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700199{
Steven Moreland5553ac42020-11-11 02:14:45 +0000200 if (isForRpc()) {
201 if (binder) {
202 status_t status = writeInt32(1); // non-null
203 if (status != OK) return status;
204 RpcAddress address = RpcAddress::zero();
205 status = mConnection->state()->onBinderLeaving(mConnection, binder, &address);
206 if (status != OK) return status;
207 status = address.writeToParcel(this);
208 if (status != OK) return status;
209 } else {
210 status_t status = writeInt32(0); // null
211 if (status != OK) return status;
212 }
213 return finishFlattenBinder(binder);
214 }
215
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700216 flat_binder_object obj;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000217 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700218
Steven Morelandbf1915b2020-07-16 22:43:02 +0000219 int schedBits = 0;
220 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
221 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700222 }
223
Yi Kong91635562018-06-07 14:38:36 -0700224 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800225 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700226 if (!local) {
227 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700228 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000229 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000230 } else {
231 if (proxy->isRpcBinder()) {
232 ALOGE("Sending a socket binder over RPC is prohibited");
233 return INVALID_OPERATION;
234 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000236 const int32_t handle = proxy ? proxy->getPrivateAccessorForId().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700237 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800238 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700239 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800240 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700241 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000242 int policy = local->getMinSchedulerPolicy();
243 int priority = local->getMinSchedulerPriority();
244
245 if (policy != 0 || priority != 0) {
246 // override value, since it is set explicitly
247 schedBits = schedPolicyMask(policy, priority);
248 }
Steven Morelandf0212002018-12-26 13:59:23 -0800249 if (local->isRequestingSid()) {
250 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
251 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000252 if (local->isInheritRt()) {
253 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
254 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700255 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800256 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
257 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258 }
259 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700260 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800261 obj.binder = 0;
262 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700263 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700264
Steven Morelandbf1915b2020-07-16 22:43:02 +0000265 obj.flags |= schedBits;
266
Steven Moreland34b48cb2020-12-01 22:45:38 +0000267 status_t status = writeObject(obj, false);
268 if (status != OK) return status;
269
270 return finishFlattenBinder(binder);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700271}
272
Steven Morelanda86a3562019-08-01 23:28:34 +0000273status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700274{
Steven Moreland5553ac42020-11-11 02:14:45 +0000275 if (isForRpc()) {
276 LOG_ALWAYS_FATAL_IF(mConnection == nullptr,
277 "RpcConnection required to read from remote parcel");
278
279 int32_t isNull;
280 status_t status = readInt32(&isNull);
281 if (status != OK) return status;
282
283 sp<IBinder> binder;
284
285 if (isNull & 1) {
286 auto addr = RpcAddress::zero();
287 status_t status = addr.readFromParcel(*this);
288 if (status != OK) return status;
289 binder = mConnection->state()->onBinderEntering(mConnection, addr);
290 }
291
292 return finishUnflattenBinder(binder, out);
293 }
294
Steven Morelanda86a3562019-08-01 23:28:34 +0000295 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700296
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700297 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700298 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000299 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000300 sp<IBinder> binder =
301 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000302 return finishUnflattenBinder(binder, out);
303 }
304 case BINDER_TYPE_HANDLE: {
305 sp<IBinder> binder =
306 ProcessState::self()->getStrongProxyForHandle(flat->handle);
307 return finishUnflattenBinder(binder, out);
308 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700309 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700310 }
311 return BAD_TYPE;
312}
313
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700314// ---------------------------------------------------------------------------
315
316Parcel::Parcel()
317{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800318 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700319 initState();
320}
321
322Parcel::~Parcel()
323{
324 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800325 LOG_ALLOC("Parcel %p: destroyed", this);
326}
327
328size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600329 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800330}
331
332size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600333 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700334}
335
336const uint8_t* Parcel::data() const
337{
338 return mData;
339}
340
341size_t Parcel::dataSize() const
342{
343 return (mDataSize > mDataPos ? mDataSize : mDataPos);
344}
345
346size_t Parcel::dataAvail() const
347{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700348 size_t result = dataSize() - dataPosition();
349 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700350 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700351 }
352 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700353}
354
355size_t Parcel::dataPosition() const
356{
357 return mDataPos;
358}
359
360size_t Parcel::dataCapacity() const
361{
362 return mDataCapacity;
363}
364
365status_t Parcel::setDataSize(size_t size)
366{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700367 if (size > INT32_MAX) {
368 // don't accept size_t values which may have come from an
369 // inadvertent conversion from a negative int.
370 return BAD_VALUE;
371 }
372
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700373 status_t err;
374 err = continueWrite(size);
375 if (err == NO_ERROR) {
376 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700377 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700378 }
379 return err;
380}
381
382void Parcel::setDataPosition(size_t pos) const
383{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700384 if (pos > INT32_MAX) {
385 // don't accept size_t values which may have come from an
386 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700387 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700388 }
389
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700390 mDataPos = pos;
391 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800392 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700393}
394
395status_t Parcel::setDataCapacity(size_t size)
396{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700397 if (size > INT32_MAX) {
398 // don't accept size_t values which may have come from an
399 // inadvertent conversion from a negative int.
400 return BAD_VALUE;
401 }
402
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700403 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700404 return NO_ERROR;
405}
406
407status_t Parcel::setData(const uint8_t* buffer, size_t len)
408{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700409 if (len > INT32_MAX) {
410 // don't accept size_t values which may have come from an
411 // inadvertent conversion from a negative int.
412 return BAD_VALUE;
413 }
414
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700415 status_t err = restartWrite(len);
416 if (err == NO_ERROR) {
417 memcpy(const_cast<uint8_t*>(data()), buffer, len);
418 mDataSize = len;
419 mFdsKnown = false;
420 }
421 return err;
422}
423
Andreas Huber51faf462011-04-13 10:21:56 -0700424status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700425{
Steven Moreland67753c32021-04-02 18:45:19 +0000426 if (parcel->isForRpc() != isForRpc()) {
427 ALOGE("Cannot append Parcel of one format to another.");
428 return BAD_TYPE;
429 }
430
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700431 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700432 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800433 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 size_t size = parcel->mObjectsSize;
435 int startPos = mDataPos;
436 int firstIndex = -1, lastIndex = -2;
437
438 if (len == 0) {
439 return NO_ERROR;
440 }
441
Nick Kralevichb6b14232015-04-02 09:36:02 -0700442 if (len > INT32_MAX) {
443 // don't accept size_t values which may have come from an
444 // inadvertent conversion from a negative int.
445 return BAD_VALUE;
446 }
447
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 // range checks against the source parcel size
449 if ((offset > parcel->mDataSize)
450 || (len > parcel->mDataSize)
451 || (offset + len > parcel->mDataSize)) {
452 return BAD_VALUE;
453 }
454
455 // Count objects in range
456 for (int i = 0; i < (int) size; i++) {
457 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700458 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 if (firstIndex == -1) {
460 firstIndex = i;
461 }
462 lastIndex = i;
463 }
464 }
465 int numObjects = lastIndex - firstIndex + 1;
466
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700467 if ((mDataSize+len) > mDataCapacity) {
468 // grow data
469 err = growData(len);
470 if (err != NO_ERROR) {
471 return err;
472 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473 }
474
475 // append data
476 memcpy(mData + mDataPos, data + offset, len);
477 mDataPos += len;
478 mDataSize += len;
479
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400480 err = NO_ERROR;
481
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700482 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200483 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700484 // grow objects
485 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +0100486 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
487 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700488 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +0100489 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800490 binder_size_t *objects =
491 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700492 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700493 return NO_MEMORY;
494 }
495 mObjects = objects;
496 mObjectsCapacity = newSize;
497 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700498
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700499 // append and acquire objects
500 int idx = mObjectsSize;
501 for (int i = firstIndex; i <= lastIndex; i++) {
502 size_t off = objects[i] - offset + startPos;
503 mObjects[idx++] = off;
504 mObjectsSize++;
505
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700506 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700507 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700508 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700509
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700510 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700511 // If this is a file descriptor, we need to dup it so the
512 // new Parcel now owns its own fd, and can declare that we
513 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800514 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800515 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700516 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400517 if (!mAllowFds) {
518 err = FDS_NOT_ALLOWED;
519 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700520 }
521 }
522 }
523
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400524 return err;
525}
526
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700527int Parcel::compareData(const Parcel& other) {
528 size_t size = dataSize();
529 if (size != other.dataSize()) {
530 return size < other.dataSize() ? -1 : 1;
531 }
532 return memcmp(data(), other.data(), size);
533}
534
Jeff Brown13b16042014-11-11 16:44:25 -0800535bool Parcel::allowFds() const
536{
537 return mAllowFds;
538}
539
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700540bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400541{
542 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700543 if (!allowFds) {
544 mAllowFds = false;
545 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400546 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700547}
548
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700549void Parcel::restoreAllowFds(bool lastValue)
550{
551 mAllowFds = lastValue;
552}
553
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700554bool Parcel::hasFileDescriptors() const
555{
556 if (!mFdsKnown) {
557 scanForFds();
558 }
559 return mHasFds;
560}
561
Steven Morelandf183fdd2020-10-27 00:12:12 +0000562void Parcel::markSensitive() const
563{
564 mDeallocZero = true;
565}
566
Steven Moreland5553ac42020-11-11 02:14:45 +0000567void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000568 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
569
Steven Moreland5553ac42020-11-11 02:14:45 +0000570 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
571 markForRpc(binder->remoteBinder()->getPrivateAccessorForId().rpcConnection());
572 }
573}
574
575void Parcel::markForRpc(const sp<RpcConnection>& connection) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000576 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
577 "format must be set before data is written OR on IPC data");
578
Steven Moreland5553ac42020-11-11 02:14:45 +0000579 LOG_ALWAYS_FATAL_IF(connection == nullptr, "markForRpc requires connection");
580 mConnection = connection;
581}
582
583bool Parcel::isForRpc() const {
584 return mConnection != nullptr;
585}
586
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000587void Parcel::updateWorkSourceRequestHeaderPosition() const {
588 // Only update the request headers once. We only want to point
589 // to the first headers read/written.
590 if (!mRequestHeaderPresent) {
591 mWorkSourceRequestHeaderPosition = dataPosition();
592 mRequestHeaderPresent = true;
593 }
594}
595
Jooyung Han1b228f42020-01-30 13:41:12 +0900596#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700597constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
598#else
599constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
600#endif
601
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700602// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700603status_t Parcel::writeInterfaceToken(const String16& interface)
604{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000605 return writeInterfaceToken(interface.string(), interface.size());
606}
607
608status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000609 if (CC_LIKELY(!isForRpc())) {
610 const IPCThreadState* threadState = IPCThreadState::self();
611 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
612 updateWorkSourceRequestHeaderPosition();
613 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
614 : IPCThreadState::kUnsetWorkSource);
615 writeInt32(kHeader);
616 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000617
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700618 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000619 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620}
621
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000622bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
623{
624 if (!mRequestHeaderPresent) {
625 return false;
626 }
627
628 const size_t initialPosition = dataPosition();
629 setDataPosition(mWorkSourceRequestHeaderPosition);
630 status_t err = writeInt32(uid);
631 setDataPosition(initialPosition);
632 return err == NO_ERROR;
633}
634
Steven Morelandf1b1e492019-05-06 15:05:13 -0700635uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000636{
637 if (!mRequestHeaderPresent) {
638 return IPCThreadState::kUnsetWorkSource;
639 }
640
641 const size_t initialPosition = dataPosition();
642 setDataPosition(mWorkSourceRequestHeaderPosition);
643 uid_t uid = readInt32();
644 setDataPosition(initialPosition);
645 return uid;
646}
647
Mathias Agopian83c04462009-05-22 19:00:22 -0700648bool Parcel::checkInterface(IBinder* binder) const
649{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700650 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700651}
652
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700653bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700654 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700655{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700656 return enforceInterface(interface.string(), interface.size(), threadState);
657}
658
659bool Parcel::enforceInterface(const char16_t* interface,
660 size_t len,
661 IPCThreadState* threadState) const
662{
Steven Moreland3af936a2021-03-26 03:05:38 +0000663 if (CC_LIKELY(!isForRpc())) {
664 // StrictModePolicy.
665 int32_t strictPolicy = readInt32();
666 if (threadState == nullptr) {
667 threadState = IPCThreadState::self();
668 }
669 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
670 // For one-way calls, the callee is running entirely
671 // disconnected from the caller, so disable StrictMode entirely.
672 // Not only does disk/network usage not impact the caller, but
673 // there's no way to communicate back violations anyway.
674 threadState->setStrictModePolicy(0);
675 } else {
676 threadState->setStrictModePolicy(strictPolicy);
677 }
678 // WorkSource.
679 updateWorkSourceRequestHeaderPosition();
680 int32_t workSource = readInt32();
681 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
682 // vendor header
683 int32_t header = readInt32();
684 if (header != kHeader) {
685 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
686 header);
687 return false;
688 }
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700689 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000690
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100691 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700692 size_t parcel_interface_len;
693 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
694 if (len == parcel_interface_len &&
695 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700696 return true;
697 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700698 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700699 String8(interface, len).string(),
700 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700701 return false;
702 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700703}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700704
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700705size_t Parcel::objectsCount() const
706{
707 return mObjectsSize;
708}
709
710status_t Parcel::errorCheck() const
711{
712 return mError;
713}
714
715void Parcel::setError(status_t err)
716{
717 mError = err;
718}
719
720status_t Parcel::finishWrite(size_t len)
721{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700722 if (len > INT32_MAX) {
723 // don't accept size_t values which may have come from an
724 // inadvertent conversion from a negative int.
725 return BAD_VALUE;
726 }
727
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700728 //printf("Finish write of %d\n", len);
729 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700730 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700731 if (mDataPos > mDataSize) {
732 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700733 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700734 }
735 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
736 return NO_ERROR;
737}
738
739status_t Parcel::writeUnpadded(const void* data, size_t len)
740{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700741 if (len > INT32_MAX) {
742 // don't accept size_t values which may have come from an
743 // inadvertent conversion from a negative int.
744 return BAD_VALUE;
745 }
746
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700747 size_t end = mDataPos + len;
748 if (end < mDataPos) {
749 // integer overflow
750 return BAD_VALUE;
751 }
752
753 if (end <= mDataCapacity) {
754restart_write:
755 memcpy(mData+mDataPos, data, len);
756 return finishWrite(len);
757 }
758
759 status_t err = growData(len);
760 if (err == NO_ERROR) goto restart_write;
761 return err;
762}
763
764status_t Parcel::write(const void* data, size_t len)
765{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700766 if (len > INT32_MAX) {
767 // don't accept size_t values which may have come from an
768 // inadvertent conversion from a negative int.
769 return BAD_VALUE;
770 }
771
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700772 void* const d = writeInplace(len);
773 if (d) {
774 memcpy(d, data, len);
775 return NO_ERROR;
776 }
777 return mError;
778}
779
780void* Parcel::writeInplace(size_t len)
781{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700782 if (len > INT32_MAX) {
783 // don't accept size_t values which may have come from an
784 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700785 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700786 }
787
788 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700789
790 // sanity check for integer overflow
791 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700792 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700793 }
794
795 if ((mDataPos+padded) <= mDataCapacity) {
796restart_write:
797 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
798 uint8_t* const data = mData+mDataPos;
799
800 // Need to pad at end?
801 if (padded != len) {
802#if BYTE_ORDER == BIG_ENDIAN
803 static const uint32_t mask[4] = {
804 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
805 };
806#endif
807#if BYTE_ORDER == LITTLE_ENDIAN
808 static const uint32_t mask[4] = {
809 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
810 };
811#endif
812 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
813 // *reinterpret_cast<void**>(data+padded-4));
814 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
815 }
816
817 finishWrite(padded);
818 return data;
819 }
820
821 status_t err = growData(padded);
822 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700823 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700824}
825
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800826status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
827 const uint8_t* strData = (uint8_t*)str.data();
828 const size_t strLen= str.length();
829 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100830 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800831 return BAD_VALUE;
832 }
833
834 status_t err = writeInt32(utf16Len);
835 if (err) {
836 return err;
837 }
838
839 // Allocate enough bytes to hold our converted string and its terminating NULL.
840 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
841 if (!dst) {
842 return NO_MEMORY;
843 }
844
Sergio Girof4607432016-07-21 14:46:35 +0100845 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800846
847 return NO_ERROR;
848}
849
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900850
Andy Hung49198cf2020-11-18 11:02:39 -0800851status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
852status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800853
Andy Hung49198cf2020-11-18 11:02:39 -0800854status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
855status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700856
Andy Hung49198cf2020-11-18 11:02:39 -0800857status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
858status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
859status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
860status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
861status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
862status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
863status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
864status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
865status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
866status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
867status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
868status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
869status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
870status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
871status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
872status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
873status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
874status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
875status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
876status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
877status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
878status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
879status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
880status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
881status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
882status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
883status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700884
Andy Hung49198cf2020-11-18 11:02:39 -0800885status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800886status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800887 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900888status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800889 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800890status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800891 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900892status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800893 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
894status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800895
Andy Hung49198cf2020-11-18 11:02:39 -0800896status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
897status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
898status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
899
900status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
901status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
902status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
903
904status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
905
906status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
907status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
908
909status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
910status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
911
912status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
913status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
914status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
915status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
916status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
917status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
918status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
919status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
920status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
921status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
922status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
923status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
924status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
925status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
926status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
927status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
928status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
929status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
930status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
931status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
932status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
933status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
934status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
935status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
936status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
937status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
938status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
939
940status_t Parcel::readString16Vector(
941 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
942status_t Parcel::readString16Vector(
943 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
944status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
945status_t Parcel::readUtf8VectorFromUtf16Vector(
946 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
947status_t Parcel::readUtf8VectorFromUtf16Vector(
948 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
949status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
950
951status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
952status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
953status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
954
955status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
956status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
957status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
958
959status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800960
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700961status_t Parcel::writeInt32(int32_t val)
962{
Andreas Huber84a6d042009-08-17 13:33:27 -0700963 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700964}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800965
966status_t Parcel::writeUint32(uint32_t val)
967{
968 return writeAligned(val);
969}
970
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700971status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700972 if (len > INT32_MAX) {
973 // don't accept size_t values which may have come from an
974 // inadvertent conversion from a negative int.
975 return BAD_VALUE;
976 }
977
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700978 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700979 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700980 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700981 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700982 if (ret == NO_ERROR) {
983 ret = write(val, len * sizeof(*val));
984 }
985 return ret;
986}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700987status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700988 if (len > INT32_MAX) {
989 // don't accept size_t values which may have come from an
990 // inadvertent conversion from a negative int.
991 return BAD_VALUE;
992 }
993
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700994 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700995 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700996 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700997 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700998 if (ret == NO_ERROR) {
999 ret = write(val, len * sizeof(*val));
1000 }
1001 return ret;
1002}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001003
Casey Dahlind6848f52015-10-15 15:44:59 -07001004status_t Parcel::writeBool(bool val)
1005{
1006 return writeInt32(int32_t(val));
1007}
1008
1009status_t Parcel::writeChar(char16_t val)
1010{
1011 return writeInt32(int32_t(val));
1012}
1013
1014status_t Parcel::writeByte(int8_t val)
1015{
1016 return writeInt32(int32_t(val));
1017}
1018
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001019status_t Parcel::writeInt64(int64_t val)
1020{
Andreas Huber84a6d042009-08-17 13:33:27 -07001021 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001022}
1023
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001024status_t Parcel::writeUint64(uint64_t val)
1025{
1026 return writeAligned(val);
1027}
1028
Serban Constantinescuf683e012013-11-05 16:53:55 +00001029status_t Parcel::writePointer(uintptr_t val)
1030{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001031 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001032}
1033
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001034status_t Parcel::writeFloat(float val)
1035{
Andreas Huber84a6d042009-08-17 13:33:27 -07001036 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001037}
1038
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001039#if defined(__mips__) && defined(__mips_hard_float)
1040
1041status_t Parcel::writeDouble(double val)
1042{
1043 union {
1044 double d;
1045 unsigned long long ll;
1046 } u;
1047 u.d = val;
1048 return writeAligned(u.ll);
1049}
1050
1051#else
1052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001053status_t Parcel::writeDouble(double val)
1054{
Andreas Huber84a6d042009-08-17 13:33:27 -07001055 return writeAligned(val);
1056}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001057
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001058#endif
1059
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001060status_t Parcel::writeCString(const char* str)
1061{
1062 return write(str, strlen(str)+1);
1063}
1064
1065status_t Parcel::writeString8(const String8& str)
1066{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001067 return writeString8(str.string(), str.size());
1068}
1069
1070status_t Parcel::writeString8(const char* str, size_t len)
1071{
1072 if (str == nullptr) return writeInt32(-1);
1073
Jeff Sharkey18220902020-11-05 08:36:20 -07001074 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001075 status_t err = writeInt32(len);
1076 if (err == NO_ERROR) {
1077 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1078 if (data) {
1079 memcpy(data, str, len);
1080 *reinterpret_cast<char*>(data+len) = 0;
1081 return NO_ERROR;
1082 }
1083 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001084 }
1085 return err;
1086}
1087
1088status_t Parcel::writeString16(const String16& str)
1089{
1090 return writeString16(str.string(), str.size());
1091}
1092
1093status_t Parcel::writeString16(const char16_t* str, size_t len)
1094{
Yi Kong91635562018-06-07 14:38:36 -07001095 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001096
Jeff Sharkey18220902020-11-05 08:36:20 -07001097 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001098 status_t err = writeInt32(len);
1099 if (err == NO_ERROR) {
1100 len *= sizeof(char16_t);
1101 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1102 if (data) {
1103 memcpy(data, str, len);
1104 *reinterpret_cast<char16_t*>(data+len) = 0;
1105 return NO_ERROR;
1106 }
1107 err = mError;
1108 }
1109 return err;
1110}
1111
1112status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1113{
Steven Morelanda86a3562019-08-01 23:28:34 +00001114 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001115}
1116
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001117
Casey Dahlinb9872622015-11-25 15:09:45 -08001118status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1119 if (!parcelable) {
1120 return writeInt32(0);
1121 }
1122
1123 return writeParcelable(*parcelable);
1124}
1125
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001126status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001127{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001128 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001129 return BAD_TYPE;
1130
1131 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001132 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001133 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001135 err = writeInt32(handle->numInts);
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 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1139 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140
1141 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001142 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143 return err;
1144 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001145 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001146 return err;
1147}
1148
Jeff Brown93ff1f92011-11-04 19:01:44 -07001149status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001150{
Steven Moreland5553ac42020-11-11 02:14:45 +00001151 if (isForRpc()) {
1152 ALOGE("Cannot write file descriptor to remote binder.");
1153 return BAD_TYPE;
1154 }
1155
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001156 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001157 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001158 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001159 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001160 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001161 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001162 return writeObject(obj, true);
1163}
1164
1165status_t Parcel::writeDupFileDescriptor(int fd)
1166{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001167 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001168 if (dupFd < 0) {
1169 return -errno;
1170 }
1171 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001172 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001173 close(dupFd);
1174 }
1175 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001176}
1177
Dianne Hackborn1941a402016-08-29 12:30:43 -07001178status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1179{
1180 writeInt32(0);
1181 return writeFileDescriptor(fd, takeOwnership);
1182}
1183
Ryo Hashimotobf551892018-05-31 16:58:35 +09001184status_t Parcel::writeDupParcelFileDescriptor(int fd)
1185{
1186 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1187 if (dupFd < 0) {
1188 return -errno;
1189 }
1190 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1191 if (err != OK) {
1192 close(dupFd);
1193 }
1194 return err;
1195}
1196
Christopher Wiley2cf19952016-04-11 11:09:37 -07001197status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001198 return writeDupFileDescriptor(fd.get());
1199}
1200
Jeff Brown13b16042014-11-11 16:44:25 -08001201status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001202{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001203 if (len > INT32_MAX) {
1204 // don't accept size_t values which may have come from an
1205 // inadvertent conversion from a negative int.
1206 return BAD_VALUE;
1207 }
1208
Jeff Brown13b16042014-11-11 16:44:25 -08001209 status_t status;
1210 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001211 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001212 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001213 if (status) return status;
1214
1215 void* ptr = writeInplace(len);
1216 if (!ptr) return NO_MEMORY;
1217
Jeff Brown13b16042014-11-11 16:44:25 -08001218 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001219 return NO_ERROR;
1220 }
1221
Steve Block6807e592011-10-20 11:56:00 +01001222 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001223 int fd = ashmem_create_region("Parcel Blob", len);
1224 if (fd < 0) return NO_MEMORY;
1225
1226 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1227 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001228 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001229 } else {
Yi Kong91635562018-06-07 14:38:36 -07001230 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001231 if (ptr == MAP_FAILED) {
1232 status = -errno;
1233 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001234 if (!mutableCopy) {
1235 result = ashmem_set_prot_region(fd, PROT_READ);
1236 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001237 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001238 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001239 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001240 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001241 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001242 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001243 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001244 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001245 return NO_ERROR;
1246 }
1247 }
1248 }
1249 }
1250 ::munmap(ptr, len);
1251 }
1252 ::close(fd);
1253 return status;
1254}
1255
Jeff Brown13b16042014-11-11 16:44:25 -08001256status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1257{
1258 // Must match up with what's done in writeBlob.
1259 if (!mAllowFds) return FDS_NOT_ALLOWED;
1260 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1261 if (status) return status;
1262 return writeDupFileDescriptor(fd);
1263}
1264
Mathias Agopiane1424282013-07-29 21:24:40 -07001265status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001266{
1267 status_t err;
1268
1269 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001270 const size_t len = val.getFlattenedSize();
1271 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001272
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001273 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001274 // don't accept size_t values which may have come from an
1275 // inadvertent conversion from a negative int.
1276 return BAD_VALUE;
1277 }
1278
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001279 err = this->writeInt32(len);
1280 if (err) return err;
1281
1282 err = this->writeInt32(fd_count);
1283 if (err) return err;
1284
1285 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001286 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001287 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001288 return BAD_VALUE;
1289
Yi Kong91635562018-06-07 14:38:36 -07001290 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001291 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001292 fds = new (std::nothrow) int[fd_count];
1293 if (fds == nullptr) {
1294 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1295 return BAD_VALUE;
1296 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001297 }
1298
1299 err = val.flatten(buf, len, fds, fd_count);
1300 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1301 err = this->writeDupFileDescriptor( fds[i] );
1302 }
1303
1304 if (fd_count) {
1305 delete [] fds;
1306 }
1307
1308 return err;
1309}
1310
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001311status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1312{
1313 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1314 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1315 if (enoughData && enoughObjects) {
1316restart_write:
1317 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001318
Christopher Tate98e67d32015-06-03 18:44:15 -07001319 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001320 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001321 if (!mAllowFds) {
1322 // fail before modifying our object index
1323 return FDS_NOT_ALLOWED;
1324 }
1325 mHasFds = mFdsKnown = true;
1326 }
1327
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001328 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001329 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001330 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001331 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001332 mObjectsSize++;
1333 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001334
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001335 return finishWrite(sizeof(flat_binder_object));
1336 }
1337
1338 if (!enoughData) {
1339 const status_t err = growData(sizeof(val));
1340 if (err != NO_ERROR) return err;
1341 }
1342 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001343 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1344 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001345 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001346 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001347 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001348 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001349 mObjects = objects;
1350 mObjectsCapacity = newSize;
1351 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001352
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001353 goto restart_write;
1354}
1355
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001356status_t Parcel::writeNoException()
1357{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001358 binder::Status status;
1359 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001360}
1361
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001362status_t Parcel::validateReadData(size_t upperBound) const
1363{
1364 // Don't allow non-object reads on object data
1365 if (mObjectsSorted || mObjectsSize <= 1) {
1366data_sorted:
1367 // Expect to check only against the next object
1368 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1369 // For some reason the current read position is greater than the next object
1370 // hint. Iterate until we find the right object
1371 size_t nextObject = mNextObjectHint;
1372 do {
1373 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1374 // Requested info overlaps with an object
1375 ALOGE("Attempt to read from protected data in Parcel %p", this);
1376 return PERMISSION_DENIED;
1377 }
1378 nextObject++;
1379 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1380 mNextObjectHint = nextObject;
1381 }
1382 return NO_ERROR;
1383 }
1384 // Quickly determine if mObjects is sorted.
1385 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1386 binder_size_t* prevObj = currObj;
1387 while (currObj > mObjects) {
1388 prevObj--;
1389 if(*prevObj > *currObj) {
1390 goto data_unsorted;
1391 }
1392 currObj--;
1393 }
1394 mObjectsSorted = true;
1395 goto data_sorted;
1396
1397data_unsorted:
1398 // Insertion Sort mObjects
1399 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1400 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1401 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1402 binder_size_t temp = *iter0;
1403 binder_size_t* iter1 = iter0 - 1;
1404 while (iter1 >= mObjects && *iter1 > temp) {
1405 *(iter1 + 1) = *iter1;
1406 iter1--;
1407 }
1408 *(iter1 + 1) = temp;
1409 }
1410 mNextObjectHint = 0;
1411 mObjectsSorted = true;
1412 goto data_sorted;
1413}
1414
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001415status_t Parcel::read(void* outData, size_t len) const
1416{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001417 if (len > INT32_MAX) {
1418 // don't accept size_t values which may have come from an
1419 // inadvertent conversion from a negative int.
1420 return BAD_VALUE;
1421 }
1422
1423 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1424 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001425 if (mObjectsSize > 0) {
1426 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001427 if(err != NO_ERROR) {
1428 // Still increment the data position by the expected length
1429 mDataPos += pad_size(len);
1430 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1431 return err;
1432 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001433 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001434 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001435 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001436 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001437 return NO_ERROR;
1438 }
1439 return NOT_ENOUGH_DATA;
1440}
1441
1442const void* Parcel::readInplace(size_t len) const
1443{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001444 if (len > INT32_MAX) {
1445 // don't accept size_t values which may have come from an
1446 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001447 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001448 }
1449
1450 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1451 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001452 if (mObjectsSize > 0) {
1453 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001454 if(err != NO_ERROR) {
1455 // Still increment the data position by the expected length
1456 mDataPos += pad_size(len);
1457 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001458 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001459 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001460 }
1461
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001462 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001463 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001464 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001465 return data;
1466 }
Yi Kong91635562018-06-07 14:38:36 -07001467 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001468}
1469
Andreas Huber84a6d042009-08-17 13:33:27 -07001470template<class T>
1471status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001472 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001473
1474 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001475 if (mObjectsSize > 0) {
1476 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001477 if(err != NO_ERROR) {
1478 // Still increment the data position by the expected length
1479 mDataPos += sizeof(T);
1480 return err;
1481 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001482 }
1483
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001484 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001485 mDataPos += sizeof(T);
1486 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001487 return NO_ERROR;
1488 } else {
1489 return NOT_ENOUGH_DATA;
1490 }
1491}
1492
Andreas Huber84a6d042009-08-17 13:33:27 -07001493template<class T>
1494T Parcel::readAligned() const {
1495 T result;
1496 if (readAligned(&result) != NO_ERROR) {
1497 result = 0;
1498 }
1499
1500 return result;
1501}
1502
1503template<class T>
1504status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001505 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001506
1507 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1508restart_write:
1509 *reinterpret_cast<T*>(mData+mDataPos) = val;
1510 return finishWrite(sizeof(val));
1511 }
1512
1513 status_t err = growData(sizeof(val));
1514 if (err == NO_ERROR) goto restart_write;
1515 return err;
1516}
1517
1518status_t Parcel::readInt32(int32_t *pArg) const
1519{
1520 return readAligned(pArg);
1521}
1522
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001523int32_t Parcel::readInt32() const
1524{
Andreas Huber84a6d042009-08-17 13:33:27 -07001525 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001526}
1527
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001528status_t Parcel::readUint32(uint32_t *pArg) const
1529{
1530 return readAligned(pArg);
1531}
1532
1533uint32_t Parcel::readUint32() const
1534{
1535 return readAligned<uint32_t>();
1536}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001537
1538status_t Parcel::readInt64(int64_t *pArg) const
1539{
Andreas Huber84a6d042009-08-17 13:33:27 -07001540 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001541}
1542
1543
1544int64_t Parcel::readInt64() const
1545{
Andreas Huber84a6d042009-08-17 13:33:27 -07001546 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001547}
1548
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001549status_t Parcel::readUint64(uint64_t *pArg) const
1550{
1551 return readAligned(pArg);
1552}
1553
1554uint64_t Parcel::readUint64() const
1555{
1556 return readAligned<uint64_t>();
1557}
1558
Serban Constantinescuf683e012013-11-05 16:53:55 +00001559status_t Parcel::readPointer(uintptr_t *pArg) const
1560{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001561 status_t ret;
1562 binder_uintptr_t ptr;
1563 ret = readAligned(&ptr);
1564 if (!ret)
1565 *pArg = ptr;
1566 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001567}
1568
1569uintptr_t Parcel::readPointer() const
1570{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001571 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001572}
1573
1574
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001575status_t Parcel::readFloat(float *pArg) const
1576{
Andreas Huber84a6d042009-08-17 13:33:27 -07001577 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001578}
1579
1580
1581float Parcel::readFloat() const
1582{
Andreas Huber84a6d042009-08-17 13:33:27 -07001583 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001584}
1585
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001586#if defined(__mips__) && defined(__mips_hard_float)
1587
1588status_t Parcel::readDouble(double *pArg) const
1589{
1590 union {
1591 double d;
1592 unsigned long long ll;
1593 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001594 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001595 status_t status;
1596 status = readAligned(&u.ll);
1597 *pArg = u.d;
1598 return status;
1599}
1600
1601double Parcel::readDouble() const
1602{
1603 union {
1604 double d;
1605 unsigned long long ll;
1606 } u;
1607 u.ll = readAligned<unsigned long long>();
1608 return u.d;
1609}
1610
1611#else
1612
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001613status_t Parcel::readDouble(double *pArg) const
1614{
Andreas Huber84a6d042009-08-17 13:33:27 -07001615 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001616}
1617
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001618double Parcel::readDouble() const
1619{
Andreas Huber84a6d042009-08-17 13:33:27 -07001620 return readAligned<double>();
1621}
1622
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001623#endif
1624
Casey Dahlind6848f52015-10-15 15:44:59 -07001625status_t Parcel::readBool(bool *pArg) const
1626{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001627 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001628 status_t ret = readInt32(&tmp);
1629 *pArg = (tmp != 0);
1630 return ret;
1631}
1632
1633bool Parcel::readBool() const
1634{
1635 return readInt32() != 0;
1636}
1637
1638status_t Parcel::readChar(char16_t *pArg) const
1639{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001640 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001641 status_t ret = readInt32(&tmp);
1642 *pArg = char16_t(tmp);
1643 return ret;
1644}
1645
1646char16_t Parcel::readChar() const
1647{
1648 return char16_t(readInt32());
1649}
1650
1651status_t Parcel::readByte(int8_t *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 = int8_t(tmp);
1656 return ret;
1657}
1658
1659int8_t Parcel::readByte() const
1660{
1661 return int8_t(readInt32());
1662}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001663
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001664status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1665 size_t utf16Size = 0;
1666 const char16_t* src = readString16Inplace(&utf16Size);
1667 if (!src) {
1668 return UNEXPECTED_NULL;
1669 }
1670
1671 // Save ourselves the trouble, we're done.
1672 if (utf16Size == 0u) {
1673 str->clear();
1674 return NO_ERROR;
1675 }
1676
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001677 // Allow for closing '\0'
1678 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1679 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001680 return BAD_VALUE;
1681 }
1682 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001683 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001684 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001685 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1686 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001687 return NO_ERROR;
1688}
1689
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001690const char* Parcel::readCString() const
1691{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001692 if (mDataPos < mDataSize) {
1693 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001694 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1695 // is the string's trailing NUL within the parcel's valid bounds?
1696 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1697 if (eos) {
1698 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001699 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001700 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001701 return str;
1702 }
1703 }
Yi Kong91635562018-06-07 14:38:36 -07001704 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001705}
1706
1707String8 Parcel::readString8() const
1708{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001709 size_t len;
1710 const char* str = readString8Inplace(&len);
1711 if (str) return String8(str, len);
1712 ALOGE("Reading a NULL string not supported here.");
1713 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07001714}
1715
1716status_t Parcel::readString8(String8* pArg) const
1717{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001718 size_t len;
1719 const char* str = readString8Inplace(&len);
1720 if (str) {
1721 pArg->setTo(str, len);
1722 return 0;
1723 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07001724 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001725 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07001726 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001727}
1728
1729const char* Parcel::readString8Inplace(size_t* outLen) const
1730{
1731 int32_t size = readInt32();
1732 // watch for potential int overflow from size+1
1733 if (size >= 0 && size < INT32_MAX) {
1734 *outLen = size;
1735 const char* str = (const char*)readInplace(size+1);
1736 if (str != nullptr) {
Steven Moreland61d0f842020-12-04 21:13:03 +00001737 if (str[size] == '\0') {
1738 return str;
1739 }
1740 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001741 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001742 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001743 *outLen = 0;
1744 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001745}
1746
1747String16 Parcel::readString16() const
1748{
1749 size_t len;
1750 const char16_t* str = readString16Inplace(&len);
1751 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001752 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001753 return String16();
1754}
1755
Casey Dahlinb9872622015-11-25 15:09:45 -08001756
Casey Dahlin451ff582015-10-19 18:12:18 -07001757status_t Parcel::readString16(String16* pArg) const
1758{
1759 size_t len;
1760 const char16_t* str = readString16Inplace(&len);
1761 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001762 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001763 return 0;
1764 } else {
1765 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001766 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001767 }
1768}
1769
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001770const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1771{
1772 int32_t size = readInt32();
1773 // watch for potential int overflow from size+1
1774 if (size >= 0 && size < INT32_MAX) {
1775 *outLen = size;
1776 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001777 if (str != nullptr) {
Steven Moreland61d0f842020-12-04 21:13:03 +00001778 if (str[size] == u'\0') {
1779 return str;
1780 }
1781 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001782 }
1783 }
1784 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001785 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001786}
1787
Casey Dahlinf0c13772015-10-27 18:33:56 -07001788status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1789{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001790 status_t status = readNullableStrongBinder(val);
1791 if (status == OK && !val->get()) {
1792 status = UNEXPECTED_NULL;
1793 }
1794 return status;
1795}
1796
1797status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1798{
Steven Morelanda86a3562019-08-01 23:28:34 +00001799 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001800}
1801
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001802sp<IBinder> Parcel::readStrongBinder() const
1803{
1804 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001805 // Note that a lot of code in Android reads binders by hand with this
1806 // method, and that code has historically been ok with getting nullptr
1807 // back (while ignoring error codes).
1808 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001809 return val;
1810}
1811
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001812int32_t Parcel::readExceptionCode() const
1813{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001814 binder::Status status;
1815 status.readFromParcel(*this);
1816 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001817}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001818
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001819native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001820{
1821 int numFds, numInts;
1822 status_t err;
1823 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001824 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001825 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001826 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001827
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001828 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001829 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001830 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001831 }
1832
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001833 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001834 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001835 if (h->data[i] < 0) {
1836 for (int j = 0; j < i; j++) {
1837 close(h->data[j]);
1838 }
1839 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001840 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001841 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001842 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001843 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001844 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001845 native_handle_close(h);
1846 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001847 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001848 }
1849 return h;
1850}
1851
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001852int Parcel::readFileDescriptor() const
1853{
1854 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001855
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001856 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001857 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001858 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001859
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001860 return BAD_TYPE;
1861}
1862
Dianne Hackborn1941a402016-08-29 12:30:43 -07001863int Parcel::readParcelFileDescriptor() const
1864{
1865 int32_t hasComm = readInt32();
1866 int fd = readFileDescriptor();
1867 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001868 // detach (owned by the binder driver)
1869 int comm = readFileDescriptor();
1870
1871 // warning: this must be kept in sync with:
1872 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1873 enum ParcelFileDescriptorStatus {
1874 DETACHED = 2,
1875 };
1876
1877#if BYTE_ORDER == BIG_ENDIAN
1878 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
1879#endif
1880#if BYTE_ORDER == LITTLE_ENDIAN
1881 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
1882#endif
1883
1884 ssize_t written = TEMP_FAILURE_RETRY(
1885 ::write(comm, &message, sizeof(message)));
1886
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08001887 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001888 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
1889 written, strerror(errno));
1890 return BAD_TYPE;
1891 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07001892 }
1893 return fd;
1894}
1895
Christopher Wiley2cf19952016-04-11 11:09:37 -07001896status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08001897{
1898 int got = readFileDescriptor();
1899
1900 if (got == BAD_TYPE) {
1901 return BAD_TYPE;
1902 }
1903
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001904 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08001905
1906 if (val->get() < 0) {
1907 return BAD_VALUE;
1908 }
1909
1910 return OK;
1911}
1912
Ryo Hashimotobf551892018-05-31 16:58:35 +09001913status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
1914{
1915 int got = readParcelFileDescriptor();
1916
1917 if (got == BAD_TYPE) {
1918 return BAD_TYPE;
1919 }
1920
1921 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
1922
1923 if (val->get() < 0) {
1924 return BAD_VALUE;
1925 }
1926
1927 return OK;
1928}
Casey Dahlin06673e32015-11-23 13:24:23 -08001929
Jeff Brown5707dbf2011-09-23 21:17:56 -07001930status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1931{
Jeff Brown13b16042014-11-11 16:44:25 -08001932 int32_t blobType;
1933 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001934 if (status) return status;
1935
Jeff Brown13b16042014-11-11 16:44:25 -08001936 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001937 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001938 const void* ptr = readInplace(len);
1939 if (!ptr) return BAD_VALUE;
1940
Jeff Brown13b16042014-11-11 16:44:25 -08001941 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001942 return NO_ERROR;
1943 }
1944
Steve Block6807e592011-10-20 11:56:00 +01001945 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001946 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001947 int fd = readFileDescriptor();
1948 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1949
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001950 if (!ashmem_valid(fd)) {
1951 ALOGE("invalid fd");
1952 return BAD_VALUE;
1953 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001954 int size = ashmem_get_size_region(fd);
1955 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001956 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001957 return BAD_VALUE;
1958 }
Yi Kong91635562018-06-07 14:38:36 -07001959 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08001960 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001961 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001962
Jeff Brown13b16042014-11-11 16:44:25 -08001963 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001964 return NO_ERROR;
1965}
1966
Mathias Agopiane1424282013-07-29 21:24:40 -07001967status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001968{
1969 // size
1970 const size_t len = this->readInt32();
1971 const size_t fd_count = this->readInt32();
1972
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001973 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001974 // don't accept size_t values which may have come from an
1975 // inadvertent conversion from a negative int.
1976 return BAD_VALUE;
1977 }
1978
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001979 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001980 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07001981 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001982 return BAD_VALUE;
1983
Yi Kong91635562018-06-07 14:38:36 -07001984 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001985 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001986 fds = new (std::nothrow) int[fd_count];
1987 if (fds == nullptr) {
1988 ALOGE("read: failed to allocate requested %zu fds", fd_count);
1989 return BAD_VALUE;
1990 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001991 }
1992
1993 status_t err = NO_ERROR;
1994 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07001995 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001996 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001997 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001998 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 -07001999 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2000 // Close all the file descriptors that were dup-ed.
2001 for (size_t j=0; j<i ;j++) {
2002 close(fds[j]);
2003 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002004 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002005 }
2006
2007 if (err == NO_ERROR) {
2008 err = val.unflatten(buf, len, fds, fd_count);
2009 }
2010
2011 if (fd_count) {
2012 delete [] fds;
2013 }
2014
2015 return err;
2016}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002017const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2018{
2019 const size_t DPOS = mDataPos;
2020 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2021 const flat_binder_object* obj
2022 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2023 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002024 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002025 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002026 // the object list, so we don't want to check for it when
2027 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002028 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002029 return obj;
2030 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002031
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002032 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002033 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002034 const size_t N = mObjectsSize;
2035 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002036
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002037 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002038 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002039 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002040
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002041 // Start at the current hint position, looking for an object at
2042 // the current data position.
2043 if (opos < N) {
2044 while (opos < (N-1) && OBJS[opos] < DPOS) {
2045 opos++;
2046 }
2047 } else {
2048 opos = N-1;
2049 }
2050 if (OBJS[opos] == DPOS) {
2051 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002052 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002053 this, DPOS, opos);
2054 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002055 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002056 return obj;
2057 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002058
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002059 // Look backwards for it...
2060 while (opos > 0 && OBJS[opos] > DPOS) {
2061 opos--;
2062 }
2063 if (OBJS[opos] == DPOS) {
2064 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002065 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002066 this, DPOS, opos);
2067 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002068 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002069 return obj;
2070 }
2071 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002072 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 -07002073 this, DPOS);
2074 }
Yi Kong91635562018-06-07 14:38:36 -07002075 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002076}
2077
2078void Parcel::closeFileDescriptors()
2079{
2080 size_t i = mObjectsSize;
2081 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002082 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002083 }
2084 while (i > 0) {
2085 i--;
2086 const flat_binder_object* flat
2087 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002088 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002089 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002090 close(flat->handle);
2091 }
2092 }
2093}
2094
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002095uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002096{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002097 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002098}
2099
2100size_t Parcel::ipcDataSize() const
2101{
2102 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2103}
2104
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002105uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002106{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002107 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002108}
2109
2110size_t Parcel::ipcObjectsCount() const
2111{
2112 return mObjectsSize;
2113}
2114
2115void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Steven Moreland161fe122020-11-12 23:16:47 +00002116 const binder_size_t* objects, size_t objectsCount, release_func relFunc)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002117{
Steven Moreland438cce82021-04-02 18:04:08 +00002118 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2119 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2120
Steven Morelandceed9bb2020-12-17 01:01:06 +00002121 freeData();
2122
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002123 mData = const_cast<uint8_t*>(data);
2124 mDataSize = mDataCapacity = dataSize;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002125 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002126 mObjectsSize = mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002127 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002128
2129 binder_size_t minOffset = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002130 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002131 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002132 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002133 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002134 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002135 mObjectsSize = 0;
2136 break;
2137 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002138 const flat_binder_object* flat
2139 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2140 uint32_t type = flat->hdr.type;
2141 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2142 type == BINDER_TYPE_FD)) {
2143 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2144 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2145 // recover gracefully by clearing out the objects, and releasing the objects we do
2146 // know about.
2147 android_errorWriteLog(0x534e4554, "135930648");
2148 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2149 __func__, type, (uint64_t)offset);
2150 releaseObjects();
2151 mObjectsSize = 0;
2152 break;
2153 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002154 minOffset = offset + sizeof(flat_binder_object);
2155 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002156 scanForFds();
2157}
2158
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002159void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002160{
2161 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002162
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002163 if (errorCheck() != NO_ERROR) {
2164 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002165 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002166 } else if (dataSize() > 0) {
2167 const uint8_t* DATA = data();
2168 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002169 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002170 const size_t N = objectsCount();
2171 for (size_t i=0; i<N; i++) {
2172 const flat_binder_object* flat
2173 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2174 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002175 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002176 << " = " << flat->binder;
2177 }
2178 } else {
2179 to << "NULL";
2180 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002181
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002182 to << ")";
2183}
2184
2185void Parcel::releaseObjects()
2186{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002187 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002188 if (i == 0) {
2189 return;
2190 }
2191 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002192 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002193 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002194 while (i > 0) {
2195 i--;
2196 const flat_binder_object* flat
2197 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002198 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002199 }
2200}
2201
2202void Parcel::acquireObjects()
2203{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002204 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002205 if (i == 0) {
2206 return;
2207 }
2208 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002209 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002210 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002211 while (i > 0) {
2212 i--;
2213 const flat_binder_object* flat
2214 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002215 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002216 }
2217}
2218
2219void Parcel::freeData()
2220{
2221 freeDataNoInit();
2222 initState();
2223}
2224
2225void Parcel::freeDataNoInit()
2226{
2227 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002228 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002229 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002230 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002232 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002233 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002234 if (mData) {
2235 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002236 gParcelGlobalAllocSize -= mDataCapacity;
2237 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002238 if (mDeallocZero) {
2239 zeroMemory(mData, mDataSize);
2240 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002241 free(mData);
2242 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002243 if (mObjects) free(mObjects);
2244 }
2245}
2246
2247status_t Parcel::growData(size_t len)
2248{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002249 if (len > INT32_MAX) {
2250 // don't accept size_t values which may have come from an
2251 // inadvertent conversion from a negative int.
2252 return BAD_VALUE;
2253 }
2254
Martijn Coenen93fe5182020-01-22 10:46:25 +01002255 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2256 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002257 size_t newSize = ((mDataSize+len)*3)/2;
2258 return (newSize <= mDataSize)
2259 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002260 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002261}
2262
Steven Morelandf183fdd2020-10-27 00:12:12 +00002263static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2264 if (!zero) {
2265 return (uint8_t*)realloc(data, newCapacity);
2266 }
2267 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2268 if (!newData) {
2269 return nullptr;
2270 }
2271
2272 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2273 zeroMemory(data, oldCapacity);
2274 free(data);
2275 return newData;
2276}
2277
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002278status_t Parcel::restartWrite(size_t desired)
2279{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002280 if (desired > INT32_MAX) {
2281 // don't accept size_t values which may have come from an
2282 // inadvertent conversion from a negative int.
2283 return BAD_VALUE;
2284 }
2285
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002286 if (mOwner) {
2287 freeData();
2288 return continueWrite(desired);
2289 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002290
Steven Morelandf183fdd2020-10-27 00:12:12 +00002291 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002292 if (!data && desired > mDataCapacity) {
2293 mError = NO_MEMORY;
2294 return NO_MEMORY;
2295 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002296
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002297 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002298
Devin Moore4a0a55e2020-06-04 13:23:10 -07002299 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002300 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002301 if (mDataCapacity > desired) {
2302 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2303 } else {
2304 gParcelGlobalAllocSize += (desired - mDataCapacity);
2305 }
2306
Colin Cross83ec65e2015-12-08 17:15:50 -08002307 if (!mData) {
2308 gParcelGlobalAllocCount++;
2309 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002310 mData = data;
2311 mDataCapacity = desired;
2312 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002313
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002314 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002315 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2316 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2317
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002318 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002319 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002320 mObjectsSize = mObjectsCapacity = 0;
2321 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002322 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002323 mHasFds = false;
2324 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002325 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002326
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002327 return NO_ERROR;
2328}
2329
2330status_t Parcel::continueWrite(size_t desired)
2331{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002332 if (desired > INT32_MAX) {
2333 // don't accept size_t values which may have come from an
2334 // inadvertent conversion from a negative int.
2335 return BAD_VALUE;
2336 }
2337
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002338 // If shrinking, first adjust for any objects that appear
2339 // after the new data size.
2340 size_t objectsSize = mObjectsSize;
2341 if (desired < mDataSize) {
2342 if (desired == 0) {
2343 objectsSize = 0;
2344 } else {
2345 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002346 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002347 break;
2348 objectsSize--;
2349 }
2350 }
2351 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002352
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002353 if (mOwner) {
2354 // If the size is going to zero, just release the owner's data.
2355 if (desired == 0) {
2356 freeData();
2357 return NO_ERROR;
2358 }
2359
2360 // If there is a different owner, we need to take
2361 // posession.
2362 uint8_t* data = (uint8_t*)malloc(desired);
2363 if (!data) {
2364 mError = NO_MEMORY;
2365 return NO_MEMORY;
2366 }
Yi Kong91635562018-06-07 14:38:36 -07002367 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002368
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002369 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002370 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002371 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002372 free(data);
2373
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002374 mError = NO_MEMORY;
2375 return NO_MEMORY;
2376 }
2377
2378 // Little hack to only acquire references on objects
2379 // we will be keeping.
2380 size_t oldObjectsSize = mObjectsSize;
2381 mObjectsSize = objectsSize;
2382 acquireObjects();
2383 mObjectsSize = oldObjectsSize;
2384 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002385
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002386 if (mData) {
2387 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2388 }
2389 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002390 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002391 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002392 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002393 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
Yi Kong91635562018-06-07 14:38:36 -07002394 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002395
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002396 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002397 gParcelGlobalAllocSize += desired;
2398 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002399
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002400 mData = data;
2401 mObjects = objects;
2402 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002403 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002404 mDataCapacity = desired;
2405 mObjectsSize = mObjectsCapacity = objectsSize;
2406 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002407 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002408
2409 } else if (mData) {
2410 if (objectsSize < mObjectsSize) {
2411 // Need to release refs on any objects we are dropping.
2412 const sp<ProcessState> proc(ProcessState::self());
2413 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2414 const flat_binder_object* flat
2415 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002416 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002417 // will need to rescan because we may have lopped off the only FDs
2418 mFdsKnown = false;
2419 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002420 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002422
2423 if (objectsSize == 0) {
2424 free(mObjects);
2425 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002426 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002427 } else {
2428 binder_size_t* objects =
2429 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2430 if (objects) {
2431 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002432 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002433 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002434 }
2435 mObjectsSize = objectsSize;
2436 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002437 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002438 }
2439
2440 // We own the data, so we can just do a realloc().
2441 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002442 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002443 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002444 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2445 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002446 gParcelGlobalAllocSize += desired;
2447 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002448 mData = data;
2449 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002450 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002451 mError = NO_MEMORY;
2452 return NO_MEMORY;
2453 }
2454 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002455 if (mDataSize > desired) {
2456 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002457 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002458 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002459 if (mDataPos > desired) {
2460 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002461 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002462 }
2463 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002464
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002465 } else {
2466 // This is the first data. Easy!
2467 uint8_t* data = (uint8_t*)malloc(desired);
2468 if (!data) {
2469 mError = NO_MEMORY;
2470 return NO_MEMORY;
2471 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002472
Yi Kong91635562018-06-07 14:38:36 -07002473 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002474 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002475 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002476 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002477
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002478 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002479 gParcelGlobalAllocSize += desired;
2480 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002481
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002482 mData = data;
2483 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002484 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2485 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002486 mDataCapacity = desired;
2487 }
2488
2489 return NO_ERROR;
2490}
2491
2492void Parcel::initState()
2493{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002494 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002495 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002496 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002497 mDataSize = 0;
2498 mDataCapacity = 0;
2499 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002500 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2501 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Steven Moreland5553ac42020-11-11 02:14:45 +00002502 mConnection = nullptr;
Yi Kong91635562018-06-07 14:38:36 -07002503 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002504 mObjectsSize = 0;
2505 mObjectsCapacity = 0;
2506 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002507 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508 mHasFds = false;
2509 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002510 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002511 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07002512 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002513 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002514 mWorkSourceRequestHeaderPosition = 0;
2515 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002516
2517 // racing multiple init leads only to multiple identical write
2518 if (gMaxFds == 0) {
2519 struct rlimit result;
2520 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2521 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002522 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002523 } else {
2524 ALOGW("Unable to getrlimit: %s", strerror(errno));
2525 gMaxFds = 1024;
2526 }
2527 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002528}
2529
2530void Parcel::scanForFds() const
2531{
2532 bool hasFds = false;
2533 for (size_t i=0; i<mObjectsSize; i++) {
2534 const flat_binder_object* flat
2535 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002536 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002537 hasFds = true;
2538 break;
2539 }
2540 }
2541 mHasFds = hasFds;
2542 mFdsKnown = true;
2543}
2544
Dan Sandleraa5c2342015-04-10 10:08:45 -04002545size_t Parcel::getBlobAshmemSize() const
2546{
Adrian Roos6bb31142015-10-22 16:46:12 -07002547 // This used to return the size of all blobs that were written to ashmem, now we're returning
2548 // the ashmem currently referenced by this Parcel, which should be equivalent.
2549 // TODO: Remove method once ABI can be changed.
2550 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002551}
2552
Adrian Rooscbf37262015-10-22 16:12:53 -07002553size_t Parcel::getOpenAshmemSize() const
2554{
2555 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002556}
2557
2558// --- Parcel::Blob ---
2559
2560Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002561 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002562}
2563
2564Parcel::Blob::~Blob() {
2565 release();
2566}
2567
2568void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002569 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002570 ::munmap(mData, mSize);
2571 }
2572 clear();
2573}
2574
Jeff Brown13b16042014-11-11 16:44:25 -08002575void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2576 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002577 mData = data;
2578 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002579 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002580}
2581
2582void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002583 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002584 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002585 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002586 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002587}
2588
Steven Moreland61ff8492019-09-26 16:05:45 -07002589} // namespace android