blob: 40dd09b18059cf9f0798073b0133db5d85a2c59f [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"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080054//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080055#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080056//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070057
58// ---------------------------------------------------------------------------
59
Nick Kralevichb6b14232015-04-02 09:36:02 -070060// This macro should never be used at runtime, as a too large value
61// of s could cause an integer overflow. Instead, you should always
62// use the wrapper function pad_size()
63#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
64
65static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070066 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070067 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070068 }
69 return PAD_SIZE_UNSAFE(s);
70}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070071
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070072// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060073#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070074
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075namespace android {
76
Steven Moreland7b102262019-08-01 15:48:43 -070077// many things compile this into prebuilts on the stack
78static_assert(sizeof(Parcel) == 60 || sizeof(Parcel) == 120);
79
Jeff Sharkey8994c182020-09-11 12:07:10 -060080static std::atomic<size_t> gParcelGlobalAllocCount;
81static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -080082
Christopher Tatee4e0ae82016-03-24 16:03:44 -070083static size_t gMaxFds = 0;
84
Jeff Brown13b16042014-11-11 16:44:25 -080085// Maximum size of a blob to transfer in-place.
86static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
87
88enum {
89 BLOB_INPLACE = 0,
90 BLOB_ASHMEM_IMMUTABLE = 1,
91 BLOB_ASHMEM_MUTABLE = 2,
92};
93
Steven Morelandb1c81202019-04-05 18:49:55 -070094static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070095 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070096{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -070097 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098 case BINDER_TYPE_BINDER:
99 if (obj.binder) {
100 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800101 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700102 }
103 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104 case BINDER_TYPE_HANDLE: {
105 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700106 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700107 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
108 b->incStrong(who);
109 }
110 return;
111 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700112 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000113 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700114 // If we own an ashmem fd, keep track of how much memory it refers to.
115 int size = ashmem_get_size_region(obj.handle);
116 if (size > 0) {
117 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700118 }
119 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700120 return;
121 }
122 }
123
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700124 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700125}
126
Adrian Roos6bb31142015-10-22 16:46:12 -0700127static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700128 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700129{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700130 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700131 case BINDER_TYPE_BINDER:
132 if (obj.binder) {
133 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800134 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700135 }
136 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700137 case BINDER_TYPE_HANDLE: {
138 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700139 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700140 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
141 b->decStrong(who);
142 }
143 return;
144 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700145 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800146 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000147 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700148 int size = ashmem_get_size_region(obj.handle);
149 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800150 // ashmem size might have changed since last time it was accounted for, e.g.
151 // in acquire_object(). Value of *outAshmemSize is not critical since we are
152 // releasing the object anyway. Check for integer overflow condition.
153 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700154 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700155 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800156
157 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700158 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700159 return;
160 }
161 }
162
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700163 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700164}
165
Steven Morelanda86a3562019-08-01 23:28:34 +0000166status_t Parcel::finishFlattenBinder(
167 const sp<IBinder>& binder, const flat_binder_object& flat)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700168{
Steven Morelanda86a3562019-08-01 23:28:34 +0000169 status_t status = writeObject(flat, false);
170 if (status != OK) return status;
171
Steven Moreland6e5a7752019-08-05 20:30:14 -0700172 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Morelanda86a3562019-08-01 23:28:34 +0000173 return writeInt32(internal::Stability::get(binder.get()));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700174}
175
Steven Morelanda86a3562019-08-01 23:28:34 +0000176status_t Parcel::finishUnflattenBinder(
177 const sp<IBinder>& binder, sp<IBinder>* out) const
178{
179 int32_t stability;
180 status_t status = readInt32(&stability);
181 if (status != OK) return status;
182
Steven Moreland05929552019-07-31 17:51:25 -0700183 status = internal::Stability::set(binder.get(), stability, true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000184 if (status != OK) return status;
185
186 *out = binder;
187 return OK;
188}
189
Steven Morelandbf1915b2020-07-16 22:43:02 +0000190static constexpr inline int schedPolicyMask(int policy, int priority) {
191 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
192}
193
Steven Morelanda86a3562019-08-01 23:28:34 +0000194status_t Parcel::flattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700195{
196 flat_binder_object obj;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000197 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700198
Steven Morelandbf1915b2020-07-16 22:43:02 +0000199 int schedBits = 0;
200 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
201 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700202 }
203
Yi Kong91635562018-06-07 14:38:36 -0700204 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800205 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700206 if (!local) {
207 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700208 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000209 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700210 }
211 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700212 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800213 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700214 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800215 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700216 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000217 int policy = local->getMinSchedulerPolicy();
218 int priority = local->getMinSchedulerPriority();
219
220 if (policy != 0 || priority != 0) {
221 // override value, since it is set explicitly
222 schedBits = schedPolicyMask(policy, priority);
223 }
Steven Morelandf0212002018-12-26 13:59:23 -0800224 if (local->isRequestingSid()) {
225 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
226 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700227 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800228 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
229 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230 }
231 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700232 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800233 obj.binder = 0;
234 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700236
Steven Morelandbf1915b2020-07-16 22:43:02 +0000237 obj.flags |= schedBits;
238
Steven Morelanda86a3562019-08-01 23:28:34 +0000239 return finishFlattenBinder(binder, obj);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700240}
241
Steven Morelanda86a3562019-08-01 23:28:34 +0000242status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243{
Steven Morelanda86a3562019-08-01 23:28:34 +0000244 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700245
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700246 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700247 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000248 case BINDER_TYPE_BINDER: {
249 sp<IBinder> binder = reinterpret_cast<IBinder*>(flat->cookie);
250 return finishUnflattenBinder(binder, out);
251 }
252 case BINDER_TYPE_HANDLE: {
253 sp<IBinder> binder =
254 ProcessState::self()->getStrongProxyForHandle(flat->handle);
255 return finishUnflattenBinder(binder, out);
256 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700257 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258 }
259 return BAD_TYPE;
260}
261
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700262// ---------------------------------------------------------------------------
263
264Parcel::Parcel()
265{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800266 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700267 initState();
268}
269
270Parcel::~Parcel()
271{
272 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800273 LOG_ALLOC("Parcel %p: destroyed", this);
274}
275
276size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600277 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800278}
279
280size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600281 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700282}
283
284const uint8_t* Parcel::data() const
285{
286 return mData;
287}
288
289size_t Parcel::dataSize() const
290{
291 return (mDataSize > mDataPos ? mDataSize : mDataPos);
292}
293
294size_t Parcel::dataAvail() const
295{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700296 size_t result = dataSize() - dataPosition();
297 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700298 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700299 }
300 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700301}
302
303size_t Parcel::dataPosition() const
304{
305 return mDataPos;
306}
307
308size_t Parcel::dataCapacity() const
309{
310 return mDataCapacity;
311}
312
313status_t Parcel::setDataSize(size_t size)
314{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700315 if (size > INT32_MAX) {
316 // don't accept size_t values which may have come from an
317 // inadvertent conversion from a negative int.
318 return BAD_VALUE;
319 }
320
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700321 status_t err;
322 err = continueWrite(size);
323 if (err == NO_ERROR) {
324 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700325 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700326 }
327 return err;
328}
329
330void Parcel::setDataPosition(size_t pos) const
331{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700332 if (pos > INT32_MAX) {
333 // don't accept size_t values which may have come from an
334 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700335 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700336 }
337
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700338 mDataPos = pos;
339 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800340 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700341}
342
343status_t Parcel::setDataCapacity(size_t size)
344{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700345 if (size > INT32_MAX) {
346 // don't accept size_t values which may have come from an
347 // inadvertent conversion from a negative int.
348 return BAD_VALUE;
349 }
350
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700351 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700352 return NO_ERROR;
353}
354
355status_t Parcel::setData(const uint8_t* buffer, size_t len)
356{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700357 if (len > INT32_MAX) {
358 // don't accept size_t values which may have come from an
359 // inadvertent conversion from a negative int.
360 return BAD_VALUE;
361 }
362
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700363 status_t err = restartWrite(len);
364 if (err == NO_ERROR) {
365 memcpy(const_cast<uint8_t*>(data()), buffer, len);
366 mDataSize = len;
367 mFdsKnown = false;
368 }
369 return err;
370}
371
Andreas Huber51faf462011-04-13 10:21:56 -0700372status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700373{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700374 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700375 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800376 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700377 size_t size = parcel->mObjectsSize;
378 int startPos = mDataPos;
379 int firstIndex = -1, lastIndex = -2;
380
381 if (len == 0) {
382 return NO_ERROR;
383 }
384
Nick Kralevichb6b14232015-04-02 09:36:02 -0700385 if (len > INT32_MAX) {
386 // don't accept size_t values which may have come from an
387 // inadvertent conversion from a negative int.
388 return BAD_VALUE;
389 }
390
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700391 // range checks against the source parcel size
392 if ((offset > parcel->mDataSize)
393 || (len > parcel->mDataSize)
394 || (offset + len > parcel->mDataSize)) {
395 return BAD_VALUE;
396 }
397
398 // Count objects in range
399 for (int i = 0; i < (int) size; i++) {
400 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700401 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700402 if (firstIndex == -1) {
403 firstIndex = i;
404 }
405 lastIndex = i;
406 }
407 }
408 int numObjects = lastIndex - firstIndex + 1;
409
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700410 if ((mDataSize+len) > mDataCapacity) {
411 // grow data
412 err = growData(len);
413 if (err != NO_ERROR) {
414 return err;
415 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700416 }
417
418 // append data
419 memcpy(mData + mDataPos, data + offset, len);
420 mDataPos += len;
421 mDataSize += len;
422
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400423 err = NO_ERROR;
424
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700425 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200426 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700427 // grow objects
428 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +0100429 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
430 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700431 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +0100432 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800433 binder_size_t *objects =
434 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700435 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700436 return NO_MEMORY;
437 }
438 mObjects = objects;
439 mObjectsCapacity = newSize;
440 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700441
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700442 // append and acquire objects
443 int idx = mObjectsSize;
444 for (int i = firstIndex; i <= lastIndex; i++) {
445 size_t off = objects[i] - offset + startPos;
446 mObjects[idx++] = off;
447 mObjectsSize++;
448
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700449 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700450 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700451 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700452
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700453 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700454 // If this is a file descriptor, we need to dup it so the
455 // new Parcel now owns its own fd, and can declare that we
456 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800457 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800458 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400460 if (!mAllowFds) {
461 err = FDS_NOT_ALLOWED;
462 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700463 }
464 }
465 }
466
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400467 return err;
468}
469
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700470int Parcel::compareData(const Parcel& other) {
471 size_t size = dataSize();
472 if (size != other.dataSize()) {
473 return size < other.dataSize() ? -1 : 1;
474 }
475 return memcmp(data(), other.data(), size);
476}
477
Jeff Brown13b16042014-11-11 16:44:25 -0800478bool Parcel::allowFds() const
479{
480 return mAllowFds;
481}
482
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700483bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400484{
485 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700486 if (!allowFds) {
487 mAllowFds = false;
488 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400489 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700490}
491
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700492void Parcel::restoreAllowFds(bool lastValue)
493{
494 mAllowFds = lastValue;
495}
496
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700497bool Parcel::hasFileDescriptors() const
498{
499 if (!mFdsKnown) {
500 scanForFds();
501 }
502 return mHasFds;
503}
504
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000505void Parcel::updateWorkSourceRequestHeaderPosition() const {
506 // Only update the request headers once. We only want to point
507 // to the first headers read/written.
508 if (!mRequestHeaderPresent) {
509 mWorkSourceRequestHeaderPosition = dataPosition();
510 mRequestHeaderPresent = true;
511 }
512}
513
Jooyung Han1b228f42020-01-30 13:41:12 +0900514#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700515constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
516#else
517constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
518#endif
519
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700520// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521status_t Parcel::writeInterfaceToken(const String16& interface)
522{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000523 return writeInterfaceToken(interface.string(), interface.size());
524}
525
526status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Olivier Gaillard91a04802018-11-14 17:32:41 +0000527 const IPCThreadState* threadState = IPCThreadState::self();
528 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000529 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000530 writeInt32(threadState->shouldPropagateWorkSource() ?
531 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700532 writeInt32(kHeader);
Steven Morelanddbc76c72020-10-01 18:02:48 +0000533
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700534 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000535 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700536}
537
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000538bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
539{
540 if (!mRequestHeaderPresent) {
541 return false;
542 }
543
544 const size_t initialPosition = dataPosition();
545 setDataPosition(mWorkSourceRequestHeaderPosition);
546 status_t err = writeInt32(uid);
547 setDataPosition(initialPosition);
548 return err == NO_ERROR;
549}
550
Steven Morelandf1b1e492019-05-06 15:05:13 -0700551uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000552{
553 if (!mRequestHeaderPresent) {
554 return IPCThreadState::kUnsetWorkSource;
555 }
556
557 const size_t initialPosition = dataPosition();
558 setDataPosition(mWorkSourceRequestHeaderPosition);
559 uid_t uid = readInt32();
560 setDataPosition(initialPosition);
561 return uid;
562}
563
Mathias Agopian83c04462009-05-22 19:00:22 -0700564bool Parcel::checkInterface(IBinder* binder) const
565{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700566 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700567}
568
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700569bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700570 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700571{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700572 return enforceInterface(interface.string(), interface.size(), threadState);
573}
574
575bool Parcel::enforceInterface(const char16_t* interface,
576 size_t len,
577 IPCThreadState* threadState) const
578{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100579 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700580 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700581 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700582 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700583 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700584 if ((threadState->getLastTransactionBinderFlags() &
585 IBinder::FLAG_ONEWAY) != 0) {
586 // For one-way calls, the callee is running entirely
587 // disconnected from the caller, so disable StrictMode entirely.
588 // Not only does disk/network usage not impact the caller, but
589 // there's no way to commuicate back any violations anyway.
590 threadState->setStrictModePolicy(0);
591 } else {
592 threadState->setStrictModePolicy(strictPolicy);
593 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100594 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000595 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100596 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000597 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700598 // vendor header
599 int32_t header = readInt32();
600 if (header != kHeader) {
601 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
602 return false;
603 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100604 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700605 size_t parcel_interface_len;
606 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
607 if (len == parcel_interface_len &&
608 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700609 return true;
610 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700611 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700612 String8(interface, len).string(),
613 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700614 return false;
615 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700616}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700617
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700618size_t Parcel::objectsCount() const
619{
620 return mObjectsSize;
621}
622
623status_t Parcel::errorCheck() const
624{
625 return mError;
626}
627
628void Parcel::setError(status_t err)
629{
630 mError = err;
631}
632
633status_t Parcel::finishWrite(size_t len)
634{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700635 if (len > INT32_MAX) {
636 // don't accept size_t values which may have come from an
637 // inadvertent conversion from a negative int.
638 return BAD_VALUE;
639 }
640
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700641 //printf("Finish write of %d\n", len);
642 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700643 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700644 if (mDataPos > mDataSize) {
645 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700646 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700647 }
648 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
649 return NO_ERROR;
650}
651
652status_t Parcel::writeUnpadded(const void* data, size_t len)
653{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700654 if (len > INT32_MAX) {
655 // don't accept size_t values which may have come from an
656 // inadvertent conversion from a negative int.
657 return BAD_VALUE;
658 }
659
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700660 size_t end = mDataPos + len;
661 if (end < mDataPos) {
662 // integer overflow
663 return BAD_VALUE;
664 }
665
666 if (end <= mDataCapacity) {
667restart_write:
668 memcpy(mData+mDataPos, data, len);
669 return finishWrite(len);
670 }
671
672 status_t err = growData(len);
673 if (err == NO_ERROR) goto restart_write;
674 return err;
675}
676
677status_t Parcel::write(const void* data, size_t len)
678{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700679 if (len > INT32_MAX) {
680 // don't accept size_t values which may have come from an
681 // inadvertent conversion from a negative int.
682 return BAD_VALUE;
683 }
684
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700685 void* const d = writeInplace(len);
686 if (d) {
687 memcpy(d, data, len);
688 return NO_ERROR;
689 }
690 return mError;
691}
692
693void* Parcel::writeInplace(size_t len)
694{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700695 if (len > INT32_MAX) {
696 // don't accept size_t values which may have come from an
697 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700698 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700699 }
700
701 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700702
703 // sanity check for integer overflow
704 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700705 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700706 }
707
708 if ((mDataPos+padded) <= mDataCapacity) {
709restart_write:
710 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
711 uint8_t* const data = mData+mDataPos;
712
713 // Need to pad at end?
714 if (padded != len) {
715#if BYTE_ORDER == BIG_ENDIAN
716 static const uint32_t mask[4] = {
717 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
718 };
719#endif
720#if BYTE_ORDER == LITTLE_ENDIAN
721 static const uint32_t mask[4] = {
722 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
723 };
724#endif
725 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
726 // *reinterpret_cast<void**>(data+padded-4));
727 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
728 }
729
730 finishWrite(padded);
731 return data;
732 }
733
734 status_t err = growData(padded);
735 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700736 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700737}
738
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800739status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
740 const uint8_t* strData = (uint8_t*)str.data();
741 const size_t strLen= str.length();
742 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100743 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800744 return BAD_VALUE;
745 }
746
747 status_t err = writeInt32(utf16Len);
748 if (err) {
749 return err;
750 }
751
752 // Allocate enough bytes to hold our converted string and its terminating NULL.
753 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
754 if (!dst) {
755 return NO_MEMORY;
756 }
757
Sergio Girof4607432016-07-21 14:46:35 +0100758 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800759
760 return NO_ERROR;
761}
762
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900763status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) {
764 if (!str) {
765 return writeInt32(-1);
766 }
767 return writeUtf8AsUtf16(*str);
768}
769
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800770status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
771 if (!str) {
772 return writeInt32(-1);
773 }
774 return writeUtf8AsUtf16(*str);
775}
776
Daniel Normand0337ef2019-09-20 15:46:03 -0700777status_t Parcel::writeByteVectorInternal(const int8_t* data, size_t size) {
778 if (size > std::numeric_limits<int32_t>::max()) {
779 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -0700780 }
781
Daniel Normand0337ef2019-09-20 15:46:03 -0700782 status_t status = writeInt32(size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700783 if (status != OK) {
784 return status;
785 }
786
Daniel Normand0337ef2019-09-20 15:46:03 -0700787 return write(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700788}
789
Casey Dahlin185d3442016-02-09 11:08:35 -0800790status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700791 return writeByteVectorInternal(val.data(), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800792}
793
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900794status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val)
795{
796 if (!val) return writeInt32(-1);
797 return writeByteVectorInternal(val->data(), val->size());
798}
799
Casey Dahlin185d3442016-02-09 11:08:35 -0800800status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
801{
Daniel Normand0337ef2019-09-20 15:46:03 -0700802 if (!val) return writeInt32(-1);
803 return writeByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800804}
805
806status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700807 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val.data()), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800808}
809
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900810status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val)
811{
812 if (!val) return writeInt32(-1);
813 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
814}
815
Casey Dahlin185d3442016-02-09 11:08:35 -0800816status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
817{
Daniel Normand0337ef2019-09-20 15:46:03 -0700818 if (!val) return writeInt32(-1);
819 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800820}
821
Casey Dahlin451ff582015-10-19 18:12:18 -0700822status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
823{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800824 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700825}
826
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900827status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val)
828{
829 return writeNullableTypedVector(val, &Parcel::writeInt32);
830}
831
Casey Dahlinb9872622015-11-25 15:09:45 -0800832status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
833{
834 return writeNullableTypedVector(val, &Parcel::writeInt32);
835}
836
Casey Dahlin451ff582015-10-19 18:12:18 -0700837status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
838{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800839 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700840}
841
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900842status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val)
843{
844 return writeNullableTypedVector(val, &Parcel::writeInt64);
845}
846
Casey Dahlinb9872622015-11-25 15:09:45 -0800847status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
848{
849 return writeNullableTypedVector(val, &Parcel::writeInt64);
850}
851
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800852status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
853{
854 return writeTypedVector(val, &Parcel::writeUint64);
855}
856
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900857status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val)
858{
859 return writeNullableTypedVector(val, &Parcel::writeUint64);
860}
861
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800862status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
863{
864 return writeNullableTypedVector(val, &Parcel::writeUint64);
865}
866
Casey Dahlin451ff582015-10-19 18:12:18 -0700867status_t Parcel::writeFloatVector(const std::vector<float>& val)
868{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800869 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700870}
871
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900872status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val)
873{
874 return writeNullableTypedVector(val, &Parcel::writeFloat);
875}
876
Casey Dahlinb9872622015-11-25 15:09:45 -0800877status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
878{
879 return writeNullableTypedVector(val, &Parcel::writeFloat);
880}
881
Casey Dahlin451ff582015-10-19 18:12:18 -0700882status_t Parcel::writeDoubleVector(const std::vector<double>& val)
883{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800884 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700885}
886
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900887status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val)
888{
889 return writeNullableTypedVector(val, &Parcel::writeDouble);
890}
891
Casey Dahlinb9872622015-11-25 15:09:45 -0800892status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
893{
894 return writeNullableTypedVector(val, &Parcel::writeDouble);
895}
896
Casey Dahlin451ff582015-10-19 18:12:18 -0700897status_t Parcel::writeBoolVector(const std::vector<bool>& val)
898{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800899 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700900}
901
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900902status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val)
903{
904 return writeNullableTypedVector(val, &Parcel::writeBool);
905}
906
Casey Dahlinb9872622015-11-25 15:09:45 -0800907status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
908{
909 return writeNullableTypedVector(val, &Parcel::writeBool);
910}
911
Casey Dahlin451ff582015-10-19 18:12:18 -0700912status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
913{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800914 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700915}
916
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900917status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val)
918{
919 return writeNullableTypedVector(val, &Parcel::writeChar);
920}
921
Casey Dahlinb9872622015-11-25 15:09:45 -0800922status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
923{
924 return writeNullableTypedVector(val, &Parcel::writeChar);
925}
926
Casey Dahlin451ff582015-10-19 18:12:18 -0700927status_t Parcel::writeString16Vector(const std::vector<String16>& val)
928{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800929 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700930}
931
Casey Dahlinb9872622015-11-25 15:09:45 -0800932status_t Parcel::writeString16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900933 const std::optional<std::vector<std::optional<String16>>>& val)
934{
935 return writeNullableTypedVector(val, &Parcel::writeString16);
936}
937
938status_t Parcel::writeString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800939 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
940{
941 return writeNullableTypedVector(val, &Parcel::writeString16);
942}
943
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800944status_t Parcel::writeUtf8VectorAsUtf16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900945 const std::optional<std::vector<std::optional<std::string>>>& val) {
946 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
947}
948
949status_t Parcel::writeUtf8VectorAsUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800950 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
951 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
952}
953
954status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
955 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
956}
957
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700958status_t Parcel::writeInt32(int32_t val)
959{
Andreas Huber84a6d042009-08-17 13:33:27 -0700960 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700961}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800962
963status_t Parcel::writeUint32(uint32_t val)
964{
965 return writeAligned(val);
966}
967
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700968status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700969 if (len > INT32_MAX) {
970 // don't accept size_t values which may have come from an
971 // inadvertent conversion from a negative int.
972 return BAD_VALUE;
973 }
974
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700975 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700976 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700977 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700978 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700979 if (ret == NO_ERROR) {
980 ret = write(val, len * sizeof(*val));
981 }
982 return ret;
983}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700984status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700985 if (len > INT32_MAX) {
986 // don't accept size_t values which may have come from an
987 // inadvertent conversion from a negative int.
988 return BAD_VALUE;
989 }
990
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700991 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700992 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700993 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700994 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700995 if (ret == NO_ERROR) {
996 ret = write(val, len * sizeof(*val));
997 }
998 return ret;
999}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001000
Casey Dahlind6848f52015-10-15 15:44:59 -07001001status_t Parcel::writeBool(bool val)
1002{
1003 return writeInt32(int32_t(val));
1004}
1005
1006status_t Parcel::writeChar(char16_t val)
1007{
1008 return writeInt32(int32_t(val));
1009}
1010
1011status_t Parcel::writeByte(int8_t val)
1012{
1013 return writeInt32(int32_t(val));
1014}
1015
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001016status_t Parcel::writeInt64(int64_t val)
1017{
Andreas Huber84a6d042009-08-17 13:33:27 -07001018 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001019}
1020
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001021status_t Parcel::writeUint64(uint64_t val)
1022{
1023 return writeAligned(val);
1024}
1025
Serban Constantinescuf683e012013-11-05 16:53:55 +00001026status_t Parcel::writePointer(uintptr_t val)
1027{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001028 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001029}
1030
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001031status_t Parcel::writeFloat(float val)
1032{
Andreas Huber84a6d042009-08-17 13:33:27 -07001033 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001034}
1035
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001036#if defined(__mips__) && defined(__mips_hard_float)
1037
1038status_t Parcel::writeDouble(double val)
1039{
1040 union {
1041 double d;
1042 unsigned long long ll;
1043 } u;
1044 u.d = val;
1045 return writeAligned(u.ll);
1046}
1047
1048#else
1049
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001050status_t Parcel::writeDouble(double val)
1051{
Andreas Huber84a6d042009-08-17 13:33:27 -07001052 return writeAligned(val);
1053}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001054
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001055#endif
1056
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001057status_t Parcel::writeCString(const char* str)
1058{
1059 return write(str, strlen(str)+1);
1060}
1061
1062status_t Parcel::writeString8(const String8& str)
1063{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001064 return writeString8(str.string(), str.size());
1065}
1066
1067status_t Parcel::writeString8(const char* str, size_t len)
1068{
1069 if (str == nullptr) return writeInt32(-1);
1070
1071 status_t err = writeInt32(len);
1072 if (err == NO_ERROR) {
1073 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1074 if (data) {
1075 memcpy(data, str, len);
1076 *reinterpret_cast<char*>(data+len) = 0;
1077 return NO_ERROR;
1078 }
1079 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001080 }
1081 return err;
1082}
1083
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001084status_t Parcel::writeString16(const std::optional<String16>& str)
1085{
1086 if (!str) {
1087 return writeInt32(-1);
1088 }
1089
1090 return writeString16(*str);
1091}
1092
Casey Dahlinb9872622015-11-25 15:09:45 -08001093status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1094{
1095 if (!str) {
1096 return writeInt32(-1);
1097 }
1098
1099 return writeString16(*str);
1100}
1101
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001102status_t Parcel::writeString16(const String16& str)
1103{
1104 return writeString16(str.string(), str.size());
1105}
1106
1107status_t Parcel::writeString16(const char16_t* str, size_t len)
1108{
Yi Kong91635562018-06-07 14:38:36 -07001109 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001110
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001111 status_t err = writeInt32(len);
1112 if (err == NO_ERROR) {
1113 len *= sizeof(char16_t);
1114 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1115 if (data) {
1116 memcpy(data, str, len);
1117 *reinterpret_cast<char16_t*>(data+len) = 0;
1118 return NO_ERROR;
1119 }
1120 err = mError;
1121 }
1122 return err;
1123}
1124
1125status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1126{
Steven Morelanda86a3562019-08-01 23:28:34 +00001127 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001128}
1129
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001130status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1131{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001132 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001133}
1134
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001135status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val)
1136{
1137 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1138}
1139
Casey Dahlinb9872622015-11-25 15:09:45 -08001140status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1141{
1142 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1143}
1144
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001145status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const {
1146 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
1147}
1148
Casey Dahlinb9872622015-11-25 15:09:45 -08001149status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001150 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001151}
1152
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001153status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001154 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001155}
1156
Casey Dahlinb9872622015-11-25 15:09:45 -08001157status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1158 if (!parcelable) {
1159 return writeInt32(0);
1160 }
1161
1162 return writeParcelable(*parcelable);
1163}
1164
Christopher Wiley97f048d2015-11-19 06:49:05 -08001165status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1166 status_t status = writeInt32(1); // parcelable is not null.
1167 if (status != OK) {
1168 return status;
1169 }
1170 return parcelable.writeToParcel(this);
1171}
1172
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001173status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001174{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001175 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001176 return BAD_TYPE;
1177
1178 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001179 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001180 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001182 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001183 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001185 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1186 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187
1188 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001189 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 return err;
1191 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001192 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001193 return err;
1194}
1195
Jeff Brown93ff1f92011-11-04 19:01:44 -07001196status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001197{
1198 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001199 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001200 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001201 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001202 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001203 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001204 return writeObject(obj, true);
1205}
1206
1207status_t Parcel::writeDupFileDescriptor(int fd)
1208{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001209 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001210 if (dupFd < 0) {
1211 return -errno;
1212 }
1213 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001214 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001215 close(dupFd);
1216 }
1217 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001218}
1219
Dianne Hackborn1941a402016-08-29 12:30:43 -07001220status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1221{
1222 writeInt32(0);
1223 return writeFileDescriptor(fd, takeOwnership);
1224}
1225
Ryo Hashimotobf551892018-05-31 16:58:35 +09001226status_t Parcel::writeDupParcelFileDescriptor(int fd)
1227{
1228 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1229 if (dupFd < 0) {
1230 return -errno;
1231 }
1232 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1233 if (err != OK) {
1234 close(dupFd);
1235 }
1236 return err;
1237}
1238
Christopher Wiley2cf19952016-04-11 11:09:37 -07001239status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001240 return writeDupFileDescriptor(fd.get());
1241}
1242
Christopher Wiley2cf19952016-04-11 11:09:37 -07001243status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001244 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1245}
1246
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001247status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) {
1248 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1249}
1250
Christopher Wiley2cf19952016-04-11 11:09:37 -07001251status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001252 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1253}
1254
Jeff Brown13b16042014-11-11 16:44:25 -08001255status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001256{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001257 if (len > INT32_MAX) {
1258 // don't accept size_t values which may have come from an
1259 // inadvertent conversion from a negative int.
1260 return BAD_VALUE;
1261 }
1262
Jeff Brown13b16042014-11-11 16:44:25 -08001263 status_t status;
1264 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001265 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001266 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001267 if (status) return status;
1268
1269 void* ptr = writeInplace(len);
1270 if (!ptr) return NO_MEMORY;
1271
Jeff Brown13b16042014-11-11 16:44:25 -08001272 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001273 return NO_ERROR;
1274 }
1275
Steve Block6807e592011-10-20 11:56:00 +01001276 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001277 int fd = ashmem_create_region("Parcel Blob", len);
1278 if (fd < 0) return NO_MEMORY;
1279
1280 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1281 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001282 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001283 } else {
Yi Kong91635562018-06-07 14:38:36 -07001284 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001285 if (ptr == MAP_FAILED) {
1286 status = -errno;
1287 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001288 if (!mutableCopy) {
1289 result = ashmem_set_prot_region(fd, PROT_READ);
1290 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001291 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001292 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001293 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001294 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001295 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001296 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001297 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001298 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001299 return NO_ERROR;
1300 }
1301 }
1302 }
1303 }
1304 ::munmap(ptr, len);
1305 }
1306 ::close(fd);
1307 return status;
1308}
1309
Jeff Brown13b16042014-11-11 16:44:25 -08001310status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1311{
1312 // Must match up with what's done in writeBlob.
1313 if (!mAllowFds) return FDS_NOT_ALLOWED;
1314 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1315 if (status) return status;
1316 return writeDupFileDescriptor(fd);
1317}
1318
Mathias Agopiane1424282013-07-29 21:24:40 -07001319status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001320{
1321 status_t err;
1322
1323 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001324 const size_t len = val.getFlattenedSize();
1325 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001326
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001327 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001328 // don't accept size_t values which may have come from an
1329 // inadvertent conversion from a negative int.
1330 return BAD_VALUE;
1331 }
1332
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001333 err = this->writeInt32(len);
1334 if (err) return err;
1335
1336 err = this->writeInt32(fd_count);
1337 if (err) return err;
1338
1339 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001340 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001341 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001342 return BAD_VALUE;
1343
Yi Kong91635562018-06-07 14:38:36 -07001344 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001345 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001346 fds = new (std::nothrow) int[fd_count];
1347 if (fds == nullptr) {
1348 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1349 return BAD_VALUE;
1350 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001351 }
1352
1353 err = val.flatten(buf, len, fds, fd_count);
1354 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1355 err = this->writeDupFileDescriptor( fds[i] );
1356 }
1357
1358 if (fd_count) {
1359 delete [] fds;
1360 }
1361
1362 return err;
1363}
1364
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001365status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1366{
1367 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1368 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1369 if (enoughData && enoughObjects) {
1370restart_write:
1371 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001372
Christopher Tate98e67d32015-06-03 18:44:15 -07001373 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001374 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001375 if (!mAllowFds) {
1376 // fail before modifying our object index
1377 return FDS_NOT_ALLOWED;
1378 }
1379 mHasFds = mFdsKnown = true;
1380 }
1381
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001382 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001383 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001384 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001385 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001386 mObjectsSize++;
1387 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001388
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001389 return finishWrite(sizeof(flat_binder_object));
1390 }
1391
1392 if (!enoughData) {
1393 const status_t err = growData(sizeof(val));
1394 if (err != NO_ERROR) return err;
1395 }
1396 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001397 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1398 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001399 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001400 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001401 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001402 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001403 mObjects = objects;
1404 mObjectsCapacity = newSize;
1405 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001406
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001407 goto restart_write;
1408}
1409
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001410status_t Parcel::writeNoException()
1411{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001412 binder::Status status;
1413 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001414}
1415
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001416status_t Parcel::validateReadData(size_t upperBound) const
1417{
1418 // Don't allow non-object reads on object data
1419 if (mObjectsSorted || mObjectsSize <= 1) {
1420data_sorted:
1421 // Expect to check only against the next object
1422 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1423 // For some reason the current read position is greater than the next object
1424 // hint. Iterate until we find the right object
1425 size_t nextObject = mNextObjectHint;
1426 do {
1427 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1428 // Requested info overlaps with an object
1429 ALOGE("Attempt to read from protected data in Parcel %p", this);
1430 return PERMISSION_DENIED;
1431 }
1432 nextObject++;
1433 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1434 mNextObjectHint = nextObject;
1435 }
1436 return NO_ERROR;
1437 }
1438 // Quickly determine if mObjects is sorted.
1439 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1440 binder_size_t* prevObj = currObj;
1441 while (currObj > mObjects) {
1442 prevObj--;
1443 if(*prevObj > *currObj) {
1444 goto data_unsorted;
1445 }
1446 currObj--;
1447 }
1448 mObjectsSorted = true;
1449 goto data_sorted;
1450
1451data_unsorted:
1452 // Insertion Sort mObjects
1453 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1454 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1455 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1456 binder_size_t temp = *iter0;
1457 binder_size_t* iter1 = iter0 - 1;
1458 while (iter1 >= mObjects && *iter1 > temp) {
1459 *(iter1 + 1) = *iter1;
1460 iter1--;
1461 }
1462 *(iter1 + 1) = temp;
1463 }
1464 mNextObjectHint = 0;
1465 mObjectsSorted = true;
1466 goto data_sorted;
1467}
1468
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001469status_t Parcel::read(void* outData, size_t len) const
1470{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001471 if (len > INT32_MAX) {
1472 // don't accept size_t values which may have come from an
1473 // inadvertent conversion from a negative int.
1474 return BAD_VALUE;
1475 }
1476
1477 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1478 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001479 if (mObjectsSize > 0) {
1480 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001481 if(err != NO_ERROR) {
1482 // Still increment the data position by the expected length
1483 mDataPos += pad_size(len);
1484 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1485 return err;
1486 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001487 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001488 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001489 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001490 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001491 return NO_ERROR;
1492 }
1493 return NOT_ENOUGH_DATA;
1494}
1495
1496const void* Parcel::readInplace(size_t len) const
1497{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001498 if (len > INT32_MAX) {
1499 // don't accept size_t values which may have come from an
1500 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001501 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001502 }
1503
1504 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1505 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001506 if (mObjectsSize > 0) {
1507 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001508 if(err != NO_ERROR) {
1509 // Still increment the data position by the expected length
1510 mDataPos += pad_size(len);
1511 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001512 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001513 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001514 }
1515
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001516 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001517 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001518 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001519 return data;
1520 }
Yi Kong91635562018-06-07 14:38:36 -07001521 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001522}
1523
Andreas Huber84a6d042009-08-17 13:33:27 -07001524template<class T>
1525status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001526 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001527
1528 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001529 if (mObjectsSize > 0) {
1530 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001531 if(err != NO_ERROR) {
1532 // Still increment the data position by the expected length
1533 mDataPos += sizeof(T);
1534 return err;
1535 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001536 }
1537
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001538 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001539 mDataPos += sizeof(T);
1540 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001541 return NO_ERROR;
1542 } else {
1543 return NOT_ENOUGH_DATA;
1544 }
1545}
1546
Andreas Huber84a6d042009-08-17 13:33:27 -07001547template<class T>
1548T Parcel::readAligned() const {
1549 T result;
1550 if (readAligned(&result) != NO_ERROR) {
1551 result = 0;
1552 }
1553
1554 return result;
1555}
1556
1557template<class T>
1558status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001559 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001560
1561 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1562restart_write:
1563 *reinterpret_cast<T*>(mData+mDataPos) = val;
1564 return finishWrite(sizeof(val));
1565 }
1566
1567 status_t err = growData(sizeof(val));
1568 if (err == NO_ERROR) goto restart_write;
1569 return err;
1570}
1571
Casey Dahlin185d3442016-02-09 11:08:35 -08001572status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001573 size_t size;
1574 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1575 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001576}
1577
1578status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001579 size_t size;
1580 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1581 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001582}
1583
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001584status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const {
1585 size_t size;
1586 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1587 if (!*val) {
1588 // reserveOutVector does not create the out vector if size is < 0.
1589 // This occurs when writing a null byte vector.
1590 return OK;
1591 }
1592 return readByteVectorInternal(&**val, size);
1593}
1594
Casey Dahlin185d3442016-02-09 11:08:35 -08001595status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001596 size_t size;
1597 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001598 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001599 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001600 // This occurs when writing a null byte vector.
1601 return OK;
1602 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001603 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001604}
1605
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001606status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const {
1607 size_t size;
1608 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1609 if (!*val) {
1610 // reserveOutVector does not create the out vector if size is < 0.
1611 // This occurs when writing a null byte vector.
1612 return OK;
1613 }
1614 return readByteVectorInternal(&**val, size);
1615}
1616
Casey Dahlin185d3442016-02-09 11:08:35 -08001617status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001618 size_t size;
1619 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001620 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001621 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001622 // This occurs when writing a null byte vector.
1623 return OK;
1624 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001625 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001626}
1627
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001628status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const {
1629 return readNullableTypedVector(val, &Parcel::readInt32);
1630}
1631
Casey Dahlinb9872622015-11-25 15:09:45 -08001632status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1633 return readNullableTypedVector(val, &Parcel::readInt32);
1634}
1635
Casey Dahlin451ff582015-10-19 18:12:18 -07001636status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001637 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001638}
1639
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001640status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const {
1641 return readNullableTypedVector(val, &Parcel::readInt64);
1642}
1643
Casey Dahlinb9872622015-11-25 15:09:45 -08001644status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1645 return readNullableTypedVector(val, &Parcel::readInt64);
1646}
1647
Casey Dahlin451ff582015-10-19 18:12:18 -07001648status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001649 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001650}
1651
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001652status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const {
1653 return readNullableTypedVector(val, &Parcel::readUint64);
1654}
1655
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001656status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1657 return readNullableTypedVector(val, &Parcel::readUint64);
1658}
1659
1660status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1661 return readTypedVector(val, &Parcel::readUint64);
1662}
1663
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001664status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const {
1665 return readNullableTypedVector(val, &Parcel::readFloat);
1666}
1667
Casey Dahlinb9872622015-11-25 15:09:45 -08001668status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1669 return readNullableTypedVector(val, &Parcel::readFloat);
1670}
1671
Casey Dahlin451ff582015-10-19 18:12:18 -07001672status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001673 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001674}
1675
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001676status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const {
1677 return readNullableTypedVector(val, &Parcel::readDouble);
1678}
1679
Casey Dahlinb9872622015-11-25 15:09:45 -08001680status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1681 return readNullableTypedVector(val, &Parcel::readDouble);
1682}
1683
Casey Dahlin451ff582015-10-19 18:12:18 -07001684status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001685 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001686}
1687
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001688status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const {
1689 const int32_t start = dataPosition();
1690 int32_t size;
1691 status_t status = readInt32(&size);
1692 val->reset();
1693
1694 if (status != OK || size < 0) {
1695 return status;
1696 }
1697
1698 setDataPosition(start);
1699 val->emplace();
1700
1701 status = readBoolVector(&**val);
1702
1703 if (status != OK) {
1704 val->reset();
1705 }
1706
1707 return status;
1708}
1709
Casey Dahlinb9872622015-11-25 15:09:45 -08001710status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1711 const int32_t start = dataPosition();
1712 int32_t size;
1713 status_t status = readInt32(&size);
1714 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001715
Casey Dahlinb9872622015-11-25 15:09:45 -08001716 if (status != OK || size < 0) {
1717 return status;
1718 }
1719
1720 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001721 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001722
1723 status = readBoolVector(val->get());
1724
1725 if (status != OK) {
1726 val->reset();
1727 }
1728
1729 return status;
1730}
1731
1732status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001733 int32_t size;
1734 status_t status = readInt32(&size);
1735
1736 if (status != OK) {
1737 return status;
1738 }
1739
1740 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001741 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001742 }
1743
1744 val->resize(size);
1745
1746 /* C++ bool handling means a vector of bools isn't necessarily addressable
1747 * (we might use individual bits)
1748 */
Christopher Wiley97887982015-10-27 16:33:47 -07001749 bool data;
1750 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001751 status = readBool(&data);
1752 (*val)[i] = data;
1753
1754 if (status != OK) {
1755 return status;
1756 }
1757 }
1758
1759 return OK;
1760}
1761
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001762status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const {
1763 return readNullableTypedVector(val, &Parcel::readChar);
1764}
1765
Casey Dahlinb9872622015-11-25 15:09:45 -08001766status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1767 return readNullableTypedVector(val, &Parcel::readChar);
1768}
1769
Casey Dahlin451ff582015-10-19 18:12:18 -07001770status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001771 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001772}
1773
Casey Dahlinb9872622015-11-25 15:09:45 -08001774status_t Parcel::readString16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001775 std::optional<std::vector<std::optional<String16>>>* val) const {
1776 return readNullableTypedVector(val, &Parcel::readString16);
1777}
1778
1779status_t Parcel::readString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -08001780 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1781 return readNullableTypedVector(val, &Parcel::readString16);
1782}
1783
Casey Dahlin451ff582015-10-19 18:12:18 -07001784status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001785 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001786}
1787
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001788status_t Parcel::readUtf8VectorFromUtf16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001789 std::optional<std::vector<std::optional<std::string>>>* val) const {
1790 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1791}
1792
1793status_t Parcel::readUtf8VectorFromUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001794 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1795 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1796}
1797
1798status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1799 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1800}
Casey Dahlin451ff582015-10-19 18:12:18 -07001801
Andreas Huber84a6d042009-08-17 13:33:27 -07001802status_t Parcel::readInt32(int32_t *pArg) const
1803{
1804 return readAligned(pArg);
1805}
1806
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001807int32_t Parcel::readInt32() const
1808{
Andreas Huber84a6d042009-08-17 13:33:27 -07001809 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001810}
1811
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001812status_t Parcel::readUint32(uint32_t *pArg) const
1813{
1814 return readAligned(pArg);
1815}
1816
1817uint32_t Parcel::readUint32() const
1818{
1819 return readAligned<uint32_t>();
1820}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001821
1822status_t Parcel::readInt64(int64_t *pArg) const
1823{
Andreas Huber84a6d042009-08-17 13:33:27 -07001824 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001825}
1826
1827
1828int64_t Parcel::readInt64() const
1829{
Andreas Huber84a6d042009-08-17 13:33:27 -07001830 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001831}
1832
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001833status_t Parcel::readUint64(uint64_t *pArg) const
1834{
1835 return readAligned(pArg);
1836}
1837
1838uint64_t Parcel::readUint64() const
1839{
1840 return readAligned<uint64_t>();
1841}
1842
Serban Constantinescuf683e012013-11-05 16:53:55 +00001843status_t Parcel::readPointer(uintptr_t *pArg) const
1844{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001845 status_t ret;
1846 binder_uintptr_t ptr;
1847 ret = readAligned(&ptr);
1848 if (!ret)
1849 *pArg = ptr;
1850 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001851}
1852
1853uintptr_t Parcel::readPointer() const
1854{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001855 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001856}
1857
1858
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001859status_t Parcel::readFloat(float *pArg) const
1860{
Andreas Huber84a6d042009-08-17 13:33:27 -07001861 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001862}
1863
1864
1865float Parcel::readFloat() const
1866{
Andreas Huber84a6d042009-08-17 13:33:27 -07001867 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001868}
1869
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001870#if defined(__mips__) && defined(__mips_hard_float)
1871
1872status_t Parcel::readDouble(double *pArg) const
1873{
1874 union {
1875 double d;
1876 unsigned long long ll;
1877 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001878 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001879 status_t status;
1880 status = readAligned(&u.ll);
1881 *pArg = u.d;
1882 return status;
1883}
1884
1885double Parcel::readDouble() const
1886{
1887 union {
1888 double d;
1889 unsigned long long ll;
1890 } u;
1891 u.ll = readAligned<unsigned long long>();
1892 return u.d;
1893}
1894
1895#else
1896
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001897status_t Parcel::readDouble(double *pArg) const
1898{
Andreas Huber84a6d042009-08-17 13:33:27 -07001899 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001900}
1901
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001902double Parcel::readDouble() const
1903{
Andreas Huber84a6d042009-08-17 13:33:27 -07001904 return readAligned<double>();
1905}
1906
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001907#endif
1908
Andreas Huber84a6d042009-08-17 13:33:27 -07001909status_t Parcel::readIntPtr(intptr_t *pArg) const
1910{
1911 return readAligned(pArg);
1912}
1913
1914
1915intptr_t Parcel::readIntPtr() const
1916{
1917 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001918}
1919
Casey Dahlind6848f52015-10-15 15:44:59 -07001920status_t Parcel::readBool(bool *pArg) const
1921{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001922 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001923 status_t ret = readInt32(&tmp);
1924 *pArg = (tmp != 0);
1925 return ret;
1926}
1927
1928bool Parcel::readBool() const
1929{
1930 return readInt32() != 0;
1931}
1932
1933status_t Parcel::readChar(char16_t *pArg) const
1934{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001935 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001936 status_t ret = readInt32(&tmp);
1937 *pArg = char16_t(tmp);
1938 return ret;
1939}
1940
1941char16_t Parcel::readChar() const
1942{
1943 return char16_t(readInt32());
1944}
1945
1946status_t Parcel::readByte(int8_t *pArg) const
1947{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001948 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001949 status_t ret = readInt32(&tmp);
1950 *pArg = int8_t(tmp);
1951 return ret;
1952}
1953
1954int8_t Parcel::readByte() const
1955{
1956 return int8_t(readInt32());
1957}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001958
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001959status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1960 size_t utf16Size = 0;
1961 const char16_t* src = readString16Inplace(&utf16Size);
1962 if (!src) {
1963 return UNEXPECTED_NULL;
1964 }
1965
1966 // Save ourselves the trouble, we're done.
1967 if (utf16Size == 0u) {
1968 str->clear();
1969 return NO_ERROR;
1970 }
1971
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001972 // Allow for closing '\0'
1973 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1974 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001975 return BAD_VALUE;
1976 }
1977 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001978 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001979 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001980 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1981 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001982 return NO_ERROR;
1983}
1984
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001985status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const {
1986 const int32_t start = dataPosition();
1987 int32_t size;
1988 status_t status = readInt32(&size);
1989 str->reset();
1990
1991 if (status != OK || size < 0) {
1992 return status;
1993 }
1994
1995 setDataPosition(start);
1996 str->emplace();
1997 return readUtf8FromUtf16(&**str);
1998}
1999
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002000status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2001 const int32_t start = dataPosition();
2002 int32_t size;
2003 status_t status = readInt32(&size);
2004 str->reset();
2005
2006 if (status != OK || size < 0) {
2007 return status;
2008 }
2009
2010 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002011 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002012 return readUtf8FromUtf16(str->get());
2013}
2014
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002015const char* Parcel::readCString() const
2016{
Steven Morelandd0d4b582019-05-17 13:14:06 -07002017 if (mDataPos < mDataSize) {
2018 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002019 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2020 // is the string's trailing NUL within the parcel's valid bounds?
2021 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2022 if (eos) {
2023 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002024 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002025 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002026 return str;
2027 }
2028 }
Yi Kong91635562018-06-07 14:38:36 -07002029 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002030}
2031
2032String8 Parcel::readString8() const
2033{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002034 size_t len;
2035 const char* str = readString8Inplace(&len);
2036 if (str) return String8(str, len);
2037 ALOGE("Reading a NULL string not supported here.");
2038 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002039}
2040
2041status_t Parcel::readString8(String8* pArg) const
2042{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002043 size_t len;
2044 const char* str = readString8Inplace(&len);
2045 if (str) {
2046 pArg->setTo(str, len);
2047 return 0;
2048 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002049 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002050 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002051 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002052}
2053
2054const char* Parcel::readString8Inplace(size_t* outLen) const
2055{
2056 int32_t size = readInt32();
2057 // watch for potential int overflow from size+1
2058 if (size >= 0 && size < INT32_MAX) {
2059 *outLen = size;
2060 const char* str = (const char*)readInplace(size+1);
2061 if (str != nullptr) {
2062 return str;
2063 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002064 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002065 *outLen = 0;
2066 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002067}
2068
2069String16 Parcel::readString16() const
2070{
2071 size_t len;
2072 const char16_t* str = readString16Inplace(&len);
2073 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002074 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002075 return String16();
2076}
2077
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002078status_t Parcel::readString16(std::optional<String16>* pArg) const
2079{
2080 const int32_t start = dataPosition();
2081 int32_t size;
2082 status_t status = readInt32(&size);
2083 pArg->reset();
2084
2085 if (status != OK || size < 0) {
2086 return status;
2087 }
2088
2089 setDataPosition(start);
2090 pArg->emplace();
2091
2092 status = readString16(&**pArg);
2093
2094 if (status != OK) {
2095 pArg->reset();
2096 }
2097
2098 return status;
2099}
2100
Casey Dahlinb9872622015-11-25 15:09:45 -08002101status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2102{
2103 const int32_t start = dataPosition();
2104 int32_t size;
2105 status_t status = readInt32(&size);
2106 pArg->reset();
2107
2108 if (status != OK || size < 0) {
2109 return status;
2110 }
2111
2112 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002113 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002114
2115 status = readString16(pArg->get());
2116
2117 if (status != OK) {
2118 pArg->reset();
2119 }
2120
2121 return status;
2122}
2123
Casey Dahlin451ff582015-10-19 18:12:18 -07002124status_t Parcel::readString16(String16* pArg) const
2125{
2126 size_t len;
2127 const char16_t* str = readString16Inplace(&len);
2128 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002129 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002130 return 0;
2131 } else {
2132 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002133 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002134 }
2135}
2136
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002137const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2138{
2139 int32_t size = readInt32();
2140 // watch for potential int overflow from size+1
2141 if (size >= 0 && size < INT32_MAX) {
2142 *outLen = size;
2143 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002144 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002145 return str;
2146 }
2147 }
2148 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002149 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002150}
2151
Casey Dahlinf0c13772015-10-27 18:33:56 -07002152status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2153{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002154 status_t status = readNullableStrongBinder(val);
2155 if (status == OK && !val->get()) {
2156 status = UNEXPECTED_NULL;
2157 }
2158 return status;
2159}
2160
2161status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2162{
Steven Morelanda86a3562019-08-01 23:28:34 +00002163 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002164}
2165
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002166sp<IBinder> Parcel::readStrongBinder() const
2167{
2168 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002169 // Note that a lot of code in Android reads binders by hand with this
2170 // method, and that code has historically been ok with getting nullptr
2171 // back (while ignoring error codes).
2172 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002173 return val;
2174}
2175
Christopher Wiley97f048d2015-11-19 06:49:05 -08002176status_t Parcel::readParcelable(Parcelable* parcelable) const {
2177 int32_t have_parcelable = 0;
2178 status_t status = readInt32(&have_parcelable);
2179 if (status != OK) {
2180 return status;
2181 }
2182 if (!have_parcelable) {
2183 return UNEXPECTED_NULL;
2184 }
2185 return parcelable->readFromParcel(this);
2186}
2187
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002188int32_t Parcel::readExceptionCode() const
2189{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002190 binder::Status status;
2191 status.readFromParcel(*this);
2192 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002193}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002194
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002195native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002196{
2197 int numFds, numInts;
2198 status_t err;
2199 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002200 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002201 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002202 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002203
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002204 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002205 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002206 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002207 }
2208
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002209 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002210 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002211 if (h->data[i] < 0) {
2212 for (int j = 0; j < i; j++) {
2213 close(h->data[j]);
2214 }
2215 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002216 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002217 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002218 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002219 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002220 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002221 native_handle_close(h);
2222 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002223 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002224 }
2225 return h;
2226}
2227
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002228int Parcel::readFileDescriptor() const
2229{
2230 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002231
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002232 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002233 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002234 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002235
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002236 return BAD_TYPE;
2237}
2238
Dianne Hackborn1941a402016-08-29 12:30:43 -07002239int Parcel::readParcelFileDescriptor() const
2240{
2241 int32_t hasComm = readInt32();
2242 int fd = readFileDescriptor();
2243 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002244 // detach (owned by the binder driver)
2245 int comm = readFileDescriptor();
2246
2247 // warning: this must be kept in sync with:
2248 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2249 enum ParcelFileDescriptorStatus {
2250 DETACHED = 2,
2251 };
2252
2253#if BYTE_ORDER == BIG_ENDIAN
2254 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2255#endif
2256#if BYTE_ORDER == LITTLE_ENDIAN
2257 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2258#endif
2259
2260 ssize_t written = TEMP_FAILURE_RETRY(
2261 ::write(comm, &message, sizeof(message)));
2262
2263 if (written == -1 || written != sizeof(message)) {
2264 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2265 written, strerror(errno));
2266 return BAD_TYPE;
2267 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002268 }
2269 return fd;
2270}
2271
Christopher Wiley2cf19952016-04-11 11:09:37 -07002272status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002273{
2274 int got = readFileDescriptor();
2275
2276 if (got == BAD_TYPE) {
2277 return BAD_TYPE;
2278 }
2279
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002280 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002281
2282 if (val->get() < 0) {
2283 return BAD_VALUE;
2284 }
2285
2286 return OK;
2287}
2288
Ryo Hashimotobf551892018-05-31 16:58:35 +09002289status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2290{
2291 int got = readParcelFileDescriptor();
2292
2293 if (got == BAD_TYPE) {
2294 return BAD_TYPE;
2295 }
2296
2297 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2298
2299 if (val->get() < 0) {
2300 return BAD_VALUE;
2301 }
2302
2303 return OK;
2304}
Casey Dahlin06673e32015-11-23 13:24:23 -08002305
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002306status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const {
2307 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2308}
2309
Christopher Wiley2cf19952016-04-11 11:09:37 -07002310status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002311 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2312}
2313
Christopher Wiley2cf19952016-04-11 11:09:37 -07002314status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002315 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2316}
2317
Jeff Brown5707dbf2011-09-23 21:17:56 -07002318status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2319{
Jeff Brown13b16042014-11-11 16:44:25 -08002320 int32_t blobType;
2321 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002322 if (status) return status;
2323
Jeff Brown13b16042014-11-11 16:44:25 -08002324 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002325 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002326 const void* ptr = readInplace(len);
2327 if (!ptr) return BAD_VALUE;
2328
Jeff Brown13b16042014-11-11 16:44:25 -08002329 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002330 return NO_ERROR;
2331 }
2332
Steve Block6807e592011-10-20 11:56:00 +01002333 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002334 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002335 int fd = readFileDescriptor();
2336 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2337
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002338 if (!ashmem_valid(fd)) {
2339 ALOGE("invalid fd");
2340 return BAD_VALUE;
2341 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002342 int size = ashmem_get_size_region(fd);
2343 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002344 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002345 return BAD_VALUE;
2346 }
Yi Kong91635562018-06-07 14:38:36 -07002347 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002348 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002349 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002350
Jeff Brown13b16042014-11-11 16:44:25 -08002351 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002352 return NO_ERROR;
2353}
2354
Mathias Agopiane1424282013-07-29 21:24:40 -07002355status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002356{
2357 // size
2358 const size_t len = this->readInt32();
2359 const size_t fd_count = this->readInt32();
2360
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002361 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002362 // don't accept size_t values which may have come from an
2363 // inadvertent conversion from a negative int.
2364 return BAD_VALUE;
2365 }
2366
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002367 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002368 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002369 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002370 return BAD_VALUE;
2371
Yi Kong91635562018-06-07 14:38:36 -07002372 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002373 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002374 fds = new (std::nothrow) int[fd_count];
2375 if (fds == nullptr) {
2376 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2377 return BAD_VALUE;
2378 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002379 }
2380
2381 status_t err = NO_ERROR;
2382 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002383 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002384 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002385 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002386 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 -07002387 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2388 // Close all the file descriptors that were dup-ed.
2389 for (size_t j=0; j<i ;j++) {
2390 close(fds[j]);
2391 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002392 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002393 }
2394
2395 if (err == NO_ERROR) {
2396 err = val.unflatten(buf, len, fds, fd_count);
2397 }
2398
2399 if (fd_count) {
2400 delete [] fds;
2401 }
2402
2403 return err;
2404}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002405const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2406{
2407 const size_t DPOS = mDataPos;
2408 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2409 const flat_binder_object* obj
2410 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2411 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002412 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002413 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002414 // the object list, so we don't want to check for it when
2415 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002416 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002417 return obj;
2418 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002419
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002420 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002421 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002422 const size_t N = mObjectsSize;
2423 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002424
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002425 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002426 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002428
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002429 // Start at the current hint position, looking for an object at
2430 // the current data position.
2431 if (opos < N) {
2432 while (opos < (N-1) && OBJS[opos] < DPOS) {
2433 opos++;
2434 }
2435 } else {
2436 opos = N-1;
2437 }
2438 if (OBJS[opos] == DPOS) {
2439 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002440 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002441 this, DPOS, opos);
2442 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002443 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002444 return obj;
2445 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002446
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002447 // Look backwards for it...
2448 while (opos > 0 && OBJS[opos] > DPOS) {
2449 opos--;
2450 }
2451 if (OBJS[opos] == DPOS) {
2452 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002453 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002454 this, DPOS, opos);
2455 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002456 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002457 return obj;
2458 }
2459 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002460 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 -07002461 this, DPOS);
2462 }
Yi Kong91635562018-06-07 14:38:36 -07002463 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002464}
2465
2466void Parcel::closeFileDescriptors()
2467{
2468 size_t i = mObjectsSize;
2469 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002470 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002471 }
2472 while (i > 0) {
2473 i--;
2474 const flat_binder_object* flat
2475 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002476 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002477 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002478 close(flat->handle);
2479 }
2480 }
2481}
2482
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002483uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002484{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002485 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002486}
2487
2488size_t Parcel::ipcDataSize() const
2489{
2490 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2491}
2492
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002493uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002494{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002495 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002496}
2497
2498size_t Parcel::ipcObjectsCount() const
2499{
2500 return mObjectsSize;
2501}
2502
2503void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002504 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002505{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002506 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002507 freeDataNoInit();
2508 mError = NO_ERROR;
2509 mData = const_cast<uint8_t*>(data);
2510 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002511 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002512 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002513 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002514 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002515 mObjectsSize = mObjectsCapacity = objectsCount;
2516 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002517 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002518 mOwner = relFunc;
2519 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002520 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002521 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002522 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002523 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002524 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002525 mObjectsSize = 0;
2526 break;
2527 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002528 const flat_binder_object* flat
2529 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2530 uint32_t type = flat->hdr.type;
2531 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2532 type == BINDER_TYPE_FD)) {
2533 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2534 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2535 // recover gracefully by clearing out the objects, and releasing the objects we do
2536 // know about.
2537 android_errorWriteLog(0x534e4554, "135930648");
2538 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2539 __func__, type, (uint64_t)offset);
2540 releaseObjects();
2541 mObjectsSize = 0;
2542 break;
2543 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002544 minOffset = offset + sizeof(flat_binder_object);
2545 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002546 scanForFds();
2547}
2548
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002549void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002550{
2551 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002552
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002553 if (errorCheck() != NO_ERROR) {
2554 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002555 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002556 } else if (dataSize() > 0) {
2557 const uint8_t* DATA = data();
2558 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002559 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002560 const size_t N = objectsCount();
2561 for (size_t i=0; i<N; i++) {
2562 const flat_binder_object* flat
2563 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2564 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002565 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002566 << " = " << flat->binder;
2567 }
2568 } else {
2569 to << "NULL";
2570 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002571
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002572 to << ")";
2573}
2574
2575void Parcel::releaseObjects()
2576{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002577 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002578 if (i == 0) {
2579 return;
2580 }
2581 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002582 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002583 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002584 while (i > 0) {
2585 i--;
2586 const flat_binder_object* flat
2587 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002588 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002589 }
2590}
2591
2592void Parcel::acquireObjects()
2593{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002594 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002595 if (i == 0) {
2596 return;
2597 }
2598 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002599 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002600 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002601 while (i > 0) {
2602 i--;
2603 const flat_binder_object* flat
2604 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002605 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002606 }
2607}
2608
2609void Parcel::freeData()
2610{
2611 freeDataNoInit();
2612 initState();
2613}
2614
2615void Parcel::freeDataNoInit()
2616{
2617 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002618 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002619 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002620 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2621 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002622 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002623 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002624 if (mData) {
2625 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002626 gParcelGlobalAllocSize -= mDataCapacity;
2627 gParcelGlobalAllocCount--;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002628 free(mData);
2629 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002630 if (mObjects) free(mObjects);
2631 }
2632}
2633
2634status_t Parcel::growData(size_t len)
2635{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002636 if (len > INT32_MAX) {
2637 // don't accept size_t values which may have come from an
2638 // inadvertent conversion from a negative int.
2639 return BAD_VALUE;
2640 }
2641
Martijn Coenen93fe5182020-01-22 10:46:25 +01002642 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2643 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002644 size_t newSize = ((mDataSize+len)*3)/2;
2645 return (newSize <= mDataSize)
2646 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002647 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002648}
2649
2650status_t Parcel::restartWrite(size_t desired)
2651{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002652 if (desired > INT32_MAX) {
2653 // don't accept size_t values which may have come from an
2654 // inadvertent conversion from a negative int.
2655 return BAD_VALUE;
2656 }
2657
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002658 if (mOwner) {
2659 freeData();
2660 return continueWrite(desired);
2661 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002662
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002663 uint8_t* data = (uint8_t*)realloc(mData, desired);
2664 if (!data && desired > mDataCapacity) {
2665 mError = NO_MEMORY;
2666 return NO_MEMORY;
2667 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002668
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002669 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002670
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002671 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002672 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002673 if (mDataCapacity > desired) {
2674 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2675 } else {
2676 gParcelGlobalAllocSize += (desired - mDataCapacity);
2677 }
2678
Colin Cross83ec65e2015-12-08 17:15:50 -08002679 if (!mData) {
2680 gParcelGlobalAllocCount++;
2681 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002682 mData = data;
2683 mDataCapacity = desired;
2684 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002685
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002686 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002687 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2688 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2689
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002690 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002691 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002692 mObjectsSize = mObjectsCapacity = 0;
2693 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002694 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002695 mHasFds = false;
2696 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002697 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002698
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002699 return NO_ERROR;
2700}
2701
2702status_t Parcel::continueWrite(size_t desired)
2703{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002704 if (desired > INT32_MAX) {
2705 // don't accept size_t values which may have come from an
2706 // inadvertent conversion from a negative int.
2707 return BAD_VALUE;
2708 }
2709
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002710 // If shrinking, first adjust for any objects that appear
2711 // after the new data size.
2712 size_t objectsSize = mObjectsSize;
2713 if (desired < mDataSize) {
2714 if (desired == 0) {
2715 objectsSize = 0;
2716 } else {
2717 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002718 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002719 break;
2720 objectsSize--;
2721 }
2722 }
2723 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002724
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002725 if (mOwner) {
2726 // If the size is going to zero, just release the owner's data.
2727 if (desired == 0) {
2728 freeData();
2729 return NO_ERROR;
2730 }
2731
2732 // If there is a different owner, we need to take
2733 // posession.
2734 uint8_t* data = (uint8_t*)malloc(desired);
2735 if (!data) {
2736 mError = NO_MEMORY;
2737 return NO_MEMORY;
2738 }
Yi Kong91635562018-06-07 14:38:36 -07002739 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002740
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002741 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002742 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002743 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002744 free(data);
2745
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002746 mError = NO_MEMORY;
2747 return NO_MEMORY;
2748 }
2749
2750 // Little hack to only acquire references on objects
2751 // we will be keeping.
2752 size_t oldObjectsSize = mObjectsSize;
2753 mObjectsSize = objectsSize;
2754 acquireObjects();
2755 mObjectsSize = oldObjectsSize;
2756 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002757
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002758 if (mData) {
2759 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2760 }
2761 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002762 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002763 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002764 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002765 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002766 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002767
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002768 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002769 gParcelGlobalAllocSize += desired;
2770 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002771
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002772 mData = data;
2773 mObjects = objects;
2774 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002775 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002776 mDataCapacity = desired;
2777 mObjectsSize = mObjectsCapacity = objectsSize;
2778 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002779 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002780
2781 } else if (mData) {
2782 if (objectsSize < mObjectsSize) {
2783 // Need to release refs on any objects we are dropping.
2784 const sp<ProcessState> proc(ProcessState::self());
2785 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2786 const flat_binder_object* flat
2787 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002788 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002789 // will need to rescan because we may have lopped off the only FDs
2790 mFdsKnown = false;
2791 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002792 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002793 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002794
2795 if (objectsSize == 0) {
2796 free(mObjects);
2797 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002798 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002799 } else {
2800 binder_size_t* objects =
2801 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2802 if (objects) {
2803 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002804 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002805 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002806 }
2807 mObjectsSize = objectsSize;
2808 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002809 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002810 }
2811
2812 // We own the data, so we can just do a realloc().
2813 if (desired > mDataCapacity) {
2814 uint8_t* data = (uint8_t*)realloc(mData, desired);
2815 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002816 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2817 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002818 gParcelGlobalAllocSize += desired;
2819 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002820 mData = data;
2821 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002822 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002823 mError = NO_MEMORY;
2824 return NO_MEMORY;
2825 }
2826 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002827 if (mDataSize > desired) {
2828 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002829 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002830 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002831 if (mDataPos > desired) {
2832 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002833 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002834 }
2835 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002836
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002837 } else {
2838 // This is the first data. Easy!
2839 uint8_t* data = (uint8_t*)malloc(desired);
2840 if (!data) {
2841 mError = NO_MEMORY;
2842 return NO_MEMORY;
2843 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002844
Yi Kong91635562018-06-07 14:38:36 -07002845 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002846 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002847 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002848 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002849
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002850 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002851 gParcelGlobalAllocSize += desired;
2852 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002853
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002854 mData = data;
2855 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002856 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2857 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002858 mDataCapacity = desired;
2859 }
2860
2861 return NO_ERROR;
2862}
2863
2864void Parcel::initState()
2865{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002866 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002867 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002868 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002869 mDataSize = 0;
2870 mDataCapacity = 0;
2871 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002872 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2873 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002874 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002875 mObjectsSize = 0;
2876 mObjectsCapacity = 0;
2877 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002878 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002879 mHasFds = false;
2880 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002881 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002882 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002883 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002884 mWorkSourceRequestHeaderPosition = 0;
2885 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002886
2887 // racing multiple init leads only to multiple identical write
2888 if (gMaxFds == 0) {
2889 struct rlimit result;
2890 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2891 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002892 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002893 } else {
2894 ALOGW("Unable to getrlimit: %s", strerror(errno));
2895 gMaxFds = 1024;
2896 }
2897 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002898}
2899
2900void Parcel::scanForFds() const
2901{
2902 bool hasFds = false;
2903 for (size_t i=0; i<mObjectsSize; i++) {
2904 const flat_binder_object* flat
2905 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002906 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002907 hasFds = true;
2908 break;
2909 }
2910 }
2911 mHasFds = hasFds;
2912 mFdsKnown = true;
2913}
2914
Dan Sandleraa5c2342015-04-10 10:08:45 -04002915size_t Parcel::getBlobAshmemSize() const
2916{
Adrian Roos6bb31142015-10-22 16:46:12 -07002917 // This used to return the size of all blobs that were written to ashmem, now we're returning
2918 // the ashmem currently referenced by this Parcel, which should be equivalent.
2919 // TODO: Remove method once ABI can be changed.
2920 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002921}
2922
Adrian Rooscbf37262015-10-22 16:12:53 -07002923size_t Parcel::getOpenAshmemSize() const
2924{
2925 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002926}
2927
2928// --- Parcel::Blob ---
2929
2930Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002931 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002932}
2933
2934Parcel::Blob::~Blob() {
2935 release();
2936}
2937
2938void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002939 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002940 ::munmap(mData, mSize);
2941 }
2942 clear();
2943}
2944
Jeff Brown13b16042014-11-11 16:44:25 -08002945void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2946 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002947 mData = data;
2948 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002949 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002950}
2951
2952void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002953 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002954 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002955 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002956 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002957}
2958
Steven Moreland61ff8492019-09-26 16:05:45 -07002959} // namespace android