blob: 6ce09226d9e621cf75cf084795d4512acee35c08 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Steven Morelandbf1915b2020-07-16 22:43:02 +000023#include <linux/sched.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080024#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080029#include <sys/stat.h>
30#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070031#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080032#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070033
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/Binder.h>
35#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080036#include <binder/IPCThreadState.h>
37#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070038#include <binder/ProcessState.h>
Steven Moreland6e5a7752019-08-05 20:30:14 -070039#include <binder/Stability.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080040#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041#include <binder/TextOutput.h>
42
Mark Salyzynabed7f72016-01-27 08:02:48 -080043#include <cutils/ashmem.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000044#include <cutils/compiler.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080045#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#include <utils/Log.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String16.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000048#include <utils/String8.h>
49#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070050
Steven Moreland5553ac42020-11-11 02:14:45 +000051#include "RpcState.h"
Steven Morelanda4853cd2019-07-12 15:44:37 -070052#include "Static.h"
Steven Morelandf183fdd2020-10-27 00:12:12 +000053#include "Utils.h"
Steven Moreland6ba5a252021-05-04 22:49:00 +000054#include "binder_module.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070055
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070056#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080057//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080058#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080059//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070060
61// ---------------------------------------------------------------------------
62
Nick Kralevichb6b14232015-04-02 09:36:02 -070063// This macro should never be used at runtime, as a too large value
64// of s could cause an integer overflow. Instead, you should always
65// use the wrapper function pad_size()
66#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
67
68static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070069 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070070 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070071 }
72 return PAD_SIZE_UNSAFE(s);
73}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070074
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070075// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060076#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070077
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070078namespace android {
79
Steven Moreland7b102262019-08-01 15:48:43 -070080// many things compile this into prebuilts on the stack
Steven Moreland90c1f9a2021-05-03 18:27:24 +000081#ifdef __LP64__
82static_assert(sizeof(Parcel) == 120);
83#else
84static_assert(sizeof(Parcel) == 60);
85#endif
Steven Moreland7b102262019-08-01 15:48:43 -070086
Jeff Sharkey8994c182020-09-11 12:07:10 -060087static std::atomic<size_t> gParcelGlobalAllocCount;
88static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -080089
Christopher Tatee4e0ae82016-03-24 16:03:44 -070090static size_t gMaxFds = 0;
91
Jeff Brown13b16042014-11-11 16:44:25 -080092// Maximum size of a blob to transfer in-place.
93static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
94
95enum {
96 BLOB_INPLACE = 0,
97 BLOB_ASHMEM_IMMUTABLE = 1,
98 BLOB_ASHMEM_MUTABLE = 2,
99};
100
Steven Morelandb1c81202019-04-05 18:49:55 -0700101static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700102 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700103{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700104 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700105 case BINDER_TYPE_BINDER:
106 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000107 LOG_REFS("Parcel %p acquiring reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800108 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 }
110 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700111 case BINDER_TYPE_HANDLE: {
112 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700113 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700114 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
115 b->incStrong(who);
116 }
117 return;
118 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700119 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000120 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700121 // If we own an ashmem fd, keep track of how much memory it refers to.
122 int size = ashmem_get_size_region(obj.handle);
123 if (size > 0) {
124 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700125 }
126 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700127 return;
128 }
129 }
130
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700131 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700132}
133
Adrian Roos6bb31142015-10-22 16:46:12 -0700134static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700135 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700137 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700138 case BINDER_TYPE_BINDER:
139 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000140 LOG_REFS("Parcel %p releasing reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800141 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142 }
143 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144 case BINDER_TYPE_HANDLE: {
145 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700146 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
148 b->decStrong(who);
149 }
150 return;
151 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700152 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800153 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000154 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700155 int size = ashmem_get_size_region(obj.handle);
156 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800157 // ashmem size might have changed since last time it was accounted for, e.g.
158 // in acquire_object(). Value of *outAshmemSize is not critical since we are
159 // releasing the object anyway. Check for integer overflow condition.
160 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700161 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700162 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800163
164 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700165 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700166 return;
167 }
168 }
169
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700170 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700171}
172
Steven Moreland34b48cb2020-12-01 22:45:38 +0000173status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700174{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700175 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland16a41062021-07-23 13:35:25 -0700176 int16_t rep = internal::Stability::getRepr(binder.get());
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000177 return writeInt32(rep);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700178}
179
Steven Morelanda86a3562019-08-01 23:28:34 +0000180status_t Parcel::finishUnflattenBinder(
181 const sp<IBinder>& binder, sp<IBinder>* out) const
182{
183 int32_t stability;
184 status_t status = readInt32(&stability);
185 if (status != OK) return status;
186
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000187 status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability),
188 true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000189 if (status != OK) return status;
190
191 *out = binder;
192 return OK;
193}
194
Steven Morelandbf1915b2020-07-16 22:43:02 +0000195static constexpr inline int schedPolicyMask(int policy, int priority) {
196 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
197}
198
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500199status_t Parcel::flattenBinder(const sp<IBinder>& binder) {
200 BBinder* local = nullptr;
201 if (binder) local = binder->localBinder();
202 if (local) local->setParceled();
203
Steven Moreland5553ac42020-11-11 02:14:45 +0000204 if (isForRpc()) {
205 if (binder) {
206 status_t status = writeInt32(1); // non-null
207 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700208 uint64_t address;
Steven Morelanda5036f02021-06-08 02:26:57 +0000209 // TODO(b/167966510): need to undo this if the Parcel is not sent
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000210 status = mSession->state()->onBinderLeaving(mSession, binder, &address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000211 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700212 status = writeUint64(address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000213 if (status != OK) return status;
214 } else {
215 status_t status = writeInt32(0); // null
216 if (status != OK) return status;
217 }
218 return finishFlattenBinder(binder);
219 }
220
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700221 flat_binder_object obj;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000222 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700223
Steven Morelandbf1915b2020-07-16 22:43:02 +0000224 int schedBits = 0;
225 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
226 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700227 }
228
Yi Kong91635562018-06-07 14:38:36 -0700229 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230 if (!local) {
231 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700232 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000233 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000234 } else {
235 if (proxy->isRpcBinder()) {
Steven Morelanda9231112021-09-22 10:08:14 -0700236 ALOGE("Sending a socket binder over kernel binder is prohibited");
Steven Moreland5553ac42020-11-11 02:14:45 +0000237 return INVALID_OPERATION;
238 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700239 }
Steven Moreland99157622021-09-13 16:27:34 -0700240 const int32_t handle = proxy ? proxy->getPrivateAccessor().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700241 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800242 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800244 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700245 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000246 int policy = local->getMinSchedulerPolicy();
247 int priority = local->getMinSchedulerPriority();
248
249 if (policy != 0 || priority != 0) {
250 // override value, since it is set explicitly
251 schedBits = schedPolicyMask(policy, priority);
252 }
Steven Morelandf0212002018-12-26 13:59:23 -0800253 if (local->isRequestingSid()) {
254 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
255 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000256 if (local->isInheritRt()) {
257 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
258 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700259 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800260 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
261 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700262 }
263 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700264 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800265 obj.binder = 0;
266 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700267 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700268
Steven Morelandbf1915b2020-07-16 22:43:02 +0000269 obj.flags |= schedBits;
270
Steven Moreland34b48cb2020-12-01 22:45:38 +0000271 status_t status = writeObject(obj, false);
272 if (status != OK) return status;
273
274 return finishFlattenBinder(binder);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700275}
276
Steven Morelanda86a3562019-08-01 23:28:34 +0000277status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700278{
Steven Moreland5553ac42020-11-11 02:14:45 +0000279 if (isForRpc()) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000280 LOG_ALWAYS_FATAL_IF(mSession == nullptr, "RpcSession required to read from remote parcel");
Steven Moreland5553ac42020-11-11 02:14:45 +0000281
Steven Moreland5623d1a2021-09-10 15:45:34 -0700282 int32_t isPresent;
283 status_t status = readInt32(&isPresent);
Steven Moreland5553ac42020-11-11 02:14:45 +0000284 if (status != OK) return status;
285
286 sp<IBinder> binder;
287
Steven Moreland5623d1a2021-09-10 15:45:34 -0700288 if (isPresent & 1) {
289 uint64_t addr;
290 if (status_t status = readUint64(&addr); status != OK) return status;
Steven Moreland7227c8a2021-06-02 00:24:32 +0000291 if (status_t status = mSession->state()->onBinderEntering(mSession, addr, &binder);
292 status != OK)
293 return status;
Steven Morelandd8083312021-09-22 13:37:10 -0700294 if (status_t status = mSession->state()->flushExcessBinderRefs(mSession, addr, binder);
295 status != OK)
296 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000297 }
298
299 return finishUnflattenBinder(binder, out);
300 }
301
Steven Morelanda86a3562019-08-01 23:28:34 +0000302 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700303
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700304 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700305 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000306 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000307 sp<IBinder> binder =
308 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000309 return finishUnflattenBinder(binder, out);
310 }
311 case BINDER_TYPE_HANDLE: {
312 sp<IBinder> binder =
313 ProcessState::self()->getStrongProxyForHandle(flat->handle);
314 return finishUnflattenBinder(binder, out);
315 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700316 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700317 }
318 return BAD_TYPE;
319}
320
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700321// ---------------------------------------------------------------------------
322
323Parcel::Parcel()
324{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800325 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700326 initState();
327}
328
329Parcel::~Parcel()
330{
331 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800332 LOG_ALLOC("Parcel %p: destroyed", this);
333}
334
335size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600336 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800337}
338
339size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600340 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700341}
342
343const uint8_t* Parcel::data() const
344{
345 return mData;
346}
347
348size_t Parcel::dataSize() const
349{
350 return (mDataSize > mDataPos ? mDataSize : mDataPos);
351}
352
353size_t Parcel::dataAvail() const
354{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700355 size_t result = dataSize() - dataPosition();
356 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700357 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700358 }
359 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700360}
361
362size_t Parcel::dataPosition() const
363{
364 return mDataPos;
365}
366
367size_t Parcel::dataCapacity() const
368{
369 return mDataCapacity;
370}
371
372status_t Parcel::setDataSize(size_t size)
373{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700374 if (size > INT32_MAX) {
375 // don't accept size_t values which may have come from an
376 // inadvertent conversion from a negative int.
377 return BAD_VALUE;
378 }
379
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700380 status_t err;
381 err = continueWrite(size);
382 if (err == NO_ERROR) {
383 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700384 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700385 }
386 return err;
387}
388
389void Parcel::setDataPosition(size_t pos) const
390{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700391 if (pos > INT32_MAX) {
392 // don't accept size_t values which may have come from an
393 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700394 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700395 }
396
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700397 mDataPos = pos;
398 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800399 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700400}
401
402status_t Parcel::setDataCapacity(size_t size)
403{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700404 if (size > INT32_MAX) {
405 // don't accept size_t values which may have come from an
406 // inadvertent conversion from a negative int.
407 return BAD_VALUE;
408 }
409
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700410 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700411 return NO_ERROR;
412}
413
414status_t Parcel::setData(const uint8_t* buffer, size_t len)
415{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700416 if (len > INT32_MAX) {
417 // don't accept size_t values which may have come from an
418 // inadvertent conversion from a negative int.
419 return BAD_VALUE;
420 }
421
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700422 status_t err = restartWrite(len);
423 if (err == NO_ERROR) {
424 memcpy(const_cast<uint8_t*>(data()), buffer, len);
425 mDataSize = len;
426 mFdsKnown = false;
427 }
428 return err;
429}
430
Andreas Huber51faf462011-04-13 10:21:56 -0700431status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700432{
Steven Moreland67753c32021-04-02 18:45:19 +0000433 if (parcel->isForRpc() != isForRpc()) {
434 ALOGE("Cannot append Parcel of one format to another.");
435 return BAD_TYPE;
436 }
437
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700438 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700439 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800440 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700441 size_t size = parcel->mObjectsSize;
442 int startPos = mDataPos;
443 int firstIndex = -1, lastIndex = -2;
444
445 if (len == 0) {
446 return NO_ERROR;
447 }
448
Nick Kralevichb6b14232015-04-02 09:36:02 -0700449 if (len > INT32_MAX) {
450 // don't accept size_t values which may have come from an
451 // inadvertent conversion from a negative int.
452 return BAD_VALUE;
453 }
454
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700455 // range checks against the source parcel size
456 if ((offset > parcel->mDataSize)
457 || (len > parcel->mDataSize)
458 || (offset + len > parcel->mDataSize)) {
459 return BAD_VALUE;
460 }
461
462 // Count objects in range
463 for (int i = 0; i < (int) size; i++) {
464 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700465 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700466 if (firstIndex == -1) {
467 firstIndex = i;
468 }
469 lastIndex = i;
470 }
471 }
472 int numObjects = lastIndex - firstIndex + 1;
473
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700474 if ((mDataSize+len) > mDataCapacity) {
475 // grow data
476 err = growData(len);
477 if (err != NO_ERROR) {
478 return err;
479 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700480 }
481
482 // append data
483 memcpy(mData + mDataPos, data + offset, len);
484 mDataPos += len;
485 mDataSize += len;
486
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400487 err = NO_ERROR;
488
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700489 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200490 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700491 // grow objects
492 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +0100493 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
494 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700495 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +0100496 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800497 binder_size_t *objects =
498 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700499 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700500 return NO_MEMORY;
501 }
502 mObjects = objects;
503 mObjectsCapacity = newSize;
504 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700505
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700506 // append and acquire objects
507 int idx = mObjectsSize;
508 for (int i = firstIndex; i <= lastIndex; i++) {
509 size_t off = objects[i] - offset + startPos;
510 mObjects[idx++] = off;
511 mObjectsSize++;
512
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700513 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700514 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700515 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700516
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700517 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700518 // If this is a file descriptor, we need to dup it so the
519 // new Parcel now owns its own fd, and can declare that we
520 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800521 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800522 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700523 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400524 if (!mAllowFds) {
525 err = FDS_NOT_ALLOWED;
526 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700527 }
528 }
529 }
530
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400531 return err;
532}
533
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700534int Parcel::compareData(const Parcel& other) {
535 size_t size = dataSize();
536 if (size != other.dataSize()) {
537 return size < other.dataSize() ? -1 : 1;
538 }
539 return memcmp(data(), other.data(), size);
540}
541
Jeff Brown13b16042014-11-11 16:44:25 -0800542bool Parcel::allowFds() const
543{
544 return mAllowFds;
545}
546
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700547bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400548{
549 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700550 if (!allowFds) {
551 mAllowFds = false;
552 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400553 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700554}
555
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700556void Parcel::restoreAllowFds(bool lastValue)
557{
558 mAllowFds = lastValue;
559}
560
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700561bool Parcel::hasFileDescriptors() const
562{
563 if (!mFdsKnown) {
564 scanForFds();
565 }
566 return mHasFds;
567}
568
Steven Morelandf183fdd2020-10-27 00:12:12 +0000569void Parcel::markSensitive() const
570{
571 mDeallocZero = true;
572}
573
Steven Moreland5553ac42020-11-11 02:14:45 +0000574void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000575 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
576
Steven Moreland5553ac42020-11-11 02:14:45 +0000577 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700578 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000579 }
580}
581
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000582void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000583 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
584 "format must be set before data is written OR on IPC data");
585
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000586 LOG_ALWAYS_FATAL_IF(session == nullptr, "markForRpc requires session");
587 mSession = session;
Steven Moreland5553ac42020-11-11 02:14:45 +0000588}
589
590bool Parcel::isForRpc() const {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000591 return mSession != nullptr;
Steven Moreland5553ac42020-11-11 02:14:45 +0000592}
593
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000594void Parcel::updateWorkSourceRequestHeaderPosition() const {
595 // Only update the request headers once. We only want to point
596 // to the first headers read/written.
597 if (!mRequestHeaderPresent) {
598 mWorkSourceRequestHeaderPosition = dataPosition();
599 mRequestHeaderPresent = true;
600 }
601}
602
Jooyung Han1b228f42020-01-30 13:41:12 +0900603#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700604constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
605#else
606constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
607#endif
608
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700609// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700610status_t Parcel::writeInterfaceToken(const String16& interface)
611{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000612 return writeInterfaceToken(interface.string(), interface.size());
613}
614
615status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000616 if (CC_LIKELY(!isForRpc())) {
617 const IPCThreadState* threadState = IPCThreadState::self();
618 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
619 updateWorkSourceRequestHeaderPosition();
620 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
621 : IPCThreadState::kUnsetWorkSource);
622 writeInt32(kHeader);
623 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000624
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700625 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000626 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700627}
628
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000629bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
630{
631 if (!mRequestHeaderPresent) {
632 return false;
633 }
634
635 const size_t initialPosition = dataPosition();
636 setDataPosition(mWorkSourceRequestHeaderPosition);
637 status_t err = writeInt32(uid);
638 setDataPosition(initialPosition);
639 return err == NO_ERROR;
640}
641
Steven Morelandf1b1e492019-05-06 15:05:13 -0700642uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000643{
644 if (!mRequestHeaderPresent) {
645 return IPCThreadState::kUnsetWorkSource;
646 }
647
648 const size_t initialPosition = dataPosition();
649 setDataPosition(mWorkSourceRequestHeaderPosition);
650 uid_t uid = readInt32();
651 setDataPosition(initialPosition);
652 return uid;
653}
654
Mathias Agopian83c04462009-05-22 19:00:22 -0700655bool Parcel::checkInterface(IBinder* binder) const
656{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700657 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700658}
659
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700660bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700661 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700662{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700663 return enforceInterface(interface.string(), interface.size(), threadState);
664}
665
666bool Parcel::enforceInterface(const char16_t* interface,
667 size_t len,
668 IPCThreadState* threadState) const
669{
Steven Moreland3af936a2021-03-26 03:05:38 +0000670 if (CC_LIKELY(!isForRpc())) {
671 // StrictModePolicy.
672 int32_t strictPolicy = readInt32();
673 if (threadState == nullptr) {
674 threadState = IPCThreadState::self();
675 }
676 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
677 // For one-way calls, the callee is running entirely
678 // disconnected from the caller, so disable StrictMode entirely.
679 // Not only does disk/network usage not impact the caller, but
680 // there's no way to communicate back violations anyway.
681 threadState->setStrictModePolicy(0);
682 } else {
683 threadState->setStrictModePolicy(strictPolicy);
684 }
685 // WorkSource.
686 updateWorkSourceRequestHeaderPosition();
687 int32_t workSource = readInt32();
688 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
689 // vendor header
690 int32_t header = readInt32();
691 if (header != kHeader) {
692 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
693 header);
694 return false;
695 }
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700696 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000697
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100698 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700699 size_t parcel_interface_len;
700 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
701 if (len == parcel_interface_len &&
702 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700703 return true;
704 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700705 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700706 String8(interface, len).string(),
707 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700708 return false;
709 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700710}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700711
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700712size_t Parcel::objectsCount() const
713{
714 return mObjectsSize;
715}
716
717status_t Parcel::errorCheck() const
718{
719 return mError;
720}
721
722void Parcel::setError(status_t err)
723{
724 mError = err;
725}
726
727status_t Parcel::finishWrite(size_t len)
728{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700729 if (len > INT32_MAX) {
730 // don't accept size_t values which may have come from an
731 // inadvertent conversion from a negative int.
732 return BAD_VALUE;
733 }
734
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700735 //printf("Finish write of %d\n", len);
736 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700737 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700738 if (mDataPos > mDataSize) {
739 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700740 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700741 }
742 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
743 return NO_ERROR;
744}
745
746status_t Parcel::writeUnpadded(const void* data, size_t len)
747{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700748 if (len > INT32_MAX) {
749 // don't accept size_t values which may have come from an
750 // inadvertent conversion from a negative int.
751 return BAD_VALUE;
752 }
753
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700754 size_t end = mDataPos + len;
755 if (end < mDataPos) {
756 // integer overflow
757 return BAD_VALUE;
758 }
759
760 if (end <= mDataCapacity) {
761restart_write:
762 memcpy(mData+mDataPos, data, len);
763 return finishWrite(len);
764 }
765
766 status_t err = growData(len);
767 if (err == NO_ERROR) goto restart_write;
768 return err;
769}
770
771status_t Parcel::write(const void* data, size_t len)
772{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700773 if (len > INT32_MAX) {
774 // don't accept size_t values which may have come from an
775 // inadvertent conversion from a negative int.
776 return BAD_VALUE;
777 }
778
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700779 void* const d = writeInplace(len);
780 if (d) {
781 memcpy(d, data, len);
782 return NO_ERROR;
783 }
784 return mError;
785}
786
787void* Parcel::writeInplace(size_t len)
788{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700789 if (len > INT32_MAX) {
790 // don't accept size_t values which may have come from an
791 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700792 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700793 }
794
795 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700796
797 // sanity check for integer overflow
798 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700799 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700800 }
801
802 if ((mDataPos+padded) <= mDataCapacity) {
803restart_write:
804 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
805 uint8_t* const data = mData+mDataPos;
806
807 // Need to pad at end?
808 if (padded != len) {
809#if BYTE_ORDER == BIG_ENDIAN
810 static const uint32_t mask[4] = {
811 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
812 };
813#endif
814#if BYTE_ORDER == LITTLE_ENDIAN
815 static const uint32_t mask[4] = {
816 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
817 };
818#endif
819 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
820 // *reinterpret_cast<void**>(data+padded-4));
821 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
822 }
823
824 finishWrite(padded);
825 return data;
826 }
827
828 status_t err = growData(padded);
829 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700830 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700831}
832
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800833status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
834 const uint8_t* strData = (uint8_t*)str.data();
835 const size_t strLen= str.length();
836 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100837 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800838 return BAD_VALUE;
839 }
840
841 status_t err = writeInt32(utf16Len);
842 if (err) {
843 return err;
844 }
845
846 // Allocate enough bytes to hold our converted string and its terminating NULL.
847 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
848 if (!dst) {
849 return NO_MEMORY;
850 }
851
Sergio Girof4607432016-07-21 14:46:35 +0100852 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800853
854 return NO_ERROR;
855}
856
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900857
Andy Hung49198cf2020-11-18 11:02:39 -0800858status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
859status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800860
Andy Hung49198cf2020-11-18 11:02:39 -0800861status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
862status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700863
Andy Hung49198cf2020-11-18 11:02:39 -0800864status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
865status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
866status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
867status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
868status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
869status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
870status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
871status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
872status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
873status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
874status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
875status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
876status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
877status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
878status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
879status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
880status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
881status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
882status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
883status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
884status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
885status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
886status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
887status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
888status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
889status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
890status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700891
Andy Hung49198cf2020-11-18 11:02:39 -0800892status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800893status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800894 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900895status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800896 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800897status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800898 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900899status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800900 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
901status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800902
Andy Hung49198cf2020-11-18 11:02:39 -0800903status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
904status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
905status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
906
907status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
908status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
909status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
910
911status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
912
913status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
914status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
915
916status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
917status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
918
919status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
920status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
921status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
922status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
923status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
924status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
925status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
926status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
927status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
928status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
929status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
930status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
931status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
932status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
933status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
934status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
935status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
936status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
937status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
938status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
939status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
940status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
941status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
942status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
943status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
944status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
945status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
946
947status_t Parcel::readString16Vector(
948 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
949status_t Parcel::readString16Vector(
950 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
951status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
952status_t Parcel::readUtf8VectorFromUtf16Vector(
953 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
954status_t Parcel::readUtf8VectorFromUtf16Vector(
955 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
956status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
957
958status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
959status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
960status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
961
962status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
963status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
964status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
965
966status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800967
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700968status_t Parcel::writeInt32(int32_t val)
969{
Andreas Huber84a6d042009-08-17 13:33:27 -0700970 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700971}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800972
973status_t Parcel::writeUint32(uint32_t val)
974{
975 return writeAligned(val);
976}
977
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700978status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700979 if (len > INT32_MAX) {
980 // don't accept size_t values which may have come from an
981 // inadvertent conversion from a negative int.
982 return BAD_VALUE;
983 }
984
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700985 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700986 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700987 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700988 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700989 if (ret == NO_ERROR) {
990 ret = write(val, len * sizeof(*val));
991 }
992 return ret;
993}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700994status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700995 if (len > INT32_MAX) {
996 // don't accept size_t values which may have come from an
997 // inadvertent conversion from a negative int.
998 return BAD_VALUE;
999 }
1000
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001001 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001002 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001003 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001004 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001005 if (ret == NO_ERROR) {
1006 ret = write(val, len * sizeof(*val));
1007 }
1008 return ret;
1009}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001010
Casey Dahlind6848f52015-10-15 15:44:59 -07001011status_t Parcel::writeBool(bool val)
1012{
1013 return writeInt32(int32_t(val));
1014}
1015
1016status_t Parcel::writeChar(char16_t val)
1017{
1018 return writeInt32(int32_t(val));
1019}
1020
1021status_t Parcel::writeByte(int8_t val)
1022{
1023 return writeInt32(int32_t(val));
1024}
1025
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001026status_t Parcel::writeInt64(int64_t val)
1027{
Andreas Huber84a6d042009-08-17 13:33:27 -07001028 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001029}
1030
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001031status_t Parcel::writeUint64(uint64_t val)
1032{
1033 return writeAligned(val);
1034}
1035
Serban Constantinescuf683e012013-11-05 16:53:55 +00001036status_t Parcel::writePointer(uintptr_t val)
1037{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001038 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001039}
1040
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001041status_t Parcel::writeFloat(float val)
1042{
Andreas Huber84a6d042009-08-17 13:33:27 -07001043 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001044}
1045
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001046#if defined(__mips__) && defined(__mips_hard_float)
1047
1048status_t Parcel::writeDouble(double val)
1049{
1050 union {
1051 double d;
1052 unsigned long long ll;
1053 } u;
1054 u.d = val;
1055 return writeAligned(u.ll);
1056}
1057
1058#else
1059
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001060status_t Parcel::writeDouble(double val)
1061{
Andreas Huber84a6d042009-08-17 13:33:27 -07001062 return writeAligned(val);
1063}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001064
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001065#endif
1066
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001067status_t Parcel::writeCString(const char* str)
1068{
1069 return write(str, strlen(str)+1);
1070}
1071
1072status_t Parcel::writeString8(const String8& str)
1073{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001074 return writeString8(str.string(), str.size());
1075}
1076
1077status_t Parcel::writeString8(const char* str, size_t len)
1078{
1079 if (str == nullptr) return writeInt32(-1);
1080
Jeff Sharkey18220902020-11-05 08:36:20 -07001081 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001082 status_t err = writeInt32(len);
1083 if (err == NO_ERROR) {
1084 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1085 if (data) {
1086 memcpy(data, str, len);
1087 *reinterpret_cast<char*>(data+len) = 0;
1088 return NO_ERROR;
1089 }
1090 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001091 }
1092 return err;
1093}
1094
1095status_t Parcel::writeString16(const String16& str)
1096{
1097 return writeString16(str.string(), str.size());
1098}
1099
1100status_t Parcel::writeString16(const char16_t* str, size_t len)
1101{
Yi Kong91635562018-06-07 14:38:36 -07001102 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001103
Jeff Sharkey18220902020-11-05 08:36:20 -07001104 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001105 status_t err = writeInt32(len);
1106 if (err == NO_ERROR) {
1107 len *= sizeof(char16_t);
1108 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1109 if (data) {
1110 memcpy(data, str, len);
1111 *reinterpret_cast<char16_t*>(data+len) = 0;
1112 return NO_ERROR;
1113 }
1114 err = mError;
1115 }
1116 return err;
1117}
1118
1119status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1120{
Steven Morelanda86a3562019-08-01 23:28:34 +00001121 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001122}
1123
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001124
Casey Dahlinb9872622015-11-25 15:09:45 -08001125status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1126 if (!parcelable) {
1127 return writeInt32(0);
1128 }
1129
1130 return writeParcelable(*parcelable);
1131}
1132
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001133status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001134{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001135 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001136 return BAD_TYPE;
1137
1138 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001139 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001140 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001142 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001143 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001144
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001145 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1146 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147
1148 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001149 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150 return err;
1151 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001152 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001153 return err;
1154}
1155
Jeff Brown93ff1f92011-11-04 19:01:44 -07001156status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001157{
Steven Moreland5553ac42020-11-11 02:14:45 +00001158 if (isForRpc()) {
1159 ALOGE("Cannot write file descriptor to remote binder.");
1160 return BAD_TYPE;
1161 }
1162
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001163 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001164 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001165 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001166 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001167 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001168 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001169 return writeObject(obj, true);
1170}
1171
1172status_t Parcel::writeDupFileDescriptor(int fd)
1173{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001174 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001175 if (dupFd < 0) {
1176 return -errno;
1177 }
1178 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001179 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001180 close(dupFd);
1181 }
1182 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001183}
1184
Dianne Hackborn1941a402016-08-29 12:30:43 -07001185status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1186{
1187 writeInt32(0);
1188 return writeFileDescriptor(fd, takeOwnership);
1189}
1190
Ryo Hashimotobf551892018-05-31 16:58:35 +09001191status_t Parcel::writeDupParcelFileDescriptor(int fd)
1192{
1193 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1194 if (dupFd < 0) {
1195 return -errno;
1196 }
1197 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1198 if (err != OK) {
1199 close(dupFd);
1200 }
1201 return err;
1202}
1203
Christopher Wiley2cf19952016-04-11 11:09:37 -07001204status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001205 return writeDupFileDescriptor(fd.get());
1206}
1207
Jeff Brown13b16042014-11-11 16:44:25 -08001208status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001209{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001210 if (len > INT32_MAX) {
1211 // don't accept size_t values which may have come from an
1212 // inadvertent conversion from a negative int.
1213 return BAD_VALUE;
1214 }
1215
Jeff Brown13b16042014-11-11 16:44:25 -08001216 status_t status;
1217 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001218 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001219 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001220 if (status) return status;
1221
1222 void* ptr = writeInplace(len);
1223 if (!ptr) return NO_MEMORY;
1224
Jeff Brown13b16042014-11-11 16:44:25 -08001225 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001226 return NO_ERROR;
1227 }
1228
Steve Block6807e592011-10-20 11:56:00 +01001229 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001230 int fd = ashmem_create_region("Parcel Blob", len);
1231 if (fd < 0) return NO_MEMORY;
1232
1233 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1234 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001235 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001236 } else {
Yi Kong91635562018-06-07 14:38:36 -07001237 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001238 if (ptr == MAP_FAILED) {
1239 status = -errno;
1240 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001241 if (!mutableCopy) {
1242 result = ashmem_set_prot_region(fd, PROT_READ);
1243 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001244 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001245 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001246 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001247 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001248 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001249 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001250 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001251 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001252 return NO_ERROR;
1253 }
1254 }
1255 }
1256 }
1257 ::munmap(ptr, len);
1258 }
1259 ::close(fd);
1260 return status;
1261}
1262
Jeff Brown13b16042014-11-11 16:44:25 -08001263status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1264{
1265 // Must match up with what's done in writeBlob.
1266 if (!mAllowFds) return FDS_NOT_ALLOWED;
1267 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1268 if (status) return status;
1269 return writeDupFileDescriptor(fd);
1270}
1271
Mathias Agopiane1424282013-07-29 21:24:40 -07001272status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001273{
1274 status_t err;
1275
1276 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001277 const size_t len = val.getFlattenedSize();
1278 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001279
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001280 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001281 // don't accept size_t values which may have come from an
1282 // inadvertent conversion from a negative int.
1283 return BAD_VALUE;
1284 }
1285
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001286 err = this->writeInt32(len);
1287 if (err) return err;
1288
1289 err = this->writeInt32(fd_count);
1290 if (err) return err;
1291
1292 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001293 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001294 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001295 return BAD_VALUE;
1296
Yi Kong91635562018-06-07 14:38:36 -07001297 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001298 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001299 fds = new (std::nothrow) int[fd_count];
1300 if (fds == nullptr) {
1301 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1302 return BAD_VALUE;
1303 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001304 }
1305
1306 err = val.flatten(buf, len, fds, fd_count);
1307 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1308 err = this->writeDupFileDescriptor( fds[i] );
1309 }
1310
1311 if (fd_count) {
1312 delete [] fds;
1313 }
1314
1315 return err;
1316}
1317
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001318status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1319{
1320 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1321 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1322 if (enoughData && enoughObjects) {
1323restart_write:
1324 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001325
Christopher Tate98e67d32015-06-03 18:44:15 -07001326 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001327 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001328 if (!mAllowFds) {
1329 // fail before modifying our object index
1330 return FDS_NOT_ALLOWED;
1331 }
1332 mHasFds = mFdsKnown = true;
1333 }
1334
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001335 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001336 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001337 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001338 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001339 mObjectsSize++;
1340 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001341
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001342 return finishWrite(sizeof(flat_binder_object));
1343 }
1344
1345 if (!enoughData) {
1346 const status_t err = growData(sizeof(val));
1347 if (err != NO_ERROR) return err;
1348 }
1349 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001350 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1351 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001352 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001353 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001354 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001355 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001356 mObjects = objects;
1357 mObjectsCapacity = newSize;
1358 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001359
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001360 goto restart_write;
1361}
1362
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001363status_t Parcel::writeNoException()
1364{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001365 binder::Status status;
1366 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001367}
1368
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001369status_t Parcel::validateReadData(size_t upperBound) const
1370{
1371 // Don't allow non-object reads on object data
1372 if (mObjectsSorted || mObjectsSize <= 1) {
1373data_sorted:
1374 // Expect to check only against the next object
1375 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1376 // For some reason the current read position is greater than the next object
1377 // hint. Iterate until we find the right object
1378 size_t nextObject = mNextObjectHint;
1379 do {
1380 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1381 // Requested info overlaps with an object
1382 ALOGE("Attempt to read from protected data in Parcel %p", this);
1383 return PERMISSION_DENIED;
1384 }
1385 nextObject++;
1386 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1387 mNextObjectHint = nextObject;
1388 }
1389 return NO_ERROR;
1390 }
1391 // Quickly determine if mObjects is sorted.
1392 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1393 binder_size_t* prevObj = currObj;
1394 while (currObj > mObjects) {
1395 prevObj--;
1396 if(*prevObj > *currObj) {
1397 goto data_unsorted;
1398 }
1399 currObj--;
1400 }
1401 mObjectsSorted = true;
1402 goto data_sorted;
1403
1404data_unsorted:
1405 // Insertion Sort mObjects
1406 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1407 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1408 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1409 binder_size_t temp = *iter0;
1410 binder_size_t* iter1 = iter0 - 1;
1411 while (iter1 >= mObjects && *iter1 > temp) {
1412 *(iter1 + 1) = *iter1;
1413 iter1--;
1414 }
1415 *(iter1 + 1) = temp;
1416 }
1417 mNextObjectHint = 0;
1418 mObjectsSorted = true;
1419 goto data_sorted;
1420}
1421
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001422status_t Parcel::read(void* outData, size_t len) const
1423{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001424 if (len > INT32_MAX) {
1425 // don't accept size_t values which may have come from an
1426 // inadvertent conversion from a negative int.
1427 return BAD_VALUE;
1428 }
1429
1430 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1431 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001432 if (mObjectsSize > 0) {
1433 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001434 if(err != NO_ERROR) {
1435 // Still increment the data position by the expected length
1436 mDataPos += pad_size(len);
1437 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1438 return err;
1439 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001440 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001441 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001442 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001443 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001444 return NO_ERROR;
1445 }
1446 return NOT_ENOUGH_DATA;
1447}
1448
1449const void* Parcel::readInplace(size_t len) const
1450{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001451 if (len > INT32_MAX) {
1452 // don't accept size_t values which may have come from an
1453 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001454 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001455 }
1456
1457 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1458 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001459 if (mObjectsSize > 0) {
1460 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001461 if(err != NO_ERROR) {
1462 // Still increment the data position by the expected length
1463 mDataPos += pad_size(len);
1464 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001465 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001466 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001467 }
1468
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001469 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001470 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001471 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001472 return data;
1473 }
Yi Kong91635562018-06-07 14:38:36 -07001474 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001475}
1476
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001477status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1478 if (status_t status = readInt32(size); status != OK) return status;
1479 if (*size < 0) return OK; // may be null, client to handle
1480
1481 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1482
1483 // approximation, can't know max element size (e.g. if it makes heap
1484 // allocations)
1485 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1486 int32_t allocationSize;
1487 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1488
1489 // High limit of 1MB since something this big could never be returned. Could
1490 // probably scope this down, but might impact very specific usecases.
1491 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1492
1493 if (allocationSize >= kMaxAllocationSize) {
1494 return NO_MEMORY;
1495 }
1496
1497 return OK;
1498}
1499
Andreas Huber84a6d042009-08-17 13:33:27 -07001500template<class T>
1501status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001502 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001503
1504 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001505 if (mObjectsSize > 0) {
1506 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001507 if(err != NO_ERROR) {
1508 // Still increment the data position by the expected length
1509 mDataPos += sizeof(T);
1510 return err;
1511 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001512 }
1513
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001514 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001515 mDataPos += sizeof(T);
1516 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001517 return NO_ERROR;
1518 } else {
1519 return NOT_ENOUGH_DATA;
1520 }
1521}
1522
Andreas Huber84a6d042009-08-17 13:33:27 -07001523template<class T>
1524T Parcel::readAligned() const {
1525 T result;
1526 if (readAligned(&result) != NO_ERROR) {
1527 result = 0;
1528 }
1529
1530 return result;
1531}
1532
1533template<class T>
1534status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001535 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001536
1537 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1538restart_write:
1539 *reinterpret_cast<T*>(mData+mDataPos) = val;
1540 return finishWrite(sizeof(val));
1541 }
1542
1543 status_t err = growData(sizeof(val));
1544 if (err == NO_ERROR) goto restart_write;
1545 return err;
1546}
1547
1548status_t Parcel::readInt32(int32_t *pArg) const
1549{
1550 return readAligned(pArg);
1551}
1552
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001553int32_t Parcel::readInt32() const
1554{
Andreas Huber84a6d042009-08-17 13:33:27 -07001555 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001556}
1557
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001558status_t Parcel::readUint32(uint32_t *pArg) const
1559{
1560 return readAligned(pArg);
1561}
1562
1563uint32_t Parcel::readUint32() const
1564{
1565 return readAligned<uint32_t>();
1566}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001567
1568status_t Parcel::readInt64(int64_t *pArg) const
1569{
Andreas Huber84a6d042009-08-17 13:33:27 -07001570 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001571}
1572
1573
1574int64_t Parcel::readInt64() const
1575{
Andreas Huber84a6d042009-08-17 13:33:27 -07001576 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001577}
1578
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001579status_t Parcel::readUint64(uint64_t *pArg) const
1580{
1581 return readAligned(pArg);
1582}
1583
1584uint64_t Parcel::readUint64() const
1585{
1586 return readAligned<uint64_t>();
1587}
1588
Serban Constantinescuf683e012013-11-05 16:53:55 +00001589status_t Parcel::readPointer(uintptr_t *pArg) const
1590{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001591 status_t ret;
1592 binder_uintptr_t ptr;
1593 ret = readAligned(&ptr);
1594 if (!ret)
1595 *pArg = ptr;
1596 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001597}
1598
1599uintptr_t Parcel::readPointer() const
1600{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001601 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001602}
1603
1604
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001605status_t Parcel::readFloat(float *pArg) const
1606{
Andreas Huber84a6d042009-08-17 13:33:27 -07001607 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001608}
1609
1610
1611float Parcel::readFloat() const
1612{
Andreas Huber84a6d042009-08-17 13:33:27 -07001613 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001614}
1615
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001616#if defined(__mips__) && defined(__mips_hard_float)
1617
1618status_t Parcel::readDouble(double *pArg) const
1619{
1620 union {
1621 double d;
1622 unsigned long long ll;
1623 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001624 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001625 status_t status;
1626 status = readAligned(&u.ll);
1627 *pArg = u.d;
1628 return status;
1629}
1630
1631double Parcel::readDouble() const
1632{
1633 union {
1634 double d;
1635 unsigned long long ll;
1636 } u;
1637 u.ll = readAligned<unsigned long long>();
1638 return u.d;
1639}
1640
1641#else
1642
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001643status_t Parcel::readDouble(double *pArg) const
1644{
Andreas Huber84a6d042009-08-17 13:33:27 -07001645 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001646}
1647
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001648double Parcel::readDouble() const
1649{
Andreas Huber84a6d042009-08-17 13:33:27 -07001650 return readAligned<double>();
1651}
1652
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001653#endif
1654
Casey Dahlind6848f52015-10-15 15:44:59 -07001655status_t Parcel::readBool(bool *pArg) const
1656{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001657 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001658 status_t ret = readInt32(&tmp);
1659 *pArg = (tmp != 0);
1660 return ret;
1661}
1662
1663bool Parcel::readBool() const
1664{
1665 return readInt32() != 0;
1666}
1667
1668status_t Parcel::readChar(char16_t *pArg) const
1669{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001670 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001671 status_t ret = readInt32(&tmp);
1672 *pArg = char16_t(tmp);
1673 return ret;
1674}
1675
1676char16_t Parcel::readChar() const
1677{
1678 return char16_t(readInt32());
1679}
1680
1681status_t Parcel::readByte(int8_t *pArg) const
1682{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001683 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001684 status_t ret = readInt32(&tmp);
1685 *pArg = int8_t(tmp);
1686 return ret;
1687}
1688
1689int8_t Parcel::readByte() const
1690{
1691 return int8_t(readInt32());
1692}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001693
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001694status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1695 size_t utf16Size = 0;
1696 const char16_t* src = readString16Inplace(&utf16Size);
1697 if (!src) {
1698 return UNEXPECTED_NULL;
1699 }
1700
1701 // Save ourselves the trouble, we're done.
1702 if (utf16Size == 0u) {
1703 str->clear();
1704 return NO_ERROR;
1705 }
1706
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001707 // Allow for closing '\0'
1708 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1709 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001710 return BAD_VALUE;
1711 }
1712 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001713 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001714 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001715 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1716 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001717 return NO_ERROR;
1718}
1719
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001720const char* Parcel::readCString() const
1721{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001722 if (mDataPos < mDataSize) {
1723 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001724 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1725 // is the string's trailing NUL within the parcel's valid bounds?
1726 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1727 if (eos) {
1728 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001729 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001730 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001731 return str;
1732 }
1733 }
Yi Kong91635562018-06-07 14:38:36 -07001734 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001735}
1736
1737String8 Parcel::readString8() const
1738{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001739 size_t len;
1740 const char* str = readString8Inplace(&len);
1741 if (str) return String8(str, len);
1742 ALOGE("Reading a NULL string not supported here.");
1743 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07001744}
1745
1746status_t Parcel::readString8(String8* pArg) const
1747{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001748 size_t len;
1749 const char* str = readString8Inplace(&len);
1750 if (str) {
1751 pArg->setTo(str, len);
1752 return 0;
1753 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07001754 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001755 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07001756 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001757}
1758
1759const char* Parcel::readString8Inplace(size_t* outLen) const
1760{
1761 int32_t size = readInt32();
1762 // watch for potential int overflow from size+1
1763 if (size >= 0 && size < INT32_MAX) {
1764 *outLen = size;
1765 const char* str = (const char*)readInplace(size+1);
1766 if (str != nullptr) {
Steven Moreland61d0f842020-12-04 21:13:03 +00001767 if (str[size] == '\0') {
1768 return str;
1769 }
1770 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001771 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001772 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001773 *outLen = 0;
1774 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001775}
1776
1777String16 Parcel::readString16() const
1778{
1779 size_t len;
1780 const char16_t* str = readString16Inplace(&len);
1781 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001782 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001783 return String16();
1784}
1785
Casey Dahlinb9872622015-11-25 15:09:45 -08001786
Casey Dahlin451ff582015-10-19 18:12:18 -07001787status_t Parcel::readString16(String16* pArg) const
1788{
1789 size_t len;
1790 const char16_t* str = readString16Inplace(&len);
1791 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001792 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001793 return 0;
1794 } else {
1795 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001796 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001797 }
1798}
1799
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001800const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1801{
1802 int32_t size = readInt32();
1803 // watch for potential int overflow from size+1
1804 if (size >= 0 && size < INT32_MAX) {
1805 *outLen = size;
1806 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001807 if (str != nullptr) {
Steven Moreland61d0f842020-12-04 21:13:03 +00001808 if (str[size] == u'\0') {
1809 return str;
1810 }
1811 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001812 }
1813 }
1814 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001815 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001816}
1817
Casey Dahlinf0c13772015-10-27 18:33:56 -07001818status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1819{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001820 status_t status = readNullableStrongBinder(val);
1821 if (status == OK && !val->get()) {
1822 status = UNEXPECTED_NULL;
1823 }
1824 return status;
1825}
1826
1827status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1828{
Steven Morelanda86a3562019-08-01 23:28:34 +00001829 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001830}
1831
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001832sp<IBinder> Parcel::readStrongBinder() const
1833{
1834 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001835 // Note that a lot of code in Android reads binders by hand with this
1836 // method, and that code has historically been ok with getting nullptr
1837 // back (while ignoring error codes).
1838 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001839 return val;
1840}
1841
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001842int32_t Parcel::readExceptionCode() const
1843{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001844 binder::Status status;
1845 status.readFromParcel(*this);
1846 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001847}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001848
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001849native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001850{
1851 int numFds, numInts;
1852 status_t err;
1853 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001854 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001855 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001856 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001857
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001858 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001859 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001860 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001861 }
1862
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001863 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001864 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001865 if (h->data[i] < 0) {
1866 for (int j = 0; j < i; j++) {
1867 close(h->data[j]);
1868 }
1869 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001870 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001871 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001872 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001873 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001874 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001875 native_handle_close(h);
1876 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001877 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001878 }
1879 return h;
1880}
1881
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001882int Parcel::readFileDescriptor() const
1883{
1884 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001885
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001886 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001887 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001888 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001889
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001890 return BAD_TYPE;
1891}
1892
Dianne Hackborn1941a402016-08-29 12:30:43 -07001893int Parcel::readParcelFileDescriptor() const
1894{
1895 int32_t hasComm = readInt32();
1896 int fd = readFileDescriptor();
1897 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001898 // detach (owned by the binder driver)
1899 int comm = readFileDescriptor();
1900
1901 // warning: this must be kept in sync with:
1902 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1903 enum ParcelFileDescriptorStatus {
1904 DETACHED = 2,
1905 };
1906
1907#if BYTE_ORDER == BIG_ENDIAN
1908 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
1909#endif
1910#if BYTE_ORDER == LITTLE_ENDIAN
1911 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
1912#endif
1913
1914 ssize_t written = TEMP_FAILURE_RETRY(
1915 ::write(comm, &message, sizeof(message)));
1916
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08001917 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001918 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
1919 written, strerror(errno));
1920 return BAD_TYPE;
1921 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07001922 }
1923 return fd;
1924}
1925
Christopher Wiley2cf19952016-04-11 11:09:37 -07001926status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08001927{
1928 int got = readFileDescriptor();
1929
1930 if (got == BAD_TYPE) {
1931 return BAD_TYPE;
1932 }
1933
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001934 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08001935
1936 if (val->get() < 0) {
1937 return BAD_VALUE;
1938 }
1939
1940 return OK;
1941}
1942
Ryo Hashimotobf551892018-05-31 16:58:35 +09001943status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
1944{
1945 int got = readParcelFileDescriptor();
1946
1947 if (got == BAD_TYPE) {
1948 return BAD_TYPE;
1949 }
1950
1951 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
1952
1953 if (val->get() < 0) {
1954 return BAD_VALUE;
1955 }
1956
1957 return OK;
1958}
Casey Dahlin06673e32015-11-23 13:24:23 -08001959
Jeff Brown5707dbf2011-09-23 21:17:56 -07001960status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1961{
Jeff Brown13b16042014-11-11 16:44:25 -08001962 int32_t blobType;
1963 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001964 if (status) return status;
1965
Jeff Brown13b16042014-11-11 16:44:25 -08001966 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001967 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001968 const void* ptr = readInplace(len);
1969 if (!ptr) return BAD_VALUE;
1970
Jeff Brown13b16042014-11-11 16:44:25 -08001971 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001972 return NO_ERROR;
1973 }
1974
Steve Block6807e592011-10-20 11:56:00 +01001975 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001976 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001977 int fd = readFileDescriptor();
1978 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1979
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001980 if (!ashmem_valid(fd)) {
1981 ALOGE("invalid fd");
1982 return BAD_VALUE;
1983 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001984 int size = ashmem_get_size_region(fd);
1985 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001986 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001987 return BAD_VALUE;
1988 }
Yi Kong91635562018-06-07 14:38:36 -07001989 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08001990 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001991 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001992
Jeff Brown13b16042014-11-11 16:44:25 -08001993 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001994 return NO_ERROR;
1995}
1996
Mathias Agopiane1424282013-07-29 21:24:40 -07001997status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001998{
1999 // size
2000 const size_t len = this->readInt32();
2001 const size_t fd_count = this->readInt32();
2002
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002003 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002004 // don't accept size_t values which may have come from an
2005 // inadvertent conversion from a negative int.
2006 return BAD_VALUE;
2007 }
2008
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002009 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002010 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002011 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002012 return BAD_VALUE;
2013
Yi Kong91635562018-06-07 14:38:36 -07002014 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002015 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002016 fds = new (std::nothrow) int[fd_count];
2017 if (fds == nullptr) {
2018 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2019 return BAD_VALUE;
2020 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002021 }
2022
2023 status_t err = NO_ERROR;
2024 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002025 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002026 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002027 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002028 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 -07002029 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2030 // Close all the file descriptors that were dup-ed.
2031 for (size_t j=0; j<i ;j++) {
2032 close(fds[j]);
2033 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002034 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002035 }
2036
2037 if (err == NO_ERROR) {
2038 err = val.unflatten(buf, len, fds, fd_count);
2039 }
2040
2041 if (fd_count) {
2042 delete [] fds;
2043 }
2044
2045 return err;
2046}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002047const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2048{
2049 const size_t DPOS = mDataPos;
2050 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2051 const flat_binder_object* obj
2052 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2053 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002054 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002055 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002056 // the object list, so we don't want to check for it when
2057 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002058 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002059 return obj;
2060 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002061
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002062 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002063 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002064 const size_t N = mObjectsSize;
2065 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002066
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002067 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002068 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002069 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002070
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002071 // Start at the current hint position, looking for an object at
2072 // the current data position.
2073 if (opos < N) {
2074 while (opos < (N-1) && OBJS[opos] < DPOS) {
2075 opos++;
2076 }
2077 } else {
2078 opos = N-1;
2079 }
2080 if (OBJS[opos] == DPOS) {
2081 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002082 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002083 this, DPOS, opos);
2084 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002085 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002086 return obj;
2087 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002088
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002089 // Look backwards for it...
2090 while (opos > 0 && OBJS[opos] > DPOS) {
2091 opos--;
2092 }
2093 if (OBJS[opos] == DPOS) {
2094 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002095 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002096 this, DPOS, opos);
2097 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002098 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002099 return obj;
2100 }
2101 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002102 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 -07002103 this, DPOS);
2104 }
Yi Kong91635562018-06-07 14:38:36 -07002105 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002106}
2107
2108void Parcel::closeFileDescriptors()
2109{
2110 size_t i = mObjectsSize;
2111 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002112 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002113 }
2114 while (i > 0) {
2115 i--;
2116 const flat_binder_object* flat
2117 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002118 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002119 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002120 close(flat->handle);
2121 }
2122 }
2123}
2124
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002125uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002126{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002127 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002128}
2129
2130size_t Parcel::ipcDataSize() const
2131{
2132 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2133}
2134
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002135uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002136{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002137 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002138}
2139
2140size_t Parcel::ipcObjectsCount() const
2141{
2142 return mObjectsSize;
2143}
2144
2145void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Steven Moreland161fe122020-11-12 23:16:47 +00002146 const binder_size_t* objects, size_t objectsCount, release_func relFunc)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002147{
Steven Moreland438cce82021-04-02 18:04:08 +00002148 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2149 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2150
Steven Morelandceed9bb2020-12-17 01:01:06 +00002151 freeData();
2152
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002153 mData = const_cast<uint8_t*>(data);
2154 mDataSize = mDataCapacity = dataSize;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002155 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002156 mObjectsSize = mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002157 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002158
2159 binder_size_t minOffset = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002160 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002161 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002162 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002163 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002164 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002165 mObjectsSize = 0;
2166 break;
2167 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002168 const flat_binder_object* flat
2169 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2170 uint32_t type = flat->hdr.type;
2171 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2172 type == BINDER_TYPE_FD)) {
2173 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2174 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2175 // recover gracefully by clearing out the objects, and releasing the objects we do
2176 // know about.
2177 android_errorWriteLog(0x534e4554, "135930648");
2178 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2179 __func__, type, (uint64_t)offset);
2180 releaseObjects();
2181 mObjectsSize = 0;
2182 break;
2183 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002184 minOffset = offset + sizeof(flat_binder_object);
2185 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002186 scanForFds();
2187}
2188
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002189void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002190{
2191 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002192
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002193 if (errorCheck() != NO_ERROR) {
2194 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002195 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002196 } else if (dataSize() > 0) {
2197 const uint8_t* DATA = data();
2198 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002199 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002200 const size_t N = objectsCount();
2201 for (size_t i=0; i<N; i++) {
2202 const flat_binder_object* flat
2203 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2204 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002205 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002206 << " = " << flat->binder;
2207 }
2208 } else {
2209 to << "NULL";
2210 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002211
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002212 to << ")";
2213}
2214
2215void Parcel::releaseObjects()
2216{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002217 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002218 if (i == 0) {
2219 return;
2220 }
2221 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002222 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002223 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002224 while (i > 0) {
2225 i--;
2226 const flat_binder_object* flat
2227 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002228 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002229 }
2230}
2231
2232void Parcel::acquireObjects()
2233{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002234 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002235 if (i == 0) {
2236 return;
2237 }
2238 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002239 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002240 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002241 while (i > 0) {
2242 i--;
2243 const flat_binder_object* flat
2244 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002245 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002246 }
2247}
2248
2249void Parcel::freeData()
2250{
2251 freeDataNoInit();
2252 initState();
2253}
2254
2255void Parcel::freeDataNoInit()
2256{
2257 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002258 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002259 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002260 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002261 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002262 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002263 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002264 if (mData) {
2265 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002266 gParcelGlobalAllocSize -= mDataCapacity;
2267 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002268 if (mDeallocZero) {
2269 zeroMemory(mData, mDataSize);
2270 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002271 free(mData);
2272 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002273 if (mObjects) free(mObjects);
2274 }
2275}
2276
2277status_t Parcel::growData(size_t len)
2278{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002279 if (len > INT32_MAX) {
2280 // don't accept size_t values which may have come from an
2281 // inadvertent conversion from a negative int.
2282 return BAD_VALUE;
2283 }
2284
Martijn Coenen93fe5182020-01-22 10:46:25 +01002285 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2286 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002287 size_t newSize = ((mDataSize+len)*3)/2;
2288 return (newSize <= mDataSize)
2289 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002290 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002291}
2292
Steven Morelandf183fdd2020-10-27 00:12:12 +00002293static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2294 if (!zero) {
2295 return (uint8_t*)realloc(data, newCapacity);
2296 }
2297 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2298 if (!newData) {
2299 return nullptr;
2300 }
2301
2302 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2303 zeroMemory(data, oldCapacity);
2304 free(data);
2305 return newData;
2306}
2307
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002308status_t Parcel::restartWrite(size_t desired)
2309{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002310 if (desired > INT32_MAX) {
2311 // don't accept size_t values which may have come from an
2312 // inadvertent conversion from a negative int.
2313 return BAD_VALUE;
2314 }
2315
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002316 if (mOwner) {
2317 freeData();
2318 return continueWrite(desired);
2319 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002320
Steven Morelandf183fdd2020-10-27 00:12:12 +00002321 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002322 if (!data && desired > mDataCapacity) {
2323 mError = NO_MEMORY;
2324 return NO_MEMORY;
2325 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002326
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002327 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002328
Devin Moore4a0a55e2020-06-04 13:23:10 -07002329 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002330 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002331 if (mDataCapacity > desired) {
2332 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2333 } else {
2334 gParcelGlobalAllocSize += (desired - mDataCapacity);
2335 }
2336
Colin Cross83ec65e2015-12-08 17:15:50 -08002337 if (!mData) {
2338 gParcelGlobalAllocCount++;
2339 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002340 mData = data;
2341 mDataCapacity = desired;
2342 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002343
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002344 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002345 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2346 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2347
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002348 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002349 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002350 mObjectsSize = mObjectsCapacity = 0;
2351 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002352 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002353 mHasFds = false;
2354 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002355 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002356
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002357 return NO_ERROR;
2358}
2359
2360status_t Parcel::continueWrite(size_t desired)
2361{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002362 if (desired > INT32_MAX) {
2363 // don't accept size_t values which may have come from an
2364 // inadvertent conversion from a negative int.
2365 return BAD_VALUE;
2366 }
2367
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002368 // If shrinking, first adjust for any objects that appear
2369 // after the new data size.
2370 size_t objectsSize = mObjectsSize;
2371 if (desired < mDataSize) {
2372 if (desired == 0) {
2373 objectsSize = 0;
2374 } else {
2375 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002376 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002377 break;
2378 objectsSize--;
2379 }
2380 }
2381 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002382
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002383 if (mOwner) {
2384 // If the size is going to zero, just release the owner's data.
2385 if (desired == 0) {
2386 freeData();
2387 return NO_ERROR;
2388 }
2389
2390 // If there is a different owner, we need to take
2391 // posession.
2392 uint8_t* data = (uint8_t*)malloc(desired);
2393 if (!data) {
2394 mError = NO_MEMORY;
2395 return NO_MEMORY;
2396 }
Yi Kong91635562018-06-07 14:38:36 -07002397 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002398
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002399 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002400 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002401 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002402 free(data);
2403
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002404 mError = NO_MEMORY;
2405 return NO_MEMORY;
2406 }
2407
2408 // Little hack to only acquire references on objects
2409 // we will be keeping.
2410 size_t oldObjectsSize = mObjectsSize;
2411 mObjectsSize = objectsSize;
2412 acquireObjects();
2413 mObjectsSize = oldObjectsSize;
2414 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002415
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002416 if (mData) {
2417 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2418 }
2419 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002420 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002422 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002423 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
Yi Kong91635562018-06-07 14:38:36 -07002424 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002425
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002426 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002427 gParcelGlobalAllocSize += desired;
2428 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002429
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002430 mData = data;
2431 mObjects = objects;
2432 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002433 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002434 mDataCapacity = desired;
2435 mObjectsSize = mObjectsCapacity = 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 } else if (mData) {
2440 if (objectsSize < mObjectsSize) {
2441 // Need to release refs on any objects we are dropping.
2442 const sp<ProcessState> proc(ProcessState::self());
2443 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2444 const flat_binder_object* flat
2445 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002446 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002447 // will need to rescan because we may have lopped off the only FDs
2448 mFdsKnown = false;
2449 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002450 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002451 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002452
2453 if (objectsSize == 0) {
2454 free(mObjects);
2455 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002456 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002457 } else {
2458 binder_size_t* objects =
2459 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2460 if (objects) {
2461 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002462 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002463 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002464 }
2465 mObjectsSize = objectsSize;
2466 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002467 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002468 }
2469
2470 // We own the data, so we can just do a realloc().
2471 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002472 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002473 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002474 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2475 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002476 gParcelGlobalAllocSize += desired;
2477 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002478 mData = data;
2479 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002480 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002481 mError = NO_MEMORY;
2482 return NO_MEMORY;
2483 }
2484 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002485 if (mDataSize > desired) {
2486 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002487 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002488 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002489 if (mDataPos > desired) {
2490 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002491 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002492 }
2493 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002494
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002495 } else {
2496 // This is the first data. Easy!
2497 uint8_t* data = (uint8_t*)malloc(desired);
2498 if (!data) {
2499 mError = NO_MEMORY;
2500 return NO_MEMORY;
2501 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002502
Yi Kong91635562018-06-07 14:38:36 -07002503 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002504 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002505 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002506 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002507
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002508 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002509 gParcelGlobalAllocSize += desired;
2510 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002511
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002512 mData = data;
2513 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002514 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2515 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516 mDataCapacity = desired;
2517 }
2518
2519 return NO_ERROR;
2520}
2521
2522void Parcel::initState()
2523{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002524 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002525 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002526 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002527 mDataSize = 0;
2528 mDataCapacity = 0;
2529 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002530 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2531 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Steven Morelandbdb53ab2021-05-05 17:57:41 +00002532 mSession = nullptr;
Yi Kong91635562018-06-07 14:38:36 -07002533 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002534 mObjectsSize = 0;
2535 mObjectsCapacity = 0;
2536 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002537 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002538 mHasFds = false;
2539 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002540 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002541 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07002542 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002543 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002544 mWorkSourceRequestHeaderPosition = 0;
2545 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002546
2547 // racing multiple init leads only to multiple identical write
2548 if (gMaxFds == 0) {
2549 struct rlimit result;
2550 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2551 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002552 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002553 } else {
2554 ALOGW("Unable to getrlimit: %s", strerror(errno));
2555 gMaxFds = 1024;
2556 }
2557 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002558}
2559
2560void Parcel::scanForFds() const
2561{
2562 bool hasFds = false;
2563 for (size_t i=0; i<mObjectsSize; i++) {
2564 const flat_binder_object* flat
2565 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002566 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002567 hasFds = true;
2568 break;
2569 }
2570 }
2571 mHasFds = hasFds;
2572 mFdsKnown = true;
2573}
2574
Dan Sandleraa5c2342015-04-10 10:08:45 -04002575size_t Parcel::getBlobAshmemSize() const
2576{
Adrian Roos6bb31142015-10-22 16:46:12 -07002577 // This used to return the size of all blobs that were written to ashmem, now we're returning
2578 // the ashmem currently referenced by this Parcel, which should be equivalent.
2579 // TODO: Remove method once ABI can be changed.
2580 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002581}
2582
Adrian Rooscbf37262015-10-22 16:12:53 -07002583size_t Parcel::getOpenAshmemSize() const
2584{
2585 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002586}
2587
2588// --- Parcel::Blob ---
2589
2590Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002591 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002592}
2593
2594Parcel::Blob::~Blob() {
2595 release();
2596}
2597
2598void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002599 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002600 ::munmap(mData, mSize);
2601 }
2602 clear();
2603}
2604
Jeff Brown13b16042014-11-11 16:44:25 -08002605void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2606 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002607 mData = data;
2608 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002609 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002610}
2611
2612void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002613 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002614 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002615 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002616 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002617}
2618
Steven Moreland61ff8492019-09-26 16:05:45 -07002619} // namespace android