blob: b91d72b87a2cea6b3780419297cee160b8f5f687 [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>
Mark Salyzynabed7f72016-01-27 08:02:48 -080044#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080046#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String8.h>
48#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049
Mathias Agopian208059f2009-05-18 15:08:03 -070050#include <private/binder/binder_module.h>
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"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070054
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070055#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080056//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080057#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080058//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070059
60// ---------------------------------------------------------------------------
61
Nick Kralevichb6b14232015-04-02 09:36:02 -070062// This macro should never be used at runtime, as a too large value
63// of s could cause an integer overflow. Instead, you should always
64// use the wrapper function pad_size()
65#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
66
67static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070068 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070069 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070070 }
71 return PAD_SIZE_UNSAFE(s);
72}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070073
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070074// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060075#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070076
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070077namespace android {
78
Steven Moreland7b102262019-08-01 15:48:43 -070079// many things compile this into prebuilts on the stack
80static_assert(sizeof(Parcel) == 60 || sizeof(Parcel) == 120);
81
Jeff Sharkey8994c182020-09-11 12:07:10 -060082static std::atomic<size_t> gParcelGlobalAllocCount;
83static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -080084
Christopher Tatee4e0ae82016-03-24 16:03:44 -070085static size_t gMaxFds = 0;
86
Jeff Brown13b16042014-11-11 16:44:25 -080087// Maximum size of a blob to transfer in-place.
88static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
89
90enum {
91 BLOB_INPLACE = 0,
92 BLOB_ASHMEM_IMMUTABLE = 1,
93 BLOB_ASHMEM_MUTABLE = 2,
94};
95
Steven Morelandb1c81202019-04-05 18:49:55 -070096static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070097 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -070099 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700100 case BINDER_TYPE_BINDER:
101 if (obj.binder) {
102 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800103 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104 }
105 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106 case BINDER_TYPE_HANDLE: {
107 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700108 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
110 b->incStrong(who);
111 }
112 return;
113 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700114 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000115 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700116 // If we own an ashmem fd, keep track of how much memory it refers to.
117 int size = ashmem_get_size_region(obj.handle);
118 if (size > 0) {
119 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700120 }
121 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700122 return;
123 }
124 }
125
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700126 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700127}
128
Adrian Roos6bb31142015-10-22 16:46:12 -0700129static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700130 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700131{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700132 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700133 case BINDER_TYPE_BINDER:
134 if (obj.binder) {
135 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800136 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700137 }
138 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 case BINDER_TYPE_HANDLE: {
140 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700141 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
143 b->decStrong(who);
144 }
145 return;
146 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800148 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000149 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700150 int size = ashmem_get_size_region(obj.handle);
151 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800152 // ashmem size might have changed since last time it was accounted for, e.g.
153 // in acquire_object(). Value of *outAshmemSize is not critical since we are
154 // releasing the object anyway. Check for integer overflow condition.
155 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700156 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700157 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800158
159 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700160 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700161 return;
162 }
163 }
164
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700165 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700166}
167
Steven Moreland34b48cb2020-12-01 22:45:38 +0000168status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700170 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland89ddfc52020-11-13 02:39:26 +0000171 auto category = internal::Stability::getCategory(binder.get());
172 return writeInt32(category.repr());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700173}
174
Steven Morelanda86a3562019-08-01 23:28:34 +0000175status_t Parcel::finishUnflattenBinder(
176 const sp<IBinder>& binder, sp<IBinder>* out) const
177{
178 int32_t stability;
179 status_t status = readInt32(&stability);
180 if (status != OK) return status;
181
Steven Moreland89ddfc52020-11-13 02:39:26 +0000182 status = internal::Stability::setRepr(binder.get(), stability, true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000183 if (status != OK) return status;
184
185 *out = binder;
186 return OK;
187}
188
Steven Morelandbf1915b2020-07-16 22:43:02 +0000189static constexpr inline int schedPolicyMask(int policy, int priority) {
190 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
191}
192
Steven Morelanda86a3562019-08-01 23:28:34 +0000193status_t Parcel::flattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700194{
Steven Moreland5553ac42020-11-11 02:14:45 +0000195 if (isForRpc()) {
196 if (binder) {
197 status_t status = writeInt32(1); // non-null
198 if (status != OK) return status;
199 RpcAddress address = RpcAddress::zero();
200 status = mConnection->state()->onBinderLeaving(mConnection, binder, &address);
201 if (status != OK) return status;
202 status = address.writeToParcel(this);
203 if (status != OK) return status;
204 } else {
205 status_t status = writeInt32(0); // null
206 if (status != OK) return status;
207 }
208 return finishFlattenBinder(binder);
209 }
210
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700211 flat_binder_object obj;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000212 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700213
Steven Morelandbf1915b2020-07-16 22:43:02 +0000214 int schedBits = 0;
215 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
216 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700217 }
218
Yi Kong91635562018-06-07 14:38:36 -0700219 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800220 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700221 if (!local) {
222 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700223 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000224 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000225 } else {
226 if (proxy->isRpcBinder()) {
227 ALOGE("Sending a socket binder over RPC is prohibited");
228 return INVALID_OPERATION;
229 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000231 const int32_t handle = proxy ? proxy->getPrivateAccessorForId().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700232 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800233 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700234 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800235 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700236 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000237 int policy = local->getMinSchedulerPolicy();
238 int priority = local->getMinSchedulerPriority();
239
240 if (policy != 0 || priority != 0) {
241 // override value, since it is set explicitly
242 schedBits = schedPolicyMask(policy, priority);
243 }
Steven Morelandf0212002018-12-26 13:59:23 -0800244 if (local->isRequestingSid()) {
245 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
246 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000247 if (local->isInheritRt()) {
248 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
249 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700250 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800251 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
252 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700253 }
254 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700255 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800256 obj.binder = 0;
257 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700259
Steven Morelandbf1915b2020-07-16 22:43:02 +0000260 obj.flags |= schedBits;
261
Steven Moreland34b48cb2020-12-01 22:45:38 +0000262 status_t status = writeObject(obj, false);
263 if (status != OK) return status;
264
265 return finishFlattenBinder(binder);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700266}
267
Steven Morelanda86a3562019-08-01 23:28:34 +0000268status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700269{
Steven Moreland5553ac42020-11-11 02:14:45 +0000270 if (isForRpc()) {
271 LOG_ALWAYS_FATAL_IF(mConnection == nullptr,
272 "RpcConnection required to read from remote parcel");
273
274 int32_t isNull;
275 status_t status = readInt32(&isNull);
276 if (status != OK) return status;
277
278 sp<IBinder> binder;
279
280 if (isNull & 1) {
281 auto addr = RpcAddress::zero();
282 status_t status = addr.readFromParcel(*this);
283 if (status != OK) return status;
284 binder = mConnection->state()->onBinderEntering(mConnection, addr);
285 }
286
287 return finishUnflattenBinder(binder, out);
288 }
289
Steven Morelanda86a3562019-08-01 23:28:34 +0000290 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700291
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700292 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700293 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000294 case BINDER_TYPE_BINDER: {
295 sp<IBinder> binder = reinterpret_cast<IBinder*>(flat->cookie);
296 return finishUnflattenBinder(binder, out);
297 }
298 case BINDER_TYPE_HANDLE: {
299 sp<IBinder> binder =
300 ProcessState::self()->getStrongProxyForHandle(flat->handle);
301 return finishUnflattenBinder(binder, out);
302 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700303 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700304 }
305 return BAD_TYPE;
306}
307
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700308// ---------------------------------------------------------------------------
309
310Parcel::Parcel()
311{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800312 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700313 initState();
314}
315
316Parcel::~Parcel()
317{
318 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800319 LOG_ALLOC("Parcel %p: destroyed", this);
320}
321
322size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600323 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800324}
325
326size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600327 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700328}
329
330const uint8_t* Parcel::data() const
331{
332 return mData;
333}
334
335size_t Parcel::dataSize() const
336{
337 return (mDataSize > mDataPos ? mDataSize : mDataPos);
338}
339
340size_t Parcel::dataAvail() const
341{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700342 size_t result = dataSize() - dataPosition();
343 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700344 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700345 }
346 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700347}
348
349size_t Parcel::dataPosition() const
350{
351 return mDataPos;
352}
353
354size_t Parcel::dataCapacity() const
355{
356 return mDataCapacity;
357}
358
359status_t Parcel::setDataSize(size_t size)
360{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700361 if (size > INT32_MAX) {
362 // don't accept size_t values which may have come from an
363 // inadvertent conversion from a negative int.
364 return BAD_VALUE;
365 }
366
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700367 status_t err;
368 err = continueWrite(size);
369 if (err == NO_ERROR) {
370 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700371 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700372 }
373 return err;
374}
375
376void Parcel::setDataPosition(size_t pos) const
377{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700378 if (pos > INT32_MAX) {
379 // don't accept size_t values which may have come from an
380 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700381 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700382 }
383
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700384 mDataPos = pos;
385 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800386 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700387}
388
389status_t Parcel::setDataCapacity(size_t size)
390{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700391 if (size > INT32_MAX) {
392 // don't accept size_t values which may have come from an
393 // inadvertent conversion from a negative int.
394 return BAD_VALUE;
395 }
396
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700397 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700398 return NO_ERROR;
399}
400
401status_t Parcel::setData(const uint8_t* buffer, size_t len)
402{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700403 if (len > INT32_MAX) {
404 // don't accept size_t values which may have come from an
405 // inadvertent conversion from a negative int.
406 return BAD_VALUE;
407 }
408
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700409 status_t err = restartWrite(len);
410 if (err == NO_ERROR) {
411 memcpy(const_cast<uint8_t*>(data()), buffer, len);
412 mDataSize = len;
413 mFdsKnown = false;
414 }
415 return err;
416}
417
Andreas Huber51faf462011-04-13 10:21:56 -0700418status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700419{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700420 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700421 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800422 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700423 size_t size = parcel->mObjectsSize;
424 int startPos = mDataPos;
425 int firstIndex = -1, lastIndex = -2;
426
427 if (len == 0) {
428 return NO_ERROR;
429 }
430
Nick Kralevichb6b14232015-04-02 09:36:02 -0700431 if (len > INT32_MAX) {
432 // don't accept size_t values which may have come from an
433 // inadvertent conversion from a negative int.
434 return BAD_VALUE;
435 }
436
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437 // range checks against the source parcel size
438 if ((offset > parcel->mDataSize)
439 || (len > parcel->mDataSize)
440 || (offset + len > parcel->mDataSize)) {
441 return BAD_VALUE;
442 }
443
444 // Count objects in range
445 for (int i = 0; i < (int) size; i++) {
446 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700447 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 if (firstIndex == -1) {
449 firstIndex = i;
450 }
451 lastIndex = i;
452 }
453 }
454 int numObjects = lastIndex - firstIndex + 1;
455
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700456 if ((mDataSize+len) > mDataCapacity) {
457 // grow data
458 err = growData(len);
459 if (err != NO_ERROR) {
460 return err;
461 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700462 }
463
464 // append data
465 memcpy(mData + mDataPos, data + offset, len);
466 mDataPos += len;
467 mDataSize += len;
468
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400469 err = NO_ERROR;
470
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700471 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200472 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473 // grow objects
474 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +0100475 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
476 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700477 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +0100478 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800479 binder_size_t *objects =
480 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700481 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700482 return NO_MEMORY;
483 }
484 mObjects = objects;
485 mObjectsCapacity = newSize;
486 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700487
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700488 // append and acquire objects
489 int idx = mObjectsSize;
490 for (int i = firstIndex; i <= lastIndex; i++) {
491 size_t off = objects[i] - offset + startPos;
492 mObjects[idx++] = off;
493 mObjectsSize++;
494
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700495 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700496 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700497 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700498
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700499 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700500 // If this is a file descriptor, we need to dup it so the
501 // new Parcel now owns its own fd, and can declare that we
502 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800503 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800504 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700505 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400506 if (!mAllowFds) {
507 err = FDS_NOT_ALLOWED;
508 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700509 }
510 }
511 }
512
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400513 return err;
514}
515
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700516int Parcel::compareData(const Parcel& other) {
517 size_t size = dataSize();
518 if (size != other.dataSize()) {
519 return size < other.dataSize() ? -1 : 1;
520 }
521 return memcmp(data(), other.data(), size);
522}
523
Jeff Brown13b16042014-11-11 16:44:25 -0800524bool Parcel::allowFds() const
525{
526 return mAllowFds;
527}
528
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700529bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400530{
531 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700532 if (!allowFds) {
533 mAllowFds = false;
534 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400535 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700536}
537
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700538void Parcel::restoreAllowFds(bool lastValue)
539{
540 mAllowFds = lastValue;
541}
542
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543bool Parcel::hasFileDescriptors() const
544{
545 if (!mFdsKnown) {
546 scanForFds();
547 }
548 return mHasFds;
549}
550
Steven Morelandf183fdd2020-10-27 00:12:12 +0000551void Parcel::markSensitive() const
552{
553 mDeallocZero = true;
554}
555
Steven Moreland5553ac42020-11-11 02:14:45 +0000556void Parcel::markForBinder(const sp<IBinder>& binder) {
557 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
558 markForRpc(binder->remoteBinder()->getPrivateAccessorForId().rpcConnection());
559 }
560}
561
562void Parcel::markForRpc(const sp<RpcConnection>& connection) {
563 LOG_ALWAYS_FATAL_IF(connection == nullptr, "markForRpc requires connection");
564 mConnection = connection;
565}
566
567bool Parcel::isForRpc() const {
568 return mConnection != nullptr;
569}
570
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000571void Parcel::updateWorkSourceRequestHeaderPosition() const {
572 // Only update the request headers once. We only want to point
573 // to the first headers read/written.
574 if (!mRequestHeaderPresent) {
575 mWorkSourceRequestHeaderPosition = dataPosition();
576 mRequestHeaderPresent = true;
577 }
578}
579
Jooyung Han1b228f42020-01-30 13:41:12 +0900580#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700581constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
582#else
583constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
584#endif
585
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700586// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700587status_t Parcel::writeInterfaceToken(const String16& interface)
588{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000589 return writeInterfaceToken(interface.string(), interface.size());
590}
591
592status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Olivier Gaillard91a04802018-11-14 17:32:41 +0000593 const IPCThreadState* threadState = IPCThreadState::self();
594 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000595 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000596 writeInt32(threadState->shouldPropagateWorkSource() ?
597 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700598 writeInt32(kHeader);
Steven Morelanddbc76c72020-10-01 18:02:48 +0000599
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700600 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000601 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700602}
603
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000604bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
605{
606 if (!mRequestHeaderPresent) {
607 return false;
608 }
609
610 const size_t initialPosition = dataPosition();
611 setDataPosition(mWorkSourceRequestHeaderPosition);
612 status_t err = writeInt32(uid);
613 setDataPosition(initialPosition);
614 return err == NO_ERROR;
615}
616
Steven Morelandf1b1e492019-05-06 15:05:13 -0700617uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000618{
619 if (!mRequestHeaderPresent) {
620 return IPCThreadState::kUnsetWorkSource;
621 }
622
623 const size_t initialPosition = dataPosition();
624 setDataPosition(mWorkSourceRequestHeaderPosition);
625 uid_t uid = readInt32();
626 setDataPosition(initialPosition);
627 return uid;
628}
629
Mathias Agopian83c04462009-05-22 19:00:22 -0700630bool Parcel::checkInterface(IBinder* binder) const
631{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700632 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700633}
634
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700635bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700636 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700637{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700638 return enforceInterface(interface.string(), interface.size(), threadState);
639}
640
641bool Parcel::enforceInterface(const char16_t* interface,
642 size_t len,
643 IPCThreadState* threadState) const
644{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100645 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700646 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700647 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700648 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700649 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700650 if ((threadState->getLastTransactionBinderFlags() &
651 IBinder::FLAG_ONEWAY) != 0) {
652 // For one-way calls, the callee is running entirely
653 // disconnected from the caller, so disable StrictMode entirely.
654 // Not only does disk/network usage not impact the caller, but
655 // there's no way to commuicate back any violations anyway.
656 threadState->setStrictModePolicy(0);
657 } else {
658 threadState->setStrictModePolicy(strictPolicy);
659 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100660 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000661 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100662 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000663 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700664 // vendor header
665 int32_t header = readInt32();
666 if (header != kHeader) {
667 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
668 return false;
669 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100670 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700671 size_t parcel_interface_len;
672 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
673 if (len == parcel_interface_len &&
674 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700675 return true;
676 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700677 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700678 String8(interface, len).string(),
679 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700680 return false;
681 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700682}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700683
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700684size_t Parcel::objectsCount() const
685{
686 return mObjectsSize;
687}
688
689status_t Parcel::errorCheck() const
690{
691 return mError;
692}
693
694void Parcel::setError(status_t err)
695{
696 mError = err;
697}
698
699status_t Parcel::finishWrite(size_t len)
700{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700701 if (len > INT32_MAX) {
702 // don't accept size_t values which may have come from an
703 // inadvertent conversion from a negative int.
704 return BAD_VALUE;
705 }
706
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700707 //printf("Finish write of %d\n", len);
708 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700709 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700710 if (mDataPos > mDataSize) {
711 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700712 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700713 }
714 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
715 return NO_ERROR;
716}
717
718status_t Parcel::writeUnpadded(const void* data, size_t len)
719{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700720 if (len > INT32_MAX) {
721 // don't accept size_t values which may have come from an
722 // inadvertent conversion from a negative int.
723 return BAD_VALUE;
724 }
725
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700726 size_t end = mDataPos + len;
727 if (end < mDataPos) {
728 // integer overflow
729 return BAD_VALUE;
730 }
731
732 if (end <= mDataCapacity) {
733restart_write:
734 memcpy(mData+mDataPos, data, len);
735 return finishWrite(len);
736 }
737
738 status_t err = growData(len);
739 if (err == NO_ERROR) goto restart_write;
740 return err;
741}
742
743status_t Parcel::write(const void* data, size_t len)
744{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700745 if (len > INT32_MAX) {
746 // don't accept size_t values which may have come from an
747 // inadvertent conversion from a negative int.
748 return BAD_VALUE;
749 }
750
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700751 void* const d = writeInplace(len);
752 if (d) {
753 memcpy(d, data, len);
754 return NO_ERROR;
755 }
756 return mError;
757}
758
759void* Parcel::writeInplace(size_t len)
760{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700761 if (len > INT32_MAX) {
762 // don't accept size_t values which may have come from an
763 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700764 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700765 }
766
767 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700768
769 // sanity check for integer overflow
770 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700771 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700772 }
773
774 if ((mDataPos+padded) <= mDataCapacity) {
775restart_write:
776 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
777 uint8_t* const data = mData+mDataPos;
778
779 // Need to pad at end?
780 if (padded != len) {
781#if BYTE_ORDER == BIG_ENDIAN
782 static const uint32_t mask[4] = {
783 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
784 };
785#endif
786#if BYTE_ORDER == LITTLE_ENDIAN
787 static const uint32_t mask[4] = {
788 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
789 };
790#endif
791 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
792 // *reinterpret_cast<void**>(data+padded-4));
793 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
794 }
795
796 finishWrite(padded);
797 return data;
798 }
799
800 status_t err = growData(padded);
801 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700802 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700803}
804
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800805status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
806 const uint8_t* strData = (uint8_t*)str.data();
807 const size_t strLen= str.length();
808 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100809 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800810 return BAD_VALUE;
811 }
812
813 status_t err = writeInt32(utf16Len);
814 if (err) {
815 return err;
816 }
817
818 // Allocate enough bytes to hold our converted string and its terminating NULL.
819 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
820 if (!dst) {
821 return NO_MEMORY;
822 }
823
Sergio Girof4607432016-07-21 14:46:35 +0100824 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800825
826 return NO_ERROR;
827}
828
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900829
Andy Hung49198cf2020-11-18 11:02:39 -0800830status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
831status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800832
Andy Hung49198cf2020-11-18 11:02:39 -0800833status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
834status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700835
Andy Hung49198cf2020-11-18 11:02:39 -0800836status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
837status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
838status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
839status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
840status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
841status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
842status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
843status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
844status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
845status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
846status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
847status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
848status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
849status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
850status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
851status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
852status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
853status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
854status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
855status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
856status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
857status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
858status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
859status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
860status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
861status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
862status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700863
Andy Hung49198cf2020-11-18 11:02:39 -0800864status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800865status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800866 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900867status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800868 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800869status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800870 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900871status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800872 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
873status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800874
Andy Hung49198cf2020-11-18 11:02:39 -0800875status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
876status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
877status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
878
879status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
880status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
881status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
882
883status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
884
885status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
886status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
887
888status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
889status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
890
891status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
892status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
893status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
894status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
895status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
896status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
897status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
898status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
899status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
900status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
901status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
902status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
903status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
904status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
905status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
906status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
907status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
908status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
909status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
910status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
911status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
912status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
913status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
914status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
915status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
916status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
917status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
918
919status_t Parcel::readString16Vector(
920 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
921status_t Parcel::readString16Vector(
922 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
923status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
924status_t Parcel::readUtf8VectorFromUtf16Vector(
925 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
926status_t Parcel::readUtf8VectorFromUtf16Vector(
927 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
928status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
929
930status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
931status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
932status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
933
934status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
935status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
936status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
937
938status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800939
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700940status_t Parcel::writeInt32(int32_t val)
941{
Andreas Huber84a6d042009-08-17 13:33:27 -0700942 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700943}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800944
945status_t Parcel::writeUint32(uint32_t val)
946{
947 return writeAligned(val);
948}
949
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700950status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700951 if (len > INT32_MAX) {
952 // don't accept size_t values which may have come from an
953 // inadvertent conversion from a negative int.
954 return BAD_VALUE;
955 }
956
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700957 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700958 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700959 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700960 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700961 if (ret == NO_ERROR) {
962 ret = write(val, len * sizeof(*val));
963 }
964 return ret;
965}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700966status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700967 if (len > INT32_MAX) {
968 // don't accept size_t values which may have come from an
969 // inadvertent conversion from a negative int.
970 return BAD_VALUE;
971 }
972
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700973 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700974 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700975 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700976 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700977 if (ret == NO_ERROR) {
978 ret = write(val, len * sizeof(*val));
979 }
980 return ret;
981}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700982
Casey Dahlind6848f52015-10-15 15:44:59 -0700983status_t Parcel::writeBool(bool val)
984{
985 return writeInt32(int32_t(val));
986}
987
988status_t Parcel::writeChar(char16_t val)
989{
990 return writeInt32(int32_t(val));
991}
992
993status_t Parcel::writeByte(int8_t val)
994{
995 return writeInt32(int32_t(val));
996}
997
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700998status_t Parcel::writeInt64(int64_t val)
999{
Andreas Huber84a6d042009-08-17 13:33:27 -07001000 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001001}
1002
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001003status_t Parcel::writeUint64(uint64_t val)
1004{
1005 return writeAligned(val);
1006}
1007
Serban Constantinescuf683e012013-11-05 16:53:55 +00001008status_t Parcel::writePointer(uintptr_t val)
1009{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001010 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001011}
1012
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001013status_t Parcel::writeFloat(float val)
1014{
Andreas Huber84a6d042009-08-17 13:33:27 -07001015 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001016}
1017
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001018#if defined(__mips__) && defined(__mips_hard_float)
1019
1020status_t Parcel::writeDouble(double val)
1021{
1022 union {
1023 double d;
1024 unsigned long long ll;
1025 } u;
1026 u.d = val;
1027 return writeAligned(u.ll);
1028}
1029
1030#else
1031
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001032status_t Parcel::writeDouble(double val)
1033{
Andreas Huber84a6d042009-08-17 13:33:27 -07001034 return writeAligned(val);
1035}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001036
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001037#endif
1038
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001039status_t Parcel::writeCString(const char* str)
1040{
1041 return write(str, strlen(str)+1);
1042}
1043
1044status_t Parcel::writeString8(const String8& str)
1045{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001046 return writeString8(str.string(), str.size());
1047}
1048
1049status_t Parcel::writeString8(const char* str, size_t len)
1050{
1051 if (str == nullptr) return writeInt32(-1);
1052
Jeff Sharkey18220902020-11-05 08:36:20 -07001053 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001054 status_t err = writeInt32(len);
1055 if (err == NO_ERROR) {
1056 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1057 if (data) {
1058 memcpy(data, str, len);
1059 *reinterpret_cast<char*>(data+len) = 0;
1060 return NO_ERROR;
1061 }
1062 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001063 }
1064 return err;
1065}
1066
1067status_t Parcel::writeString16(const String16& str)
1068{
1069 return writeString16(str.string(), str.size());
1070}
1071
1072status_t Parcel::writeString16(const char16_t* str, size_t len)
1073{
Yi Kong91635562018-06-07 14:38:36 -07001074 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001075
Jeff Sharkey18220902020-11-05 08:36:20 -07001076 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001077 status_t err = writeInt32(len);
1078 if (err == NO_ERROR) {
1079 len *= sizeof(char16_t);
1080 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1081 if (data) {
1082 memcpy(data, str, len);
1083 *reinterpret_cast<char16_t*>(data+len) = 0;
1084 return NO_ERROR;
1085 }
1086 err = mError;
1087 }
1088 return err;
1089}
1090
1091status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1092{
Steven Morelanda86a3562019-08-01 23:28:34 +00001093 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001094}
1095
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001096
Casey Dahlinb9872622015-11-25 15:09:45 -08001097status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1098 if (!parcelable) {
1099 return writeInt32(0);
1100 }
1101
1102 return writeParcelable(*parcelable);
1103}
1104
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001105status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001106{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001107 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001108 return BAD_TYPE;
1109
1110 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001111 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001112 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001114 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001115 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001117 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1118 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119
1120 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001121 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122 return err;
1123 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001124 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001125 return err;
1126}
1127
Jeff Brown93ff1f92011-11-04 19:01:44 -07001128status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001129{
Steven Moreland5553ac42020-11-11 02:14:45 +00001130 if (isForRpc()) {
1131 ALOGE("Cannot write file descriptor to remote binder.");
1132 return BAD_TYPE;
1133 }
1134
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001135 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001136 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001137 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001138 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001139 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001140 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001141 return writeObject(obj, true);
1142}
1143
1144status_t Parcel::writeDupFileDescriptor(int fd)
1145{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001146 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001147 if (dupFd < 0) {
1148 return -errno;
1149 }
1150 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001151 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001152 close(dupFd);
1153 }
1154 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001155}
1156
Dianne Hackborn1941a402016-08-29 12:30:43 -07001157status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1158{
1159 writeInt32(0);
1160 return writeFileDescriptor(fd, takeOwnership);
1161}
1162
Ryo Hashimotobf551892018-05-31 16:58:35 +09001163status_t Parcel::writeDupParcelFileDescriptor(int fd)
1164{
1165 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1166 if (dupFd < 0) {
1167 return -errno;
1168 }
1169 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1170 if (err != OK) {
1171 close(dupFd);
1172 }
1173 return err;
1174}
1175
Christopher Wiley2cf19952016-04-11 11:09:37 -07001176status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001177 return writeDupFileDescriptor(fd.get());
1178}
1179
Jeff Brown13b16042014-11-11 16:44:25 -08001180status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001181{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001182 if (len > INT32_MAX) {
1183 // don't accept size_t values which may have come from an
1184 // inadvertent conversion from a negative int.
1185 return BAD_VALUE;
1186 }
1187
Jeff Brown13b16042014-11-11 16:44:25 -08001188 status_t status;
1189 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001190 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001191 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001192 if (status) return status;
1193
1194 void* ptr = writeInplace(len);
1195 if (!ptr) return NO_MEMORY;
1196
Jeff Brown13b16042014-11-11 16:44:25 -08001197 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001198 return NO_ERROR;
1199 }
1200
Steve Block6807e592011-10-20 11:56:00 +01001201 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001202 int fd = ashmem_create_region("Parcel Blob", len);
1203 if (fd < 0) return NO_MEMORY;
1204
1205 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1206 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001207 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001208 } else {
Yi Kong91635562018-06-07 14:38:36 -07001209 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001210 if (ptr == MAP_FAILED) {
1211 status = -errno;
1212 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001213 if (!mutableCopy) {
1214 result = ashmem_set_prot_region(fd, PROT_READ);
1215 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001216 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001217 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001218 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001219 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001220 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001221 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001222 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001223 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001224 return NO_ERROR;
1225 }
1226 }
1227 }
1228 }
1229 ::munmap(ptr, len);
1230 }
1231 ::close(fd);
1232 return status;
1233}
1234
Jeff Brown13b16042014-11-11 16:44:25 -08001235status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1236{
1237 // Must match up with what's done in writeBlob.
1238 if (!mAllowFds) return FDS_NOT_ALLOWED;
1239 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1240 if (status) return status;
1241 return writeDupFileDescriptor(fd);
1242}
1243
Mathias Agopiane1424282013-07-29 21:24:40 -07001244status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001245{
1246 status_t err;
1247
1248 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001249 const size_t len = val.getFlattenedSize();
1250 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001251
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001252 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001253 // don't accept size_t values which may have come from an
1254 // inadvertent conversion from a negative int.
1255 return BAD_VALUE;
1256 }
1257
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001258 err = this->writeInt32(len);
1259 if (err) return err;
1260
1261 err = this->writeInt32(fd_count);
1262 if (err) return err;
1263
1264 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001265 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001266 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001267 return BAD_VALUE;
1268
Yi Kong91635562018-06-07 14:38:36 -07001269 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001270 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001271 fds = new (std::nothrow) int[fd_count];
1272 if (fds == nullptr) {
1273 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1274 return BAD_VALUE;
1275 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001276 }
1277
1278 err = val.flatten(buf, len, fds, fd_count);
1279 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1280 err = this->writeDupFileDescriptor( fds[i] );
1281 }
1282
1283 if (fd_count) {
1284 delete [] fds;
1285 }
1286
1287 return err;
1288}
1289
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001290status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1291{
1292 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1293 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1294 if (enoughData && enoughObjects) {
1295restart_write:
1296 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001297
Christopher Tate98e67d32015-06-03 18:44:15 -07001298 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001299 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001300 if (!mAllowFds) {
1301 // fail before modifying our object index
1302 return FDS_NOT_ALLOWED;
1303 }
1304 mHasFds = mFdsKnown = true;
1305 }
1306
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001307 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001308 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001309 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001310 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001311 mObjectsSize++;
1312 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001313
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001314 return finishWrite(sizeof(flat_binder_object));
1315 }
1316
1317 if (!enoughData) {
1318 const status_t err = growData(sizeof(val));
1319 if (err != NO_ERROR) return err;
1320 }
1321 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001322 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1323 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001324 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001325 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001326 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001327 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001328 mObjects = objects;
1329 mObjectsCapacity = newSize;
1330 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001331
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001332 goto restart_write;
1333}
1334
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001335status_t Parcel::writeNoException()
1336{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001337 binder::Status status;
1338 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001339}
1340
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001341status_t Parcel::validateReadData(size_t upperBound) const
1342{
1343 // Don't allow non-object reads on object data
1344 if (mObjectsSorted || mObjectsSize <= 1) {
1345data_sorted:
1346 // Expect to check only against the next object
1347 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1348 // For some reason the current read position is greater than the next object
1349 // hint. Iterate until we find the right object
1350 size_t nextObject = mNextObjectHint;
1351 do {
1352 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1353 // Requested info overlaps with an object
1354 ALOGE("Attempt to read from protected data in Parcel %p", this);
1355 return PERMISSION_DENIED;
1356 }
1357 nextObject++;
1358 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1359 mNextObjectHint = nextObject;
1360 }
1361 return NO_ERROR;
1362 }
1363 // Quickly determine if mObjects is sorted.
1364 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1365 binder_size_t* prevObj = currObj;
1366 while (currObj > mObjects) {
1367 prevObj--;
1368 if(*prevObj > *currObj) {
1369 goto data_unsorted;
1370 }
1371 currObj--;
1372 }
1373 mObjectsSorted = true;
1374 goto data_sorted;
1375
1376data_unsorted:
1377 // Insertion Sort mObjects
1378 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1379 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1380 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1381 binder_size_t temp = *iter0;
1382 binder_size_t* iter1 = iter0 - 1;
1383 while (iter1 >= mObjects && *iter1 > temp) {
1384 *(iter1 + 1) = *iter1;
1385 iter1--;
1386 }
1387 *(iter1 + 1) = temp;
1388 }
1389 mNextObjectHint = 0;
1390 mObjectsSorted = true;
1391 goto data_sorted;
1392}
1393
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001394status_t Parcel::read(void* outData, size_t len) const
1395{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001396 if (len > INT32_MAX) {
1397 // don't accept size_t values which may have come from an
1398 // inadvertent conversion from a negative int.
1399 return BAD_VALUE;
1400 }
1401
1402 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1403 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001404 if (mObjectsSize > 0) {
1405 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001406 if(err != NO_ERROR) {
1407 // Still increment the data position by the expected length
1408 mDataPos += pad_size(len);
1409 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1410 return err;
1411 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001412 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001413 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001414 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001415 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001416 return NO_ERROR;
1417 }
1418 return NOT_ENOUGH_DATA;
1419}
1420
1421const void* Parcel::readInplace(size_t len) const
1422{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001423 if (len > INT32_MAX) {
1424 // don't accept size_t values which may have come from an
1425 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001426 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001427 }
1428
1429 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1430 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001431 if (mObjectsSize > 0) {
1432 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001433 if(err != NO_ERROR) {
1434 // Still increment the data position by the expected length
1435 mDataPos += pad_size(len);
1436 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001437 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001438 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001439 }
1440
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001441 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001442 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001443 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001444 return data;
1445 }
Yi Kong91635562018-06-07 14:38:36 -07001446 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001447}
1448
Andreas Huber84a6d042009-08-17 13:33:27 -07001449template<class T>
1450status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001451 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001452
1453 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001454 if (mObjectsSize > 0) {
1455 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001456 if(err != NO_ERROR) {
1457 // Still increment the data position by the expected length
1458 mDataPos += sizeof(T);
1459 return err;
1460 }
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;
Andreas Huber84a6d042009-08-17 13:33:27 -07001464 mDataPos += sizeof(T);
1465 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001466 return NO_ERROR;
1467 } else {
1468 return NOT_ENOUGH_DATA;
1469 }
1470}
1471
Andreas Huber84a6d042009-08-17 13:33:27 -07001472template<class T>
1473T Parcel::readAligned() const {
1474 T result;
1475 if (readAligned(&result) != NO_ERROR) {
1476 result = 0;
1477 }
1478
1479 return result;
1480}
1481
1482template<class T>
1483status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001484 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001485
1486 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1487restart_write:
1488 *reinterpret_cast<T*>(mData+mDataPos) = val;
1489 return finishWrite(sizeof(val));
1490 }
1491
1492 status_t err = growData(sizeof(val));
1493 if (err == NO_ERROR) goto restart_write;
1494 return err;
1495}
1496
1497status_t Parcel::readInt32(int32_t *pArg) const
1498{
1499 return readAligned(pArg);
1500}
1501
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001502int32_t Parcel::readInt32() const
1503{
Andreas Huber84a6d042009-08-17 13:33:27 -07001504 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001505}
1506
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001507status_t Parcel::readUint32(uint32_t *pArg) const
1508{
1509 return readAligned(pArg);
1510}
1511
1512uint32_t Parcel::readUint32() const
1513{
1514 return readAligned<uint32_t>();
1515}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001516
1517status_t Parcel::readInt64(int64_t *pArg) const
1518{
Andreas Huber84a6d042009-08-17 13:33:27 -07001519 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001520}
1521
1522
1523int64_t Parcel::readInt64() const
1524{
Andreas Huber84a6d042009-08-17 13:33:27 -07001525 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001526}
1527
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001528status_t Parcel::readUint64(uint64_t *pArg) const
1529{
1530 return readAligned(pArg);
1531}
1532
1533uint64_t Parcel::readUint64() const
1534{
1535 return readAligned<uint64_t>();
1536}
1537
Serban Constantinescuf683e012013-11-05 16:53:55 +00001538status_t Parcel::readPointer(uintptr_t *pArg) const
1539{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001540 status_t ret;
1541 binder_uintptr_t ptr;
1542 ret = readAligned(&ptr);
1543 if (!ret)
1544 *pArg = ptr;
1545 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001546}
1547
1548uintptr_t Parcel::readPointer() const
1549{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001550 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001551}
1552
1553
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001554status_t Parcel::readFloat(float *pArg) const
1555{
Andreas Huber84a6d042009-08-17 13:33:27 -07001556 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001557}
1558
1559
1560float Parcel::readFloat() const
1561{
Andreas Huber84a6d042009-08-17 13:33:27 -07001562 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001563}
1564
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001565#if defined(__mips__) && defined(__mips_hard_float)
1566
1567status_t Parcel::readDouble(double *pArg) const
1568{
1569 union {
1570 double d;
1571 unsigned long long ll;
1572 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001573 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001574 status_t status;
1575 status = readAligned(&u.ll);
1576 *pArg = u.d;
1577 return status;
1578}
1579
1580double Parcel::readDouble() const
1581{
1582 union {
1583 double d;
1584 unsigned long long ll;
1585 } u;
1586 u.ll = readAligned<unsigned long long>();
1587 return u.d;
1588}
1589
1590#else
1591
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001592status_t Parcel::readDouble(double *pArg) const
1593{
Andreas Huber84a6d042009-08-17 13:33:27 -07001594 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001595}
1596
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001597double Parcel::readDouble() const
1598{
Andreas Huber84a6d042009-08-17 13:33:27 -07001599 return readAligned<double>();
1600}
1601
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001602#endif
1603
Casey Dahlind6848f52015-10-15 15:44:59 -07001604status_t Parcel::readBool(bool *pArg) const
1605{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001606 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001607 status_t ret = readInt32(&tmp);
1608 *pArg = (tmp != 0);
1609 return ret;
1610}
1611
1612bool Parcel::readBool() const
1613{
1614 return readInt32() != 0;
1615}
1616
1617status_t Parcel::readChar(char16_t *pArg) const
1618{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001619 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001620 status_t ret = readInt32(&tmp);
1621 *pArg = char16_t(tmp);
1622 return ret;
1623}
1624
1625char16_t Parcel::readChar() const
1626{
1627 return char16_t(readInt32());
1628}
1629
1630status_t Parcel::readByte(int8_t *pArg) const
1631{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001632 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001633 status_t ret = readInt32(&tmp);
1634 *pArg = int8_t(tmp);
1635 return ret;
1636}
1637
1638int8_t Parcel::readByte() const
1639{
1640 return int8_t(readInt32());
1641}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001642
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001643status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1644 size_t utf16Size = 0;
1645 const char16_t* src = readString16Inplace(&utf16Size);
1646 if (!src) {
1647 return UNEXPECTED_NULL;
1648 }
1649
1650 // Save ourselves the trouble, we're done.
1651 if (utf16Size == 0u) {
1652 str->clear();
1653 return NO_ERROR;
1654 }
1655
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001656 // Allow for closing '\0'
1657 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1658 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001659 return BAD_VALUE;
1660 }
1661 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001662 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001663 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001664 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1665 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001666 return NO_ERROR;
1667}
1668
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001669const char* Parcel::readCString() const
1670{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001671 if (mDataPos < mDataSize) {
1672 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001673 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1674 // is the string's trailing NUL within the parcel's valid bounds?
1675 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1676 if (eos) {
1677 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001678 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001679 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001680 return str;
1681 }
1682 }
Yi Kong91635562018-06-07 14:38:36 -07001683 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001684}
1685
1686String8 Parcel::readString8() const
1687{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001688 size_t len;
1689 const char* str = readString8Inplace(&len);
1690 if (str) return String8(str, len);
1691 ALOGE("Reading a NULL string not supported here.");
1692 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07001693}
1694
1695status_t Parcel::readString8(String8* pArg) const
1696{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001697 size_t len;
1698 const char* str = readString8Inplace(&len);
1699 if (str) {
1700 pArg->setTo(str, len);
1701 return 0;
1702 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07001703 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001704 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07001705 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001706}
1707
1708const char* Parcel::readString8Inplace(size_t* outLen) const
1709{
1710 int32_t size = readInt32();
1711 // watch for potential int overflow from size+1
1712 if (size >= 0 && size < INT32_MAX) {
1713 *outLen = size;
1714 const char* str = (const char*)readInplace(size+1);
1715 if (str != nullptr) {
Steven Moreland61d0f842020-12-04 21:13:03 +00001716 if (str[size] == '\0') {
1717 return str;
1718 }
1719 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001720 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001721 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001722 *outLen = 0;
1723 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001724}
1725
1726String16 Parcel::readString16() const
1727{
1728 size_t len;
1729 const char16_t* str = readString16Inplace(&len);
1730 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001731 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001732 return String16();
1733}
1734
Casey Dahlinb9872622015-11-25 15:09:45 -08001735
Casey Dahlin451ff582015-10-19 18:12:18 -07001736status_t Parcel::readString16(String16* pArg) const
1737{
1738 size_t len;
1739 const char16_t* str = readString16Inplace(&len);
1740 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001741 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001742 return 0;
1743 } else {
1744 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001745 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001746 }
1747}
1748
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001749const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1750{
1751 int32_t size = readInt32();
1752 // watch for potential int overflow from size+1
1753 if (size >= 0 && size < INT32_MAX) {
1754 *outLen = size;
1755 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001756 if (str != nullptr) {
Steven Moreland61d0f842020-12-04 21:13:03 +00001757 if (str[size] == u'\0') {
1758 return str;
1759 }
1760 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001761 }
1762 }
1763 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001764 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001765}
1766
Casey Dahlinf0c13772015-10-27 18:33:56 -07001767status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1768{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001769 status_t status = readNullableStrongBinder(val);
1770 if (status == OK && !val->get()) {
1771 status = UNEXPECTED_NULL;
1772 }
1773 return status;
1774}
1775
1776status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1777{
Steven Morelanda86a3562019-08-01 23:28:34 +00001778 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001779}
1780
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001781sp<IBinder> Parcel::readStrongBinder() const
1782{
1783 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001784 // Note that a lot of code in Android reads binders by hand with this
1785 // method, and that code has historically been ok with getting nullptr
1786 // back (while ignoring error codes).
1787 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001788 return val;
1789}
1790
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001791int32_t Parcel::readExceptionCode() const
1792{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001793 binder::Status status;
1794 status.readFromParcel(*this);
1795 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001796}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001797
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001798native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001799{
1800 int numFds, numInts;
1801 status_t err;
1802 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001803 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001804 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001805 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001806
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001807 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001808 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001809 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001810 }
1811
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001812 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001813 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001814 if (h->data[i] < 0) {
1815 for (int j = 0; j < i; j++) {
1816 close(h->data[j]);
1817 }
1818 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001819 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001820 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001821 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001822 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001823 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001824 native_handle_close(h);
1825 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001826 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001827 }
1828 return h;
1829}
1830
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001831int Parcel::readFileDescriptor() const
1832{
1833 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001834
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001835 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001836 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001837 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001838
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001839 return BAD_TYPE;
1840}
1841
Dianne Hackborn1941a402016-08-29 12:30:43 -07001842int Parcel::readParcelFileDescriptor() const
1843{
1844 int32_t hasComm = readInt32();
1845 int fd = readFileDescriptor();
1846 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001847 // detach (owned by the binder driver)
1848 int comm = readFileDescriptor();
1849
1850 // warning: this must be kept in sync with:
1851 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1852 enum ParcelFileDescriptorStatus {
1853 DETACHED = 2,
1854 };
1855
1856#if BYTE_ORDER == BIG_ENDIAN
1857 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
1858#endif
1859#if BYTE_ORDER == LITTLE_ENDIAN
1860 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
1861#endif
1862
1863 ssize_t written = TEMP_FAILURE_RETRY(
1864 ::write(comm, &message, sizeof(message)));
1865
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08001866 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001867 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
1868 written, strerror(errno));
1869 return BAD_TYPE;
1870 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07001871 }
1872 return fd;
1873}
1874
Christopher Wiley2cf19952016-04-11 11:09:37 -07001875status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08001876{
1877 int got = readFileDescriptor();
1878
1879 if (got == BAD_TYPE) {
1880 return BAD_TYPE;
1881 }
1882
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001883 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08001884
1885 if (val->get() < 0) {
1886 return BAD_VALUE;
1887 }
1888
1889 return OK;
1890}
1891
Ryo Hashimotobf551892018-05-31 16:58:35 +09001892status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
1893{
1894 int got = readParcelFileDescriptor();
1895
1896 if (got == BAD_TYPE) {
1897 return BAD_TYPE;
1898 }
1899
1900 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
1901
1902 if (val->get() < 0) {
1903 return BAD_VALUE;
1904 }
1905
1906 return OK;
1907}
Casey Dahlin06673e32015-11-23 13:24:23 -08001908
Jeff Brown5707dbf2011-09-23 21:17:56 -07001909status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1910{
Jeff Brown13b16042014-11-11 16:44:25 -08001911 int32_t blobType;
1912 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001913 if (status) return status;
1914
Jeff Brown13b16042014-11-11 16:44:25 -08001915 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001916 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001917 const void* ptr = readInplace(len);
1918 if (!ptr) return BAD_VALUE;
1919
Jeff Brown13b16042014-11-11 16:44:25 -08001920 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001921 return NO_ERROR;
1922 }
1923
Steve Block6807e592011-10-20 11:56:00 +01001924 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001925 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001926 int fd = readFileDescriptor();
1927 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1928
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001929 if (!ashmem_valid(fd)) {
1930 ALOGE("invalid fd");
1931 return BAD_VALUE;
1932 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001933 int size = ashmem_get_size_region(fd);
1934 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001935 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001936 return BAD_VALUE;
1937 }
Yi Kong91635562018-06-07 14:38:36 -07001938 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08001939 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001940 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001941
Jeff Brown13b16042014-11-11 16:44:25 -08001942 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001943 return NO_ERROR;
1944}
1945
Mathias Agopiane1424282013-07-29 21:24:40 -07001946status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001947{
1948 // size
1949 const size_t len = this->readInt32();
1950 const size_t fd_count = this->readInt32();
1951
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001952 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001953 // don't accept size_t values which may have come from an
1954 // inadvertent conversion from a negative int.
1955 return BAD_VALUE;
1956 }
1957
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001958 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001959 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07001960 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001961 return BAD_VALUE;
1962
Yi Kong91635562018-06-07 14:38:36 -07001963 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001964 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001965 fds = new (std::nothrow) int[fd_count];
1966 if (fds == nullptr) {
1967 ALOGE("read: failed to allocate requested %zu fds", fd_count);
1968 return BAD_VALUE;
1969 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001970 }
1971
1972 status_t err = NO_ERROR;
1973 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07001974 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001975 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001976 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001977 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 -07001978 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
1979 // Close all the file descriptors that were dup-ed.
1980 for (size_t j=0; j<i ;j++) {
1981 close(fds[j]);
1982 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001983 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001984 }
1985
1986 if (err == NO_ERROR) {
1987 err = val.unflatten(buf, len, fds, fd_count);
1988 }
1989
1990 if (fd_count) {
1991 delete [] fds;
1992 }
1993
1994 return err;
1995}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001996const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1997{
1998 const size_t DPOS = mDataPos;
1999 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2000 const flat_binder_object* obj
2001 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2002 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002003 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002004 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002005 // the object list, so we don't want to check for it when
2006 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002007 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002008 return obj;
2009 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002010
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002011 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002012 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002013 const size_t N = mObjectsSize;
2014 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002015
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002016 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002017 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002018 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002019
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002020 // Start at the current hint position, looking for an object at
2021 // the current data position.
2022 if (opos < N) {
2023 while (opos < (N-1) && OBJS[opos] < DPOS) {
2024 opos++;
2025 }
2026 } else {
2027 opos = N-1;
2028 }
2029 if (OBJS[opos] == DPOS) {
2030 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002031 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002032 this, DPOS, opos);
2033 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002034 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002035 return obj;
2036 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002037
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002038 // Look backwards for it...
2039 while (opos > 0 && OBJS[opos] > DPOS) {
2040 opos--;
2041 }
2042 if (OBJS[opos] == DPOS) {
2043 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002044 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002045 this, DPOS, opos);
2046 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002047 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002048 return obj;
2049 }
2050 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002051 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 -07002052 this, DPOS);
2053 }
Yi Kong91635562018-06-07 14:38:36 -07002054 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002055}
2056
2057void Parcel::closeFileDescriptors()
2058{
2059 size_t i = mObjectsSize;
2060 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002061 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002062 }
2063 while (i > 0) {
2064 i--;
2065 const flat_binder_object* flat
2066 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002067 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002068 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002069 close(flat->handle);
2070 }
2071 }
2072}
2073
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002074uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002075{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002076 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002077}
2078
2079size_t Parcel::ipcDataSize() const
2080{
2081 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2082}
2083
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002084uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002085{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002086 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002087}
2088
2089size_t Parcel::ipcObjectsCount() const
2090{
2091 return mObjectsSize;
2092}
2093
2094void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Steven Moreland161fe122020-11-12 23:16:47 +00002095 const binder_size_t* objects, size_t objectsCount, release_func relFunc)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002096{
Steven Morelandceed9bb2020-12-17 01:01:06 +00002097 freeData();
2098
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002099 mData = const_cast<uint8_t*>(data);
2100 mDataSize = mDataCapacity = dataSize;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002101 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002102 mObjectsSize = mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002103 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002104
2105 binder_size_t minOffset = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002106 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002107 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002108 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002109 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002110 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002111 mObjectsSize = 0;
2112 break;
2113 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002114 const flat_binder_object* flat
2115 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2116 uint32_t type = flat->hdr.type;
2117 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2118 type == BINDER_TYPE_FD)) {
2119 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2120 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2121 // recover gracefully by clearing out the objects, and releasing the objects we do
2122 // know about.
2123 android_errorWriteLog(0x534e4554, "135930648");
2124 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2125 __func__, type, (uint64_t)offset);
2126 releaseObjects();
2127 mObjectsSize = 0;
2128 break;
2129 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002130 minOffset = offset + sizeof(flat_binder_object);
2131 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002132 scanForFds();
2133}
2134
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002135void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002136{
2137 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002138
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002139 if (errorCheck() != NO_ERROR) {
2140 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002141 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002142 } else if (dataSize() > 0) {
2143 const uint8_t* DATA = data();
2144 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002145 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002146 const size_t N = objectsCount();
2147 for (size_t i=0; i<N; i++) {
2148 const flat_binder_object* flat
2149 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2150 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002151 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002152 << " = " << flat->binder;
2153 }
2154 } else {
2155 to << "NULL";
2156 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002157
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002158 to << ")";
2159}
2160
2161void Parcel::releaseObjects()
2162{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002163 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002164 if (i == 0) {
2165 return;
2166 }
2167 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002168 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002169 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002170 while (i > 0) {
2171 i--;
2172 const flat_binder_object* flat
2173 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002174 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002175 }
2176}
2177
2178void Parcel::acquireObjects()
2179{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002180 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002181 if (i == 0) {
2182 return;
2183 }
2184 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002185 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002186 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002187 while (i > 0) {
2188 i--;
2189 const flat_binder_object* flat
2190 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002191 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002192 }
2193}
2194
2195void Parcel::freeData()
2196{
2197 freeDataNoInit();
2198 initState();
2199}
2200
2201void Parcel::freeDataNoInit()
2202{
2203 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002204 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002205 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002206 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002207 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002208 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002209 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002210 if (mData) {
2211 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002212 gParcelGlobalAllocSize -= mDataCapacity;
2213 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002214 if (mDeallocZero) {
2215 zeroMemory(mData, mDataSize);
2216 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002217 free(mData);
2218 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002219 if (mObjects) free(mObjects);
2220 }
2221}
2222
2223status_t Parcel::growData(size_t len)
2224{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002225 if (len > INT32_MAX) {
2226 // don't accept size_t values which may have come from an
2227 // inadvertent conversion from a negative int.
2228 return BAD_VALUE;
2229 }
2230
Martijn Coenen93fe5182020-01-22 10:46:25 +01002231 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2232 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002233 size_t newSize = ((mDataSize+len)*3)/2;
2234 return (newSize <= mDataSize)
2235 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002236 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002237}
2238
Steven Morelandf183fdd2020-10-27 00:12:12 +00002239static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2240 if (!zero) {
2241 return (uint8_t*)realloc(data, newCapacity);
2242 }
2243 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2244 if (!newData) {
2245 return nullptr;
2246 }
2247
2248 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2249 zeroMemory(data, oldCapacity);
2250 free(data);
2251 return newData;
2252}
2253
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002254status_t Parcel::restartWrite(size_t desired)
2255{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002256 if (desired > INT32_MAX) {
2257 // don't accept size_t values which may have come from an
2258 // inadvertent conversion from a negative int.
2259 return BAD_VALUE;
2260 }
2261
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002262 if (mOwner) {
2263 freeData();
2264 return continueWrite(desired);
2265 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002266
Steven Morelandf183fdd2020-10-27 00:12:12 +00002267 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002268 if (!data && desired > mDataCapacity) {
2269 mError = NO_MEMORY;
2270 return NO_MEMORY;
2271 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002272
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002273 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002274
Devin Moore4a0a55e2020-06-04 13:23:10 -07002275 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002276 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002277 if (mDataCapacity > desired) {
2278 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2279 } else {
2280 gParcelGlobalAllocSize += (desired - mDataCapacity);
2281 }
2282
Colin Cross83ec65e2015-12-08 17:15:50 -08002283 if (!mData) {
2284 gParcelGlobalAllocCount++;
2285 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002286 mData = data;
2287 mDataCapacity = desired;
2288 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002289
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002290 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002291 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2292 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2293
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002294 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002295 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002296 mObjectsSize = mObjectsCapacity = 0;
2297 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002298 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002299 mHasFds = false;
2300 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002301 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002302
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002303 return NO_ERROR;
2304}
2305
2306status_t Parcel::continueWrite(size_t desired)
2307{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002308 if (desired > INT32_MAX) {
2309 // don't accept size_t values which may have come from an
2310 // inadvertent conversion from a negative int.
2311 return BAD_VALUE;
2312 }
2313
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002314 // If shrinking, first adjust for any objects that appear
2315 // after the new data size.
2316 size_t objectsSize = mObjectsSize;
2317 if (desired < mDataSize) {
2318 if (desired == 0) {
2319 objectsSize = 0;
2320 } else {
2321 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002322 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002323 break;
2324 objectsSize--;
2325 }
2326 }
2327 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002328
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002329 if (mOwner) {
2330 // If the size is going to zero, just release the owner's data.
2331 if (desired == 0) {
2332 freeData();
2333 return NO_ERROR;
2334 }
2335
2336 // If there is a different owner, we need to take
2337 // posession.
2338 uint8_t* data = (uint8_t*)malloc(desired);
2339 if (!data) {
2340 mError = NO_MEMORY;
2341 return NO_MEMORY;
2342 }
Yi Kong91635562018-06-07 14:38:36 -07002343 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002344
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002345 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002346 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002347 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002348 free(data);
2349
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002350 mError = NO_MEMORY;
2351 return NO_MEMORY;
2352 }
2353
2354 // Little hack to only acquire references on objects
2355 // we will be keeping.
2356 size_t oldObjectsSize = mObjectsSize;
2357 mObjectsSize = objectsSize;
2358 acquireObjects();
2359 mObjectsSize = oldObjectsSize;
2360 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002361
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002362 if (mData) {
2363 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2364 }
2365 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002366 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002367 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002368 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002369 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
Yi Kong91635562018-06-07 14:38:36 -07002370 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002371
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002372 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002373 gParcelGlobalAllocSize += desired;
2374 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002375
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002376 mData = data;
2377 mObjects = objects;
2378 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002379 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002380 mDataCapacity = desired;
2381 mObjectsSize = mObjectsCapacity = objectsSize;
2382 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002383 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002384
2385 } else if (mData) {
2386 if (objectsSize < mObjectsSize) {
2387 // Need to release refs on any objects we are dropping.
2388 const sp<ProcessState> proc(ProcessState::self());
2389 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2390 const flat_binder_object* flat
2391 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002392 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002393 // will need to rescan because we may have lopped off the only FDs
2394 mFdsKnown = false;
2395 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002396 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002397 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002398
2399 if (objectsSize == 0) {
2400 free(mObjects);
2401 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002402 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002403 } else {
2404 binder_size_t* objects =
2405 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2406 if (objects) {
2407 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002408 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002409 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002410 }
2411 mObjectsSize = objectsSize;
2412 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002413 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002414 }
2415
2416 // We own the data, so we can just do a realloc().
2417 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002418 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002419 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002420 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2421 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002422 gParcelGlobalAllocSize += desired;
2423 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002424 mData = data;
2425 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002426 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427 mError = NO_MEMORY;
2428 return NO_MEMORY;
2429 }
2430 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002431 if (mDataSize > desired) {
2432 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002433 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002434 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435 if (mDataPos > desired) {
2436 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002437 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002438 }
2439 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002440
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002441 } else {
2442 // This is the first data. Easy!
2443 uint8_t* data = (uint8_t*)malloc(desired);
2444 if (!data) {
2445 mError = NO_MEMORY;
2446 return NO_MEMORY;
2447 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002448
Yi Kong91635562018-06-07 14:38:36 -07002449 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002450 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002451 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002452 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002453
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002454 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002455 gParcelGlobalAllocSize += desired;
2456 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002457
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002458 mData = data;
2459 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002460 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2461 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002462 mDataCapacity = desired;
2463 }
2464
2465 return NO_ERROR;
2466}
2467
2468void Parcel::initState()
2469{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002470 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002471 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002472 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002473 mDataSize = 0;
2474 mDataCapacity = 0;
2475 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002476 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2477 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Steven Moreland5553ac42020-11-11 02:14:45 +00002478 mConnection = nullptr;
Yi Kong91635562018-06-07 14:38:36 -07002479 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002480 mObjectsSize = 0;
2481 mObjectsCapacity = 0;
2482 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002483 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002484 mHasFds = false;
2485 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002486 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002487 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07002488 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002489 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002490 mWorkSourceRequestHeaderPosition = 0;
2491 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002492
2493 // racing multiple init leads only to multiple identical write
2494 if (gMaxFds == 0) {
2495 struct rlimit result;
2496 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2497 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002498 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002499 } else {
2500 ALOGW("Unable to getrlimit: %s", strerror(errno));
2501 gMaxFds = 1024;
2502 }
2503 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002504}
2505
2506void Parcel::scanForFds() const
2507{
2508 bool hasFds = false;
2509 for (size_t i=0; i<mObjectsSize; i++) {
2510 const flat_binder_object* flat
2511 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002512 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513 hasFds = true;
2514 break;
2515 }
2516 }
2517 mHasFds = hasFds;
2518 mFdsKnown = true;
2519}
2520
Dan Sandleraa5c2342015-04-10 10:08:45 -04002521size_t Parcel::getBlobAshmemSize() const
2522{
Adrian Roos6bb31142015-10-22 16:46:12 -07002523 // This used to return the size of all blobs that were written to ashmem, now we're returning
2524 // the ashmem currently referenced by this Parcel, which should be equivalent.
2525 // TODO: Remove method once ABI can be changed.
2526 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002527}
2528
Adrian Rooscbf37262015-10-22 16:12:53 -07002529size_t Parcel::getOpenAshmemSize() const
2530{
2531 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002532}
2533
2534// --- Parcel::Blob ---
2535
2536Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002537 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002538}
2539
2540Parcel::Blob::~Blob() {
2541 release();
2542}
2543
2544void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002545 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002546 ::munmap(mData, mSize);
2547 }
2548 clear();
2549}
2550
Jeff Brown13b16042014-11-11 16:44:25 -08002551void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2552 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002553 mData = data;
2554 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002555 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002556}
2557
2558void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002559 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002560 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002561 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002562 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002563}
2564
Steven Moreland61ff8492019-09-26 16:05:45 -07002565} // namespace android