blob: 632458e78475049c2eea16657d5d24795da126f0 [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>
Mathias Agopian002e1e52013-05-06 20:20:50 -070044#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080045#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080047#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070048#include <utils/String8.h>
49#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070050
Mathias Agopian208059f2009-05-18 15:08:03 -070051#include <private/binder/binder_module.h>
Steven Morelanda4853cd2019-07-12 15:44:37 -070052#include "Static.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070054#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080055//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080056#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080057//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070058
59// ---------------------------------------------------------------------------
60
Nick Kralevichb6b14232015-04-02 09:36:02 -070061// This macro should never be used at runtime, as a too large value
62// of s could cause an integer overflow. Instead, you should always
63// use the wrapper function pad_size()
64#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
65
66static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070067 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070068 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070069 }
70 return PAD_SIZE_UNSAFE(s);
71}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070072
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070073// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060074#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070075
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070076namespace android {
77
Steven Moreland7b102262019-08-01 15:48:43 -070078// many things compile this into prebuilts on the stack
79static_assert(sizeof(Parcel) == 60 || sizeof(Parcel) == 120);
80
Dianne Hackborna4cff882014-11-13 17:07:40 -080081static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
82static size_t gParcelGlobalAllocSize = 0;
83static size_t gParcelGlobalAllocCount = 0;
84
Christopher Tatee4e0ae82016-03-24 16:03:44 -070085static size_t gMaxFds = 0;
86
Jeff Brown13b16042014-11-11 16:44:25 -080087// Maximum size of a blob to transfer in-place.
88static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
89
90enum {
91 BLOB_INPLACE = 0,
92 BLOB_ASHMEM_IMMUTABLE = 1,
93 BLOB_ASHMEM_MUTABLE = 2,
94};
95
Steven Morelandb1c81202019-04-05 18:49:55 -070096static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070097 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -070099 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700100 case BINDER_TYPE_BINDER:
101 if (obj.binder) {
102 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800103 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104 }
105 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106 case BINDER_TYPE_HANDLE: {
107 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700108 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
110 b->incStrong(who);
111 }
112 return;
113 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700114 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000115 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700116 // If we own an ashmem fd, keep track of how much memory it refers to.
117 int size = ashmem_get_size_region(obj.handle);
118 if (size > 0) {
119 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700120 }
121 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700122 return;
123 }
124 }
125
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700126 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700127}
128
Adrian Roos6bb31142015-10-22 16:46:12 -0700129static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700130 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700131{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700132 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700133 case BINDER_TYPE_BINDER:
134 if (obj.binder) {
135 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800136 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700137 }
138 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 case BINDER_TYPE_HANDLE: {
140 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700141 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
143 b->decStrong(who);
144 }
145 return;
146 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800148 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000149 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700150 int size = ashmem_get_size_region(obj.handle);
151 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800152 // ashmem size might have changed since last time it was accounted for, e.g.
153 // in acquire_object(). Value of *outAshmemSize is not critical since we are
154 // releasing the object anyway. Check for integer overflow condition.
155 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700156 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700157 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800158
159 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700160 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700161 return;
162 }
163 }
164
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700165 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700166}
167
Steven Morelanda86a3562019-08-01 23:28:34 +0000168status_t Parcel::finishFlattenBinder(
169 const sp<IBinder>& binder, const flat_binder_object& flat)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700170{
Steven Morelanda86a3562019-08-01 23:28:34 +0000171 status_t status = writeObject(flat, false);
172 if (status != OK) return status;
173
Steven Moreland6e5a7752019-08-05 20:30:14 -0700174 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Morelanda86a3562019-08-01 23:28:34 +0000175 return writeInt32(internal::Stability::get(binder.get()));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700176}
177
Steven Morelanda86a3562019-08-01 23:28:34 +0000178status_t Parcel::finishUnflattenBinder(
179 const sp<IBinder>& binder, sp<IBinder>* out) const
180{
181 int32_t stability;
182 status_t status = readInt32(&stability);
183 if (status != OK) return status;
184
Steven Moreland05929552019-07-31 17:51:25 -0700185 status = internal::Stability::set(binder.get(), stability, true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000186 if (status != OK) return status;
187
188 *out = binder;
189 return OK;
190}
191
Steven Morelandbf1915b2020-07-16 22:43:02 +0000192static constexpr inline int schedPolicyMask(int policy, int priority) {
193 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
194}
195
Steven Morelanda86a3562019-08-01 23:28:34 +0000196status_t Parcel::flattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700197{
198 flat_binder_object obj;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000199 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700200
Steven Morelandbf1915b2020-07-16 22:43:02 +0000201 int schedBits = 0;
202 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
203 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700204 }
205
Yi Kong91635562018-06-07 14:38:36 -0700206 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800207 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700208 if (!local) {
209 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700210 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000211 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700212 }
213 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700214 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800215 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700216 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800217 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700218 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000219 int policy = local->getMinSchedulerPolicy();
220 int priority = local->getMinSchedulerPriority();
221
222 if (policy != 0 || priority != 0) {
223 // override value, since it is set explicitly
224 schedBits = schedPolicyMask(policy, priority);
225 }
Steven Morelandf0212002018-12-26 13:59:23 -0800226 if (local->isRequestingSid()) {
227 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
228 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700229 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800230 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
231 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700232 }
233 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700234 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800235 obj.binder = 0;
236 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700237 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700238
Steven Morelandbf1915b2020-07-16 22:43:02 +0000239 obj.flags |= schedBits;
240
Steven Morelanda86a3562019-08-01 23:28:34 +0000241 return finishFlattenBinder(binder, obj);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700242}
243
Steven Morelanda86a3562019-08-01 23:28:34 +0000244status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700245{
Steven Morelanda86a3562019-08-01 23:28:34 +0000246 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700247
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700249 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000250 case BINDER_TYPE_BINDER: {
251 sp<IBinder> binder = reinterpret_cast<IBinder*>(flat->cookie);
252 return finishUnflattenBinder(binder, out);
253 }
254 case BINDER_TYPE_HANDLE: {
255 sp<IBinder> binder =
256 ProcessState::self()->getStrongProxyForHandle(flat->handle);
257 return finishUnflattenBinder(binder, out);
258 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700259 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700260 }
261 return BAD_TYPE;
262}
263
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700264// ---------------------------------------------------------------------------
265
266Parcel::Parcel()
267{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800268 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700269 initState();
270}
271
272Parcel::~Parcel()
273{
274 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800275 LOG_ALLOC("Parcel %p: destroyed", this);
276}
277
278size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800279 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
280 size_t size = gParcelGlobalAllocSize;
281 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
282 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800283}
284
285size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800286 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
287 size_t count = gParcelGlobalAllocCount;
288 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
289 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700290}
291
292const uint8_t* Parcel::data() const
293{
294 return mData;
295}
296
297size_t Parcel::dataSize() const
298{
299 return (mDataSize > mDataPos ? mDataSize : mDataPos);
300}
301
302size_t Parcel::dataAvail() const
303{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700304 size_t result = dataSize() - dataPosition();
305 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700306 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700307 }
308 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700309}
310
311size_t Parcel::dataPosition() const
312{
313 return mDataPos;
314}
315
316size_t Parcel::dataCapacity() const
317{
318 return mDataCapacity;
319}
320
321status_t Parcel::setDataSize(size_t size)
322{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700323 if (size > INT32_MAX) {
324 // don't accept size_t values which may have come from an
325 // inadvertent conversion from a negative int.
326 return BAD_VALUE;
327 }
328
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700329 status_t err;
330 err = continueWrite(size);
331 if (err == NO_ERROR) {
332 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700333 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700334 }
335 return err;
336}
337
338void Parcel::setDataPosition(size_t pos) const
339{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700340 if (pos > INT32_MAX) {
341 // don't accept size_t values which may have come from an
342 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700343 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700344 }
345
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700346 mDataPos = pos;
347 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800348 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700349}
350
351status_t Parcel::setDataCapacity(size_t size)
352{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700353 if (size > INT32_MAX) {
354 // don't accept size_t values which may have come from an
355 // inadvertent conversion from a negative int.
356 return BAD_VALUE;
357 }
358
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700359 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700360 return NO_ERROR;
361}
362
363status_t Parcel::setData(const uint8_t* buffer, size_t len)
364{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700365 if (len > INT32_MAX) {
366 // don't accept size_t values which may have come from an
367 // inadvertent conversion from a negative int.
368 return BAD_VALUE;
369 }
370
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700371 status_t err = restartWrite(len);
372 if (err == NO_ERROR) {
373 memcpy(const_cast<uint8_t*>(data()), buffer, len);
374 mDataSize = len;
375 mFdsKnown = false;
376 }
377 return err;
378}
379
Andreas Huber51faf462011-04-13 10:21:56 -0700380status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700381{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700382 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700383 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800384 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700385 size_t size = parcel->mObjectsSize;
386 int startPos = mDataPos;
387 int firstIndex = -1, lastIndex = -2;
388
389 if (len == 0) {
390 return NO_ERROR;
391 }
392
Nick Kralevichb6b14232015-04-02 09:36:02 -0700393 if (len > INT32_MAX) {
394 // don't accept size_t values which may have come from an
395 // inadvertent conversion from a negative int.
396 return BAD_VALUE;
397 }
398
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700399 // range checks against the source parcel size
400 if ((offset > parcel->mDataSize)
401 || (len > parcel->mDataSize)
402 || (offset + len > parcel->mDataSize)) {
403 return BAD_VALUE;
404 }
405
406 // Count objects in range
407 for (int i = 0; i < (int) size; i++) {
408 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700409 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700410 if (firstIndex == -1) {
411 firstIndex = i;
412 }
413 lastIndex = i;
414 }
415 }
416 int numObjects = lastIndex - firstIndex + 1;
417
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700418 if ((mDataSize+len) > mDataCapacity) {
419 // grow data
420 err = growData(len);
421 if (err != NO_ERROR) {
422 return err;
423 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700424 }
425
426 // append data
427 memcpy(mData + mDataPos, data + offset, len);
428 mDataPos += len;
429 mDataSize += len;
430
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400431 err = NO_ERROR;
432
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700433 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200434 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700435 // grow objects
436 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenenda2f2fd2020-01-22 10:46:25 +0100437 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
438 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700439 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenenda2f2fd2020-01-22 10:46:25 +0100440 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800441 binder_size_t *objects =
442 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700443 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700444 return NO_MEMORY;
445 }
446 mObjects = objects;
447 mObjectsCapacity = newSize;
448 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700449
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700450 // append and acquire objects
451 int idx = mObjectsSize;
452 for (int i = firstIndex; i <= lastIndex; i++) {
453 size_t off = objects[i] - offset + startPos;
454 mObjects[idx++] = off;
455 mObjectsSize++;
456
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700457 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700458 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700459 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700460
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700461 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700462 // If this is a file descriptor, we need to dup it so the
463 // new Parcel now owns its own fd, and can declare that we
464 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800465 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800466 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700467 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400468 if (!mAllowFds) {
469 err = FDS_NOT_ALLOWED;
470 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700471 }
472 }
473 }
474
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400475 return err;
476}
477
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700478int Parcel::compareData(const Parcel& other) {
479 size_t size = dataSize();
480 if (size != other.dataSize()) {
481 return size < other.dataSize() ? -1 : 1;
482 }
483 return memcmp(data(), other.data(), size);
484}
485
Jeff Brown13b16042014-11-11 16:44:25 -0800486bool Parcel::allowFds() const
487{
488 return mAllowFds;
489}
490
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700491bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400492{
493 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700494 if (!allowFds) {
495 mAllowFds = false;
496 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400497 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700498}
499
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700500void Parcel::restoreAllowFds(bool lastValue)
501{
502 mAllowFds = lastValue;
503}
504
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700505bool Parcel::hasFileDescriptors() const
506{
507 if (!mFdsKnown) {
508 scanForFds();
509 }
510 return mHasFds;
511}
512
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000513void Parcel::updateWorkSourceRequestHeaderPosition() const {
514 // Only update the request headers once. We only want to point
515 // to the first headers read/written.
516 if (!mRequestHeaderPresent) {
517 mWorkSourceRequestHeaderPosition = dataPosition();
518 mRequestHeaderPresent = true;
519 }
520}
521
Jooyung Han1b228f42020-01-30 13:41:12 +0900522#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Moreland0f452742019-07-31 15:50:51 +0000523constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
524#else
525constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
526#endif
527
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700528// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700529status_t Parcel::writeInterfaceToken(const String16& interface)
530{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000531 const IPCThreadState* threadState = IPCThreadState::self();
532 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000533 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000534 writeInt32(threadState->shouldPropagateWorkSource() ?
535 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Moreland0f452742019-07-31 15:50:51 +0000536 writeInt32(kHeader);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700537 // currently the interface identification token is just its name as a string
538 return writeString16(interface);
539}
540
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000541bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
542{
543 if (!mRequestHeaderPresent) {
544 return false;
545 }
546
547 const size_t initialPosition = dataPosition();
548 setDataPosition(mWorkSourceRequestHeaderPosition);
549 status_t err = writeInt32(uid);
550 setDataPosition(initialPosition);
551 return err == NO_ERROR;
552}
553
Steven Moreland0891c9b2019-05-06 15:05:13 -0700554uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000555{
556 if (!mRequestHeaderPresent) {
557 return IPCThreadState::kUnsetWorkSource;
558 }
559
560 const size_t initialPosition = dataPosition();
561 setDataPosition(mWorkSourceRequestHeaderPosition);
562 uid_t uid = readInt32();
563 setDataPosition(initialPosition);
564 return uid;
565}
566
Mathias Agopian83c04462009-05-22 19:00:22 -0700567bool Parcel::checkInterface(IBinder* binder) const
568{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700569 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700570}
571
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700572bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700573 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700574{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700575 return enforceInterface(interface.string(), interface.size(), threadState);
576}
577
578bool Parcel::enforceInterface(const char16_t* interface,
579 size_t len,
580 IPCThreadState* threadState) const
581{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100582 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700583 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700584 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700585 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700586 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700587 if ((threadState->getLastTransactionBinderFlags() &
588 IBinder::FLAG_ONEWAY) != 0) {
589 // For one-way calls, the callee is running entirely
590 // disconnected from the caller, so disable StrictMode entirely.
591 // Not only does disk/network usage not impact the caller, but
592 // there's no way to commuicate back any violations anyway.
593 threadState->setStrictModePolicy(0);
594 } else {
595 threadState->setStrictModePolicy(strictPolicy);
596 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100597 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000598 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100599 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000600 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Moreland0f452742019-07-31 15:50:51 +0000601 // vendor header
602 int32_t header = readInt32();
603 if (header != kHeader) {
604 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
605 return false;
606 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100607 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700608 size_t parcel_interface_len;
609 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
610 if (len == parcel_interface_len &&
611 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700612 return true;
613 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700614 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700615 String8(interface, len).string(),
616 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700617 return false;
618 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700619}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700621size_t Parcel::objectsCount() const
622{
623 return mObjectsSize;
624}
625
626status_t Parcel::errorCheck() const
627{
628 return mError;
629}
630
631void Parcel::setError(status_t err)
632{
633 mError = err;
634}
635
636status_t Parcel::finishWrite(size_t len)
637{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700638 if (len > INT32_MAX) {
639 // don't accept size_t values which may have come from an
640 // inadvertent conversion from a negative int.
641 return BAD_VALUE;
642 }
643
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700644 //printf("Finish write of %d\n", len);
645 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700646 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700647 if (mDataPos > mDataSize) {
648 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700649 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700650 }
651 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
652 return NO_ERROR;
653}
654
655status_t Parcel::writeUnpadded(const void* data, size_t len)
656{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700657 if (len > INT32_MAX) {
658 // don't accept size_t values which may have come from an
659 // inadvertent conversion from a negative int.
660 return BAD_VALUE;
661 }
662
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700663 size_t end = mDataPos + len;
664 if (end < mDataPos) {
665 // integer overflow
666 return BAD_VALUE;
667 }
668
669 if (end <= mDataCapacity) {
670restart_write:
671 memcpy(mData+mDataPos, data, len);
672 return finishWrite(len);
673 }
674
675 status_t err = growData(len);
676 if (err == NO_ERROR) goto restart_write;
677 return err;
678}
679
680status_t Parcel::write(const void* data, size_t len)
681{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700682 if (len > INT32_MAX) {
683 // don't accept size_t values which may have come from an
684 // inadvertent conversion from a negative int.
685 return BAD_VALUE;
686 }
687
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700688 void* const d = writeInplace(len);
689 if (d) {
690 memcpy(d, data, len);
691 return NO_ERROR;
692 }
693 return mError;
694}
695
696void* Parcel::writeInplace(size_t len)
697{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700698 if (len > INT32_MAX) {
699 // don't accept size_t values which may have come from an
700 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700701 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700702 }
703
704 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700705
706 // sanity check for integer overflow
707 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700708 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700709 }
710
711 if ((mDataPos+padded) <= mDataCapacity) {
712restart_write:
713 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
714 uint8_t* const data = mData+mDataPos;
715
716 // Need to pad at end?
717 if (padded != len) {
718#if BYTE_ORDER == BIG_ENDIAN
719 static const uint32_t mask[4] = {
720 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
721 };
722#endif
723#if BYTE_ORDER == LITTLE_ENDIAN
724 static const uint32_t mask[4] = {
725 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
726 };
727#endif
728 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
729 // *reinterpret_cast<void**>(data+padded-4));
730 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
731 }
732
733 finishWrite(padded);
734 return data;
735 }
736
737 status_t err = growData(padded);
738 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700739 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700740}
741
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800742status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
743 const uint8_t* strData = (uint8_t*)str.data();
744 const size_t strLen= str.length();
745 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100746 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800747 return BAD_VALUE;
748 }
749
750 status_t err = writeInt32(utf16Len);
751 if (err) {
752 return err;
753 }
754
755 // Allocate enough bytes to hold our converted string and its terminating NULL.
756 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
757 if (!dst) {
758 return NO_MEMORY;
759 }
760
Sergio Girof4607432016-07-21 14:46:35 +0100761 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800762
763 return NO_ERROR;
764}
765
Jooyung Han2d5878e2020-01-23 12:45:10 +0900766status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) {
767 if (!str) {
768 return writeInt32(-1);
769 }
770 return writeUtf8AsUtf16(*str);
771}
772
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800773status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
774 if (!str) {
775 return writeInt32(-1);
776 }
777 return writeUtf8AsUtf16(*str);
778}
779
Daniel Normand0337ef2019-09-20 15:46:03 -0700780status_t Parcel::writeByteVectorInternal(const int8_t* data, size_t size) {
781 if (size > std::numeric_limits<int32_t>::max()) {
782 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -0700783 }
784
Daniel Normand0337ef2019-09-20 15:46:03 -0700785 status_t status = writeInt32(size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700786 if (status != OK) {
787 return status;
788 }
789
Daniel Normand0337ef2019-09-20 15:46:03 -0700790 return write(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700791}
792
Casey Dahlin185d3442016-02-09 11:08:35 -0800793status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700794 return writeByteVectorInternal(val.data(), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800795}
796
Jooyung Han2d5878e2020-01-23 12:45:10 +0900797status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val)
798{
799 if (!val) return writeInt32(-1);
800 return writeByteVectorInternal(val->data(), val->size());
801}
802
Casey Dahlin185d3442016-02-09 11:08:35 -0800803status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
804{
Daniel Normand0337ef2019-09-20 15:46:03 -0700805 if (!val) return writeInt32(-1);
806 return writeByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800807}
808
809status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700810 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val.data()), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800811}
812
Jooyung Han2d5878e2020-01-23 12:45:10 +0900813status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val)
814{
815 if (!val) return writeInt32(-1);
816 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
817}
818
Casey Dahlin185d3442016-02-09 11:08:35 -0800819status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
820{
Daniel Normand0337ef2019-09-20 15:46:03 -0700821 if (!val) return writeInt32(-1);
822 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800823}
824
Casey Dahlin451ff582015-10-19 18:12:18 -0700825status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
826{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800827 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700828}
829
Jooyung Han2d5878e2020-01-23 12:45:10 +0900830status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val)
831{
832 return writeNullableTypedVector(val, &Parcel::writeInt32);
833}
834
Casey Dahlinb9872622015-11-25 15:09:45 -0800835status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
836{
837 return writeNullableTypedVector(val, &Parcel::writeInt32);
838}
839
Casey Dahlin451ff582015-10-19 18:12:18 -0700840status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
841{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800842 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700843}
844
Jooyung Han2d5878e2020-01-23 12:45:10 +0900845status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val)
846{
847 return writeNullableTypedVector(val, &Parcel::writeInt64);
848}
849
Casey Dahlinb9872622015-11-25 15:09:45 -0800850status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
851{
852 return writeNullableTypedVector(val, &Parcel::writeInt64);
853}
854
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800855status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
856{
857 return writeTypedVector(val, &Parcel::writeUint64);
858}
859
Jooyung Han2d5878e2020-01-23 12:45:10 +0900860status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val)
861{
862 return writeNullableTypedVector(val, &Parcel::writeUint64);
863}
864
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800865status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
866{
867 return writeNullableTypedVector(val, &Parcel::writeUint64);
868}
869
Casey Dahlin451ff582015-10-19 18:12:18 -0700870status_t Parcel::writeFloatVector(const std::vector<float>& val)
871{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800872 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700873}
874
Jooyung Han2d5878e2020-01-23 12:45:10 +0900875status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val)
876{
877 return writeNullableTypedVector(val, &Parcel::writeFloat);
878}
879
Casey Dahlinb9872622015-11-25 15:09:45 -0800880status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
881{
882 return writeNullableTypedVector(val, &Parcel::writeFloat);
883}
884
Casey Dahlin451ff582015-10-19 18:12:18 -0700885status_t Parcel::writeDoubleVector(const std::vector<double>& val)
886{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800887 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700888}
889
Jooyung Han2d5878e2020-01-23 12:45:10 +0900890status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val)
891{
892 return writeNullableTypedVector(val, &Parcel::writeDouble);
893}
894
Casey Dahlinb9872622015-11-25 15:09:45 -0800895status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
896{
897 return writeNullableTypedVector(val, &Parcel::writeDouble);
898}
899
Casey Dahlin451ff582015-10-19 18:12:18 -0700900status_t Parcel::writeBoolVector(const std::vector<bool>& val)
901{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800902 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700903}
904
Jooyung Han2d5878e2020-01-23 12:45:10 +0900905status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val)
906{
907 return writeNullableTypedVector(val, &Parcel::writeBool);
908}
909
Casey Dahlinb9872622015-11-25 15:09:45 -0800910status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
911{
912 return writeNullableTypedVector(val, &Parcel::writeBool);
913}
914
Casey Dahlin451ff582015-10-19 18:12:18 -0700915status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
916{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800917 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700918}
919
Jooyung Han2d5878e2020-01-23 12:45:10 +0900920status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val)
921{
922 return writeNullableTypedVector(val, &Parcel::writeChar);
923}
924
Casey Dahlinb9872622015-11-25 15:09:45 -0800925status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
926{
927 return writeNullableTypedVector(val, &Parcel::writeChar);
928}
929
Casey Dahlin451ff582015-10-19 18:12:18 -0700930status_t Parcel::writeString16Vector(const std::vector<String16>& val)
931{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800932 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700933}
934
Casey Dahlinb9872622015-11-25 15:09:45 -0800935status_t Parcel::writeString16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +0900936 const std::optional<std::vector<std::optional<String16>>>& val)
937{
938 return writeNullableTypedVector(val, &Parcel::writeString16);
939}
940
941status_t Parcel::writeString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800942 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
943{
944 return writeNullableTypedVector(val, &Parcel::writeString16);
945}
946
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800947status_t Parcel::writeUtf8VectorAsUtf16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +0900948 const std::optional<std::vector<std::optional<std::string>>>& val) {
949 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
950}
951
952status_t Parcel::writeUtf8VectorAsUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800953 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
954 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
955}
956
957status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
958 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
959}
960
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700961status_t Parcel::writeInt32(int32_t val)
962{
Andreas Huber84a6d042009-08-17 13:33:27 -0700963 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700964}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800965
966status_t Parcel::writeUint32(uint32_t val)
967{
968 return writeAligned(val);
969}
970
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700971status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700972 if (len > INT32_MAX) {
973 // don't accept size_t values which may have come from an
974 // inadvertent conversion from a negative int.
975 return BAD_VALUE;
976 }
977
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700978 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700979 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700980 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700981 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700982 if (ret == NO_ERROR) {
983 ret = write(val, len * sizeof(*val));
984 }
985 return ret;
986}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700987status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700988 if (len > INT32_MAX) {
989 // don't accept size_t values which may have come from an
990 // inadvertent conversion from a negative int.
991 return BAD_VALUE;
992 }
993
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700994 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700995 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700996 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700997 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700998 if (ret == NO_ERROR) {
999 ret = write(val, len * sizeof(*val));
1000 }
1001 return ret;
1002}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001003
Casey Dahlind6848f52015-10-15 15:44:59 -07001004status_t Parcel::writeBool(bool val)
1005{
1006 return writeInt32(int32_t(val));
1007}
1008
1009status_t Parcel::writeChar(char16_t val)
1010{
1011 return writeInt32(int32_t(val));
1012}
1013
1014status_t Parcel::writeByte(int8_t val)
1015{
1016 return writeInt32(int32_t(val));
1017}
1018
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001019status_t Parcel::writeInt64(int64_t val)
1020{
Andreas Huber84a6d042009-08-17 13:33:27 -07001021 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001022}
1023
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001024status_t Parcel::writeUint64(uint64_t val)
1025{
1026 return writeAligned(val);
1027}
1028
Serban Constantinescuf683e012013-11-05 16:53:55 +00001029status_t Parcel::writePointer(uintptr_t val)
1030{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001031 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001032}
1033
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001034status_t Parcel::writeFloat(float val)
1035{
Andreas Huber84a6d042009-08-17 13:33:27 -07001036 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001037}
1038
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001039#if defined(__mips__) && defined(__mips_hard_float)
1040
1041status_t Parcel::writeDouble(double val)
1042{
1043 union {
1044 double d;
1045 unsigned long long ll;
1046 } u;
1047 u.d = val;
1048 return writeAligned(u.ll);
1049}
1050
1051#else
1052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001053status_t Parcel::writeDouble(double val)
1054{
Andreas Huber84a6d042009-08-17 13:33:27 -07001055 return writeAligned(val);
1056}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001057
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001058#endif
1059
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001060status_t Parcel::writeCString(const char* str)
1061{
1062 return write(str, strlen(str)+1);
1063}
1064
1065status_t Parcel::writeString8(const String8& str)
1066{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001067 return writeString8(str.string(), str.size());
1068}
1069
1070status_t Parcel::writeString8(const char* str, size_t len)
1071{
1072 if (str == nullptr) return writeInt32(-1);
1073
1074 status_t err = writeInt32(len);
1075 if (err == NO_ERROR) {
1076 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1077 if (data) {
1078 memcpy(data, str, len);
1079 *reinterpret_cast<char*>(data+len) = 0;
1080 return NO_ERROR;
1081 }
1082 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001083 }
1084 return err;
1085}
1086
Jooyung Han2d5878e2020-01-23 12:45:10 +09001087status_t Parcel::writeString16(const std::optional<String16>& str)
1088{
1089 if (!str) {
1090 return writeInt32(-1);
1091 }
1092
1093 return writeString16(*str);
1094}
1095
Casey Dahlinb9872622015-11-25 15:09:45 -08001096status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1097{
1098 if (!str) {
1099 return writeInt32(-1);
1100 }
1101
1102 return writeString16(*str);
1103}
1104
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001105status_t Parcel::writeString16(const String16& str)
1106{
1107 return writeString16(str.string(), str.size());
1108}
1109
1110status_t Parcel::writeString16(const char16_t* str, size_t len)
1111{
Yi Kong91635562018-06-07 14:38:36 -07001112 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001113
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001114 status_t err = writeInt32(len);
1115 if (err == NO_ERROR) {
1116 len *= sizeof(char16_t);
1117 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1118 if (data) {
1119 memcpy(data, str, len);
1120 *reinterpret_cast<char16_t*>(data+len) = 0;
1121 return NO_ERROR;
1122 }
1123 err = mError;
1124 }
1125 return err;
1126}
1127
1128status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1129{
Steven Morelanda86a3562019-08-01 23:28:34 +00001130 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001131}
1132
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001133status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1134{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001135 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001136}
1137
Jooyung Han2d5878e2020-01-23 12:45:10 +09001138status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val)
1139{
1140 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1141}
1142
Casey Dahlinb9872622015-11-25 15:09:45 -08001143status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1144{
1145 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1146}
1147
Jooyung Han2d5878e2020-01-23 12:45:10 +09001148status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const {
1149 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
1150}
1151
Casey Dahlinb9872622015-11-25 15:09:45 -08001152status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001153 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001154}
1155
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001156status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001157 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001158}
1159
Casey Dahlinb9872622015-11-25 15:09:45 -08001160status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1161 if (!parcelable) {
1162 return writeInt32(0);
1163 }
1164
1165 return writeParcelable(*parcelable);
1166}
1167
Christopher Wiley97f048d2015-11-19 06:49:05 -08001168status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1169 status_t status = writeInt32(1); // parcelable is not null.
1170 if (status != OK) {
1171 return status;
1172 }
1173 return parcelable.writeToParcel(this);
1174}
1175
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001176status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001177{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001178 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001179 return BAD_TYPE;
1180
1181 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001182 err = writeInt32(handle->numFds);
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 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001186 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001188 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1189 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190
1191 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001192 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193 return err;
1194 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001195 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001196 return err;
1197}
1198
Jeff Brown93ff1f92011-11-04 19:01:44 -07001199status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001200{
1201 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001202 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001203 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001204 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001205 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001206 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001207 return writeObject(obj, true);
1208}
1209
1210status_t Parcel::writeDupFileDescriptor(int fd)
1211{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001212 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001213 if (dupFd < 0) {
1214 return -errno;
1215 }
1216 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001217 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001218 close(dupFd);
1219 }
1220 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001221}
1222
Dianne Hackborn1941a402016-08-29 12:30:43 -07001223status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1224{
1225 writeInt32(0);
1226 return writeFileDescriptor(fd, takeOwnership);
1227}
1228
Ryo Hashimotobf551892018-05-31 16:58:35 +09001229status_t Parcel::writeDupParcelFileDescriptor(int fd)
1230{
1231 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1232 if (dupFd < 0) {
1233 return -errno;
1234 }
1235 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1236 if (err != OK) {
1237 close(dupFd);
1238 }
1239 return err;
1240}
1241
Christopher Wiley2cf19952016-04-11 11:09:37 -07001242status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001243 return writeDupFileDescriptor(fd.get());
1244}
1245
Christopher Wiley2cf19952016-04-11 11:09:37 -07001246status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001247 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1248}
1249
Jooyung Han2d5878e2020-01-23 12:45:10 +09001250status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) {
1251 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1252}
1253
Christopher Wiley2cf19952016-04-11 11:09:37 -07001254status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001255 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1256}
1257
Jeff Brown13b16042014-11-11 16:44:25 -08001258status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001259{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001260 if (len > INT32_MAX) {
1261 // don't accept size_t values which may have come from an
1262 // inadvertent conversion from a negative int.
1263 return BAD_VALUE;
1264 }
1265
Jeff Brown13b16042014-11-11 16:44:25 -08001266 status_t status;
1267 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001268 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001269 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001270 if (status) return status;
1271
1272 void* ptr = writeInplace(len);
1273 if (!ptr) return NO_MEMORY;
1274
Jeff Brown13b16042014-11-11 16:44:25 -08001275 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001276 return NO_ERROR;
1277 }
1278
Steve Block6807e592011-10-20 11:56:00 +01001279 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001280 int fd = ashmem_create_region("Parcel Blob", len);
1281 if (fd < 0) return NO_MEMORY;
1282
1283 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1284 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001285 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001286 } else {
Yi Kong91635562018-06-07 14:38:36 -07001287 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001288 if (ptr == MAP_FAILED) {
1289 status = -errno;
1290 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001291 if (!mutableCopy) {
1292 result = ashmem_set_prot_region(fd, PROT_READ);
1293 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001294 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001295 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001296 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001297 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001298 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001299 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001300 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001301 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001302 return NO_ERROR;
1303 }
1304 }
1305 }
1306 }
1307 ::munmap(ptr, len);
1308 }
1309 ::close(fd);
1310 return status;
1311}
1312
Jeff Brown13b16042014-11-11 16:44:25 -08001313status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1314{
1315 // Must match up with what's done in writeBlob.
1316 if (!mAllowFds) return FDS_NOT_ALLOWED;
1317 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1318 if (status) return status;
1319 return writeDupFileDescriptor(fd);
1320}
1321
Mathias Agopiane1424282013-07-29 21:24:40 -07001322status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001323{
1324 status_t err;
1325
1326 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001327 const size_t len = val.getFlattenedSize();
1328 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001329
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001330 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001331 // don't accept size_t values which may have come from an
1332 // inadvertent conversion from a negative int.
1333 return BAD_VALUE;
1334 }
1335
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001336 err = this->writeInt32(len);
1337 if (err) return err;
1338
1339 err = this->writeInt32(fd_count);
1340 if (err) return err;
1341
1342 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001343 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001344 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001345 return BAD_VALUE;
1346
Yi Kong91635562018-06-07 14:38:36 -07001347 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001348 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001349 fds = new (std::nothrow) int[fd_count];
1350 if (fds == nullptr) {
1351 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1352 return BAD_VALUE;
1353 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001354 }
1355
1356 err = val.flatten(buf, len, fds, fd_count);
1357 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1358 err = this->writeDupFileDescriptor( fds[i] );
1359 }
1360
1361 if (fd_count) {
1362 delete [] fds;
1363 }
1364
1365 return err;
1366}
1367
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001368status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1369{
1370 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1371 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1372 if (enoughData && enoughObjects) {
1373restart_write:
1374 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001375
Christopher Tate98e67d32015-06-03 18:44:15 -07001376 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001377 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001378 if (!mAllowFds) {
1379 // fail before modifying our object index
1380 return FDS_NOT_ALLOWED;
1381 }
1382 mHasFds = mFdsKnown = true;
1383 }
1384
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001385 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001386 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001387 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001388 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001389 mObjectsSize++;
1390 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001391
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001392 return finishWrite(sizeof(flat_binder_object));
1393 }
1394
1395 if (!enoughData) {
1396 const status_t err = growData(sizeof(val));
1397 if (err != NO_ERROR) return err;
1398 }
1399 if (!enoughObjects) {
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01001400 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1401 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001402 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01001403 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001404 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001405 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001406 mObjects = objects;
1407 mObjectsCapacity = newSize;
1408 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001409
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001410 goto restart_write;
1411}
1412
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001413status_t Parcel::writeNoException()
1414{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001415 binder::Status status;
1416 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001417}
1418
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001419status_t Parcel::validateReadData(size_t upperBound) const
1420{
1421 // Don't allow non-object reads on object data
1422 if (mObjectsSorted || mObjectsSize <= 1) {
1423data_sorted:
1424 // Expect to check only against the next object
1425 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1426 // For some reason the current read position is greater than the next object
1427 // hint. Iterate until we find the right object
1428 size_t nextObject = mNextObjectHint;
1429 do {
1430 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1431 // Requested info overlaps with an object
1432 ALOGE("Attempt to read from protected data in Parcel %p", this);
1433 return PERMISSION_DENIED;
1434 }
1435 nextObject++;
1436 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1437 mNextObjectHint = nextObject;
1438 }
1439 return NO_ERROR;
1440 }
1441 // Quickly determine if mObjects is sorted.
1442 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1443 binder_size_t* prevObj = currObj;
1444 while (currObj > mObjects) {
1445 prevObj--;
1446 if(*prevObj > *currObj) {
1447 goto data_unsorted;
1448 }
1449 currObj--;
1450 }
1451 mObjectsSorted = true;
1452 goto data_sorted;
1453
1454data_unsorted:
1455 // Insertion Sort mObjects
1456 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1457 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1458 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1459 binder_size_t temp = *iter0;
1460 binder_size_t* iter1 = iter0 - 1;
1461 while (iter1 >= mObjects && *iter1 > temp) {
1462 *(iter1 + 1) = *iter1;
1463 iter1--;
1464 }
1465 *(iter1 + 1) = temp;
1466 }
1467 mNextObjectHint = 0;
1468 mObjectsSorted = true;
1469 goto data_sorted;
1470}
1471
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001472status_t Parcel::read(void* outData, size_t len) const
1473{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001474 if (len > INT32_MAX) {
1475 // don't accept size_t values which may have come from an
1476 // inadvertent conversion from a negative int.
1477 return BAD_VALUE;
1478 }
1479
1480 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1481 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001482 if (mObjectsSize > 0) {
1483 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001484 if(err != NO_ERROR) {
1485 // Still increment the data position by the expected length
1486 mDataPos += pad_size(len);
1487 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1488 return err;
1489 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001490 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001491 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001492 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001493 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001494 return NO_ERROR;
1495 }
1496 return NOT_ENOUGH_DATA;
1497}
1498
1499const void* Parcel::readInplace(size_t len) const
1500{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001501 if (len > INT32_MAX) {
1502 // don't accept size_t values which may have come from an
1503 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001504 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001505 }
1506
1507 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1508 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001509 if (mObjectsSize > 0) {
1510 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001511 if(err != NO_ERROR) {
1512 // Still increment the data position by the expected length
1513 mDataPos += pad_size(len);
1514 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001515 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001516 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001517 }
1518
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001519 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001520 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001521 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001522 return data;
1523 }
Yi Kong91635562018-06-07 14:38:36 -07001524 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001525}
1526
Andreas Huber84a6d042009-08-17 13:33:27 -07001527template<class T>
1528status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001529 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001530
1531 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001532 if (mObjectsSize > 0) {
1533 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001534 if(err != NO_ERROR) {
1535 // Still increment the data position by the expected length
1536 mDataPos += sizeof(T);
1537 return err;
1538 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001539 }
1540
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001541 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001542 mDataPos += sizeof(T);
1543 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001544 return NO_ERROR;
1545 } else {
1546 return NOT_ENOUGH_DATA;
1547 }
1548}
1549
Andreas Huber84a6d042009-08-17 13:33:27 -07001550template<class T>
1551T Parcel::readAligned() const {
1552 T result;
1553 if (readAligned(&result) != NO_ERROR) {
1554 result = 0;
1555 }
1556
1557 return result;
1558}
1559
1560template<class T>
1561status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001562 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001563
1564 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1565restart_write:
1566 *reinterpret_cast<T*>(mData+mDataPos) = val;
1567 return finishWrite(sizeof(val));
1568 }
1569
1570 status_t err = growData(sizeof(val));
1571 if (err == NO_ERROR) goto restart_write;
1572 return err;
1573}
1574
Casey Dahlin185d3442016-02-09 11:08:35 -08001575status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001576 size_t size;
1577 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1578 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001579}
1580
1581status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001582 size_t size;
1583 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1584 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001585}
1586
Jooyung Han2d5878e2020-01-23 12:45:10 +09001587status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const {
1588 size_t size;
1589 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1590 if (!*val) {
1591 // reserveOutVector does not create the out vector if size is < 0.
1592 // This occurs when writing a null byte vector.
1593 return OK;
1594 }
1595 return readByteVectorInternal(&**val, size);
1596}
1597
Casey Dahlin185d3442016-02-09 11:08:35 -08001598status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001599 size_t size;
1600 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001601 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001602 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001603 // This occurs when writing a null byte vector.
1604 return OK;
1605 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001606 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001607}
1608
Jooyung Han2d5878e2020-01-23 12:45:10 +09001609status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const {
1610 size_t size;
1611 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1612 if (!*val) {
1613 // reserveOutVector does not create the out vector if size is < 0.
1614 // This occurs when writing a null byte vector.
1615 return OK;
1616 }
1617 return readByteVectorInternal(&**val, size);
1618}
1619
Casey Dahlin185d3442016-02-09 11:08:35 -08001620status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001621 size_t size;
1622 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001623 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001624 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001625 // This occurs when writing a null byte vector.
1626 return OK;
1627 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001628 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001629}
1630
Jooyung Han2d5878e2020-01-23 12:45:10 +09001631status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const {
1632 return readNullableTypedVector(val, &Parcel::readInt32);
1633}
1634
Casey Dahlinb9872622015-11-25 15:09:45 -08001635status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1636 return readNullableTypedVector(val, &Parcel::readInt32);
1637}
1638
Casey Dahlin451ff582015-10-19 18:12:18 -07001639status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001640 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001641}
1642
Jooyung Han2d5878e2020-01-23 12:45:10 +09001643status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const {
1644 return readNullableTypedVector(val, &Parcel::readInt64);
1645}
1646
Casey Dahlinb9872622015-11-25 15:09:45 -08001647status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1648 return readNullableTypedVector(val, &Parcel::readInt64);
1649}
1650
Casey Dahlin451ff582015-10-19 18:12:18 -07001651status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001652 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001653}
1654
Jooyung Han2d5878e2020-01-23 12:45:10 +09001655status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const {
1656 return readNullableTypedVector(val, &Parcel::readUint64);
1657}
1658
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001659status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1660 return readNullableTypedVector(val, &Parcel::readUint64);
1661}
1662
1663status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1664 return readTypedVector(val, &Parcel::readUint64);
1665}
1666
Jooyung Han2d5878e2020-01-23 12:45:10 +09001667status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const {
1668 return readNullableTypedVector(val, &Parcel::readFloat);
1669}
1670
Casey Dahlinb9872622015-11-25 15:09:45 -08001671status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1672 return readNullableTypedVector(val, &Parcel::readFloat);
1673}
1674
Casey Dahlin451ff582015-10-19 18:12:18 -07001675status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001676 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001677}
1678
Jooyung Han2d5878e2020-01-23 12:45:10 +09001679status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const {
1680 return readNullableTypedVector(val, &Parcel::readDouble);
1681}
1682
Casey Dahlinb9872622015-11-25 15:09:45 -08001683status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1684 return readNullableTypedVector(val, &Parcel::readDouble);
1685}
1686
Casey Dahlin451ff582015-10-19 18:12:18 -07001687status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001688 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001689}
1690
Jooyung Han2d5878e2020-01-23 12:45:10 +09001691status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const {
1692 const int32_t start = dataPosition();
1693 int32_t size;
1694 status_t status = readInt32(&size);
1695 val->reset();
1696
1697 if (status != OK || size < 0) {
1698 return status;
1699 }
1700
1701 setDataPosition(start);
1702 val->emplace();
1703
1704 status = readBoolVector(&**val);
1705
1706 if (status != OK) {
1707 val->reset();
1708 }
1709
1710 return status;
1711}
1712
Casey Dahlinb9872622015-11-25 15:09:45 -08001713status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1714 const int32_t start = dataPosition();
1715 int32_t size;
1716 status_t status = readInt32(&size);
1717 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001718
Casey Dahlinb9872622015-11-25 15:09:45 -08001719 if (status != OK || size < 0) {
1720 return status;
1721 }
1722
1723 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001724 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001725
1726 status = readBoolVector(val->get());
1727
1728 if (status != OK) {
1729 val->reset();
1730 }
1731
1732 return status;
1733}
1734
1735status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001736 int32_t size;
1737 status_t status = readInt32(&size);
1738
1739 if (status != OK) {
1740 return status;
1741 }
1742
1743 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001744 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001745 }
1746
1747 val->resize(size);
1748
1749 /* C++ bool handling means a vector of bools isn't necessarily addressable
1750 * (we might use individual bits)
1751 */
Christopher Wiley97887982015-10-27 16:33:47 -07001752 bool data;
1753 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001754 status = readBool(&data);
1755 (*val)[i] = data;
1756
1757 if (status != OK) {
1758 return status;
1759 }
1760 }
1761
1762 return OK;
1763}
1764
Jooyung Han2d5878e2020-01-23 12:45:10 +09001765status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const {
1766 return readNullableTypedVector(val, &Parcel::readChar);
1767}
1768
Casey Dahlinb9872622015-11-25 15:09:45 -08001769status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1770 return readNullableTypedVector(val, &Parcel::readChar);
1771}
1772
Casey Dahlin451ff582015-10-19 18:12:18 -07001773status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001774 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001775}
1776
Casey Dahlinb9872622015-11-25 15:09:45 -08001777status_t Parcel::readString16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +09001778 std::optional<std::vector<std::optional<String16>>>* val) const {
1779 return readNullableTypedVector(val, &Parcel::readString16);
1780}
1781
1782status_t Parcel::readString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -08001783 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1784 return readNullableTypedVector(val, &Parcel::readString16);
1785}
1786
Casey Dahlin451ff582015-10-19 18:12:18 -07001787status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001788 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001789}
1790
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001791status_t Parcel::readUtf8VectorFromUtf16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +09001792 std::optional<std::vector<std::optional<std::string>>>* val) const {
1793 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1794}
1795
1796status_t Parcel::readUtf8VectorFromUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001797 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1798 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1799}
1800
1801status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1802 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1803}
Casey Dahlin451ff582015-10-19 18:12:18 -07001804
Andreas Huber84a6d042009-08-17 13:33:27 -07001805status_t Parcel::readInt32(int32_t *pArg) const
1806{
1807 return readAligned(pArg);
1808}
1809
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001810int32_t Parcel::readInt32() const
1811{
Andreas Huber84a6d042009-08-17 13:33:27 -07001812 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001813}
1814
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001815status_t Parcel::readUint32(uint32_t *pArg) const
1816{
1817 return readAligned(pArg);
1818}
1819
1820uint32_t Parcel::readUint32() const
1821{
1822 return readAligned<uint32_t>();
1823}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001824
1825status_t Parcel::readInt64(int64_t *pArg) const
1826{
Andreas Huber84a6d042009-08-17 13:33:27 -07001827 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001828}
1829
1830
1831int64_t Parcel::readInt64() const
1832{
Andreas Huber84a6d042009-08-17 13:33:27 -07001833 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001834}
1835
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001836status_t Parcel::readUint64(uint64_t *pArg) const
1837{
1838 return readAligned(pArg);
1839}
1840
1841uint64_t Parcel::readUint64() const
1842{
1843 return readAligned<uint64_t>();
1844}
1845
Serban Constantinescuf683e012013-11-05 16:53:55 +00001846status_t Parcel::readPointer(uintptr_t *pArg) const
1847{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001848 status_t ret;
1849 binder_uintptr_t ptr;
1850 ret = readAligned(&ptr);
1851 if (!ret)
1852 *pArg = ptr;
1853 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001854}
1855
1856uintptr_t Parcel::readPointer() const
1857{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001858 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001859}
1860
1861
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001862status_t Parcel::readFloat(float *pArg) const
1863{
Andreas Huber84a6d042009-08-17 13:33:27 -07001864 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001865}
1866
1867
1868float Parcel::readFloat() const
1869{
Andreas Huber84a6d042009-08-17 13:33:27 -07001870 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001871}
1872
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001873#if defined(__mips__) && defined(__mips_hard_float)
1874
1875status_t Parcel::readDouble(double *pArg) const
1876{
1877 union {
1878 double d;
1879 unsigned long long ll;
1880 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001881 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001882 status_t status;
1883 status = readAligned(&u.ll);
1884 *pArg = u.d;
1885 return status;
1886}
1887
1888double Parcel::readDouble() const
1889{
1890 union {
1891 double d;
1892 unsigned long long ll;
1893 } u;
1894 u.ll = readAligned<unsigned long long>();
1895 return u.d;
1896}
1897
1898#else
1899
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001900status_t Parcel::readDouble(double *pArg) const
1901{
Andreas Huber84a6d042009-08-17 13:33:27 -07001902 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001903}
1904
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001905double Parcel::readDouble() const
1906{
Andreas Huber84a6d042009-08-17 13:33:27 -07001907 return readAligned<double>();
1908}
1909
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001910#endif
1911
Andreas Huber84a6d042009-08-17 13:33:27 -07001912status_t Parcel::readIntPtr(intptr_t *pArg) const
1913{
1914 return readAligned(pArg);
1915}
1916
1917
1918intptr_t Parcel::readIntPtr() const
1919{
1920 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001921}
1922
Casey Dahlind6848f52015-10-15 15:44:59 -07001923status_t Parcel::readBool(bool *pArg) const
1924{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001925 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001926 status_t ret = readInt32(&tmp);
1927 *pArg = (tmp != 0);
1928 return ret;
1929}
1930
1931bool Parcel::readBool() const
1932{
1933 return readInt32() != 0;
1934}
1935
1936status_t Parcel::readChar(char16_t *pArg) const
1937{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001938 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001939 status_t ret = readInt32(&tmp);
1940 *pArg = char16_t(tmp);
1941 return ret;
1942}
1943
1944char16_t Parcel::readChar() const
1945{
1946 return char16_t(readInt32());
1947}
1948
1949status_t Parcel::readByte(int8_t *pArg) const
1950{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001951 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001952 status_t ret = readInt32(&tmp);
1953 *pArg = int8_t(tmp);
1954 return ret;
1955}
1956
1957int8_t Parcel::readByte() const
1958{
1959 return int8_t(readInt32());
1960}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001961
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001962status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1963 size_t utf16Size = 0;
1964 const char16_t* src = readString16Inplace(&utf16Size);
1965 if (!src) {
1966 return UNEXPECTED_NULL;
1967 }
1968
1969 // Save ourselves the trouble, we're done.
1970 if (utf16Size == 0u) {
1971 str->clear();
1972 return NO_ERROR;
1973 }
1974
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001975 // Allow for closing '\0'
1976 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1977 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001978 return BAD_VALUE;
1979 }
1980 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001981 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001982 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001983 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1984 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001985 return NO_ERROR;
1986}
1987
Jooyung Han2d5878e2020-01-23 12:45:10 +09001988status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const {
1989 const int32_t start = dataPosition();
1990 int32_t size;
1991 status_t status = readInt32(&size);
1992 str->reset();
1993
1994 if (status != OK || size < 0) {
1995 return status;
1996 }
1997
1998 setDataPosition(start);
1999 str->emplace();
2000 return readUtf8FromUtf16(&**str);
2001}
2002
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002003status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2004 const int32_t start = dataPosition();
2005 int32_t size;
2006 status_t status = readInt32(&size);
2007 str->reset();
2008
2009 if (status != OK || size < 0) {
2010 return status;
2011 }
2012
2013 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002014 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002015 return readUtf8FromUtf16(str->get());
2016}
2017
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002018const char* Parcel::readCString() const
2019{
Steven Morelandf5e6c7e2019-05-17 13:14:06 -07002020 if (mDataPos < mDataSize) {
2021 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002022 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2023 // is the string's trailing NUL within the parcel's valid bounds?
2024 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2025 if (eos) {
2026 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002027 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002028 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002029 return str;
2030 }
2031 }
Yi Kong91635562018-06-07 14:38:36 -07002032 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002033}
2034
2035String8 Parcel::readString8() const
2036{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002037 size_t len;
2038 const char* str = readString8Inplace(&len);
2039 if (str) return String8(str, len);
2040 ALOGE("Reading a NULL string not supported here.");
2041 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002042}
2043
2044status_t Parcel::readString8(String8* pArg) const
2045{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002046 size_t len;
2047 const char* str = readString8Inplace(&len);
2048 if (str) {
2049 pArg->setTo(str, len);
2050 return 0;
2051 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002052 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002053 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002054 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002055}
2056
2057const char* Parcel::readString8Inplace(size_t* outLen) const
2058{
2059 int32_t size = readInt32();
2060 // watch for potential int overflow from size+1
2061 if (size >= 0 && size < INT32_MAX) {
2062 *outLen = size;
2063 const char* str = (const char*)readInplace(size+1);
2064 if (str != nullptr) {
2065 return str;
2066 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002067 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002068 *outLen = 0;
2069 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002070}
2071
2072String16 Parcel::readString16() const
2073{
2074 size_t len;
2075 const char16_t* str = readString16Inplace(&len);
2076 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002077 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002078 return String16();
2079}
2080
Jooyung Han2d5878e2020-01-23 12:45:10 +09002081status_t Parcel::readString16(std::optional<String16>* pArg) const
2082{
2083 const int32_t start = dataPosition();
2084 int32_t size;
2085 status_t status = readInt32(&size);
2086 pArg->reset();
2087
2088 if (status != OK || size < 0) {
2089 return status;
2090 }
2091
2092 setDataPosition(start);
2093 pArg->emplace();
2094
2095 status = readString16(&**pArg);
2096
2097 if (status != OK) {
2098 pArg->reset();
2099 }
2100
2101 return status;
2102}
2103
Casey Dahlinb9872622015-11-25 15:09:45 -08002104status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2105{
2106 const int32_t start = dataPosition();
2107 int32_t size;
2108 status_t status = readInt32(&size);
2109 pArg->reset();
2110
2111 if (status != OK || size < 0) {
2112 return status;
2113 }
2114
2115 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002116 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002117
2118 status = readString16(pArg->get());
2119
2120 if (status != OK) {
2121 pArg->reset();
2122 }
2123
2124 return status;
2125}
2126
Casey Dahlin451ff582015-10-19 18:12:18 -07002127status_t Parcel::readString16(String16* pArg) const
2128{
2129 size_t len;
2130 const char16_t* str = readString16Inplace(&len);
2131 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002132 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002133 return 0;
2134 } else {
2135 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002136 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002137 }
2138}
2139
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002140const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2141{
2142 int32_t size = readInt32();
2143 // watch for potential int overflow from size+1
2144 if (size >= 0 && size < INT32_MAX) {
2145 *outLen = size;
2146 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002147 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002148 return str;
2149 }
2150 }
2151 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002152 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002153}
2154
Casey Dahlinf0c13772015-10-27 18:33:56 -07002155status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2156{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002157 status_t status = readNullableStrongBinder(val);
2158 if (status == OK && !val->get()) {
2159 status = UNEXPECTED_NULL;
2160 }
2161 return status;
2162}
2163
2164status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2165{
Steven Morelanda86a3562019-08-01 23:28:34 +00002166 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002167}
2168
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002169sp<IBinder> Parcel::readStrongBinder() const
2170{
2171 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002172 // Note that a lot of code in Android reads binders by hand with this
2173 // method, and that code has historically been ok with getting nullptr
2174 // back (while ignoring error codes).
2175 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002176 return val;
2177}
2178
Christopher Wiley97f048d2015-11-19 06:49:05 -08002179status_t Parcel::readParcelable(Parcelable* parcelable) const {
2180 int32_t have_parcelable = 0;
2181 status_t status = readInt32(&have_parcelable);
2182 if (status != OK) {
2183 return status;
2184 }
2185 if (!have_parcelable) {
2186 return UNEXPECTED_NULL;
2187 }
2188 return parcelable->readFromParcel(this);
2189}
2190
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002191int32_t Parcel::readExceptionCode() const
2192{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002193 binder::Status status;
2194 status.readFromParcel(*this);
2195 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002196}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002197
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002198native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002199{
2200 int numFds, numInts;
2201 status_t err;
2202 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002203 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002204 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002205 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002206
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002207 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002208 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002209 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002210 }
2211
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002212 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002213 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002214 if (h->data[i] < 0) {
2215 for (int j = 0; j < i; j++) {
2216 close(h->data[j]);
2217 }
2218 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002219 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002220 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002221 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002222 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002223 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002224 native_handle_close(h);
2225 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002226 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002227 }
2228 return h;
2229}
2230
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231int Parcel::readFileDescriptor() const
2232{
2233 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002234
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002235 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002236 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002237 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002238
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002239 return BAD_TYPE;
2240}
2241
Dianne Hackborn1941a402016-08-29 12:30:43 -07002242int Parcel::readParcelFileDescriptor() const
2243{
2244 int32_t hasComm = readInt32();
2245 int fd = readFileDescriptor();
2246 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002247 // detach (owned by the binder driver)
2248 int comm = readFileDescriptor();
2249
2250 // warning: this must be kept in sync with:
2251 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2252 enum ParcelFileDescriptorStatus {
2253 DETACHED = 2,
2254 };
2255
2256#if BYTE_ORDER == BIG_ENDIAN
2257 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2258#endif
2259#if BYTE_ORDER == LITTLE_ENDIAN
2260 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2261#endif
2262
2263 ssize_t written = TEMP_FAILURE_RETRY(
2264 ::write(comm, &message, sizeof(message)));
2265
2266 if (written == -1 || written != sizeof(message)) {
2267 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2268 written, strerror(errno));
2269 return BAD_TYPE;
2270 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002271 }
2272 return fd;
2273}
2274
Christopher Wiley2cf19952016-04-11 11:09:37 -07002275status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002276{
2277 int got = readFileDescriptor();
2278
2279 if (got == BAD_TYPE) {
2280 return BAD_TYPE;
2281 }
2282
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002283 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002284
2285 if (val->get() < 0) {
2286 return BAD_VALUE;
2287 }
2288
2289 return OK;
2290}
2291
Ryo Hashimotobf551892018-05-31 16:58:35 +09002292status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2293{
2294 int got = readParcelFileDescriptor();
2295
2296 if (got == BAD_TYPE) {
2297 return BAD_TYPE;
2298 }
2299
2300 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2301
2302 if (val->get() < 0) {
2303 return BAD_VALUE;
2304 }
2305
2306 return OK;
2307}
Casey Dahlin06673e32015-11-23 13:24:23 -08002308
Jooyung Han2d5878e2020-01-23 12:45:10 +09002309status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const {
2310 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2311}
2312
Christopher Wiley2cf19952016-04-11 11:09:37 -07002313status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002314 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2315}
2316
Christopher Wiley2cf19952016-04-11 11:09:37 -07002317status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002318 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2319}
2320
Jeff Brown5707dbf2011-09-23 21:17:56 -07002321status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2322{
Jeff Brown13b16042014-11-11 16:44:25 -08002323 int32_t blobType;
2324 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002325 if (status) return status;
2326
Jeff Brown13b16042014-11-11 16:44:25 -08002327 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002328 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002329 const void* ptr = readInplace(len);
2330 if (!ptr) return BAD_VALUE;
2331
Jeff Brown13b16042014-11-11 16:44:25 -08002332 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002333 return NO_ERROR;
2334 }
2335
Steve Block6807e592011-10-20 11:56:00 +01002336 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002337 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002338 int fd = readFileDescriptor();
2339 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2340
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002341 if (!ashmem_valid(fd)) {
2342 ALOGE("invalid fd");
2343 return BAD_VALUE;
2344 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002345 int size = ashmem_get_size_region(fd);
2346 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002347 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002348 return BAD_VALUE;
2349 }
Yi Kong91635562018-06-07 14:38:36 -07002350 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002351 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002352 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002353
Jeff Brown13b16042014-11-11 16:44:25 -08002354 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002355 return NO_ERROR;
2356}
2357
Mathias Agopiane1424282013-07-29 21:24:40 -07002358status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002359{
2360 // size
2361 const size_t len = this->readInt32();
2362 const size_t fd_count = this->readInt32();
2363
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002364 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002365 // don't accept size_t values which may have come from an
2366 // inadvertent conversion from a negative int.
2367 return BAD_VALUE;
2368 }
2369
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002370 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002371 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002372 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002373 return BAD_VALUE;
2374
Yi Kong91635562018-06-07 14:38:36 -07002375 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002376 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002377 fds = new (std::nothrow) int[fd_count];
2378 if (fds == nullptr) {
2379 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2380 return BAD_VALUE;
2381 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002382 }
2383
2384 status_t err = NO_ERROR;
2385 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002386 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002387 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002388 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002389 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 -07002390 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2391 // Close all the file descriptors that were dup-ed.
2392 for (size_t j=0; j<i ;j++) {
2393 close(fds[j]);
2394 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002395 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002396 }
2397
2398 if (err == NO_ERROR) {
2399 err = val.unflatten(buf, len, fds, fd_count);
2400 }
2401
2402 if (fd_count) {
2403 delete [] fds;
2404 }
2405
2406 return err;
2407}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002408const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2409{
2410 const size_t DPOS = mDataPos;
2411 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2412 const flat_binder_object* obj
2413 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2414 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002415 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002416 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002417 // the object list, so we don't want to check for it when
2418 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002419 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002420 return obj;
2421 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002422
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002423 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002424 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002425 const size_t N = mObjectsSize;
2426 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002427
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002428 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002429 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002430 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002431
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002432 // Start at the current hint position, looking for an object at
2433 // the current data position.
2434 if (opos < N) {
2435 while (opos < (N-1) && OBJS[opos] < DPOS) {
2436 opos++;
2437 }
2438 } else {
2439 opos = N-1;
2440 }
2441 if (OBJS[opos] == DPOS) {
2442 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002443 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002444 this, DPOS, opos);
2445 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002446 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002447 return obj;
2448 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002449
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002450 // Look backwards for it...
2451 while (opos > 0 && OBJS[opos] > DPOS) {
2452 opos--;
2453 }
2454 if (OBJS[opos] == DPOS) {
2455 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002456 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002457 this, DPOS, opos);
2458 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002459 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002460 return obj;
2461 }
2462 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002463 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 -07002464 this, DPOS);
2465 }
Yi Kong91635562018-06-07 14:38:36 -07002466 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002467}
2468
2469void Parcel::closeFileDescriptors()
2470{
2471 size_t i = mObjectsSize;
2472 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002473 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002474 }
2475 while (i > 0) {
2476 i--;
2477 const flat_binder_object* flat
2478 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002479 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002480 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002481 close(flat->handle);
2482 }
2483 }
2484}
2485
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002486uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002487{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002488 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002489}
2490
2491size_t Parcel::ipcDataSize() const
2492{
2493 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2494}
2495
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002496uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002497{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002498 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002499}
2500
2501size_t Parcel::ipcObjectsCount() const
2502{
2503 return mObjectsSize;
2504}
2505
2506void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002507 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002509 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002510 freeDataNoInit();
2511 mError = NO_ERROR;
2512 mData = const_cast<uint8_t*>(data);
2513 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002514 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002515 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002516 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002517 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002518 mObjectsSize = mObjectsCapacity = objectsCount;
2519 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002520 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002521 mOwner = relFunc;
2522 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002523 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002524 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002525 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002526 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002527 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002528 mObjectsSize = 0;
2529 break;
2530 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002531 const flat_binder_object* flat
2532 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2533 uint32_t type = flat->hdr.type;
2534 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2535 type == BINDER_TYPE_FD)) {
2536 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2537 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2538 // recover gracefully by clearing out the objects, and releasing the objects we do
2539 // know about.
2540 android_errorWriteLog(0x534e4554, "135930648");
2541 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2542 __func__, type, (uint64_t)offset);
2543 releaseObjects();
2544 mObjectsSize = 0;
2545 break;
2546 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002547 minOffset = offset + sizeof(flat_binder_object);
2548 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002549 scanForFds();
2550}
2551
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002552void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002553{
2554 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002555
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002556 if (errorCheck() != NO_ERROR) {
2557 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002558 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002559 } else if (dataSize() > 0) {
2560 const uint8_t* DATA = data();
2561 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002562 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002563 const size_t N = objectsCount();
2564 for (size_t i=0; i<N; i++) {
2565 const flat_binder_object* flat
2566 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2567 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002568 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002569 << " = " << flat->binder;
2570 }
2571 } else {
2572 to << "NULL";
2573 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002574
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002575 to << ")";
2576}
2577
2578void Parcel::releaseObjects()
2579{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002580 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002581 if (i == 0) {
2582 return;
2583 }
2584 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002585 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002586 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002587 while (i > 0) {
2588 i--;
2589 const flat_binder_object* flat
2590 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002591 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002592 }
2593}
2594
2595void Parcel::acquireObjects()
2596{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002597 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002598 if (i == 0) {
2599 return;
2600 }
2601 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002602 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002603 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002604 while (i > 0) {
2605 i--;
2606 const flat_binder_object* flat
2607 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002608 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002609 }
2610}
2611
2612void Parcel::freeData()
2613{
2614 freeDataNoInit();
2615 initState();
2616}
2617
2618void Parcel::freeDataNoInit()
2619{
2620 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002621 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002622 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002623 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2624 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002625 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002626 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002627 if (mData) {
2628 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002629 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002630 if (mDataCapacity <= gParcelGlobalAllocSize) {
2631 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2632 } else {
2633 gParcelGlobalAllocSize = 0;
2634 }
2635 if (gParcelGlobalAllocCount > 0) {
2636 gParcelGlobalAllocCount--;
2637 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002638 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002639 free(mData);
2640 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002641 if (mObjects) free(mObjects);
2642 }
2643}
2644
2645status_t Parcel::growData(size_t len)
2646{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002647 if (len > INT32_MAX) {
2648 // don't accept size_t values which may have come from an
2649 // inadvertent conversion from a negative int.
2650 return BAD_VALUE;
2651 }
2652
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01002653 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2654 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002655 size_t newSize = ((mDataSize+len)*3)/2;
2656 return (newSize <= mDataSize)
2657 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002658 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002659}
2660
2661status_t Parcel::restartWrite(size_t desired)
2662{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002663 if (desired > INT32_MAX) {
2664 // don't accept size_t values which may have come from an
2665 // inadvertent conversion from a negative int.
2666 return BAD_VALUE;
2667 }
2668
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002669 if (mOwner) {
2670 freeData();
2671 return continueWrite(desired);
2672 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002673
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002674 uint8_t* data = (uint8_t*)realloc(mData, desired);
2675 if (!data && desired > mDataCapacity) {
2676 mError = NO_MEMORY;
2677 return NO_MEMORY;
2678 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002679
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002680 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002681
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002682 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002683 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002684 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002685 gParcelGlobalAllocSize += desired;
2686 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002687 if (!mData) {
2688 gParcelGlobalAllocCount++;
2689 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002690 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002691 mData = data;
2692 mDataCapacity = desired;
2693 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002694
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002695 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002696 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2697 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2698
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002699 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002700 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002701 mObjectsSize = mObjectsCapacity = 0;
2702 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002703 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002704 mHasFds = false;
2705 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002706 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002707
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002708 return NO_ERROR;
2709}
2710
2711status_t Parcel::continueWrite(size_t desired)
2712{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002713 if (desired > INT32_MAX) {
2714 // don't accept size_t values which may have come from an
2715 // inadvertent conversion from a negative int.
2716 return BAD_VALUE;
2717 }
2718
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002719 // If shrinking, first adjust for any objects that appear
2720 // after the new data size.
2721 size_t objectsSize = mObjectsSize;
2722 if (desired < mDataSize) {
2723 if (desired == 0) {
2724 objectsSize = 0;
2725 } else {
2726 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002727 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002728 break;
2729 objectsSize--;
2730 }
2731 }
2732 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002733
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002734 if (mOwner) {
2735 // If the size is going to zero, just release the owner's data.
2736 if (desired == 0) {
2737 freeData();
2738 return NO_ERROR;
2739 }
2740
2741 // If there is a different owner, we need to take
2742 // posession.
2743 uint8_t* data = (uint8_t*)malloc(desired);
2744 if (!data) {
2745 mError = NO_MEMORY;
2746 return NO_MEMORY;
2747 }
Yi Kong91635562018-06-07 14:38:36 -07002748 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002749
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002750 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002751 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002752 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002753 free(data);
2754
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002755 mError = NO_MEMORY;
2756 return NO_MEMORY;
2757 }
2758
2759 // Little hack to only acquire references on objects
2760 // we will be keeping.
2761 size_t oldObjectsSize = mObjectsSize;
2762 mObjectsSize = objectsSize;
2763 acquireObjects();
2764 mObjectsSize = oldObjectsSize;
2765 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002766
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002767 if (mData) {
2768 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2769 }
2770 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002771 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002772 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002773 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002774 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002775 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002776
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002777 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002778 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002779 gParcelGlobalAllocSize += desired;
2780 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002781 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002782
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002783 mData = data;
2784 mObjects = objects;
2785 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002786 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002787 mDataCapacity = desired;
2788 mObjectsSize = mObjectsCapacity = objectsSize;
2789 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002790 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002791
2792 } else if (mData) {
2793 if (objectsSize < mObjectsSize) {
2794 // Need to release refs on any objects we are dropping.
2795 const sp<ProcessState> proc(ProcessState::self());
2796 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2797 const flat_binder_object* flat
2798 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002799 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002800 // will need to rescan because we may have lopped off the only FDs
2801 mFdsKnown = false;
2802 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002803 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002804 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002805
2806 if (objectsSize == 0) {
2807 free(mObjects);
2808 mObjects = nullptr;
Michael Wachenschwanzc67d9f32019-10-15 11:49:22 -07002809 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002810 } else {
2811 binder_size_t* objects =
2812 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2813 if (objects) {
2814 mObjects = objects;
Michael Wachenschwanzc67d9f32019-10-15 11:49:22 -07002815 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002816 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002817 }
2818 mObjectsSize = objectsSize;
2819 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002820 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002821 }
2822
2823 // We own the data, so we can just do a realloc().
2824 if (desired > mDataCapacity) {
2825 uint8_t* data = (uint8_t*)realloc(mData, desired);
2826 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002827 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2828 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002829 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002830 gParcelGlobalAllocSize += desired;
2831 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002832 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002833 mData = data;
2834 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002835 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002836 mError = NO_MEMORY;
2837 return NO_MEMORY;
2838 }
2839 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002840 if (mDataSize > desired) {
2841 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002842 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002843 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002844 if (mDataPos > desired) {
2845 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002846 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002847 }
2848 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002849
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002850 } else {
2851 // This is the first data. Easy!
2852 uint8_t* data = (uint8_t*)malloc(desired);
2853 if (!data) {
2854 mError = NO_MEMORY;
2855 return NO_MEMORY;
2856 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002857
Yi Kong91635562018-06-07 14:38:36 -07002858 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002859 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002860 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002861 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002862
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002863 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002864 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002865 gParcelGlobalAllocSize += desired;
2866 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002867 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002868
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002869 mData = data;
2870 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002871 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2872 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002873 mDataCapacity = desired;
2874 }
2875
2876 return NO_ERROR;
2877}
2878
2879void Parcel::initState()
2880{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002881 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002882 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002883 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002884 mDataSize = 0;
2885 mDataCapacity = 0;
2886 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002887 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2888 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002889 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002890 mObjectsSize = 0;
2891 mObjectsCapacity = 0;
2892 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002893 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002894 mHasFds = false;
2895 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002896 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002897 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002898 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002899 mWorkSourceRequestHeaderPosition = 0;
2900 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002901
2902 // racing multiple init leads only to multiple identical write
2903 if (gMaxFds == 0) {
2904 struct rlimit result;
2905 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2906 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002907 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002908 } else {
2909 ALOGW("Unable to getrlimit: %s", strerror(errno));
2910 gMaxFds = 1024;
2911 }
2912 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002913}
2914
2915void Parcel::scanForFds() const
2916{
2917 bool hasFds = false;
2918 for (size_t i=0; i<mObjectsSize; i++) {
2919 const flat_binder_object* flat
2920 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002921 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002922 hasFds = true;
2923 break;
2924 }
2925 }
2926 mHasFds = hasFds;
2927 mFdsKnown = true;
2928}
2929
Dan Sandleraa5c2342015-04-10 10:08:45 -04002930size_t Parcel::getBlobAshmemSize() const
2931{
Adrian Roos6bb31142015-10-22 16:46:12 -07002932 // This used to return the size of all blobs that were written to ashmem, now we're returning
2933 // the ashmem currently referenced by this Parcel, which should be equivalent.
2934 // TODO: Remove method once ABI can be changed.
2935 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002936}
2937
Adrian Rooscbf37262015-10-22 16:12:53 -07002938size_t Parcel::getOpenAshmemSize() const
2939{
2940 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002941}
2942
2943// --- Parcel::Blob ---
2944
2945Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002946 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002947}
2948
2949Parcel::Blob::~Blob() {
2950 release();
2951}
2952
2953void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002954 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002955 ::munmap(mData, mSize);
2956 }
2957 clear();
2958}
2959
Jeff Brown13b16042014-11-11 16:44:25 -08002960void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2961 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002962 mData = data;
2963 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002964 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002965}
2966
2967void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002968 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002969 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002970 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002971 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002972}
2973
Steven Moreland6511af52019-09-26 16:05:45 -07002974} // namespace android