blob: 10188fee6e63df826dc023f8b82fa278a73e5c99 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Steven Morelandbf1915b2020-07-16 22:43:02 +000023#include <linux/sched.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080024#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080029#include <sys/stat.h>
30#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070031#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080032#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070033
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/Binder.h>
35#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080036#include <binder/IPCThreadState.h>
37#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070038#include <binder/ProcessState.h>
Steven Moreland6e5a7752019-08-05 20:30:14 -070039#include <binder/Stability.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080040#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041#include <binder/TextOutput.h>
42
Mark Salyzynabed7f72016-01-27 08:02:48 -080043#include <cutils/ashmem.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000044#include <cutils/compiler.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080045#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#include <utils/Log.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String16.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000048#include <utils/String8.h>
49#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070050
Steven Moreland5553ac42020-11-11 02:14:45 +000051#include "RpcState.h"
Steven Morelanda4853cd2019-07-12 15:44:37 -070052#include "Static.h"
Steven Morelandf183fdd2020-10-27 00:12:12 +000053#include "Utils.h"
Steven Moreland6ba5a252021-05-04 22:49:00 +000054#include "binder_module.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070055
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070056#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080057//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080058#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080059//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070060
61// ---------------------------------------------------------------------------
62
Nick Kralevichb6b14232015-04-02 09:36:02 -070063// This macro should never be used at runtime, as a too large value
64// of s could cause an integer overflow. Instead, you should always
65// use the wrapper function pad_size()
66#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
67
68static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070069 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070070 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070071 }
72 return PAD_SIZE_UNSAFE(s);
73}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070074
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070075// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060076#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070077
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070078namespace android {
79
Steven Moreland7b102262019-08-01 15:48:43 -070080// many things compile this into prebuilts on the stack
Steven Moreland90c1f9a2021-05-03 18:27:24 +000081#ifdef __LP64__
82static_assert(sizeof(Parcel) == 120);
83#else
84static_assert(sizeof(Parcel) == 60);
85#endif
Steven Moreland7b102262019-08-01 15:48:43 -070086
Jeff Sharkey8994c182020-09-11 12:07:10 -060087static std::atomic<size_t> gParcelGlobalAllocCount;
88static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -080089
Christopher Tatee4e0ae82016-03-24 16:03:44 -070090static size_t gMaxFds = 0;
91
Jeff Brown13b16042014-11-11 16:44:25 -080092// Maximum size of a blob to transfer in-place.
93static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
94
95enum {
96 BLOB_INPLACE = 0,
97 BLOB_ASHMEM_IMMUTABLE = 1,
98 BLOB_ASHMEM_MUTABLE = 2,
99};
100
Steven Morelandb1c81202019-04-05 18:49:55 -0700101static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700102 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700103{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700104 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700105 case BINDER_TYPE_BINDER:
106 if (obj.binder) {
107 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800108 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 }
110 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700111 case BINDER_TYPE_HANDLE: {
112 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700113 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700114 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
115 b->incStrong(who);
116 }
117 return;
118 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700119 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000120 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700121 // If we own an ashmem fd, keep track of how much memory it refers to.
122 int size = ashmem_get_size_region(obj.handle);
123 if (size > 0) {
124 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700125 }
126 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700127 return;
128 }
129 }
130
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700131 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700132}
133
Adrian Roos6bb31142015-10-22 16:46:12 -0700134static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700135 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700137 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700138 case BINDER_TYPE_BINDER:
139 if (obj.binder) {
140 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800141 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142 }
143 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144 case BINDER_TYPE_HANDLE: {
145 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700146 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
148 b->decStrong(who);
149 }
150 return;
151 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700152 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800153 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000154 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700155 int size = ashmem_get_size_region(obj.handle);
156 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800157 // ashmem size might have changed since last time it was accounted for, e.g.
158 // in acquire_object(). Value of *outAshmemSize is not critical since we are
159 // releasing the object anyway. Check for integer overflow condition.
160 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700161 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700162 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800163
164 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700165 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700166 return;
167 }
168 }
169
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700170 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700171}
172
Steven Moreland34b48cb2020-12-01 22:45:38 +0000173status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700174{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700175 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000176 int16_t rep = internal::Stability::getCategory(binder.get()).repr();
177 return writeInt32(rep);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700178}
179
Steven Morelanda86a3562019-08-01 23:28:34 +0000180status_t Parcel::finishUnflattenBinder(
181 const sp<IBinder>& binder, sp<IBinder>* out) const
182{
183 int32_t stability;
184 status_t status = readInt32(&stability);
185 if (status != OK) return status;
186
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000187 status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability),
188 true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000189 if (status != OK) return status;
190
191 *out = binder;
192 return OK;
193}
194
Steven Morelandbf1915b2020-07-16 22:43:02 +0000195static constexpr inline int schedPolicyMask(int policy, int priority) {
196 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
197}
198
Steven Morelanda86a3562019-08-01 23:28:34 +0000199status_t Parcel::flattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700200{
Steven Moreland5553ac42020-11-11 02:14:45 +0000201 if (isForRpc()) {
202 if (binder) {
203 status_t status = writeInt32(1); // non-null
204 if (status != OK) return status;
205 RpcAddress address = RpcAddress::zero();
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000206 status = mSession->state()->onBinderLeaving(mSession, binder, &address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000207 if (status != OK) return status;
208 status = address.writeToParcel(this);
209 if (status != OK) return status;
210 } else {
211 status_t status = writeInt32(0); // null
212 if (status != OK) return status;
213 }
214 return finishFlattenBinder(binder);
215 }
216
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700217 flat_binder_object obj;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000218 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700219
Steven Morelandbf1915b2020-07-16 22:43:02 +0000220 int schedBits = 0;
221 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
222 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700223 }
224
Yi Kong91635562018-06-07 14:38:36 -0700225 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800226 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 if (!local) {
228 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700229 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000230 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000231 } else {
232 if (proxy->isRpcBinder()) {
233 ALOGE("Sending a socket binder over RPC is prohibited");
234 return INVALID_OPERATION;
235 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700236 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000237 const int32_t handle = proxy ? proxy->getPrivateAccessorForId().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700238 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800239 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700240 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800241 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700242 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000243 int policy = local->getMinSchedulerPolicy();
244 int priority = local->getMinSchedulerPriority();
245
246 if (policy != 0 || priority != 0) {
247 // override value, since it is set explicitly
248 schedBits = schedPolicyMask(policy, priority);
249 }
Steven Morelandf0212002018-12-26 13:59:23 -0800250 if (local->isRequestingSid()) {
251 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
252 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000253 if (local->isInheritRt()) {
254 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
255 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700256 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800257 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
258 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700259 }
260 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700261 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800262 obj.binder = 0;
263 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700264 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700265
Steven Morelandbf1915b2020-07-16 22:43:02 +0000266 obj.flags |= schedBits;
267
Steven Moreland34b48cb2020-12-01 22:45:38 +0000268 status_t status = writeObject(obj, false);
269 if (status != OK) return status;
270
271 return finishFlattenBinder(binder);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700272}
273
Steven Morelanda86a3562019-08-01 23:28:34 +0000274status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700275{
Steven Moreland5553ac42020-11-11 02:14:45 +0000276 if (isForRpc()) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000277 LOG_ALWAYS_FATAL_IF(mSession == nullptr, "RpcSession required to read from remote parcel");
Steven Moreland5553ac42020-11-11 02:14:45 +0000278
279 int32_t isNull;
280 status_t status = readInt32(&isNull);
281 if (status != OK) return status;
282
283 sp<IBinder> binder;
284
285 if (isNull & 1) {
286 auto addr = RpcAddress::zero();
Steven Moreland7227c8a2021-06-02 00:24:32 +0000287 if (status_t status = addr.readFromParcel(*this); status != OK) return status;
288 if (status_t status = mSession->state()->onBinderEntering(mSession, addr, &binder);
289 status != OK)
290 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000291 }
292
293 return finishUnflattenBinder(binder, out);
294 }
295
Steven Morelanda86a3562019-08-01 23:28:34 +0000296 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700297
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700298 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700299 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000300 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000301 sp<IBinder> binder =
302 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000303 return finishUnflattenBinder(binder, out);
304 }
305 case BINDER_TYPE_HANDLE: {
306 sp<IBinder> binder =
307 ProcessState::self()->getStrongProxyForHandle(flat->handle);
308 return finishUnflattenBinder(binder, out);
309 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700310 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700311 }
312 return BAD_TYPE;
313}
314
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700315// ---------------------------------------------------------------------------
316
317Parcel::Parcel()
318{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800319 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700320 initState();
321}
322
323Parcel::~Parcel()
324{
325 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800326 LOG_ALLOC("Parcel %p: destroyed", this);
327}
328
329size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600330 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800331}
332
333size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600334 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700335}
336
337const uint8_t* Parcel::data() const
338{
339 return mData;
340}
341
342size_t Parcel::dataSize() const
343{
344 return (mDataSize > mDataPos ? mDataSize : mDataPos);
345}
346
347size_t Parcel::dataAvail() const
348{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700349 size_t result = dataSize() - dataPosition();
350 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700351 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700352 }
353 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700354}
355
356size_t Parcel::dataPosition() const
357{
358 return mDataPos;
359}
360
361size_t Parcel::dataCapacity() const
362{
363 return mDataCapacity;
364}
365
366status_t Parcel::setDataSize(size_t size)
367{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700368 if (size > INT32_MAX) {
369 // don't accept size_t values which may have come from an
370 // inadvertent conversion from a negative int.
371 return BAD_VALUE;
372 }
373
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700374 status_t err;
375 err = continueWrite(size);
376 if (err == NO_ERROR) {
377 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700378 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700379 }
380 return err;
381}
382
383void Parcel::setDataPosition(size_t pos) const
384{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700385 if (pos > INT32_MAX) {
386 // don't accept size_t values which may have come from an
387 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700388 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700389 }
390
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700391 mDataPos = pos;
392 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800393 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700394}
395
396status_t Parcel::setDataCapacity(size_t size)
397{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700398 if (size > INT32_MAX) {
399 // don't accept size_t values which may have come from an
400 // inadvertent conversion from a negative int.
401 return BAD_VALUE;
402 }
403
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700404 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700405 return NO_ERROR;
406}
407
408status_t Parcel::setData(const uint8_t* buffer, size_t len)
409{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700410 if (len > INT32_MAX) {
411 // don't accept size_t values which may have come from an
412 // inadvertent conversion from a negative int.
413 return BAD_VALUE;
414 }
415
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700416 status_t err = restartWrite(len);
417 if (err == NO_ERROR) {
418 memcpy(const_cast<uint8_t*>(data()), buffer, len);
419 mDataSize = len;
420 mFdsKnown = false;
421 }
422 return err;
423}
424
Andreas Huber51faf462011-04-13 10:21:56 -0700425status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700426{
Steven Moreland67753c32021-04-02 18:45:19 +0000427 if (parcel->isForRpc() != isForRpc()) {
428 ALOGE("Cannot append Parcel of one format to another.");
429 return BAD_TYPE;
430 }
431
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700432 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700433 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800434 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700435 size_t size = parcel->mObjectsSize;
436 int startPos = mDataPos;
437 int firstIndex = -1, lastIndex = -2;
438
439 if (len == 0) {
440 return NO_ERROR;
441 }
442
Nick Kralevichb6b14232015-04-02 09:36:02 -0700443 if (len > INT32_MAX) {
444 // don't accept size_t values which may have come from an
445 // inadvertent conversion from a negative int.
446 return BAD_VALUE;
447 }
448
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700449 // range checks against the source parcel size
450 if ((offset > parcel->mDataSize)
451 || (len > parcel->mDataSize)
452 || (offset + len > parcel->mDataSize)) {
453 return BAD_VALUE;
454 }
455
456 // Count objects in range
457 for (int i = 0; i < (int) size; i++) {
458 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700459 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700460 if (firstIndex == -1) {
461 firstIndex = i;
462 }
463 lastIndex = i;
464 }
465 }
466 int numObjects = lastIndex - firstIndex + 1;
467
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700468 if ((mDataSize+len) > mDataCapacity) {
469 // grow data
470 err = growData(len);
471 if (err != NO_ERROR) {
472 return err;
473 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700474 }
475
476 // append data
477 memcpy(mData + mDataPos, data + offset, len);
478 mDataPos += len;
479 mDataSize += len;
480
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400481 err = NO_ERROR;
482
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700483 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200484 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700485 // grow objects
486 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +0100487 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
488 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700489 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +0100490 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800491 binder_size_t *objects =
492 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700493 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700494 return NO_MEMORY;
495 }
496 mObjects = objects;
497 mObjectsCapacity = newSize;
498 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700499
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700500 // append and acquire objects
501 int idx = mObjectsSize;
502 for (int i = firstIndex; i <= lastIndex; i++) {
503 size_t off = objects[i] - offset + startPos;
504 mObjects[idx++] = off;
505 mObjectsSize++;
506
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700507 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700508 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700509 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700510
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700511 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700512 // If this is a file descriptor, we need to dup it so the
513 // new Parcel now owns its own fd, and can declare that we
514 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800515 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800516 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700517 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400518 if (!mAllowFds) {
519 err = FDS_NOT_ALLOWED;
520 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521 }
522 }
523 }
524
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400525 return err;
526}
527
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700528int Parcel::compareData(const Parcel& other) {
529 size_t size = dataSize();
530 if (size != other.dataSize()) {
531 return size < other.dataSize() ? -1 : 1;
532 }
533 return memcmp(data(), other.data(), size);
534}
535
Jeff Brown13b16042014-11-11 16:44:25 -0800536bool Parcel::allowFds() const
537{
538 return mAllowFds;
539}
540
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700541bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400542{
543 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700544 if (!allowFds) {
545 mAllowFds = false;
546 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400547 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700548}
549
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700550void Parcel::restoreAllowFds(bool lastValue)
551{
552 mAllowFds = lastValue;
553}
554
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700555bool Parcel::hasFileDescriptors() const
556{
557 if (!mFdsKnown) {
558 scanForFds();
559 }
560 return mHasFds;
561}
562
Steven Morelandf183fdd2020-10-27 00:12:12 +0000563void Parcel::markSensitive() const
564{
565 mDeallocZero = true;
566}
567
Steven Moreland5553ac42020-11-11 02:14:45 +0000568void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000569 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
570
Steven Moreland5553ac42020-11-11 02:14:45 +0000571 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000572 markForRpc(binder->remoteBinder()->getPrivateAccessorForId().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000573 }
574}
575
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000576void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000577 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
578 "format must be set before data is written OR on IPC data");
579
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000580 LOG_ALWAYS_FATAL_IF(session == nullptr, "markForRpc requires session");
581 mSession = session;
Steven Moreland5553ac42020-11-11 02:14:45 +0000582}
583
584bool Parcel::isForRpc() const {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000585 return mSession != nullptr;
Steven Moreland5553ac42020-11-11 02:14:45 +0000586}
587
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000588void Parcel::updateWorkSourceRequestHeaderPosition() const {
589 // Only update the request headers once. We only want to point
590 // to the first headers read/written.
591 if (!mRequestHeaderPresent) {
592 mWorkSourceRequestHeaderPosition = dataPosition();
593 mRequestHeaderPresent = true;
594 }
595}
596
Jooyung Han1b228f42020-01-30 13:41:12 +0900597#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700598constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
599#else
600constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
601#endif
602
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700603// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700604status_t Parcel::writeInterfaceToken(const String16& interface)
605{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000606 return writeInterfaceToken(interface.string(), interface.size());
607}
608
609status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000610 if (CC_LIKELY(!isForRpc())) {
611 const IPCThreadState* threadState = IPCThreadState::self();
612 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
613 updateWorkSourceRequestHeaderPosition();
614 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
615 : IPCThreadState::kUnsetWorkSource);
616 writeInt32(kHeader);
617 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000618
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700619 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000620 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700621}
622
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000623bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
624{
625 if (!mRequestHeaderPresent) {
626 return false;
627 }
628
629 const size_t initialPosition = dataPosition();
630 setDataPosition(mWorkSourceRequestHeaderPosition);
631 status_t err = writeInt32(uid);
632 setDataPosition(initialPosition);
633 return err == NO_ERROR;
634}
635
Steven Morelandf1b1e492019-05-06 15:05:13 -0700636uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000637{
638 if (!mRequestHeaderPresent) {
639 return IPCThreadState::kUnsetWorkSource;
640 }
641
642 const size_t initialPosition = dataPosition();
643 setDataPosition(mWorkSourceRequestHeaderPosition);
644 uid_t uid = readInt32();
645 setDataPosition(initialPosition);
646 return uid;
647}
648
Mathias Agopian83c04462009-05-22 19:00:22 -0700649bool Parcel::checkInterface(IBinder* binder) const
650{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700651 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700652}
653
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700654bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700655 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700656{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700657 return enforceInterface(interface.string(), interface.size(), threadState);
658}
659
660bool Parcel::enforceInterface(const char16_t* interface,
661 size_t len,
662 IPCThreadState* threadState) const
663{
Steven Moreland3af936a2021-03-26 03:05:38 +0000664 if (CC_LIKELY(!isForRpc())) {
665 // StrictModePolicy.
666 int32_t strictPolicy = readInt32();
667 if (threadState == nullptr) {
668 threadState = IPCThreadState::self();
669 }
670 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
671 // For one-way calls, the callee is running entirely
672 // disconnected from the caller, so disable StrictMode entirely.
673 // Not only does disk/network usage not impact the caller, but
674 // there's no way to communicate back violations anyway.
675 threadState->setStrictModePolicy(0);
676 } else {
677 threadState->setStrictModePolicy(strictPolicy);
678 }
679 // WorkSource.
680 updateWorkSourceRequestHeaderPosition();
681 int32_t workSource = readInt32();
682 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
683 // vendor header
684 int32_t header = readInt32();
685 if (header != kHeader) {
686 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
687 header);
688 return false;
689 }
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700690 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000691
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100692 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700693 size_t parcel_interface_len;
694 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
695 if (len == parcel_interface_len &&
696 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700697 return true;
698 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700699 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700700 String8(interface, len).string(),
701 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700702 return false;
703 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700704}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700705
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700706size_t Parcel::objectsCount() const
707{
708 return mObjectsSize;
709}
710
711status_t Parcel::errorCheck() const
712{
713 return mError;
714}
715
716void Parcel::setError(status_t err)
717{
718 mError = err;
719}
720
721status_t Parcel::finishWrite(size_t len)
722{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700723 if (len > INT32_MAX) {
724 // don't accept size_t values which may have come from an
725 // inadvertent conversion from a negative int.
726 return BAD_VALUE;
727 }
728
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700729 //printf("Finish write of %d\n", len);
730 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700731 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700732 if (mDataPos > mDataSize) {
733 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700734 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700735 }
736 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
737 return NO_ERROR;
738}
739
740status_t Parcel::writeUnpadded(const void* data, size_t len)
741{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700742 if (len > INT32_MAX) {
743 // don't accept size_t values which may have come from an
744 // inadvertent conversion from a negative int.
745 return BAD_VALUE;
746 }
747
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700748 size_t end = mDataPos + len;
749 if (end < mDataPos) {
750 // integer overflow
751 return BAD_VALUE;
752 }
753
754 if (end <= mDataCapacity) {
755restart_write:
756 memcpy(mData+mDataPos, data, len);
757 return finishWrite(len);
758 }
759
760 status_t err = growData(len);
761 if (err == NO_ERROR) goto restart_write;
762 return err;
763}
764
765status_t Parcel::write(const void* data, size_t len)
766{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700767 if (len > INT32_MAX) {
768 // don't accept size_t values which may have come from an
769 // inadvertent conversion from a negative int.
770 return BAD_VALUE;
771 }
772
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700773 void* const d = writeInplace(len);
774 if (d) {
775 memcpy(d, data, len);
776 return NO_ERROR;
777 }
778 return mError;
779}
780
781void* Parcel::writeInplace(size_t len)
782{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700783 if (len > INT32_MAX) {
784 // don't accept size_t values which may have come from an
785 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700786 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700787 }
788
789 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700790
791 // sanity check for integer overflow
792 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700793 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700794 }
795
796 if ((mDataPos+padded) <= mDataCapacity) {
797restart_write:
798 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
799 uint8_t* const data = mData+mDataPos;
800
801 // Need to pad at end?
802 if (padded != len) {
803#if BYTE_ORDER == BIG_ENDIAN
804 static const uint32_t mask[4] = {
805 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
806 };
807#endif
808#if BYTE_ORDER == LITTLE_ENDIAN
809 static const uint32_t mask[4] = {
810 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
811 };
812#endif
813 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
814 // *reinterpret_cast<void**>(data+padded-4));
815 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
816 }
817
818 finishWrite(padded);
819 return data;
820 }
821
822 status_t err = growData(padded);
823 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700824 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700825}
826
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800827status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
828 const uint8_t* strData = (uint8_t*)str.data();
829 const size_t strLen= str.length();
830 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100831 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800832 return BAD_VALUE;
833 }
834
835 status_t err = writeInt32(utf16Len);
836 if (err) {
837 return err;
838 }
839
840 // Allocate enough bytes to hold our converted string and its terminating NULL.
841 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
842 if (!dst) {
843 return NO_MEMORY;
844 }
845
Sergio Girof4607432016-07-21 14:46:35 +0100846 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800847
848 return NO_ERROR;
849}
850
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900851
Andy Hung49198cf2020-11-18 11:02:39 -0800852status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
853status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800854
Andy Hung49198cf2020-11-18 11:02:39 -0800855status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
856status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700857
Andy Hung49198cf2020-11-18 11:02:39 -0800858status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
859status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
860status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
861status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
862status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
863status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
864status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
865status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
866status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
867status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
868status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
869status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
870status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
871status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
872status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
873status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
874status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
875status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
876status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
877status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
878status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
879status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
880status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
881status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
882status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
883status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
884status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700885
Andy Hung49198cf2020-11-18 11:02:39 -0800886status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800887status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800888 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900889status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800890 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800891status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800892 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900893status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800894 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
895status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800896
Andy Hung49198cf2020-11-18 11:02:39 -0800897status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
898status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
899status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
900
901status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
902status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
903status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
904
905status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
906
907status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
908status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
909
910status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
911status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
912
913status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
914status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
915status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
916status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
917status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
918status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
919status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
920status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
921status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
922status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
923status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
924status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
925status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
926status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
927status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
928status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
929status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
930status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
931status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
932status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
933status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
934status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
935status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
936status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
937status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
938status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
939status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
940
941status_t Parcel::readString16Vector(
942 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
943status_t Parcel::readString16Vector(
944 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
945status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
946status_t Parcel::readUtf8VectorFromUtf16Vector(
947 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
948status_t Parcel::readUtf8VectorFromUtf16Vector(
949 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
950status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
951
952status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
953status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
954status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
955
956status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
957status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
958status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
959
960status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800961
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700962status_t Parcel::writeInt32(int32_t val)
963{
Andreas Huber84a6d042009-08-17 13:33:27 -0700964 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700965}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800966
967status_t Parcel::writeUint32(uint32_t val)
968{
969 return writeAligned(val);
970}
971
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700972status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700973 if (len > INT32_MAX) {
974 // don't accept size_t values which may have come from an
975 // inadvertent conversion from a negative int.
976 return BAD_VALUE;
977 }
978
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700979 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700980 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700981 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700982 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700983 if (ret == NO_ERROR) {
984 ret = write(val, len * sizeof(*val));
985 }
986 return ret;
987}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700988status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700989 if (len > INT32_MAX) {
990 // don't accept size_t values which may have come from an
991 // inadvertent conversion from a negative int.
992 return BAD_VALUE;
993 }
994
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700995 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700996 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700997 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700998 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700999 if (ret == NO_ERROR) {
1000 ret = write(val, len * sizeof(*val));
1001 }
1002 return ret;
1003}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001004
Casey Dahlind6848f52015-10-15 15:44:59 -07001005status_t Parcel::writeBool(bool val)
1006{
1007 return writeInt32(int32_t(val));
1008}
1009
1010status_t Parcel::writeChar(char16_t val)
1011{
1012 return writeInt32(int32_t(val));
1013}
1014
1015status_t Parcel::writeByte(int8_t val)
1016{
1017 return writeInt32(int32_t(val));
1018}
1019
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001020status_t Parcel::writeInt64(int64_t val)
1021{
Andreas Huber84a6d042009-08-17 13:33:27 -07001022 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001023}
1024
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001025status_t Parcel::writeUint64(uint64_t val)
1026{
1027 return writeAligned(val);
1028}
1029
Serban Constantinescuf683e012013-11-05 16:53:55 +00001030status_t Parcel::writePointer(uintptr_t val)
1031{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001032 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001033}
1034
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001035status_t Parcel::writeFloat(float val)
1036{
Andreas Huber84a6d042009-08-17 13:33:27 -07001037 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001038}
1039
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001040#if defined(__mips__) && defined(__mips_hard_float)
1041
1042status_t Parcel::writeDouble(double val)
1043{
1044 union {
1045 double d;
1046 unsigned long long ll;
1047 } u;
1048 u.d = val;
1049 return writeAligned(u.ll);
1050}
1051
1052#else
1053
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001054status_t Parcel::writeDouble(double val)
1055{
Andreas Huber84a6d042009-08-17 13:33:27 -07001056 return writeAligned(val);
1057}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001058
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001059#endif
1060
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001061status_t Parcel::writeCString(const char* str)
1062{
1063 return write(str, strlen(str)+1);
1064}
1065
1066status_t Parcel::writeString8(const String8& str)
1067{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001068 return writeString8(str.string(), str.size());
1069}
1070
1071status_t Parcel::writeString8(const char* str, size_t len)
1072{
1073 if (str == nullptr) return writeInt32(-1);
1074
Jeff Sharkey18220902020-11-05 08:36:20 -07001075 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001076 status_t err = writeInt32(len);
1077 if (err == NO_ERROR) {
1078 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1079 if (data) {
1080 memcpy(data, str, len);
1081 *reinterpret_cast<char*>(data+len) = 0;
1082 return NO_ERROR;
1083 }
1084 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001085 }
1086 return err;
1087}
1088
1089status_t Parcel::writeString16(const String16& str)
1090{
1091 return writeString16(str.string(), str.size());
1092}
1093
1094status_t Parcel::writeString16(const char16_t* str, size_t len)
1095{
Yi Kong91635562018-06-07 14:38:36 -07001096 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001097
Jeff Sharkey18220902020-11-05 08:36:20 -07001098 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001099 status_t err = writeInt32(len);
1100 if (err == NO_ERROR) {
1101 len *= sizeof(char16_t);
1102 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1103 if (data) {
1104 memcpy(data, str, len);
1105 *reinterpret_cast<char16_t*>(data+len) = 0;
1106 return NO_ERROR;
1107 }
1108 err = mError;
1109 }
1110 return err;
1111}
1112
1113status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1114{
Steven Morelanda86a3562019-08-01 23:28:34 +00001115 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001116}
1117
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001118
Casey Dahlinb9872622015-11-25 15:09:45 -08001119status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1120 if (!parcelable) {
1121 return writeInt32(0);
1122 }
1123
1124 return writeParcelable(*parcelable);
1125}
1126
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001127status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001128{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001129 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001130 return BAD_TYPE;
1131
1132 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001133 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001134 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001136 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001137 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001139 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1140 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141
1142 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001143 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001144 return err;
1145 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001146 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001147 return err;
1148}
1149
Jeff Brown93ff1f92011-11-04 19:01:44 -07001150status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001151{
Steven Moreland5553ac42020-11-11 02:14:45 +00001152 if (isForRpc()) {
1153 ALOGE("Cannot write file descriptor to remote binder.");
1154 return BAD_TYPE;
1155 }
1156
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001157 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001158 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001159 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001160 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001161 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001162 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001163 return writeObject(obj, true);
1164}
1165
1166status_t Parcel::writeDupFileDescriptor(int fd)
1167{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001168 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001169 if (dupFd < 0) {
1170 return -errno;
1171 }
1172 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001173 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001174 close(dupFd);
1175 }
1176 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001177}
1178
Dianne Hackborn1941a402016-08-29 12:30:43 -07001179status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1180{
1181 writeInt32(0);
1182 return writeFileDescriptor(fd, takeOwnership);
1183}
1184
Ryo Hashimotobf551892018-05-31 16:58:35 +09001185status_t Parcel::writeDupParcelFileDescriptor(int fd)
1186{
1187 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1188 if (dupFd < 0) {
1189 return -errno;
1190 }
1191 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1192 if (err != OK) {
1193 close(dupFd);
1194 }
1195 return err;
1196}
1197
Christopher Wiley2cf19952016-04-11 11:09:37 -07001198status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001199 return writeDupFileDescriptor(fd.get());
1200}
1201
Jeff Brown13b16042014-11-11 16:44:25 -08001202status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001203{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001204 if (len > INT32_MAX) {
1205 // don't accept size_t values which may have come from an
1206 // inadvertent conversion from a negative int.
1207 return BAD_VALUE;
1208 }
1209
Jeff Brown13b16042014-11-11 16:44:25 -08001210 status_t status;
1211 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001212 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001213 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001214 if (status) return status;
1215
1216 void* ptr = writeInplace(len);
1217 if (!ptr) return NO_MEMORY;
1218
Jeff Brown13b16042014-11-11 16:44:25 -08001219 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001220 return NO_ERROR;
1221 }
1222
Steve Block6807e592011-10-20 11:56:00 +01001223 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001224 int fd = ashmem_create_region("Parcel Blob", len);
1225 if (fd < 0) return NO_MEMORY;
1226
1227 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1228 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001229 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001230 } else {
Yi Kong91635562018-06-07 14:38:36 -07001231 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001232 if (ptr == MAP_FAILED) {
1233 status = -errno;
1234 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001235 if (!mutableCopy) {
1236 result = ashmem_set_prot_region(fd, PROT_READ);
1237 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001238 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001239 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001240 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001241 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001242 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001243 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001244 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001245 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001246 return NO_ERROR;
1247 }
1248 }
1249 }
1250 }
1251 ::munmap(ptr, len);
1252 }
1253 ::close(fd);
1254 return status;
1255}
1256
Jeff Brown13b16042014-11-11 16:44:25 -08001257status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1258{
1259 // Must match up with what's done in writeBlob.
1260 if (!mAllowFds) return FDS_NOT_ALLOWED;
1261 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1262 if (status) return status;
1263 return writeDupFileDescriptor(fd);
1264}
1265
Mathias Agopiane1424282013-07-29 21:24:40 -07001266status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001267{
1268 status_t err;
1269
1270 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001271 const size_t len = val.getFlattenedSize();
1272 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001273
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001274 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001275 // don't accept size_t values which may have come from an
1276 // inadvertent conversion from a negative int.
1277 return BAD_VALUE;
1278 }
1279
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001280 err = this->writeInt32(len);
1281 if (err) return err;
1282
1283 err = this->writeInt32(fd_count);
1284 if (err) return err;
1285
1286 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001287 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001288 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001289 return BAD_VALUE;
1290
Yi Kong91635562018-06-07 14:38:36 -07001291 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001292 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001293 fds = new (std::nothrow) int[fd_count];
1294 if (fds == nullptr) {
1295 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1296 return BAD_VALUE;
1297 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001298 }
1299
1300 err = val.flatten(buf, len, fds, fd_count);
1301 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1302 err = this->writeDupFileDescriptor( fds[i] );
1303 }
1304
1305 if (fd_count) {
1306 delete [] fds;
1307 }
1308
1309 return err;
1310}
1311
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001312status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1313{
1314 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1315 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1316 if (enoughData && enoughObjects) {
1317restart_write:
1318 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001319
Christopher Tate98e67d32015-06-03 18:44:15 -07001320 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001321 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001322 if (!mAllowFds) {
1323 // fail before modifying our object index
1324 return FDS_NOT_ALLOWED;
1325 }
1326 mHasFds = mFdsKnown = true;
1327 }
1328
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001329 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001330 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001331 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001332 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001333 mObjectsSize++;
1334 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001335
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001336 return finishWrite(sizeof(flat_binder_object));
1337 }
1338
1339 if (!enoughData) {
1340 const status_t err = growData(sizeof(val));
1341 if (err != NO_ERROR) return err;
1342 }
1343 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001344 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1345 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001346 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001347 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001348 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001349 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001350 mObjects = objects;
1351 mObjectsCapacity = newSize;
1352 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001353
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001354 goto restart_write;
1355}
1356
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001357status_t Parcel::writeNoException()
1358{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001359 binder::Status status;
1360 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001361}
1362
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001363status_t Parcel::validateReadData(size_t upperBound) const
1364{
1365 // Don't allow non-object reads on object data
1366 if (mObjectsSorted || mObjectsSize <= 1) {
1367data_sorted:
1368 // Expect to check only against the next object
1369 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1370 // For some reason the current read position is greater than the next object
1371 // hint. Iterate until we find the right object
1372 size_t nextObject = mNextObjectHint;
1373 do {
1374 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1375 // Requested info overlaps with an object
1376 ALOGE("Attempt to read from protected data in Parcel %p", this);
1377 return PERMISSION_DENIED;
1378 }
1379 nextObject++;
1380 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1381 mNextObjectHint = nextObject;
1382 }
1383 return NO_ERROR;
1384 }
1385 // Quickly determine if mObjects is sorted.
1386 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1387 binder_size_t* prevObj = currObj;
1388 while (currObj > mObjects) {
1389 prevObj--;
1390 if(*prevObj > *currObj) {
1391 goto data_unsorted;
1392 }
1393 currObj--;
1394 }
1395 mObjectsSorted = true;
1396 goto data_sorted;
1397
1398data_unsorted:
1399 // Insertion Sort mObjects
1400 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1401 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1402 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1403 binder_size_t temp = *iter0;
1404 binder_size_t* iter1 = iter0 - 1;
1405 while (iter1 >= mObjects && *iter1 > temp) {
1406 *(iter1 + 1) = *iter1;
1407 iter1--;
1408 }
1409 *(iter1 + 1) = temp;
1410 }
1411 mNextObjectHint = 0;
1412 mObjectsSorted = true;
1413 goto data_sorted;
1414}
1415
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001416status_t Parcel::read(void* outData, size_t len) const
1417{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001418 if (len > INT32_MAX) {
1419 // don't accept size_t values which may have come from an
1420 // inadvertent conversion from a negative int.
1421 return BAD_VALUE;
1422 }
1423
1424 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1425 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001426 if (mObjectsSize > 0) {
1427 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001428 if(err != NO_ERROR) {
1429 // Still increment the data position by the expected length
1430 mDataPos += pad_size(len);
1431 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1432 return err;
1433 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001434 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001435 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001436 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001437 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001438 return NO_ERROR;
1439 }
1440 return NOT_ENOUGH_DATA;
1441}
1442
1443const void* Parcel::readInplace(size_t len) const
1444{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001445 if (len > INT32_MAX) {
1446 // don't accept size_t values which may have come from an
1447 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001448 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001449 }
1450
1451 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1452 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001453 if (mObjectsSize > 0) {
1454 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001455 if(err != NO_ERROR) {
1456 // Still increment the data position by the expected length
1457 mDataPos += pad_size(len);
1458 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001459 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001460 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001461 }
1462
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001463 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001464 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001465 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001466 return data;
1467 }
Yi Kong91635562018-06-07 14:38:36 -07001468 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001469}
1470
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001471status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1472 if (status_t status = readInt32(size); status != OK) return status;
1473 if (*size < 0) return OK; // may be null, client to handle
1474
1475 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1476
1477 // approximation, can't know max element size (e.g. if it makes heap
1478 // allocations)
1479 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1480 int32_t allocationSize;
1481 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1482
1483 // High limit of 1MB since something this big could never be returned. Could
1484 // probably scope this down, but might impact very specific usecases.
1485 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1486
1487 if (allocationSize >= kMaxAllocationSize) {
1488 return NO_MEMORY;
1489 }
1490
1491 return OK;
1492}
1493
Andreas Huber84a6d042009-08-17 13:33:27 -07001494template<class T>
1495status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001496 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001497
1498 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001499 if (mObjectsSize > 0) {
1500 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001501 if(err != NO_ERROR) {
1502 // Still increment the data position by the expected length
1503 mDataPos += sizeof(T);
1504 return err;
1505 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001506 }
1507
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001508 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001509 mDataPos += sizeof(T);
1510 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001511 return NO_ERROR;
1512 } else {
1513 return NOT_ENOUGH_DATA;
1514 }
1515}
1516
Andreas Huber84a6d042009-08-17 13:33:27 -07001517template<class T>
1518T Parcel::readAligned() const {
1519 T result;
1520 if (readAligned(&result) != NO_ERROR) {
1521 result = 0;
1522 }
1523
1524 return result;
1525}
1526
1527template<class T>
1528status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001529 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001530
1531 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1532restart_write:
1533 *reinterpret_cast<T*>(mData+mDataPos) = val;
1534 return finishWrite(sizeof(val));
1535 }
1536
1537 status_t err = growData(sizeof(val));
1538 if (err == NO_ERROR) goto restart_write;
1539 return err;
1540}
1541
1542status_t Parcel::readInt32(int32_t *pArg) const
1543{
1544 return readAligned(pArg);
1545}
1546
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001547int32_t Parcel::readInt32() const
1548{
Andreas Huber84a6d042009-08-17 13:33:27 -07001549 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001550}
1551
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001552status_t Parcel::readUint32(uint32_t *pArg) const
1553{
1554 return readAligned(pArg);
1555}
1556
1557uint32_t Parcel::readUint32() const
1558{
1559 return readAligned<uint32_t>();
1560}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001561
1562status_t Parcel::readInt64(int64_t *pArg) const
1563{
Andreas Huber84a6d042009-08-17 13:33:27 -07001564 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001565}
1566
1567
1568int64_t Parcel::readInt64() const
1569{
Andreas Huber84a6d042009-08-17 13:33:27 -07001570 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001571}
1572
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001573status_t Parcel::readUint64(uint64_t *pArg) const
1574{
1575 return readAligned(pArg);
1576}
1577
1578uint64_t Parcel::readUint64() const
1579{
1580 return readAligned<uint64_t>();
1581}
1582
Serban Constantinescuf683e012013-11-05 16:53:55 +00001583status_t Parcel::readPointer(uintptr_t *pArg) const
1584{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001585 status_t ret;
1586 binder_uintptr_t ptr;
1587 ret = readAligned(&ptr);
1588 if (!ret)
1589 *pArg = ptr;
1590 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001591}
1592
1593uintptr_t Parcel::readPointer() const
1594{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001595 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001596}
1597
1598
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001599status_t Parcel::readFloat(float *pArg) const
1600{
Andreas Huber84a6d042009-08-17 13:33:27 -07001601 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001602}
1603
1604
1605float Parcel::readFloat() const
1606{
Andreas Huber84a6d042009-08-17 13:33:27 -07001607 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001608}
1609
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001610#if defined(__mips__) && defined(__mips_hard_float)
1611
1612status_t Parcel::readDouble(double *pArg) const
1613{
1614 union {
1615 double d;
1616 unsigned long long ll;
1617 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001618 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001619 status_t status;
1620 status = readAligned(&u.ll);
1621 *pArg = u.d;
1622 return status;
1623}
1624
1625double Parcel::readDouble() const
1626{
1627 union {
1628 double d;
1629 unsigned long long ll;
1630 } u;
1631 u.ll = readAligned<unsigned long long>();
1632 return u.d;
1633}
1634
1635#else
1636
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001637status_t Parcel::readDouble(double *pArg) const
1638{
Andreas Huber84a6d042009-08-17 13:33:27 -07001639 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001640}
1641
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001642double Parcel::readDouble() const
1643{
Andreas Huber84a6d042009-08-17 13:33:27 -07001644 return readAligned<double>();
1645}
1646
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001647#endif
1648
Casey Dahlind6848f52015-10-15 15:44:59 -07001649status_t Parcel::readBool(bool *pArg) const
1650{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001651 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001652 status_t ret = readInt32(&tmp);
1653 *pArg = (tmp != 0);
1654 return ret;
1655}
1656
1657bool Parcel::readBool() const
1658{
1659 return readInt32() != 0;
1660}
1661
1662status_t Parcel::readChar(char16_t *pArg) const
1663{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001664 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001665 status_t ret = readInt32(&tmp);
1666 *pArg = char16_t(tmp);
1667 return ret;
1668}
1669
1670char16_t Parcel::readChar() const
1671{
1672 return char16_t(readInt32());
1673}
1674
1675status_t Parcel::readByte(int8_t *pArg) const
1676{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001677 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001678 status_t ret = readInt32(&tmp);
1679 *pArg = int8_t(tmp);
1680 return ret;
1681}
1682
1683int8_t Parcel::readByte() const
1684{
1685 return int8_t(readInt32());
1686}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001687
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001688status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1689 size_t utf16Size = 0;
1690 const char16_t* src = readString16Inplace(&utf16Size);
1691 if (!src) {
1692 return UNEXPECTED_NULL;
1693 }
1694
1695 // Save ourselves the trouble, we're done.
1696 if (utf16Size == 0u) {
1697 str->clear();
1698 return NO_ERROR;
1699 }
1700
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001701 // Allow for closing '\0'
1702 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1703 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001704 return BAD_VALUE;
1705 }
1706 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001707 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001708 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001709 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1710 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001711 return NO_ERROR;
1712}
1713
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001714const char* Parcel::readCString() const
1715{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001716 if (mDataPos < mDataSize) {
1717 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001718 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1719 // is the string's trailing NUL within the parcel's valid bounds?
1720 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1721 if (eos) {
1722 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001723 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001724 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001725 return str;
1726 }
1727 }
Yi Kong91635562018-06-07 14:38:36 -07001728 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001729}
1730
1731String8 Parcel::readString8() const
1732{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001733 size_t len;
1734 const char* str = readString8Inplace(&len);
1735 if (str) return String8(str, len);
1736 ALOGE("Reading a NULL string not supported here.");
1737 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07001738}
1739
1740status_t Parcel::readString8(String8* pArg) const
1741{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001742 size_t len;
1743 const char* str = readString8Inplace(&len);
1744 if (str) {
1745 pArg->setTo(str, len);
1746 return 0;
1747 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07001748 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001749 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07001750 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001751}
1752
1753const char* Parcel::readString8Inplace(size_t* outLen) const
1754{
1755 int32_t size = readInt32();
1756 // watch for potential int overflow from size+1
1757 if (size >= 0 && size < INT32_MAX) {
1758 *outLen = size;
1759 const char* str = (const char*)readInplace(size+1);
1760 if (str != nullptr) {
Steven Moreland61d0f842020-12-04 21:13:03 +00001761 if (str[size] == '\0') {
1762 return str;
1763 }
1764 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001765 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001766 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001767 *outLen = 0;
1768 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001769}
1770
1771String16 Parcel::readString16() const
1772{
1773 size_t len;
1774 const char16_t* str = readString16Inplace(&len);
1775 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001776 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001777 return String16();
1778}
1779
Casey Dahlinb9872622015-11-25 15:09:45 -08001780
Casey Dahlin451ff582015-10-19 18:12:18 -07001781status_t Parcel::readString16(String16* pArg) const
1782{
1783 size_t len;
1784 const char16_t* str = readString16Inplace(&len);
1785 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001786 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001787 return 0;
1788 } else {
1789 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001790 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001791 }
1792}
1793
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001794const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1795{
1796 int32_t size = readInt32();
1797 // watch for potential int overflow from size+1
1798 if (size >= 0 && size < INT32_MAX) {
1799 *outLen = size;
1800 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001801 if (str != nullptr) {
Steven Moreland61d0f842020-12-04 21:13:03 +00001802 if (str[size] == u'\0') {
1803 return str;
1804 }
1805 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001806 }
1807 }
1808 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001809 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001810}
1811
Casey Dahlinf0c13772015-10-27 18:33:56 -07001812status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1813{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001814 status_t status = readNullableStrongBinder(val);
1815 if (status == OK && !val->get()) {
1816 status = UNEXPECTED_NULL;
1817 }
1818 return status;
1819}
1820
1821status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1822{
Steven Morelanda86a3562019-08-01 23:28:34 +00001823 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001824}
1825
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001826sp<IBinder> Parcel::readStrongBinder() const
1827{
1828 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001829 // Note that a lot of code in Android reads binders by hand with this
1830 // method, and that code has historically been ok with getting nullptr
1831 // back (while ignoring error codes).
1832 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001833 return val;
1834}
1835
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001836int32_t Parcel::readExceptionCode() const
1837{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001838 binder::Status status;
1839 status.readFromParcel(*this);
1840 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001841}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001842
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001843native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001844{
1845 int numFds, numInts;
1846 status_t err;
1847 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001848 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001849 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001850 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001851
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001852 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001853 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001854 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001855 }
1856
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001857 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001858 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001859 if (h->data[i] < 0) {
1860 for (int j = 0; j < i; j++) {
1861 close(h->data[j]);
1862 }
1863 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001864 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001865 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001866 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001867 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001868 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001869 native_handle_close(h);
1870 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001871 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001872 }
1873 return h;
1874}
1875
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001876int Parcel::readFileDescriptor() const
1877{
1878 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001879
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001880 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001881 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001882 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001883
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001884 return BAD_TYPE;
1885}
1886
Dianne Hackborn1941a402016-08-29 12:30:43 -07001887int Parcel::readParcelFileDescriptor() const
1888{
1889 int32_t hasComm = readInt32();
1890 int fd = readFileDescriptor();
1891 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001892 // detach (owned by the binder driver)
1893 int comm = readFileDescriptor();
1894
1895 // warning: this must be kept in sync with:
1896 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1897 enum ParcelFileDescriptorStatus {
1898 DETACHED = 2,
1899 };
1900
1901#if BYTE_ORDER == BIG_ENDIAN
1902 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
1903#endif
1904#if BYTE_ORDER == LITTLE_ENDIAN
1905 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
1906#endif
1907
1908 ssize_t written = TEMP_FAILURE_RETRY(
1909 ::write(comm, &message, sizeof(message)));
1910
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08001911 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001912 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
1913 written, strerror(errno));
1914 return BAD_TYPE;
1915 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07001916 }
1917 return fd;
1918}
1919
Christopher Wiley2cf19952016-04-11 11:09:37 -07001920status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08001921{
1922 int got = readFileDescriptor();
1923
1924 if (got == BAD_TYPE) {
1925 return BAD_TYPE;
1926 }
1927
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001928 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08001929
1930 if (val->get() < 0) {
1931 return BAD_VALUE;
1932 }
1933
1934 return OK;
1935}
1936
Ryo Hashimotobf551892018-05-31 16:58:35 +09001937status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
1938{
1939 int got = readParcelFileDescriptor();
1940
1941 if (got == BAD_TYPE) {
1942 return BAD_TYPE;
1943 }
1944
1945 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
1946
1947 if (val->get() < 0) {
1948 return BAD_VALUE;
1949 }
1950
1951 return OK;
1952}
Casey Dahlin06673e32015-11-23 13:24:23 -08001953
Jeff Brown5707dbf2011-09-23 21:17:56 -07001954status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1955{
Jeff Brown13b16042014-11-11 16:44:25 -08001956 int32_t blobType;
1957 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001958 if (status) return status;
1959
Jeff Brown13b16042014-11-11 16:44:25 -08001960 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001961 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001962 const void* ptr = readInplace(len);
1963 if (!ptr) return BAD_VALUE;
1964
Jeff Brown13b16042014-11-11 16:44:25 -08001965 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001966 return NO_ERROR;
1967 }
1968
Steve Block6807e592011-10-20 11:56:00 +01001969 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001970 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001971 int fd = readFileDescriptor();
1972 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1973
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001974 if (!ashmem_valid(fd)) {
1975 ALOGE("invalid fd");
1976 return BAD_VALUE;
1977 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001978 int size = ashmem_get_size_region(fd);
1979 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001980 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001981 return BAD_VALUE;
1982 }
Yi Kong91635562018-06-07 14:38:36 -07001983 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08001984 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001985 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001986
Jeff Brown13b16042014-11-11 16:44:25 -08001987 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001988 return NO_ERROR;
1989}
1990
Mathias Agopiane1424282013-07-29 21:24:40 -07001991status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001992{
1993 // size
1994 const size_t len = this->readInt32();
1995 const size_t fd_count = this->readInt32();
1996
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001997 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001998 // don't accept size_t values which may have come from an
1999 // inadvertent conversion from a negative int.
2000 return BAD_VALUE;
2001 }
2002
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002003 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002004 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002005 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002006 return BAD_VALUE;
2007
Yi Kong91635562018-06-07 14:38:36 -07002008 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002009 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002010 fds = new (std::nothrow) int[fd_count];
2011 if (fds == nullptr) {
2012 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2013 return BAD_VALUE;
2014 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002015 }
2016
2017 status_t err = NO_ERROR;
2018 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002019 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002020 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002021 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002022 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 -07002023 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2024 // Close all the file descriptors that were dup-ed.
2025 for (size_t j=0; j<i ;j++) {
2026 close(fds[j]);
2027 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002028 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002029 }
2030
2031 if (err == NO_ERROR) {
2032 err = val.unflatten(buf, len, fds, fd_count);
2033 }
2034
2035 if (fd_count) {
2036 delete [] fds;
2037 }
2038
2039 return err;
2040}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002041const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2042{
2043 const size_t DPOS = mDataPos;
2044 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2045 const flat_binder_object* obj
2046 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2047 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002048 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002049 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002050 // the object list, so we don't want to check for it when
2051 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002052 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002053 return obj;
2054 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002055
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002056 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002057 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002058 const size_t N = mObjectsSize;
2059 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002060
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002061 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002062 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002063 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002064
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002065 // Start at the current hint position, looking for an object at
2066 // the current data position.
2067 if (opos < N) {
2068 while (opos < (N-1) && OBJS[opos] < DPOS) {
2069 opos++;
2070 }
2071 } else {
2072 opos = N-1;
2073 }
2074 if (OBJS[opos] == DPOS) {
2075 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002076 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002077 this, DPOS, opos);
2078 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002079 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002080 return obj;
2081 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002082
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002083 // Look backwards for it...
2084 while (opos > 0 && OBJS[opos] > DPOS) {
2085 opos--;
2086 }
2087 if (OBJS[opos] == DPOS) {
2088 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002089 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002090 this, DPOS, opos);
2091 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002092 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002093 return obj;
2094 }
2095 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002096 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 -07002097 this, DPOS);
2098 }
Yi Kong91635562018-06-07 14:38:36 -07002099 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002100}
2101
2102void Parcel::closeFileDescriptors()
2103{
2104 size_t i = mObjectsSize;
2105 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002106 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002107 }
2108 while (i > 0) {
2109 i--;
2110 const flat_binder_object* flat
2111 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002112 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002113 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002114 close(flat->handle);
2115 }
2116 }
2117}
2118
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002119uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002120{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002121 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002122}
2123
2124size_t Parcel::ipcDataSize() const
2125{
2126 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2127}
2128
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002129uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002130{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002131 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002132}
2133
2134size_t Parcel::ipcObjectsCount() const
2135{
2136 return mObjectsSize;
2137}
2138
2139void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Steven Moreland161fe122020-11-12 23:16:47 +00002140 const binder_size_t* objects, size_t objectsCount, release_func relFunc)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141{
Steven Moreland438cce82021-04-02 18:04:08 +00002142 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2143 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2144
Steven Morelandceed9bb2020-12-17 01:01:06 +00002145 freeData();
2146
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002147 mData = const_cast<uint8_t*>(data);
2148 mDataSize = mDataCapacity = dataSize;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002149 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002150 mObjectsSize = mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002151 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002152
2153 binder_size_t minOffset = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002154 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002155 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002156 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002157 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002158 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002159 mObjectsSize = 0;
2160 break;
2161 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002162 const flat_binder_object* flat
2163 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2164 uint32_t type = flat->hdr.type;
2165 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2166 type == BINDER_TYPE_FD)) {
2167 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2168 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2169 // recover gracefully by clearing out the objects, and releasing the objects we do
2170 // know about.
2171 android_errorWriteLog(0x534e4554, "135930648");
2172 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2173 __func__, type, (uint64_t)offset);
2174 releaseObjects();
2175 mObjectsSize = 0;
2176 break;
2177 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002178 minOffset = offset + sizeof(flat_binder_object);
2179 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002180 scanForFds();
2181}
2182
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002183void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002184{
2185 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002186
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002187 if (errorCheck() != NO_ERROR) {
2188 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002189 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002190 } else if (dataSize() > 0) {
2191 const uint8_t* DATA = data();
2192 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002193 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002194 const size_t N = objectsCount();
2195 for (size_t i=0; i<N; i++) {
2196 const flat_binder_object* flat
2197 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2198 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002199 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002200 << " = " << flat->binder;
2201 }
2202 } else {
2203 to << "NULL";
2204 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002205
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002206 to << ")";
2207}
2208
2209void Parcel::releaseObjects()
2210{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002211 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002212 if (i == 0) {
2213 return;
2214 }
2215 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002216 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002217 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002218 while (i > 0) {
2219 i--;
2220 const flat_binder_object* flat
2221 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002222 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002223 }
2224}
2225
2226void Parcel::acquireObjects()
2227{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002228 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002229 if (i == 0) {
2230 return;
2231 }
2232 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002233 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002234 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002235 while (i > 0) {
2236 i--;
2237 const flat_binder_object* flat
2238 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002239 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002240 }
2241}
2242
2243void Parcel::freeData()
2244{
2245 freeDataNoInit();
2246 initState();
2247}
2248
2249void Parcel::freeDataNoInit()
2250{
2251 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002252 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002253 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002254 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002255 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002256 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002257 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002258 if (mData) {
2259 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002260 gParcelGlobalAllocSize -= mDataCapacity;
2261 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002262 if (mDeallocZero) {
2263 zeroMemory(mData, mDataSize);
2264 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002265 free(mData);
2266 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002267 if (mObjects) free(mObjects);
2268 }
2269}
2270
2271status_t Parcel::growData(size_t len)
2272{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002273 if (len > INT32_MAX) {
2274 // don't accept size_t values which may have come from an
2275 // inadvertent conversion from a negative int.
2276 return BAD_VALUE;
2277 }
2278
Martijn Coenen93fe5182020-01-22 10:46:25 +01002279 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2280 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002281 size_t newSize = ((mDataSize+len)*3)/2;
2282 return (newSize <= mDataSize)
2283 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002284 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002285}
2286
Steven Morelandf183fdd2020-10-27 00:12:12 +00002287static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2288 if (!zero) {
2289 return (uint8_t*)realloc(data, newCapacity);
2290 }
2291 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2292 if (!newData) {
2293 return nullptr;
2294 }
2295
2296 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2297 zeroMemory(data, oldCapacity);
2298 free(data);
2299 return newData;
2300}
2301
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002302status_t Parcel::restartWrite(size_t desired)
2303{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002304 if (desired > INT32_MAX) {
2305 // don't accept size_t values which may have come from an
2306 // inadvertent conversion from a negative int.
2307 return BAD_VALUE;
2308 }
2309
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002310 if (mOwner) {
2311 freeData();
2312 return continueWrite(desired);
2313 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002314
Steven Morelandf183fdd2020-10-27 00:12:12 +00002315 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002316 if (!data && desired > mDataCapacity) {
2317 mError = NO_MEMORY;
2318 return NO_MEMORY;
2319 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002320
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002321 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002322
Devin Moore4a0a55e2020-06-04 13:23:10 -07002323 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002324 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002325 if (mDataCapacity > desired) {
2326 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2327 } else {
2328 gParcelGlobalAllocSize += (desired - mDataCapacity);
2329 }
2330
Colin Cross83ec65e2015-12-08 17:15:50 -08002331 if (!mData) {
2332 gParcelGlobalAllocCount++;
2333 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002334 mData = data;
2335 mDataCapacity = desired;
2336 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002337
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002338 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002339 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2340 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2341
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002342 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002343 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002344 mObjectsSize = mObjectsCapacity = 0;
2345 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002346 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002347 mHasFds = false;
2348 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002349 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002350
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002351 return NO_ERROR;
2352}
2353
2354status_t Parcel::continueWrite(size_t desired)
2355{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002356 if (desired > INT32_MAX) {
2357 // don't accept size_t values which may have come from an
2358 // inadvertent conversion from a negative int.
2359 return BAD_VALUE;
2360 }
2361
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002362 // If shrinking, first adjust for any objects that appear
2363 // after the new data size.
2364 size_t objectsSize = mObjectsSize;
2365 if (desired < mDataSize) {
2366 if (desired == 0) {
2367 objectsSize = 0;
2368 } else {
2369 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002370 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002371 break;
2372 objectsSize--;
2373 }
2374 }
2375 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002376
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002377 if (mOwner) {
2378 // If the size is going to zero, just release the owner's data.
2379 if (desired == 0) {
2380 freeData();
2381 return NO_ERROR;
2382 }
2383
2384 // If there is a different owner, we need to take
2385 // posession.
2386 uint8_t* data = (uint8_t*)malloc(desired);
2387 if (!data) {
2388 mError = NO_MEMORY;
2389 return NO_MEMORY;
2390 }
Yi Kong91635562018-06-07 14:38:36 -07002391 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002392
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002393 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002394 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002395 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002396 free(data);
2397
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002398 mError = NO_MEMORY;
2399 return NO_MEMORY;
2400 }
2401
2402 // Little hack to only acquire references on objects
2403 // we will be keeping.
2404 size_t oldObjectsSize = mObjectsSize;
2405 mObjectsSize = objectsSize;
2406 acquireObjects();
2407 mObjectsSize = oldObjectsSize;
2408 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002409
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002410 if (mData) {
2411 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2412 }
2413 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002414 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002415 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002416 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002417 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
Yi Kong91635562018-06-07 14:38:36 -07002418 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002419
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002420 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002421 gParcelGlobalAllocSize += desired;
2422 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002423
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002424 mData = data;
2425 mObjects = objects;
2426 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002427 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002428 mDataCapacity = desired;
2429 mObjectsSize = mObjectsCapacity = objectsSize;
2430 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002431 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002432
2433 } else if (mData) {
2434 if (objectsSize < mObjectsSize) {
2435 // Need to release refs on any objects we are dropping.
2436 const sp<ProcessState> proc(ProcessState::self());
2437 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2438 const flat_binder_object* flat
2439 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002440 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002441 // will need to rescan because we may have lopped off the only FDs
2442 mFdsKnown = false;
2443 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002444 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002445 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002446
2447 if (objectsSize == 0) {
2448 free(mObjects);
2449 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002450 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002451 } else {
2452 binder_size_t* objects =
2453 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2454 if (objects) {
2455 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002456 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002457 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002458 }
2459 mObjectsSize = objectsSize;
2460 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002461 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002462 }
2463
2464 // We own the data, so we can just do a realloc().
2465 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002466 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002467 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002468 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2469 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002470 gParcelGlobalAllocSize += desired;
2471 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002472 mData = data;
2473 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002474 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002475 mError = NO_MEMORY;
2476 return NO_MEMORY;
2477 }
2478 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002479 if (mDataSize > desired) {
2480 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002481 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002482 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002483 if (mDataPos > desired) {
2484 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002485 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002486 }
2487 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002488
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002489 } else {
2490 // This is the first data. Easy!
2491 uint8_t* data = (uint8_t*)malloc(desired);
2492 if (!data) {
2493 mError = NO_MEMORY;
2494 return NO_MEMORY;
2495 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002496
Yi Kong91635562018-06-07 14:38:36 -07002497 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002498 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002499 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002500 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002501
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002502 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002503 gParcelGlobalAllocSize += desired;
2504 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002505
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002506 mData = data;
2507 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002508 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2509 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002510 mDataCapacity = desired;
2511 }
2512
2513 return NO_ERROR;
2514}
2515
2516void Parcel::initState()
2517{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002518 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002519 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002520 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002521 mDataSize = 0;
2522 mDataCapacity = 0;
2523 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002524 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2525 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Steven Morelandbdb53ab2021-05-05 17:57:41 +00002526 mSession = nullptr;
Yi Kong91635562018-06-07 14:38:36 -07002527 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002528 mObjectsSize = 0;
2529 mObjectsCapacity = 0;
2530 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002531 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002532 mHasFds = false;
2533 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002534 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002535 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07002536 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002537 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002538 mWorkSourceRequestHeaderPosition = 0;
2539 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002540
2541 // racing multiple init leads only to multiple identical write
2542 if (gMaxFds == 0) {
2543 struct rlimit result;
2544 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2545 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002546 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002547 } else {
2548 ALOGW("Unable to getrlimit: %s", strerror(errno));
2549 gMaxFds = 1024;
2550 }
2551 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002552}
2553
2554void Parcel::scanForFds() const
2555{
2556 bool hasFds = false;
2557 for (size_t i=0; i<mObjectsSize; i++) {
2558 const flat_binder_object* flat
2559 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002560 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002561 hasFds = true;
2562 break;
2563 }
2564 }
2565 mHasFds = hasFds;
2566 mFdsKnown = true;
2567}
2568
Dan Sandleraa5c2342015-04-10 10:08:45 -04002569size_t Parcel::getBlobAshmemSize() const
2570{
Adrian Roos6bb31142015-10-22 16:46:12 -07002571 // This used to return the size of all blobs that were written to ashmem, now we're returning
2572 // the ashmem currently referenced by this Parcel, which should be equivalent.
2573 // TODO: Remove method once ABI can be changed.
2574 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002575}
2576
Adrian Rooscbf37262015-10-22 16:12:53 -07002577size_t Parcel::getOpenAshmemSize() const
2578{
2579 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002580}
2581
2582// --- Parcel::Blob ---
2583
2584Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002585 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002586}
2587
2588Parcel::Blob::~Blob() {
2589 release();
2590}
2591
2592void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002593 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002594 ::munmap(mData, mSize);
2595 }
2596 clear();
2597}
2598
Jeff Brown13b16042014-11-11 16:44:25 -08002599void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2600 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002601 mData = data;
2602 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002603 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002604}
2605
2606void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002607 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002608 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002609 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002610 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002611}
2612
Steven Moreland61ff8492019-09-26 16:05:45 -07002613} // namespace android