blob: 8bed621ab466462d068a15d78dd07eeb9d4bcc51 [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 Morelanda4853cd2019-07-12 15:44:37 -070051#include "Static.h"
Steven Morelandf183fdd2020-10-27 00:12:12 +000052#include "Utils.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070054#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080055//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080056#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080057//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070058
59// ---------------------------------------------------------------------------
60
Nick Kralevichb6b14232015-04-02 09:36:02 -070061// This macro should never be used at runtime, as a too large value
62// of s could cause an integer overflow. Instead, you should always
63// use the wrapper function pad_size()
64#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
65
66static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070067 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070068 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070069 }
70 return PAD_SIZE_UNSAFE(s);
71}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070072
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070073// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060074#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070075
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070076namespace android {
77
Steven Moreland7b102262019-08-01 15:48:43 -070078// many things compile this into prebuilts on the stack
79static_assert(sizeof(Parcel) == 60 || sizeof(Parcel) == 120);
80
Jeff Sharkey8994c182020-09-11 12:07:10 -060081static std::atomic<size_t> gParcelGlobalAllocCount;
82static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -080083
Christopher Tatee4e0ae82016-03-24 16:03:44 -070084static size_t gMaxFds = 0;
85
Jeff Brown13b16042014-11-11 16:44:25 -080086// Maximum size of a blob to transfer in-place.
87static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
88
89enum {
90 BLOB_INPLACE = 0,
91 BLOB_ASHMEM_IMMUTABLE = 1,
92 BLOB_ASHMEM_MUTABLE = 2,
93};
94
Steven Morelandb1c81202019-04-05 18:49:55 -070095static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070096 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070097{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -070098 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070099 case BINDER_TYPE_BINDER:
100 if (obj.binder) {
101 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800102 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700103 }
104 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700105 case BINDER_TYPE_HANDLE: {
106 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700107 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
109 b->incStrong(who);
110 }
111 return;
112 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700113 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000114 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700115 // If we own an ashmem fd, keep track of how much memory it refers to.
116 int size = ashmem_get_size_region(obj.handle);
117 if (size > 0) {
118 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700119 }
120 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121 return;
122 }
123 }
124
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700125 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700126}
127
Adrian Roos6bb31142015-10-22 16:46:12 -0700128static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700129 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700130{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700131 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700132 case BINDER_TYPE_BINDER:
133 if (obj.binder) {
134 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800135 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136 }
137 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700138 case BINDER_TYPE_HANDLE: {
139 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700140 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700141 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
142 b->decStrong(who);
143 }
144 return;
145 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700146 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800147 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000148 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700149 int size = ashmem_get_size_region(obj.handle);
150 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800151 // ashmem size might have changed since last time it was accounted for, e.g.
152 // in acquire_object(). Value of *outAshmemSize is not critical since we are
153 // releasing the object anyway. Check for integer overflow condition.
154 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700155 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700156 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800157
158 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700159 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 return;
161 }
162 }
163
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700164 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165}
166
Steven Moreland34b48cb2020-12-01 22:45:38 +0000167status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700168{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700169 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland89ddfc52020-11-13 02:39:26 +0000170 auto category = internal::Stability::getCategory(binder.get());
171 return writeInt32(category.repr());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700172}
173
Steven Morelanda86a3562019-08-01 23:28:34 +0000174status_t Parcel::finishUnflattenBinder(
175 const sp<IBinder>& binder, sp<IBinder>* out) const
176{
177 int32_t stability;
178 status_t status = readInt32(&stability);
179 if (status != OK) return status;
180
Steven Moreland89ddfc52020-11-13 02:39:26 +0000181 status = internal::Stability::setRepr(binder.get(), stability, true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000182 if (status != OK) return status;
183
184 *out = binder;
185 return OK;
186}
187
Steven Morelandbf1915b2020-07-16 22:43:02 +0000188static constexpr inline int schedPolicyMask(int policy, int priority) {
189 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
190}
191
Steven Morelanda86a3562019-08-01 23:28:34 +0000192status_t Parcel::flattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700193{
194 flat_binder_object obj;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000195 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700196
Steven Morelandbf1915b2020-07-16 22:43:02 +0000197 int schedBits = 0;
198 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
199 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700200 }
201
Yi Kong91635562018-06-07 14:38:36 -0700202 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800203 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700204 if (!local) {
205 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700206 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000207 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700208 }
Steven Morelande8393882020-12-18 02:27:20 +0000209 const int32_t handle = proxy ? proxy->getPrivateAccessorForHandle().handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700210 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800211 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700212 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800213 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700214 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000215 int policy = local->getMinSchedulerPolicy();
216 int priority = local->getMinSchedulerPriority();
217
218 if (policy != 0 || priority != 0) {
219 // override value, since it is set explicitly
220 schedBits = schedPolicyMask(policy, priority);
221 }
Steven Morelandf0212002018-12-26 13:59:23 -0800222 if (local->isRequestingSid()) {
223 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
224 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000225 if (local->isInheritRt()) {
226 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
227 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700228 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800229 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
230 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 }
232 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700233 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800234 obj.binder = 0;
235 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700236 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700237
Steven Morelandbf1915b2020-07-16 22:43:02 +0000238 obj.flags |= schedBits;
239
Steven Moreland34b48cb2020-12-01 22:45:38 +0000240 status_t status = writeObject(obj, false);
241 if (status != OK) return status;
242
243 return finishFlattenBinder(binder);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700244}
245
Steven Morelanda86a3562019-08-01 23:28:34 +0000246status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700247{
Steven Morelanda86a3562019-08-01 23:28:34 +0000248 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700249
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700250 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700251 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000252 case BINDER_TYPE_BINDER: {
253 sp<IBinder> binder = reinterpret_cast<IBinder*>(flat->cookie);
254 return finishUnflattenBinder(binder, out);
255 }
256 case BINDER_TYPE_HANDLE: {
257 sp<IBinder> binder =
258 ProcessState::self()->getStrongProxyForHandle(flat->handle);
259 return finishUnflattenBinder(binder, out);
260 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700261 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700262 }
263 return BAD_TYPE;
264}
265
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700266// ---------------------------------------------------------------------------
267
268Parcel::Parcel()
269{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800270 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700271 initState();
272}
273
274Parcel::~Parcel()
275{
276 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800277 LOG_ALLOC("Parcel %p: destroyed", this);
278}
279
280size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600281 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800282}
283
284size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600285 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700286}
287
288const uint8_t* Parcel::data() const
289{
290 return mData;
291}
292
293size_t Parcel::dataSize() const
294{
295 return (mDataSize > mDataPos ? mDataSize : mDataPos);
296}
297
298size_t Parcel::dataAvail() const
299{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700300 size_t result = dataSize() - dataPosition();
301 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700302 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700303 }
304 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700305}
306
307size_t Parcel::dataPosition() const
308{
309 return mDataPos;
310}
311
312size_t Parcel::dataCapacity() const
313{
314 return mDataCapacity;
315}
316
317status_t Parcel::setDataSize(size_t size)
318{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700319 if (size > INT32_MAX) {
320 // don't accept size_t values which may have come from an
321 // inadvertent conversion from a negative int.
322 return BAD_VALUE;
323 }
324
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700325 status_t err;
326 err = continueWrite(size);
327 if (err == NO_ERROR) {
328 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700329 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700330 }
331 return err;
332}
333
334void Parcel::setDataPosition(size_t pos) const
335{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700336 if (pos > INT32_MAX) {
337 // don't accept size_t values which may have come from an
338 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700339 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700340 }
341
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700342 mDataPos = pos;
343 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800344 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700345}
346
347status_t Parcel::setDataCapacity(size_t size)
348{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700349 if (size > INT32_MAX) {
350 // don't accept size_t values which may have come from an
351 // inadvertent conversion from a negative int.
352 return BAD_VALUE;
353 }
354
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700355 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700356 return NO_ERROR;
357}
358
359status_t Parcel::setData(const uint8_t* buffer, size_t len)
360{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700361 if (len > 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 = restartWrite(len);
368 if (err == NO_ERROR) {
369 memcpy(const_cast<uint8_t*>(data()), buffer, len);
370 mDataSize = len;
371 mFdsKnown = false;
372 }
373 return err;
374}
375
Andreas Huber51faf462011-04-13 10:21:56 -0700376status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700377{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700378 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700379 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800380 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700381 size_t size = parcel->mObjectsSize;
382 int startPos = mDataPos;
383 int firstIndex = -1, lastIndex = -2;
384
385 if (len == 0) {
386 return NO_ERROR;
387 }
388
Nick Kralevichb6b14232015-04-02 09:36:02 -0700389 if (len > INT32_MAX) {
390 // don't accept size_t values which may have come from an
391 // inadvertent conversion from a negative int.
392 return BAD_VALUE;
393 }
394
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700395 // range checks against the source parcel size
396 if ((offset > parcel->mDataSize)
397 || (len > parcel->mDataSize)
398 || (offset + len > parcel->mDataSize)) {
399 return BAD_VALUE;
400 }
401
402 // Count objects in range
403 for (int i = 0; i < (int) size; i++) {
404 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700405 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700406 if (firstIndex == -1) {
407 firstIndex = i;
408 }
409 lastIndex = i;
410 }
411 }
412 int numObjects = lastIndex - firstIndex + 1;
413
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700414 if ((mDataSize+len) > mDataCapacity) {
415 // grow data
416 err = growData(len);
417 if (err != NO_ERROR) {
418 return err;
419 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700420 }
421
422 // append data
423 memcpy(mData + mDataPos, data + offset, len);
424 mDataPos += len;
425 mDataSize += len;
426
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400427 err = NO_ERROR;
428
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700429 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200430 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700431 // grow objects
432 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +0100433 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
434 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700435 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +0100436 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800437 binder_size_t *objects =
438 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700439 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700440 return NO_MEMORY;
441 }
442 mObjects = objects;
443 mObjectsCapacity = newSize;
444 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700445
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700446 // append and acquire objects
447 int idx = mObjectsSize;
448 for (int i = firstIndex; i <= lastIndex; i++) {
449 size_t off = objects[i] - offset + startPos;
450 mObjects[idx++] = off;
451 mObjectsSize++;
452
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700453 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700454 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700455 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700456
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700457 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700458 // If this is a file descriptor, we need to dup it so the
459 // new Parcel now owns its own fd, and can declare that we
460 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800461 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800462 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700463 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400464 if (!mAllowFds) {
465 err = FDS_NOT_ALLOWED;
466 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700467 }
468 }
469 }
470
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400471 return err;
472}
473
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700474int Parcel::compareData(const Parcel& other) {
475 size_t size = dataSize();
476 if (size != other.dataSize()) {
477 return size < other.dataSize() ? -1 : 1;
478 }
479 return memcmp(data(), other.data(), size);
480}
481
Jeff Brown13b16042014-11-11 16:44:25 -0800482bool Parcel::allowFds() const
483{
484 return mAllowFds;
485}
486
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700487bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400488{
489 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700490 if (!allowFds) {
491 mAllowFds = false;
492 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400493 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700494}
495
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700496void Parcel::restoreAllowFds(bool lastValue)
497{
498 mAllowFds = lastValue;
499}
500
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700501bool Parcel::hasFileDescriptors() const
502{
503 if (!mFdsKnown) {
504 scanForFds();
505 }
506 return mHasFds;
507}
508
Steven Morelandf183fdd2020-10-27 00:12:12 +0000509void Parcel::markSensitive() const
510{
511 mDeallocZero = true;
512}
513
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000514void Parcel::updateWorkSourceRequestHeaderPosition() const {
515 // Only update the request headers once. We only want to point
516 // to the first headers read/written.
517 if (!mRequestHeaderPresent) {
518 mWorkSourceRequestHeaderPosition = dataPosition();
519 mRequestHeaderPresent = true;
520 }
521}
522
Jooyung Han1b228f42020-01-30 13:41:12 +0900523#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700524constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
525#else
526constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
527#endif
528
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700529// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700530status_t Parcel::writeInterfaceToken(const String16& interface)
531{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000532 return writeInterfaceToken(interface.string(), interface.size());
533}
534
535status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Olivier Gaillard91a04802018-11-14 17:32:41 +0000536 const IPCThreadState* threadState = IPCThreadState::self();
537 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000538 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000539 writeInt32(threadState->shouldPropagateWorkSource() ?
540 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700541 writeInt32(kHeader);
Steven Morelanddbc76c72020-10-01 18:02:48 +0000542
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000544 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700545}
546
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000547bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
548{
549 if (!mRequestHeaderPresent) {
550 return false;
551 }
552
553 const size_t initialPosition = dataPosition();
554 setDataPosition(mWorkSourceRequestHeaderPosition);
555 status_t err = writeInt32(uid);
556 setDataPosition(initialPosition);
557 return err == NO_ERROR;
558}
559
Steven Morelandf1b1e492019-05-06 15:05:13 -0700560uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000561{
562 if (!mRequestHeaderPresent) {
563 return IPCThreadState::kUnsetWorkSource;
564 }
565
566 const size_t initialPosition = dataPosition();
567 setDataPosition(mWorkSourceRequestHeaderPosition);
568 uid_t uid = readInt32();
569 setDataPosition(initialPosition);
570 return uid;
571}
572
Mathias Agopian83c04462009-05-22 19:00:22 -0700573bool Parcel::checkInterface(IBinder* binder) const
574{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700575 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700576}
577
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700578bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700579 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700580{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700581 return enforceInterface(interface.string(), interface.size(), threadState);
582}
583
584bool Parcel::enforceInterface(const char16_t* interface,
585 size_t len,
586 IPCThreadState* threadState) const
587{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100588 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700589 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700590 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700591 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700592 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700593 if ((threadState->getLastTransactionBinderFlags() &
594 IBinder::FLAG_ONEWAY) != 0) {
595 // For one-way calls, the callee is running entirely
596 // disconnected from the caller, so disable StrictMode entirely.
597 // Not only does disk/network usage not impact the caller, but
598 // there's no way to commuicate back any violations anyway.
599 threadState->setStrictModePolicy(0);
600 } else {
601 threadState->setStrictModePolicy(strictPolicy);
602 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100603 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000604 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100605 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000606 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700607 // vendor header
608 int32_t header = readInt32();
609 if (header != kHeader) {
610 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
611 return false;
612 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100613 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700614 size_t parcel_interface_len;
615 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
616 if (len == parcel_interface_len &&
617 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700618 return true;
619 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700620 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700621 String8(interface, len).string(),
622 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700623 return false;
624 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700625}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700626
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700627size_t Parcel::objectsCount() const
628{
629 return mObjectsSize;
630}
631
632status_t Parcel::errorCheck() const
633{
634 return mError;
635}
636
637void Parcel::setError(status_t err)
638{
639 mError = err;
640}
641
642status_t Parcel::finishWrite(size_t len)
643{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700644 if (len > INT32_MAX) {
645 // don't accept size_t values which may have come from an
646 // inadvertent conversion from a negative int.
647 return BAD_VALUE;
648 }
649
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700650 //printf("Finish write of %d\n", len);
651 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700652 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700653 if (mDataPos > mDataSize) {
654 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700655 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700656 }
657 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
658 return NO_ERROR;
659}
660
661status_t Parcel::writeUnpadded(const void* data, size_t len)
662{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700663 if (len > INT32_MAX) {
664 // don't accept size_t values which may have come from an
665 // inadvertent conversion from a negative int.
666 return BAD_VALUE;
667 }
668
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700669 size_t end = mDataPos + len;
670 if (end < mDataPos) {
671 // integer overflow
672 return BAD_VALUE;
673 }
674
675 if (end <= mDataCapacity) {
676restart_write:
677 memcpy(mData+mDataPos, data, len);
678 return finishWrite(len);
679 }
680
681 status_t err = growData(len);
682 if (err == NO_ERROR) goto restart_write;
683 return err;
684}
685
686status_t Parcel::write(const void* data, size_t len)
687{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700688 if (len > INT32_MAX) {
689 // don't accept size_t values which may have come from an
690 // inadvertent conversion from a negative int.
691 return BAD_VALUE;
692 }
693
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700694 void* const d = writeInplace(len);
695 if (d) {
696 memcpy(d, data, len);
697 return NO_ERROR;
698 }
699 return mError;
700}
701
702void* Parcel::writeInplace(size_t len)
703{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700704 if (len > INT32_MAX) {
705 // don't accept size_t values which may have come from an
706 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700707 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700708 }
709
710 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700711
712 // sanity check for integer overflow
713 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700714 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700715 }
716
717 if ((mDataPos+padded) <= mDataCapacity) {
718restart_write:
719 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
720 uint8_t* const data = mData+mDataPos;
721
722 // Need to pad at end?
723 if (padded != len) {
724#if BYTE_ORDER == BIG_ENDIAN
725 static const uint32_t mask[4] = {
726 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
727 };
728#endif
729#if BYTE_ORDER == LITTLE_ENDIAN
730 static const uint32_t mask[4] = {
731 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
732 };
733#endif
734 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
735 // *reinterpret_cast<void**>(data+padded-4));
736 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
737 }
738
739 finishWrite(padded);
740 return data;
741 }
742
743 status_t err = growData(padded);
744 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700745 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700746}
747
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800748status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
749 const uint8_t* strData = (uint8_t*)str.data();
750 const size_t strLen= str.length();
751 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100752 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800753 return BAD_VALUE;
754 }
755
756 status_t err = writeInt32(utf16Len);
757 if (err) {
758 return err;
759 }
760
761 // Allocate enough bytes to hold our converted string and its terminating NULL.
762 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
763 if (!dst) {
764 return NO_MEMORY;
765 }
766
Sergio Girof4607432016-07-21 14:46:35 +0100767 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800768
769 return NO_ERROR;
770}
771
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900772
Andy Hung49198cf2020-11-18 11:02:39 -0800773status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
774status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800775
Andy Hung49198cf2020-11-18 11:02:39 -0800776status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
777status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700778
Andy Hung49198cf2020-11-18 11:02:39 -0800779status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
780status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
781status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
782status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
783status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
784status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
785status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
786status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
787status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
788status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
789status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
790status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
791status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
792status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
793status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
794status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
795status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
796status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
797status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
798status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
799status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
800status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
801status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
802status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
803status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
804status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
805status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700806
Andy Hung49198cf2020-11-18 11:02:39 -0800807status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800808status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800809 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900810status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800811 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800812status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800813 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900814status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800815 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
816status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800817
Andy Hung49198cf2020-11-18 11:02:39 -0800818status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
819status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
820status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
821
822status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
823status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
824status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
825
826status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
827
828status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
829status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
830
831status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
832status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
833
834status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
835status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
836status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
837status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
838status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
839status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
840status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
841status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
842status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
843status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
844status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
845status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
846status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
847status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
848status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
849status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
850status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
851status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
852status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
853status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
854status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
855status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
856status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
857status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
858status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
859status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
860status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
861
862status_t Parcel::readString16Vector(
863 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
864status_t Parcel::readString16Vector(
865 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
866status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
867status_t Parcel::readUtf8VectorFromUtf16Vector(
868 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
869status_t Parcel::readUtf8VectorFromUtf16Vector(
870 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
871status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
872
873status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
874status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
875status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
876
877status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
878status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
879status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
880
881status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800882
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700883status_t Parcel::writeInt32(int32_t val)
884{
Andreas Huber84a6d042009-08-17 13:33:27 -0700885 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700886}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800887
888status_t Parcel::writeUint32(uint32_t val)
889{
890 return writeAligned(val);
891}
892
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700893status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700894 if (len > INT32_MAX) {
895 // don't accept size_t values which may have come from an
896 // inadvertent conversion from a negative int.
897 return BAD_VALUE;
898 }
899
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700900 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700901 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700902 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700903 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700904 if (ret == NO_ERROR) {
905 ret = write(val, len * sizeof(*val));
906 }
907 return ret;
908}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700909status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700910 if (len > INT32_MAX) {
911 // don't accept size_t values which may have come from an
912 // inadvertent conversion from a negative int.
913 return BAD_VALUE;
914 }
915
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700916 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700917 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700918 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700919 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700920 if (ret == NO_ERROR) {
921 ret = write(val, len * sizeof(*val));
922 }
923 return ret;
924}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700925
Casey Dahlind6848f52015-10-15 15:44:59 -0700926status_t Parcel::writeBool(bool val)
927{
928 return writeInt32(int32_t(val));
929}
930
931status_t Parcel::writeChar(char16_t val)
932{
933 return writeInt32(int32_t(val));
934}
935
936status_t Parcel::writeByte(int8_t val)
937{
938 return writeInt32(int32_t(val));
939}
940
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700941status_t Parcel::writeInt64(int64_t val)
942{
Andreas Huber84a6d042009-08-17 13:33:27 -0700943 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700944}
945
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700946status_t Parcel::writeUint64(uint64_t val)
947{
948 return writeAligned(val);
949}
950
Serban Constantinescuf683e012013-11-05 16:53:55 +0000951status_t Parcel::writePointer(uintptr_t val)
952{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800953 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000954}
955
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700956status_t Parcel::writeFloat(float val)
957{
Andreas Huber84a6d042009-08-17 13:33:27 -0700958 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700959}
960
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800961#if defined(__mips__) && defined(__mips_hard_float)
962
963status_t Parcel::writeDouble(double val)
964{
965 union {
966 double d;
967 unsigned long long ll;
968 } u;
969 u.d = val;
970 return writeAligned(u.ll);
971}
972
973#else
974
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700975status_t Parcel::writeDouble(double val)
976{
Andreas Huber84a6d042009-08-17 13:33:27 -0700977 return writeAligned(val);
978}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700979
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800980#endif
981
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700982status_t Parcel::writeCString(const char* str)
983{
984 return write(str, strlen(str)+1);
985}
986
987status_t Parcel::writeString8(const String8& str)
988{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -0600989 return writeString8(str.string(), str.size());
990}
991
992status_t Parcel::writeString8(const char* str, size_t len)
993{
994 if (str == nullptr) return writeInt32(-1);
995
Jeff Sharkey18220902020-11-05 08:36:20 -0700996 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -0600997 status_t err = writeInt32(len);
998 if (err == NO_ERROR) {
999 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1000 if (data) {
1001 memcpy(data, str, len);
1002 *reinterpret_cast<char*>(data+len) = 0;
1003 return NO_ERROR;
1004 }
1005 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001006 }
1007 return err;
1008}
1009
1010status_t Parcel::writeString16(const String16& str)
1011{
1012 return writeString16(str.string(), str.size());
1013}
1014
1015status_t Parcel::writeString16(const char16_t* str, size_t len)
1016{
Yi Kong91635562018-06-07 14:38:36 -07001017 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001018
Jeff Sharkey18220902020-11-05 08:36:20 -07001019 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001020 status_t err = writeInt32(len);
1021 if (err == NO_ERROR) {
1022 len *= sizeof(char16_t);
1023 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1024 if (data) {
1025 memcpy(data, str, len);
1026 *reinterpret_cast<char16_t*>(data+len) = 0;
1027 return NO_ERROR;
1028 }
1029 err = mError;
1030 }
1031 return err;
1032}
1033
1034status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1035{
Steven Morelanda86a3562019-08-01 23:28:34 +00001036 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001037}
1038
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001039
Casey Dahlinb9872622015-11-25 15:09:45 -08001040status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1041 if (!parcelable) {
1042 return writeInt32(0);
1043 }
1044
1045 return writeParcelable(*parcelable);
1046}
1047
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001048status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001049{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001050 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001051 return BAD_TYPE;
1052
1053 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001054 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001055 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001057 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001058 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001060 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1061 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062
1063 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001064 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065 return err;
1066 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001067 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001068 return err;
1069}
1070
Jeff Brown93ff1f92011-11-04 19:01:44 -07001071status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001072{
1073 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001074 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001075 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001076 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001077 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001078 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001079 return writeObject(obj, true);
1080}
1081
1082status_t Parcel::writeDupFileDescriptor(int fd)
1083{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001084 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001085 if (dupFd < 0) {
1086 return -errno;
1087 }
1088 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001089 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001090 close(dupFd);
1091 }
1092 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001093}
1094
Dianne Hackborn1941a402016-08-29 12:30:43 -07001095status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1096{
1097 writeInt32(0);
1098 return writeFileDescriptor(fd, takeOwnership);
1099}
1100
Ryo Hashimotobf551892018-05-31 16:58:35 +09001101status_t Parcel::writeDupParcelFileDescriptor(int fd)
1102{
1103 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1104 if (dupFd < 0) {
1105 return -errno;
1106 }
1107 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1108 if (err != OK) {
1109 close(dupFd);
1110 }
1111 return err;
1112}
1113
Christopher Wiley2cf19952016-04-11 11:09:37 -07001114status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001115 return writeDupFileDescriptor(fd.get());
1116}
1117
Jeff Brown13b16042014-11-11 16:44:25 -08001118status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001119{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001120 if (len > INT32_MAX) {
1121 // don't accept size_t values which may have come from an
1122 // inadvertent conversion from a negative int.
1123 return BAD_VALUE;
1124 }
1125
Jeff Brown13b16042014-11-11 16:44:25 -08001126 status_t status;
1127 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001128 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001129 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001130 if (status) return status;
1131
1132 void* ptr = writeInplace(len);
1133 if (!ptr) return NO_MEMORY;
1134
Jeff Brown13b16042014-11-11 16:44:25 -08001135 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001136 return NO_ERROR;
1137 }
1138
Steve Block6807e592011-10-20 11:56:00 +01001139 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001140 int fd = ashmem_create_region("Parcel Blob", len);
1141 if (fd < 0) return NO_MEMORY;
1142
1143 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1144 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001145 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001146 } else {
Yi Kong91635562018-06-07 14:38:36 -07001147 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001148 if (ptr == MAP_FAILED) {
1149 status = -errno;
1150 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001151 if (!mutableCopy) {
1152 result = ashmem_set_prot_region(fd, PROT_READ);
1153 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001154 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001155 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001156 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001157 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001158 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001159 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001160 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001161 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001162 return NO_ERROR;
1163 }
1164 }
1165 }
1166 }
1167 ::munmap(ptr, len);
1168 }
1169 ::close(fd);
1170 return status;
1171}
1172
Jeff Brown13b16042014-11-11 16:44:25 -08001173status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1174{
1175 // Must match up with what's done in writeBlob.
1176 if (!mAllowFds) return FDS_NOT_ALLOWED;
1177 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1178 if (status) return status;
1179 return writeDupFileDescriptor(fd);
1180}
1181
Mathias Agopiane1424282013-07-29 21:24:40 -07001182status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001183{
1184 status_t err;
1185
1186 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001187 const size_t len = val.getFlattenedSize();
1188 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001189
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001190 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001191 // don't accept size_t values which may have come from an
1192 // inadvertent conversion from a negative int.
1193 return BAD_VALUE;
1194 }
1195
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001196 err = this->writeInt32(len);
1197 if (err) return err;
1198
1199 err = this->writeInt32(fd_count);
1200 if (err) return err;
1201
1202 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001203 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001204 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001205 return BAD_VALUE;
1206
Yi Kong91635562018-06-07 14:38:36 -07001207 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001208 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001209 fds = new (std::nothrow) int[fd_count];
1210 if (fds == nullptr) {
1211 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1212 return BAD_VALUE;
1213 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001214 }
1215
1216 err = val.flatten(buf, len, fds, fd_count);
1217 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1218 err = this->writeDupFileDescriptor( fds[i] );
1219 }
1220
1221 if (fd_count) {
1222 delete [] fds;
1223 }
1224
1225 return err;
1226}
1227
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001228status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1229{
1230 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1231 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1232 if (enoughData && enoughObjects) {
1233restart_write:
1234 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001235
Christopher Tate98e67d32015-06-03 18:44:15 -07001236 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001237 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001238 if (!mAllowFds) {
1239 // fail before modifying our object index
1240 return FDS_NOT_ALLOWED;
1241 }
1242 mHasFds = mFdsKnown = true;
1243 }
1244
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001245 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001246 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001247 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001248 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001249 mObjectsSize++;
1250 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001251
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001252 return finishWrite(sizeof(flat_binder_object));
1253 }
1254
1255 if (!enoughData) {
1256 const status_t err = growData(sizeof(val));
1257 if (err != NO_ERROR) return err;
1258 }
1259 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001260 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1261 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001262 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001263 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001264 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001265 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001266 mObjects = objects;
1267 mObjectsCapacity = newSize;
1268 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001269
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001270 goto restart_write;
1271}
1272
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001273status_t Parcel::writeNoException()
1274{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001275 binder::Status status;
1276 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001277}
1278
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001279status_t Parcel::validateReadData(size_t upperBound) const
1280{
1281 // Don't allow non-object reads on object data
1282 if (mObjectsSorted || mObjectsSize <= 1) {
1283data_sorted:
1284 // Expect to check only against the next object
1285 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1286 // For some reason the current read position is greater than the next object
1287 // hint. Iterate until we find the right object
1288 size_t nextObject = mNextObjectHint;
1289 do {
1290 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1291 // Requested info overlaps with an object
1292 ALOGE("Attempt to read from protected data in Parcel %p", this);
1293 return PERMISSION_DENIED;
1294 }
1295 nextObject++;
1296 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1297 mNextObjectHint = nextObject;
1298 }
1299 return NO_ERROR;
1300 }
1301 // Quickly determine if mObjects is sorted.
1302 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1303 binder_size_t* prevObj = currObj;
1304 while (currObj > mObjects) {
1305 prevObj--;
1306 if(*prevObj > *currObj) {
1307 goto data_unsorted;
1308 }
1309 currObj--;
1310 }
1311 mObjectsSorted = true;
1312 goto data_sorted;
1313
1314data_unsorted:
1315 // Insertion Sort mObjects
1316 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1317 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1318 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1319 binder_size_t temp = *iter0;
1320 binder_size_t* iter1 = iter0 - 1;
1321 while (iter1 >= mObjects && *iter1 > temp) {
1322 *(iter1 + 1) = *iter1;
1323 iter1--;
1324 }
1325 *(iter1 + 1) = temp;
1326 }
1327 mNextObjectHint = 0;
1328 mObjectsSorted = true;
1329 goto data_sorted;
1330}
1331
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001332status_t Parcel::read(void* outData, size_t len) const
1333{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001334 if (len > INT32_MAX) {
1335 // don't accept size_t values which may have come from an
1336 // inadvertent conversion from a negative int.
1337 return BAD_VALUE;
1338 }
1339
1340 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1341 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001342 if (mObjectsSize > 0) {
1343 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001344 if(err != NO_ERROR) {
1345 // Still increment the data position by the expected length
1346 mDataPos += pad_size(len);
1347 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1348 return err;
1349 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001350 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001351 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001352 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001353 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001354 return NO_ERROR;
1355 }
1356 return NOT_ENOUGH_DATA;
1357}
1358
1359const void* Parcel::readInplace(size_t len) const
1360{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001361 if (len > INT32_MAX) {
1362 // don't accept size_t values which may have come from an
1363 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001364 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001365 }
1366
1367 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1368 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001369 if (mObjectsSize > 0) {
1370 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001371 if(err != NO_ERROR) {
1372 // Still increment the data position by the expected length
1373 mDataPos += pad_size(len);
1374 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001375 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001376 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001377 }
1378
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001379 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001380 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001381 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001382 return data;
1383 }
Yi Kong91635562018-06-07 14:38:36 -07001384 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001385}
1386
Andreas Huber84a6d042009-08-17 13:33:27 -07001387template<class T>
1388status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001389 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001390
1391 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001392 if (mObjectsSize > 0) {
1393 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001394 if(err != NO_ERROR) {
1395 // Still increment the data position by the expected length
1396 mDataPos += sizeof(T);
1397 return err;
1398 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001399 }
1400
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001401 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001402 mDataPos += sizeof(T);
1403 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001404 return NO_ERROR;
1405 } else {
1406 return NOT_ENOUGH_DATA;
1407 }
1408}
1409
Andreas Huber84a6d042009-08-17 13:33:27 -07001410template<class T>
1411T Parcel::readAligned() const {
1412 T result;
1413 if (readAligned(&result) != NO_ERROR) {
1414 result = 0;
1415 }
1416
1417 return result;
1418}
1419
1420template<class T>
1421status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001422 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001423
1424 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1425restart_write:
1426 *reinterpret_cast<T*>(mData+mDataPos) = val;
1427 return finishWrite(sizeof(val));
1428 }
1429
1430 status_t err = growData(sizeof(val));
1431 if (err == NO_ERROR) goto restart_write;
1432 return err;
1433}
1434
1435status_t Parcel::readInt32(int32_t *pArg) const
1436{
1437 return readAligned(pArg);
1438}
1439
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001440int32_t Parcel::readInt32() const
1441{
Andreas Huber84a6d042009-08-17 13:33:27 -07001442 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001443}
1444
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001445status_t Parcel::readUint32(uint32_t *pArg) const
1446{
1447 return readAligned(pArg);
1448}
1449
1450uint32_t Parcel::readUint32() const
1451{
1452 return readAligned<uint32_t>();
1453}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001454
1455status_t Parcel::readInt64(int64_t *pArg) const
1456{
Andreas Huber84a6d042009-08-17 13:33:27 -07001457 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001458}
1459
1460
1461int64_t Parcel::readInt64() const
1462{
Andreas Huber84a6d042009-08-17 13:33:27 -07001463 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001464}
1465
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001466status_t Parcel::readUint64(uint64_t *pArg) const
1467{
1468 return readAligned(pArg);
1469}
1470
1471uint64_t Parcel::readUint64() const
1472{
1473 return readAligned<uint64_t>();
1474}
1475
Serban Constantinescuf683e012013-11-05 16:53:55 +00001476status_t Parcel::readPointer(uintptr_t *pArg) const
1477{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001478 status_t ret;
1479 binder_uintptr_t ptr;
1480 ret = readAligned(&ptr);
1481 if (!ret)
1482 *pArg = ptr;
1483 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001484}
1485
1486uintptr_t Parcel::readPointer() const
1487{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001488 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001489}
1490
1491
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001492status_t Parcel::readFloat(float *pArg) const
1493{
Andreas Huber84a6d042009-08-17 13:33:27 -07001494 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001495}
1496
1497
1498float Parcel::readFloat() const
1499{
Andreas Huber84a6d042009-08-17 13:33:27 -07001500 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001501}
1502
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001503#if defined(__mips__) && defined(__mips_hard_float)
1504
1505status_t Parcel::readDouble(double *pArg) const
1506{
1507 union {
1508 double d;
1509 unsigned long long ll;
1510 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001511 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001512 status_t status;
1513 status = readAligned(&u.ll);
1514 *pArg = u.d;
1515 return status;
1516}
1517
1518double Parcel::readDouble() const
1519{
1520 union {
1521 double d;
1522 unsigned long long ll;
1523 } u;
1524 u.ll = readAligned<unsigned long long>();
1525 return u.d;
1526}
1527
1528#else
1529
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001530status_t Parcel::readDouble(double *pArg) const
1531{
Andreas Huber84a6d042009-08-17 13:33:27 -07001532 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001533}
1534
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001535double Parcel::readDouble() const
1536{
Andreas Huber84a6d042009-08-17 13:33:27 -07001537 return readAligned<double>();
1538}
1539
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001540#endif
1541
Casey Dahlind6848f52015-10-15 15:44:59 -07001542status_t Parcel::readBool(bool *pArg) const
1543{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001544 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001545 status_t ret = readInt32(&tmp);
1546 *pArg = (tmp != 0);
1547 return ret;
1548}
1549
1550bool Parcel::readBool() const
1551{
1552 return readInt32() != 0;
1553}
1554
1555status_t Parcel::readChar(char16_t *pArg) const
1556{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001557 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001558 status_t ret = readInt32(&tmp);
1559 *pArg = char16_t(tmp);
1560 return ret;
1561}
1562
1563char16_t Parcel::readChar() const
1564{
1565 return char16_t(readInt32());
1566}
1567
1568status_t Parcel::readByte(int8_t *pArg) const
1569{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001570 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001571 status_t ret = readInt32(&tmp);
1572 *pArg = int8_t(tmp);
1573 return ret;
1574}
1575
1576int8_t Parcel::readByte() const
1577{
1578 return int8_t(readInt32());
1579}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001580
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001581status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1582 size_t utf16Size = 0;
1583 const char16_t* src = readString16Inplace(&utf16Size);
1584 if (!src) {
1585 return UNEXPECTED_NULL;
1586 }
1587
1588 // Save ourselves the trouble, we're done.
1589 if (utf16Size == 0u) {
1590 str->clear();
1591 return NO_ERROR;
1592 }
1593
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001594 // Allow for closing '\0'
1595 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1596 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001597 return BAD_VALUE;
1598 }
1599 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001600 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001601 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001602 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1603 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001604 return NO_ERROR;
1605}
1606
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001607const char* Parcel::readCString() const
1608{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001609 if (mDataPos < mDataSize) {
1610 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001611 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1612 // is the string's trailing NUL within the parcel's valid bounds?
1613 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1614 if (eos) {
1615 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001616 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001617 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001618 return str;
1619 }
1620 }
Yi Kong91635562018-06-07 14:38:36 -07001621 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001622}
1623
1624String8 Parcel::readString8() const
1625{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001626 size_t len;
1627 const char* str = readString8Inplace(&len);
1628 if (str) return String8(str, len);
1629 ALOGE("Reading a NULL string not supported here.");
1630 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07001631}
1632
1633status_t Parcel::readString8(String8* pArg) const
1634{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001635 size_t len;
1636 const char* str = readString8Inplace(&len);
1637 if (str) {
1638 pArg->setTo(str, len);
1639 return 0;
1640 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07001641 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001642 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07001643 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001644}
1645
1646const char* Parcel::readString8Inplace(size_t* outLen) const
1647{
1648 int32_t size = readInt32();
1649 // watch for potential int overflow from size+1
1650 if (size >= 0 && size < INT32_MAX) {
1651 *outLen = size;
1652 const char* str = (const char*)readInplace(size+1);
1653 if (str != nullptr) {
1654 return str;
1655 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001656 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001657 *outLen = 0;
1658 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001659}
1660
1661String16 Parcel::readString16() const
1662{
1663 size_t len;
1664 const char16_t* str = readString16Inplace(&len);
1665 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001666 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001667 return String16();
1668}
1669
Casey Dahlinb9872622015-11-25 15:09:45 -08001670
Casey Dahlin451ff582015-10-19 18:12:18 -07001671status_t Parcel::readString16(String16* pArg) const
1672{
1673 size_t len;
1674 const char16_t* str = readString16Inplace(&len);
1675 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001676 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001677 return 0;
1678 } else {
1679 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001680 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001681 }
1682}
1683
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001684const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1685{
1686 int32_t size = readInt32();
1687 // watch for potential int overflow from size+1
1688 if (size >= 0 && size < INT32_MAX) {
1689 *outLen = size;
1690 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001691 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001692 return str;
1693 }
1694 }
1695 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001696 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001697}
1698
Casey Dahlinf0c13772015-10-27 18:33:56 -07001699status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1700{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001701 status_t status = readNullableStrongBinder(val);
1702 if (status == OK && !val->get()) {
1703 status = UNEXPECTED_NULL;
1704 }
1705 return status;
1706}
1707
1708status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1709{
Steven Morelanda86a3562019-08-01 23:28:34 +00001710 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001711}
1712
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001713sp<IBinder> Parcel::readStrongBinder() const
1714{
1715 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001716 // Note that a lot of code in Android reads binders by hand with this
1717 // method, and that code has historically been ok with getting nullptr
1718 // back (while ignoring error codes).
1719 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001720 return val;
1721}
1722
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001723int32_t Parcel::readExceptionCode() const
1724{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001725 binder::Status status;
1726 status.readFromParcel(*this);
1727 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001728}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001729
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001730native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001731{
1732 int numFds, numInts;
1733 status_t err;
1734 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001735 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001736 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001737 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001738
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001739 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001740 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001741 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001742 }
1743
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001744 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001745 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001746 if (h->data[i] < 0) {
1747 for (int j = 0; j < i; j++) {
1748 close(h->data[j]);
1749 }
1750 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001751 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001752 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001753 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001754 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001755 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001756 native_handle_close(h);
1757 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001758 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001759 }
1760 return h;
1761}
1762
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001763int Parcel::readFileDescriptor() const
1764{
1765 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001766
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001767 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001768 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001769 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001770
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001771 return BAD_TYPE;
1772}
1773
Dianne Hackborn1941a402016-08-29 12:30:43 -07001774int Parcel::readParcelFileDescriptor() const
1775{
1776 int32_t hasComm = readInt32();
1777 int fd = readFileDescriptor();
1778 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001779 // detach (owned by the binder driver)
1780 int comm = readFileDescriptor();
1781
1782 // warning: this must be kept in sync with:
1783 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1784 enum ParcelFileDescriptorStatus {
1785 DETACHED = 2,
1786 };
1787
1788#if BYTE_ORDER == BIG_ENDIAN
1789 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
1790#endif
1791#if BYTE_ORDER == LITTLE_ENDIAN
1792 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
1793#endif
1794
1795 ssize_t written = TEMP_FAILURE_RETRY(
1796 ::write(comm, &message, sizeof(message)));
1797
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08001798 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001799 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
1800 written, strerror(errno));
1801 return BAD_TYPE;
1802 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07001803 }
1804 return fd;
1805}
1806
Christopher Wiley2cf19952016-04-11 11:09:37 -07001807status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08001808{
1809 int got = readFileDescriptor();
1810
1811 if (got == BAD_TYPE) {
1812 return BAD_TYPE;
1813 }
1814
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001815 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08001816
1817 if (val->get() < 0) {
1818 return BAD_VALUE;
1819 }
1820
1821 return OK;
1822}
1823
Ryo Hashimotobf551892018-05-31 16:58:35 +09001824status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
1825{
1826 int got = readParcelFileDescriptor();
1827
1828 if (got == BAD_TYPE) {
1829 return BAD_TYPE;
1830 }
1831
1832 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
1833
1834 if (val->get() < 0) {
1835 return BAD_VALUE;
1836 }
1837
1838 return OK;
1839}
Casey Dahlin06673e32015-11-23 13:24:23 -08001840
Jeff Brown5707dbf2011-09-23 21:17:56 -07001841status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1842{
Jeff Brown13b16042014-11-11 16:44:25 -08001843 int32_t blobType;
1844 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001845 if (status) return status;
1846
Jeff Brown13b16042014-11-11 16:44:25 -08001847 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001848 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001849 const void* ptr = readInplace(len);
1850 if (!ptr) return BAD_VALUE;
1851
Jeff Brown13b16042014-11-11 16:44:25 -08001852 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001853 return NO_ERROR;
1854 }
1855
Steve Block6807e592011-10-20 11:56:00 +01001856 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001857 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001858 int fd = readFileDescriptor();
1859 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1860
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001861 if (!ashmem_valid(fd)) {
1862 ALOGE("invalid fd");
1863 return BAD_VALUE;
1864 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001865 int size = ashmem_get_size_region(fd);
1866 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001867 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001868 return BAD_VALUE;
1869 }
Yi Kong91635562018-06-07 14:38:36 -07001870 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08001871 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001872 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001873
Jeff Brown13b16042014-11-11 16:44:25 -08001874 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001875 return NO_ERROR;
1876}
1877
Mathias Agopiane1424282013-07-29 21:24:40 -07001878status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001879{
1880 // size
1881 const size_t len = this->readInt32();
1882 const size_t fd_count = this->readInt32();
1883
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001884 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001885 // don't accept size_t values which may have come from an
1886 // inadvertent conversion from a negative int.
1887 return BAD_VALUE;
1888 }
1889
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001890 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001891 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07001892 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001893 return BAD_VALUE;
1894
Yi Kong91635562018-06-07 14:38:36 -07001895 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001896 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001897 fds = new (std::nothrow) int[fd_count];
1898 if (fds == nullptr) {
1899 ALOGE("read: failed to allocate requested %zu fds", fd_count);
1900 return BAD_VALUE;
1901 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001902 }
1903
1904 status_t err = NO_ERROR;
1905 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07001906 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001907 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001908 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001909 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 -07001910 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
1911 // Close all the file descriptors that were dup-ed.
1912 for (size_t j=0; j<i ;j++) {
1913 close(fds[j]);
1914 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001915 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001916 }
1917
1918 if (err == NO_ERROR) {
1919 err = val.unflatten(buf, len, fds, fd_count);
1920 }
1921
1922 if (fd_count) {
1923 delete [] fds;
1924 }
1925
1926 return err;
1927}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001928const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1929{
1930 const size_t DPOS = mDataPos;
1931 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1932 const flat_binder_object* obj
1933 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1934 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001935 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001936 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001937 // the object list, so we don't want to check for it when
1938 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001939 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001940 return obj;
1941 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001942
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001943 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001944 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001945 const size_t N = mObjectsSize;
1946 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001947
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001948 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001949 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001950 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001951
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001952 // Start at the current hint position, looking for an object at
1953 // the current data position.
1954 if (opos < N) {
1955 while (opos < (N-1) && OBJS[opos] < DPOS) {
1956 opos++;
1957 }
1958 } else {
1959 opos = N-1;
1960 }
1961 if (OBJS[opos] == DPOS) {
1962 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001963 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001964 this, DPOS, opos);
1965 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001966 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001967 return obj;
1968 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001969
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001970 // Look backwards for it...
1971 while (opos > 0 && OBJS[opos] > DPOS) {
1972 opos--;
1973 }
1974 if (OBJS[opos] == DPOS) {
1975 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001976 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001977 this, DPOS, opos);
1978 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001979 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001980 return obj;
1981 }
1982 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001983 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 -07001984 this, DPOS);
1985 }
Yi Kong91635562018-06-07 14:38:36 -07001986 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001987}
1988
1989void Parcel::closeFileDescriptors()
1990{
1991 size_t i = mObjectsSize;
1992 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001993 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001994 }
1995 while (i > 0) {
1996 i--;
1997 const flat_binder_object* flat
1998 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001999 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002000 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002001 close(flat->handle);
2002 }
2003 }
2004}
2005
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002006uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002007{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002008 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002009}
2010
2011size_t Parcel::ipcDataSize() const
2012{
2013 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2014}
2015
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002016uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002017{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002018 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002019}
2020
2021size_t Parcel::ipcObjectsCount() const
2022{
2023 return mObjectsSize;
2024}
2025
2026void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Steven Moreland161fe122020-11-12 23:16:47 +00002027 const binder_size_t* objects, size_t objectsCount, release_func relFunc)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002028{
Steven Morelandceed9bb2020-12-17 01:01:06 +00002029 freeData();
2030
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002031 mData = const_cast<uint8_t*>(data);
2032 mDataSize = mDataCapacity = dataSize;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002033 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002034 mObjectsSize = mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002035 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002036
2037 binder_size_t minOffset = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002038 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002039 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002040 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002041 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002042 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002043 mObjectsSize = 0;
2044 break;
2045 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002046 const flat_binder_object* flat
2047 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2048 uint32_t type = flat->hdr.type;
2049 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2050 type == BINDER_TYPE_FD)) {
2051 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2052 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2053 // recover gracefully by clearing out the objects, and releasing the objects we do
2054 // know about.
2055 android_errorWriteLog(0x534e4554, "135930648");
2056 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2057 __func__, type, (uint64_t)offset);
2058 releaseObjects();
2059 mObjectsSize = 0;
2060 break;
2061 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002062 minOffset = offset + sizeof(flat_binder_object);
2063 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002064 scanForFds();
2065}
2066
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002067void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002068{
2069 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002070
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002071 if (errorCheck() != NO_ERROR) {
2072 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002073 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002074 } else if (dataSize() > 0) {
2075 const uint8_t* DATA = data();
2076 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002077 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002078 const size_t N = objectsCount();
2079 for (size_t i=0; i<N; i++) {
2080 const flat_binder_object* flat
2081 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2082 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002083 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002084 << " = " << flat->binder;
2085 }
2086 } else {
2087 to << "NULL";
2088 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002089
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002090 to << ")";
2091}
2092
2093void Parcel::releaseObjects()
2094{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002095 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002096 if (i == 0) {
2097 return;
2098 }
2099 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002100 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002101 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002102 while (i > 0) {
2103 i--;
2104 const flat_binder_object* flat
2105 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002106 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002107 }
2108}
2109
2110void Parcel::acquireObjects()
2111{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002112 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002113 if (i == 0) {
2114 return;
2115 }
2116 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002117 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002118 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002119 while (i > 0) {
2120 i--;
2121 const flat_binder_object* flat
2122 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002123 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002124 }
2125}
2126
2127void Parcel::freeData()
2128{
2129 freeDataNoInit();
2130 initState();
2131}
2132
2133void Parcel::freeDataNoInit()
2134{
2135 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002136 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002137 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002138 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002139 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002140 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002142 if (mData) {
2143 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002144 gParcelGlobalAllocSize -= mDataCapacity;
2145 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002146 if (mDeallocZero) {
2147 zeroMemory(mData, mDataSize);
2148 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002149 free(mData);
2150 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002151 if (mObjects) free(mObjects);
2152 }
2153}
2154
2155status_t Parcel::growData(size_t len)
2156{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002157 if (len > INT32_MAX) {
2158 // don't accept size_t values which may have come from an
2159 // inadvertent conversion from a negative int.
2160 return BAD_VALUE;
2161 }
2162
Martijn Coenen93fe5182020-01-22 10:46:25 +01002163 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2164 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002165 size_t newSize = ((mDataSize+len)*3)/2;
2166 return (newSize <= mDataSize)
2167 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002168 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002169}
2170
Steven Morelandf183fdd2020-10-27 00:12:12 +00002171static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2172 if (!zero) {
2173 return (uint8_t*)realloc(data, newCapacity);
2174 }
2175 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2176 if (!newData) {
2177 return nullptr;
2178 }
2179
2180 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2181 zeroMemory(data, oldCapacity);
2182 free(data);
2183 return newData;
2184}
2185
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002186status_t Parcel::restartWrite(size_t desired)
2187{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002188 if (desired > INT32_MAX) {
2189 // don't accept size_t values which may have come from an
2190 // inadvertent conversion from a negative int.
2191 return BAD_VALUE;
2192 }
2193
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002194 if (mOwner) {
2195 freeData();
2196 return continueWrite(desired);
2197 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002198
Steven Morelandf183fdd2020-10-27 00:12:12 +00002199 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002200 if (!data && desired > mDataCapacity) {
2201 mError = NO_MEMORY;
2202 return NO_MEMORY;
2203 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002204
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002205 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002206
Devin Moore4a0a55e2020-06-04 13:23:10 -07002207 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002208 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002209 if (mDataCapacity > desired) {
2210 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2211 } else {
2212 gParcelGlobalAllocSize += (desired - mDataCapacity);
2213 }
2214
Colin Cross83ec65e2015-12-08 17:15:50 -08002215 if (!mData) {
2216 gParcelGlobalAllocCount++;
2217 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002218 mData = data;
2219 mDataCapacity = desired;
2220 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002221
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002222 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002223 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2224 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2225
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002226 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002227 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002228 mObjectsSize = mObjectsCapacity = 0;
2229 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002230 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231 mHasFds = false;
2232 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002233 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002234
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002235 return NO_ERROR;
2236}
2237
2238status_t Parcel::continueWrite(size_t desired)
2239{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002240 if (desired > INT32_MAX) {
2241 // don't accept size_t values which may have come from an
2242 // inadvertent conversion from a negative int.
2243 return BAD_VALUE;
2244 }
2245
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002246 // If shrinking, first adjust for any objects that appear
2247 // after the new data size.
2248 size_t objectsSize = mObjectsSize;
2249 if (desired < mDataSize) {
2250 if (desired == 0) {
2251 objectsSize = 0;
2252 } else {
2253 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002254 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002255 break;
2256 objectsSize--;
2257 }
2258 }
2259 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002260
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002261 if (mOwner) {
2262 // If the size is going to zero, just release the owner's data.
2263 if (desired == 0) {
2264 freeData();
2265 return NO_ERROR;
2266 }
2267
2268 // If there is a different owner, we need to take
2269 // posession.
2270 uint8_t* data = (uint8_t*)malloc(desired);
2271 if (!data) {
2272 mError = NO_MEMORY;
2273 return NO_MEMORY;
2274 }
Yi Kong91635562018-06-07 14:38:36 -07002275 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002276
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002277 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002278 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002279 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002280 free(data);
2281
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002282 mError = NO_MEMORY;
2283 return NO_MEMORY;
2284 }
2285
2286 // Little hack to only acquire references on objects
2287 // we will be keeping.
2288 size_t oldObjectsSize = mObjectsSize;
2289 mObjectsSize = objectsSize;
2290 acquireObjects();
2291 mObjectsSize = oldObjectsSize;
2292 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002293
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002294 if (mData) {
2295 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2296 }
2297 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002298 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002299 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002300 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002301 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
Yi Kong91635562018-06-07 14:38:36 -07002302 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002303
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002304 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002305 gParcelGlobalAllocSize += desired;
2306 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002307
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002308 mData = data;
2309 mObjects = objects;
2310 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002311 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002312 mDataCapacity = desired;
2313 mObjectsSize = mObjectsCapacity = objectsSize;
2314 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002315 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002316
2317 } else if (mData) {
2318 if (objectsSize < mObjectsSize) {
2319 // Need to release refs on any objects we are dropping.
2320 const sp<ProcessState> proc(ProcessState::self());
2321 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2322 const flat_binder_object* flat
2323 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002324 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002325 // will need to rescan because we may have lopped off the only FDs
2326 mFdsKnown = false;
2327 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002328 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002329 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002330
2331 if (objectsSize == 0) {
2332 free(mObjects);
2333 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002334 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002335 } else {
2336 binder_size_t* objects =
2337 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2338 if (objects) {
2339 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002340 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002341 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002342 }
2343 mObjectsSize = objectsSize;
2344 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002345 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002346 }
2347
2348 // We own the data, so we can just do a realloc().
2349 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002350 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002351 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002352 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2353 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002354 gParcelGlobalAllocSize += desired;
2355 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002356 mData = data;
2357 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002358 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002359 mError = NO_MEMORY;
2360 return NO_MEMORY;
2361 }
2362 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002363 if (mDataSize > desired) {
2364 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002365 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002366 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002367 if (mDataPos > desired) {
2368 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002369 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002370 }
2371 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002372
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002373 } else {
2374 // This is the first data. Easy!
2375 uint8_t* data = (uint8_t*)malloc(desired);
2376 if (!data) {
2377 mError = NO_MEMORY;
2378 return NO_MEMORY;
2379 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002380
Yi Kong91635562018-06-07 14:38:36 -07002381 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002382 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002383 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002384 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002385
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002386 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002387 gParcelGlobalAllocSize += desired;
2388 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002389
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002390 mData = data;
2391 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002392 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2393 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002394 mDataCapacity = desired;
2395 }
2396
2397 return NO_ERROR;
2398}
2399
2400void Parcel::initState()
2401{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002402 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002403 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002404 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002405 mDataSize = 0;
2406 mDataCapacity = 0;
2407 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002408 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2409 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002410 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002411 mObjectsSize = 0;
2412 mObjectsCapacity = 0;
2413 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002414 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002415 mHasFds = false;
2416 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002417 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002418 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07002419 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002420 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002421 mWorkSourceRequestHeaderPosition = 0;
2422 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002423
2424 // racing multiple init leads only to multiple identical write
2425 if (gMaxFds == 0) {
2426 struct rlimit result;
2427 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2428 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002429 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002430 } else {
2431 ALOGW("Unable to getrlimit: %s", strerror(errno));
2432 gMaxFds = 1024;
2433 }
2434 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435}
2436
2437void Parcel::scanForFds() const
2438{
2439 bool hasFds = false;
2440 for (size_t i=0; i<mObjectsSize; i++) {
2441 const flat_binder_object* flat
2442 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002443 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002444 hasFds = true;
2445 break;
2446 }
2447 }
2448 mHasFds = hasFds;
2449 mFdsKnown = true;
2450}
2451
Dan Sandleraa5c2342015-04-10 10:08:45 -04002452size_t Parcel::getBlobAshmemSize() const
2453{
Adrian Roos6bb31142015-10-22 16:46:12 -07002454 // This used to return the size of all blobs that were written to ashmem, now we're returning
2455 // the ashmem currently referenced by this Parcel, which should be equivalent.
2456 // TODO: Remove method once ABI can be changed.
2457 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002458}
2459
Adrian Rooscbf37262015-10-22 16:12:53 -07002460size_t Parcel::getOpenAshmemSize() const
2461{
2462 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002463}
2464
2465// --- Parcel::Blob ---
2466
2467Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002468 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002469}
2470
2471Parcel::Blob::~Blob() {
2472 release();
2473}
2474
2475void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002476 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002477 ::munmap(mData, mSize);
2478 }
2479 clear();
2480}
2481
Jeff Brown13b16042014-11-11 16:44:25 -08002482void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2483 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002484 mData = data;
2485 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002486 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002487}
2488
2489void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002490 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002491 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002492 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002493 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002494}
2495
Steven Moreland61ff8492019-09-26 16:05:45 -07002496} // namespace android