blob: 82ea5a3cd0cdf4237b93839fbb4473f1b718599e [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>
Mark Salyzyn70f36652016-02-02 10:27:03 -080023#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080028#include <sys/stat.h>
29#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070030#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080031#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070032
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/Binder.h>
34#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080035#include <binder/IPCThreadState.h>
36#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070037#include <binder/ProcessState.h>
Steven Moreland6e5a7752019-08-05 20:30:14 -070038#include <binder/Stability.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080039#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070040#include <binder/TextOutput.h>
41
Mark Salyzynabed7f72016-01-27 08:02:48 -080042#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070043#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080044#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080046#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String8.h>
48#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049
Mathias Agopian208059f2009-05-18 15:08:03 -070050#include <private/binder/binder_module.h>
Steven Morelanda4853cd2019-07-12 15:44:37 -070051#include "Static.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053#ifndef INT32_MAX
54#define INT32_MAX ((int32_t)(2147483647))
55#endif
56
57#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080058//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080059#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080060//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070061
62// ---------------------------------------------------------------------------
63
Nick Kralevichb6b14232015-04-02 09:36:02 -070064// This macro should never be used at runtime, as a too large value
65// of s could cause an integer overflow. Instead, you should always
66// use the wrapper function pad_size()
67#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
68
69static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070070 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -070071 abort();
72 }
73 return PAD_SIZE_UNSAFE(s);
74}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070076// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060077#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070078
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070079namespace android {
80
Steven Moreland7b102262019-08-01 15:48:43 -070081// many things compile this into prebuilts on the stack
82static_assert(sizeof(Parcel) == 60 || sizeof(Parcel) == 120);
83
Dianne Hackborna4cff882014-11-13 17:07:40 -080084static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
85static size_t gParcelGlobalAllocSize = 0;
86static size_t gParcelGlobalAllocCount = 0;
87
Christopher Tatee4e0ae82016-03-24 16:03:44 -070088static size_t gMaxFds = 0;
89
Jeff Brown13b16042014-11-11 16:44:25 -080090// Maximum size of a blob to transfer in-place.
91static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
92
93enum {
94 BLOB_INPLACE = 0,
95 BLOB_ASHMEM_IMMUTABLE = 1,
96 BLOB_ASHMEM_MUTABLE = 2,
97};
98
Steven Morelandb1c81202019-04-05 18:49:55 -070099static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700100 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700101{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700102 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700103 case BINDER_TYPE_BINDER:
104 if (obj.binder) {
105 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800106 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700107 }
108 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 case BINDER_TYPE_HANDLE: {
110 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700111 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700112 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
113 b->incStrong(who);
114 }
115 return;
116 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700117 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000118 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700119 // If we own an ashmem fd, keep track of how much memory it refers to.
120 int size = ashmem_get_size_region(obj.handle);
121 if (size > 0) {
122 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700123 }
124 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700125 return;
126 }
127 }
128
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700129 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700130}
131
Adrian Roos6bb31142015-10-22 16:46:12 -0700132static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700133 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700134{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700135 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136 case BINDER_TYPE_BINDER:
137 if (obj.binder) {
138 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800139 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700140 }
141 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142 case BINDER_TYPE_HANDLE: {
143 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700144 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700145 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
146 b->decStrong(who);
147 }
148 return;
149 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700150 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800151 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000152 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700153 int size = ashmem_get_size_region(obj.handle);
154 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800155 // ashmem size might have changed since last time it was accounted for, e.g.
156 // in acquire_object(). Value of *outAshmemSize is not critical since we are
157 // releasing the object anyway. Check for integer overflow condition.
158 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700159 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700160 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800161
162 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700163 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700164 return;
165 }
166 }
167
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700168 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169}
170
Steven Morelanda86a3562019-08-01 23:28:34 +0000171status_t Parcel::finishFlattenBinder(
172 const sp<IBinder>& binder, const flat_binder_object& flat)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700173{
Steven Morelanda86a3562019-08-01 23:28:34 +0000174 status_t status = writeObject(flat, false);
175 if (status != OK) return status;
176
Steven Moreland6e5a7752019-08-05 20:30:14 -0700177 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Morelanda86a3562019-08-01 23:28:34 +0000178 return writeInt32(internal::Stability::get(binder.get()));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700179}
180
Steven Morelanda86a3562019-08-01 23:28:34 +0000181status_t Parcel::finishUnflattenBinder(
182 const sp<IBinder>& binder, sp<IBinder>* out) const
183{
184 int32_t stability;
185 status_t status = readInt32(&stability);
186 if (status != OK) return status;
187
Steven Moreland05929552019-07-31 17:51:25 -0700188 status = internal::Stability::set(binder.get(), stability, true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000189 if (status != OK) return status;
190
191 *out = binder;
192 return OK;
193}
194
195status_t Parcel::flattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700196{
197 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700198
Martijn Coenen2b631742017-05-05 11:16:59 -0700199 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
200 /* minimum priority for all nodes is nice 0 */
201 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
202 } else {
203 /* minimum priority for all nodes is MAX_NICE(19) */
204 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
205 }
206
Yi Kong91635562018-06-07 14:38:36 -0700207 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800208 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700209 if (!local) {
210 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700211 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000212 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700213 }
214 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700215 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800216 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700217 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800218 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700219 } else {
Steven Morelandf0212002018-12-26 13:59:23 -0800220 if (local->isRequestingSid()) {
221 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
222 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700223 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800224 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
225 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700226 }
227 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700228 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800229 obj.binder = 0;
230 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700232
Steven Morelanda86a3562019-08-01 23:28:34 +0000233 return finishFlattenBinder(binder, obj);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700234}
235
Steven Morelanda86a3562019-08-01 23:28:34 +0000236status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700237{
Steven Morelanda86a3562019-08-01 23:28:34 +0000238 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700239
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700240 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700241 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000242 case BINDER_TYPE_BINDER: {
243 sp<IBinder> binder = reinterpret_cast<IBinder*>(flat->cookie);
244 return finishUnflattenBinder(binder, out);
245 }
246 case BINDER_TYPE_HANDLE: {
247 sp<IBinder> binder =
248 ProcessState::self()->getStrongProxyForHandle(flat->handle);
249 return finishUnflattenBinder(binder, out);
250 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700251 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700252 }
253 return BAD_TYPE;
254}
255
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700256// ---------------------------------------------------------------------------
257
258Parcel::Parcel()
259{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800260 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261 initState();
262}
263
264Parcel::~Parcel()
265{
266 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800267 LOG_ALLOC("Parcel %p: destroyed", this);
268}
269
270size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800271 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
272 size_t size = gParcelGlobalAllocSize;
273 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
274 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800275}
276
277size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800278 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
279 size_t count = gParcelGlobalAllocCount;
280 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
281 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700282}
283
284const uint8_t* Parcel::data() const
285{
286 return mData;
287}
288
289size_t Parcel::dataSize() const
290{
291 return (mDataSize > mDataPos ? mDataSize : mDataPos);
292}
293
294size_t Parcel::dataAvail() const
295{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700296 size_t result = dataSize() - dataPosition();
297 if (result > INT32_MAX) {
298 abort();
299 }
300 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700301}
302
303size_t Parcel::dataPosition() const
304{
305 return mDataPos;
306}
307
308size_t Parcel::dataCapacity() const
309{
310 return mDataCapacity;
311}
312
313status_t Parcel::setDataSize(size_t size)
314{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700315 if (size > INT32_MAX) {
316 // don't accept size_t values which may have come from an
317 // inadvertent conversion from a negative int.
318 return BAD_VALUE;
319 }
320
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700321 status_t err;
322 err = continueWrite(size);
323 if (err == NO_ERROR) {
324 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700325 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700326 }
327 return err;
328}
329
330void Parcel::setDataPosition(size_t pos) const
331{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700332 if (pos > INT32_MAX) {
333 // don't accept size_t values which may have come from an
334 // inadvertent conversion from a negative int.
335 abort();
336 }
337
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700338 mDataPos = pos;
339 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800340 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700341}
342
343status_t Parcel::setDataCapacity(size_t size)
344{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700345 if (size > INT32_MAX) {
346 // don't accept size_t values which may have come from an
347 // inadvertent conversion from a negative int.
348 return BAD_VALUE;
349 }
350
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700351 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700352 return NO_ERROR;
353}
354
355status_t Parcel::setData(const uint8_t* buffer, size_t len)
356{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700357 if (len > INT32_MAX) {
358 // don't accept size_t values which may have come from an
359 // inadvertent conversion from a negative int.
360 return BAD_VALUE;
361 }
362
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700363 status_t err = restartWrite(len);
364 if (err == NO_ERROR) {
365 memcpy(const_cast<uint8_t*>(data()), buffer, len);
366 mDataSize = len;
367 mFdsKnown = false;
368 }
369 return err;
370}
371
Andreas Huber51faf462011-04-13 10:21:56 -0700372status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700373{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700374 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700375 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800376 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700377 size_t size = parcel->mObjectsSize;
378 int startPos = mDataPos;
379 int firstIndex = -1, lastIndex = -2;
380
381 if (len == 0) {
382 return NO_ERROR;
383 }
384
Nick Kralevichb6b14232015-04-02 09:36:02 -0700385 if (len > INT32_MAX) {
386 // don't accept size_t values which may have come from an
387 // inadvertent conversion from a negative int.
388 return BAD_VALUE;
389 }
390
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700391 // range checks against the source parcel size
392 if ((offset > parcel->mDataSize)
393 || (len > parcel->mDataSize)
394 || (offset + len > parcel->mDataSize)) {
395 return BAD_VALUE;
396 }
397
398 // Count objects in range
399 for (int i = 0; i < (int) size; i++) {
400 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700401 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700402 if (firstIndex == -1) {
403 firstIndex = i;
404 }
405 lastIndex = i;
406 }
407 }
408 int numObjects = lastIndex - firstIndex + 1;
409
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700410 if ((mDataSize+len) > mDataCapacity) {
411 // grow data
412 err = growData(len);
413 if (err != NO_ERROR) {
414 return err;
415 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700416 }
417
418 // append data
419 memcpy(mData + mDataPos, data + offset, len);
420 mDataPos += len;
421 mDataSize += len;
422
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400423 err = NO_ERROR;
424
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700425 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200426 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700427 // grow objects
428 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700429 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700430 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800431 binder_size_t *objects =
432 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700433 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 return NO_MEMORY;
435 }
436 mObjects = objects;
437 mObjectsCapacity = newSize;
438 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700439
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700440 // append and acquire objects
441 int idx = mObjectsSize;
442 for (int i = firstIndex; i <= lastIndex; i++) {
443 size_t off = objects[i] - offset + startPos;
444 mObjects[idx++] = off;
445 mObjectsSize++;
446
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700447 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700449 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700450
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700451 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700452 // If this is a file descriptor, we need to dup it so the
453 // new Parcel now owns its own fd, and can declare that we
454 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800455 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800456 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700457 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400458 if (!mAllowFds) {
459 err = FDS_NOT_ALLOWED;
460 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700461 }
462 }
463 }
464
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400465 return err;
466}
467
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700468int Parcel::compareData(const Parcel& other) {
469 size_t size = dataSize();
470 if (size != other.dataSize()) {
471 return size < other.dataSize() ? -1 : 1;
472 }
473 return memcmp(data(), other.data(), size);
474}
475
Jeff Brown13b16042014-11-11 16:44:25 -0800476bool Parcel::allowFds() const
477{
478 return mAllowFds;
479}
480
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700481bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400482{
483 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700484 if (!allowFds) {
485 mAllowFds = false;
486 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400487 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700488}
489
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700490void Parcel::restoreAllowFds(bool lastValue)
491{
492 mAllowFds = lastValue;
493}
494
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700495bool Parcel::hasFileDescriptors() const
496{
497 if (!mFdsKnown) {
498 scanForFds();
499 }
500 return mHasFds;
501}
502
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000503void Parcel::updateWorkSourceRequestHeaderPosition() const {
504 // Only update the request headers once. We only want to point
505 // to the first headers read/written.
506 if (!mRequestHeaderPresent) {
507 mWorkSourceRequestHeaderPosition = dataPosition();
508 mRequestHeaderPresent = true;
509 }
510}
511
Steven Morelandd70160f2019-07-23 10:20:38 -0700512#ifdef __ANDROID_VNDK__
513constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
514#else
515constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
516#endif
517
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700518// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700519status_t Parcel::writeInterfaceToken(const String16& interface)
520{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000521 const IPCThreadState* threadState = IPCThreadState::self();
522 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000523 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000524 writeInt32(threadState->shouldPropagateWorkSource() ?
525 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700526 writeInt32(kHeader);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700527 // currently the interface identification token is just its name as a string
528 return writeString16(interface);
529}
530
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000531bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
532{
533 if (!mRequestHeaderPresent) {
534 return false;
535 }
536
537 const size_t initialPosition = dataPosition();
538 setDataPosition(mWorkSourceRequestHeaderPosition);
539 status_t err = writeInt32(uid);
540 setDataPosition(initialPosition);
541 return err == NO_ERROR;
542}
543
Steven Morelandf1b1e492019-05-06 15:05:13 -0700544uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000545{
546 if (!mRequestHeaderPresent) {
547 return IPCThreadState::kUnsetWorkSource;
548 }
549
550 const size_t initialPosition = dataPosition();
551 setDataPosition(mWorkSourceRequestHeaderPosition);
552 uid_t uid = readInt32();
553 setDataPosition(initialPosition);
554 return uid;
555}
556
Mathias Agopian83c04462009-05-22 19:00:22 -0700557bool Parcel::checkInterface(IBinder* binder) const
558{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700559 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700560}
561
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700562bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700563 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700564{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100565 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700566 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700567 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700568 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700569 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700570 if ((threadState->getLastTransactionBinderFlags() &
571 IBinder::FLAG_ONEWAY) != 0) {
572 // For one-way calls, the callee is running entirely
573 // disconnected from the caller, so disable StrictMode entirely.
574 // Not only does disk/network usage not impact the caller, but
575 // there's no way to commuicate back any violations anyway.
576 threadState->setStrictModePolicy(0);
577 } else {
578 threadState->setStrictModePolicy(strictPolicy);
579 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100580 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000581 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100582 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000583 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700584 // vendor header
585 int32_t header = readInt32();
586 if (header != kHeader) {
587 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
588 return false;
589 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100590 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700591 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700592 if (str == interface) {
593 return true;
594 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700595 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700596 String8(interface).string(), String8(str).string());
597 return false;
598 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700599}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700600
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700601size_t Parcel::objectsCount() const
602{
603 return mObjectsSize;
604}
605
606status_t Parcel::errorCheck() const
607{
608 return mError;
609}
610
611void Parcel::setError(status_t err)
612{
613 mError = err;
614}
615
616status_t Parcel::finishWrite(size_t len)
617{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700618 if (len > INT32_MAX) {
619 // don't accept size_t values which may have come from an
620 // inadvertent conversion from a negative int.
621 return BAD_VALUE;
622 }
623
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700624 //printf("Finish write of %d\n", len);
625 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700626 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700627 if (mDataPos > mDataSize) {
628 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700629 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700630 }
631 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
632 return NO_ERROR;
633}
634
635status_t Parcel::writeUnpadded(const void* data, size_t len)
636{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700637 if (len > INT32_MAX) {
638 // don't accept size_t values which may have come from an
639 // inadvertent conversion from a negative int.
640 return BAD_VALUE;
641 }
642
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700643 size_t end = mDataPos + len;
644 if (end < mDataPos) {
645 // integer overflow
646 return BAD_VALUE;
647 }
648
649 if (end <= mDataCapacity) {
650restart_write:
651 memcpy(mData+mDataPos, data, len);
652 return finishWrite(len);
653 }
654
655 status_t err = growData(len);
656 if (err == NO_ERROR) goto restart_write;
657 return err;
658}
659
660status_t Parcel::write(const void* data, size_t len)
661{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700662 if (len > INT32_MAX) {
663 // don't accept size_t values which may have come from an
664 // inadvertent conversion from a negative int.
665 return BAD_VALUE;
666 }
667
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700668 void* const d = writeInplace(len);
669 if (d) {
670 memcpy(d, data, len);
671 return NO_ERROR;
672 }
673 return mError;
674}
675
676void* Parcel::writeInplace(size_t len)
677{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700678 if (len > INT32_MAX) {
679 // don't accept size_t values which may have come from an
680 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700681 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700682 }
683
684 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700685
686 // sanity check for integer overflow
687 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700688 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700689 }
690
691 if ((mDataPos+padded) <= mDataCapacity) {
692restart_write:
693 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
694 uint8_t* const data = mData+mDataPos;
695
696 // Need to pad at end?
697 if (padded != len) {
698#if BYTE_ORDER == BIG_ENDIAN
699 static const uint32_t mask[4] = {
700 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
701 };
702#endif
703#if BYTE_ORDER == LITTLE_ENDIAN
704 static const uint32_t mask[4] = {
705 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
706 };
707#endif
708 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
709 // *reinterpret_cast<void**>(data+padded-4));
710 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
711 }
712
713 finishWrite(padded);
714 return data;
715 }
716
717 status_t err = growData(padded);
718 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700719 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700720}
721
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800722status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
723 const uint8_t* strData = (uint8_t*)str.data();
724 const size_t strLen= str.length();
725 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100726 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800727 return BAD_VALUE;
728 }
729
730 status_t err = writeInt32(utf16Len);
731 if (err) {
732 return err;
733 }
734
735 // Allocate enough bytes to hold our converted string and its terminating NULL.
736 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
737 if (!dst) {
738 return NO_MEMORY;
739 }
740
Sergio Girof4607432016-07-21 14:46:35 +0100741 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800742
743 return NO_ERROR;
744}
745
746status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
747 if (!str) {
748 return writeInt32(-1);
749 }
750 return writeUtf8AsUtf16(*str);
751}
752
Daniel Normand0337ef2019-09-20 15:46:03 -0700753status_t Parcel::writeByteVectorInternal(const int8_t* data, size_t size) {
754 if (size > std::numeric_limits<int32_t>::max()) {
755 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -0700756 }
757
Daniel Normand0337ef2019-09-20 15:46:03 -0700758 status_t status = writeInt32(size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700759 if (status != OK) {
760 return status;
761 }
762
Daniel Normand0337ef2019-09-20 15:46:03 -0700763 return write(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700764}
765
Casey Dahlin185d3442016-02-09 11:08:35 -0800766status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700767 return writeByteVectorInternal(val.data(), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800768}
769
770status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
771{
Daniel Normand0337ef2019-09-20 15:46:03 -0700772 if (!val) return writeInt32(-1);
773 return writeByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800774}
775
776status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700777 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val.data()), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800778}
779
780status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
781{
Daniel Normand0337ef2019-09-20 15:46:03 -0700782 if (!val) return writeInt32(-1);
783 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800784}
785
Casey Dahlin451ff582015-10-19 18:12:18 -0700786status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
787{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800788 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700789}
790
Casey Dahlinb9872622015-11-25 15:09:45 -0800791status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
792{
793 return writeNullableTypedVector(val, &Parcel::writeInt32);
794}
795
Casey Dahlin451ff582015-10-19 18:12:18 -0700796status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
797{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800798 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700799}
800
Casey Dahlinb9872622015-11-25 15:09:45 -0800801status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
802{
803 return writeNullableTypedVector(val, &Parcel::writeInt64);
804}
805
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800806status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
807{
808 return writeTypedVector(val, &Parcel::writeUint64);
809}
810
811status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
812{
813 return writeNullableTypedVector(val, &Parcel::writeUint64);
814}
815
Casey Dahlin451ff582015-10-19 18:12:18 -0700816status_t Parcel::writeFloatVector(const std::vector<float>& val)
817{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800818 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700819}
820
Casey Dahlinb9872622015-11-25 15:09:45 -0800821status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
822{
823 return writeNullableTypedVector(val, &Parcel::writeFloat);
824}
825
Casey Dahlin451ff582015-10-19 18:12:18 -0700826status_t Parcel::writeDoubleVector(const std::vector<double>& val)
827{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800828 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700829}
830
Casey Dahlinb9872622015-11-25 15:09:45 -0800831status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
832{
833 return writeNullableTypedVector(val, &Parcel::writeDouble);
834}
835
Casey Dahlin451ff582015-10-19 18:12:18 -0700836status_t Parcel::writeBoolVector(const std::vector<bool>& val)
837{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800838 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700839}
840
Casey Dahlinb9872622015-11-25 15:09:45 -0800841status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
842{
843 return writeNullableTypedVector(val, &Parcel::writeBool);
844}
845
Casey Dahlin451ff582015-10-19 18:12:18 -0700846status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
847{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800848 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700849}
850
Casey Dahlinb9872622015-11-25 15:09:45 -0800851status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
852{
853 return writeNullableTypedVector(val, &Parcel::writeChar);
854}
855
Casey Dahlin451ff582015-10-19 18:12:18 -0700856status_t Parcel::writeString16Vector(const std::vector<String16>& val)
857{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800858 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700859}
860
Casey Dahlinb9872622015-11-25 15:09:45 -0800861status_t Parcel::writeString16Vector(
862 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
863{
864 return writeNullableTypedVector(val, &Parcel::writeString16);
865}
866
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800867status_t Parcel::writeUtf8VectorAsUtf16Vector(
868 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
869 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
870}
871
872status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
873 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
874}
875
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700876status_t Parcel::writeInt32(int32_t val)
877{
Andreas Huber84a6d042009-08-17 13:33:27 -0700878 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700879}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800880
881status_t Parcel::writeUint32(uint32_t val)
882{
883 return writeAligned(val);
884}
885
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700886status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700887 if (len > INT32_MAX) {
888 // don't accept size_t values which may have come from an
889 // inadvertent conversion from a negative int.
890 return BAD_VALUE;
891 }
892
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700893 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700894 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700895 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700896 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700897 if (ret == NO_ERROR) {
898 ret = write(val, len * sizeof(*val));
899 }
900 return ret;
901}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700902status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700903 if (len > INT32_MAX) {
904 // don't accept size_t values which may have come from an
905 // inadvertent conversion from a negative int.
906 return BAD_VALUE;
907 }
908
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700909 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700910 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700911 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700912 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700913 if (ret == NO_ERROR) {
914 ret = write(val, len * sizeof(*val));
915 }
916 return ret;
917}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700918
Casey Dahlind6848f52015-10-15 15:44:59 -0700919status_t Parcel::writeBool(bool val)
920{
921 return writeInt32(int32_t(val));
922}
923
924status_t Parcel::writeChar(char16_t val)
925{
926 return writeInt32(int32_t(val));
927}
928
929status_t Parcel::writeByte(int8_t val)
930{
931 return writeInt32(int32_t(val));
932}
933
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700934status_t Parcel::writeInt64(int64_t val)
935{
Andreas Huber84a6d042009-08-17 13:33:27 -0700936 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700937}
938
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700939status_t Parcel::writeUint64(uint64_t val)
940{
941 return writeAligned(val);
942}
943
Serban Constantinescuf683e012013-11-05 16:53:55 +0000944status_t Parcel::writePointer(uintptr_t val)
945{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800946 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000947}
948
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700949status_t Parcel::writeFloat(float val)
950{
Andreas Huber84a6d042009-08-17 13:33:27 -0700951 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700952}
953
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800954#if defined(__mips__) && defined(__mips_hard_float)
955
956status_t Parcel::writeDouble(double val)
957{
958 union {
959 double d;
960 unsigned long long ll;
961 } u;
962 u.d = val;
963 return writeAligned(u.ll);
964}
965
966#else
967
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700968status_t Parcel::writeDouble(double val)
969{
Andreas Huber84a6d042009-08-17 13:33:27 -0700970 return writeAligned(val);
971}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700972
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800973#endif
974
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700975status_t Parcel::writeCString(const char* str)
976{
977 return write(str, strlen(str)+1);
978}
979
980status_t Parcel::writeString8(const String8& str)
981{
982 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100983 // only write string if its length is more than zero characters,
984 // as readString8 will only read if the length field is non-zero.
985 // this is slightly different from how writeString16 works.
986 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700987 err = write(str.string(), str.bytes()+1);
988 }
989 return err;
990}
991
Casey Dahlinb9872622015-11-25 15:09:45 -0800992status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
993{
994 if (!str) {
995 return writeInt32(-1);
996 }
997
998 return writeString16(*str);
999}
1000
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001001status_t Parcel::writeString16(const String16& str)
1002{
1003 return writeString16(str.string(), str.size());
1004}
1005
1006status_t Parcel::writeString16(const char16_t* str, size_t len)
1007{
Yi Kong91635562018-06-07 14:38:36 -07001008 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001009
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001010 status_t err = writeInt32(len);
1011 if (err == NO_ERROR) {
1012 len *= sizeof(char16_t);
1013 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1014 if (data) {
1015 memcpy(data, str, len);
1016 *reinterpret_cast<char16_t*>(data+len) = 0;
1017 return NO_ERROR;
1018 }
1019 err = mError;
1020 }
1021 return err;
1022}
1023
1024status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1025{
Steven Morelanda86a3562019-08-01 23:28:34 +00001026 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001027}
1028
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001029status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1030{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001031 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001032}
1033
Casey Dahlinb9872622015-11-25 15:09:45 -08001034status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1035{
1036 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1037}
1038
1039status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001040 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001041}
1042
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001043status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001044 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001045}
1046
Casey Dahlinb9872622015-11-25 15:09:45 -08001047status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1048 if (!parcelable) {
1049 return writeInt32(0);
1050 }
1051
1052 return writeParcelable(*parcelable);
1053}
1054
Christopher Wiley97f048d2015-11-19 06:49:05 -08001055status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1056 status_t status = writeInt32(1); // parcelable is not null.
1057 if (status != OK) {
1058 return status;
1059 }
1060 return parcelable.writeToParcel(this);
1061}
1062
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001063status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001064{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001065 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001066 return BAD_TYPE;
1067
1068 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001069 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001070 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001072 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001073 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001075 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1076 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077
1078 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001079 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080 return err;
1081 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001082 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001083 return err;
1084}
1085
Jeff Brown93ff1f92011-11-04 19:01:44 -07001086status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001087{
1088 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001089 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001090 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001091 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001092 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001093 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001094 return writeObject(obj, true);
1095}
1096
1097status_t Parcel::writeDupFileDescriptor(int fd)
1098{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001099 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001100 if (dupFd < 0) {
1101 return -errno;
1102 }
1103 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001104 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001105 close(dupFd);
1106 }
1107 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001108}
1109
Dianne Hackborn1941a402016-08-29 12:30:43 -07001110status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1111{
1112 writeInt32(0);
1113 return writeFileDescriptor(fd, takeOwnership);
1114}
1115
Ryo Hashimotobf551892018-05-31 16:58:35 +09001116status_t Parcel::writeDupParcelFileDescriptor(int fd)
1117{
1118 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1119 if (dupFd < 0) {
1120 return -errno;
1121 }
1122 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1123 if (err != OK) {
1124 close(dupFd);
1125 }
1126 return err;
1127}
1128
Christopher Wiley2cf19952016-04-11 11:09:37 -07001129status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001130 return writeDupFileDescriptor(fd.get());
1131}
1132
Christopher Wiley2cf19952016-04-11 11:09:37 -07001133status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001134 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1135}
1136
Christopher Wiley2cf19952016-04-11 11:09:37 -07001137status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001138 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1139}
1140
Jeff Brown13b16042014-11-11 16:44:25 -08001141status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001142{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001143 if (len > INT32_MAX) {
1144 // don't accept size_t values which may have come from an
1145 // inadvertent conversion from a negative int.
1146 return BAD_VALUE;
1147 }
1148
Jeff Brown13b16042014-11-11 16:44:25 -08001149 status_t status;
1150 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001151 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001152 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001153 if (status) return status;
1154
1155 void* ptr = writeInplace(len);
1156 if (!ptr) return NO_MEMORY;
1157
Jeff Brown13b16042014-11-11 16:44:25 -08001158 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001159 return NO_ERROR;
1160 }
1161
Steve Block6807e592011-10-20 11:56:00 +01001162 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001163 int fd = ashmem_create_region("Parcel Blob", len);
1164 if (fd < 0) return NO_MEMORY;
1165
1166 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1167 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001168 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001169 } else {
Yi Kong91635562018-06-07 14:38:36 -07001170 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001171 if (ptr == MAP_FAILED) {
1172 status = -errno;
1173 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001174 if (!mutableCopy) {
1175 result = ashmem_set_prot_region(fd, PROT_READ);
1176 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001177 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001178 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001179 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001180 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001181 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001182 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001183 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001184 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001185 return NO_ERROR;
1186 }
1187 }
1188 }
1189 }
1190 ::munmap(ptr, len);
1191 }
1192 ::close(fd);
1193 return status;
1194}
1195
Jeff Brown13b16042014-11-11 16:44:25 -08001196status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1197{
1198 // Must match up with what's done in writeBlob.
1199 if (!mAllowFds) return FDS_NOT_ALLOWED;
1200 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1201 if (status) return status;
1202 return writeDupFileDescriptor(fd);
1203}
1204
Mathias Agopiane1424282013-07-29 21:24:40 -07001205status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001206{
1207 status_t err;
1208
1209 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001210 const size_t len = val.getFlattenedSize();
1211 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001212
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001213 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001214 // don't accept size_t values which may have come from an
1215 // inadvertent conversion from a negative int.
1216 return BAD_VALUE;
1217 }
1218
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001219 err = this->writeInt32(len);
1220 if (err) return err;
1221
1222 err = this->writeInt32(fd_count);
1223 if (err) return err;
1224
1225 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001226 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001227 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001228 return BAD_VALUE;
1229
Yi Kong91635562018-06-07 14:38:36 -07001230 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001231 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001232 fds = new (std::nothrow) int[fd_count];
1233 if (fds == nullptr) {
1234 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1235 return BAD_VALUE;
1236 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001237 }
1238
1239 err = val.flatten(buf, len, fds, fd_count);
1240 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1241 err = this->writeDupFileDescriptor( fds[i] );
1242 }
1243
1244 if (fd_count) {
1245 delete [] fds;
1246 }
1247
1248 return err;
1249}
1250
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001251status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1252{
1253 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1254 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1255 if (enoughData && enoughObjects) {
1256restart_write:
1257 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001258
Christopher Tate98e67d32015-06-03 18:44:15 -07001259 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001260 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001261 if (!mAllowFds) {
1262 // fail before modifying our object index
1263 return FDS_NOT_ALLOWED;
1264 }
1265 mHasFds = mFdsKnown = true;
1266 }
1267
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001268 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001269 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001270 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001271 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001272 mObjectsSize++;
1273 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001274
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001275 return finishWrite(sizeof(flat_binder_object));
1276 }
1277
1278 if (!enoughData) {
1279 const status_t err = growData(sizeof(val));
1280 if (err != NO_ERROR) return err;
1281 }
1282 if (!enoughObjects) {
1283 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001284 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001285 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001286 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001287 mObjects = objects;
1288 mObjectsCapacity = newSize;
1289 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001290
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001291 goto restart_write;
1292}
1293
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001294status_t Parcel::writeNoException()
1295{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001296 binder::Status status;
1297 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001298}
1299
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001300status_t Parcel::validateReadData(size_t upperBound) const
1301{
1302 // Don't allow non-object reads on object data
1303 if (mObjectsSorted || mObjectsSize <= 1) {
1304data_sorted:
1305 // Expect to check only against the next object
1306 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1307 // For some reason the current read position is greater than the next object
1308 // hint. Iterate until we find the right object
1309 size_t nextObject = mNextObjectHint;
1310 do {
1311 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1312 // Requested info overlaps with an object
1313 ALOGE("Attempt to read from protected data in Parcel %p", this);
1314 return PERMISSION_DENIED;
1315 }
1316 nextObject++;
1317 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1318 mNextObjectHint = nextObject;
1319 }
1320 return NO_ERROR;
1321 }
1322 // Quickly determine if mObjects is sorted.
1323 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1324 binder_size_t* prevObj = currObj;
1325 while (currObj > mObjects) {
1326 prevObj--;
1327 if(*prevObj > *currObj) {
1328 goto data_unsorted;
1329 }
1330 currObj--;
1331 }
1332 mObjectsSorted = true;
1333 goto data_sorted;
1334
1335data_unsorted:
1336 // Insertion Sort mObjects
1337 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1338 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1339 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1340 binder_size_t temp = *iter0;
1341 binder_size_t* iter1 = iter0 - 1;
1342 while (iter1 >= mObjects && *iter1 > temp) {
1343 *(iter1 + 1) = *iter1;
1344 iter1--;
1345 }
1346 *(iter1 + 1) = temp;
1347 }
1348 mNextObjectHint = 0;
1349 mObjectsSorted = true;
1350 goto data_sorted;
1351}
1352
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001353status_t Parcel::read(void* outData, size_t len) const
1354{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001355 if (len > INT32_MAX) {
1356 // don't accept size_t values which may have come from an
1357 // inadvertent conversion from a negative int.
1358 return BAD_VALUE;
1359 }
1360
1361 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1362 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001363 if (mObjectsSize > 0) {
1364 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001365 if(err != NO_ERROR) {
1366 // Still increment the data position by the expected length
1367 mDataPos += pad_size(len);
1368 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1369 return err;
1370 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001371 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001372 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001373 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001374 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001375 return NO_ERROR;
1376 }
1377 return NOT_ENOUGH_DATA;
1378}
1379
1380const void* Parcel::readInplace(size_t len) const
1381{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001382 if (len > INT32_MAX) {
1383 // don't accept size_t values which may have come from an
1384 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001385 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001386 }
1387
1388 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1389 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001390 if (mObjectsSize > 0) {
1391 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001392 if(err != NO_ERROR) {
1393 // Still increment the data position by the expected length
1394 mDataPos += pad_size(len);
1395 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001396 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001397 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001398 }
1399
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001400 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001401 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001402 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001403 return data;
1404 }
Yi Kong91635562018-06-07 14:38:36 -07001405 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001406}
1407
Andreas Huber84a6d042009-08-17 13:33:27 -07001408template<class T>
1409status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001410 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001411
1412 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001413 if (mObjectsSize > 0) {
1414 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001415 if(err != NO_ERROR) {
1416 // Still increment the data position by the expected length
1417 mDataPos += sizeof(T);
1418 return err;
1419 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001420 }
1421
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001422 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001423 mDataPos += sizeof(T);
1424 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001425 return NO_ERROR;
1426 } else {
1427 return NOT_ENOUGH_DATA;
1428 }
1429}
1430
Andreas Huber84a6d042009-08-17 13:33:27 -07001431template<class T>
1432T Parcel::readAligned() const {
1433 T result;
1434 if (readAligned(&result) != NO_ERROR) {
1435 result = 0;
1436 }
1437
1438 return result;
1439}
1440
1441template<class T>
1442status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001443 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001444
1445 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1446restart_write:
1447 *reinterpret_cast<T*>(mData+mDataPos) = val;
1448 return finishWrite(sizeof(val));
1449 }
1450
1451 status_t err = growData(sizeof(val));
1452 if (err == NO_ERROR) goto restart_write;
1453 return err;
1454}
1455
Daniel Normand0337ef2019-09-20 15:46:03 -07001456status_t Parcel::readByteVectorInternal(int8_t* data, size_t size) const {
1457 if (size_t(size) > dataAvail()) {
1458 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -07001459 }
Daniel Normand0337ef2019-09-20 15:46:03 -07001460 return read(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001461}
1462
Casey Dahlin185d3442016-02-09 11:08:35 -08001463status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
Daniel Normand0337ef2019-09-20 15:46:03 -07001464 if (status_t status = resizeOutVector(val); status != OK) return status;
1465 return readByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -08001466}
1467
1468status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
Daniel Normand0337ef2019-09-20 15:46:03 -07001469 if (status_t status = resizeOutVector(val); status != OK) return status;
1470 return readByteVectorInternal(reinterpret_cast<int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -08001471}
1472
1473status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
Daniel Normand0337ef2019-09-20 15:46:03 -07001474 if (status_t status = resizeOutVector(val); status != OK) return status;
1475 if (val->get() == nullptr) {
1476 // resizeOutVector does not create the out vector if size is < 0.
1477 // This occurs when writing a null byte vector.
1478 return OK;
1479 }
1480 return readByteVectorInternal((*val)->data(), (*val)->size());
Casey Dahlin185d3442016-02-09 11:08:35 -08001481}
1482
1483status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
Daniel Normand0337ef2019-09-20 15:46:03 -07001484 if (status_t status = resizeOutVector(val); status != OK) return status;
1485 if (val->get() == nullptr) {
1486 // resizeOutVector does not create the out vector if size is < 0.
1487 // This occurs when writing a null byte vector.
1488 return OK;
1489 }
1490 return readByteVectorInternal(reinterpret_cast<int8_t*>((*val)->data()), (*val)->size());
Casey Dahlin185d3442016-02-09 11:08:35 -08001491}
1492
Casey Dahlinb9872622015-11-25 15:09:45 -08001493status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1494 return readNullableTypedVector(val, &Parcel::readInt32);
1495}
1496
Casey Dahlin451ff582015-10-19 18:12:18 -07001497status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001498 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001499}
1500
Casey Dahlinb9872622015-11-25 15:09:45 -08001501status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1502 return readNullableTypedVector(val, &Parcel::readInt64);
1503}
1504
Casey Dahlin451ff582015-10-19 18:12:18 -07001505status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001506 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001507}
1508
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001509status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1510 return readNullableTypedVector(val, &Parcel::readUint64);
1511}
1512
1513status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1514 return readTypedVector(val, &Parcel::readUint64);
1515}
1516
Casey Dahlinb9872622015-11-25 15:09:45 -08001517status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1518 return readNullableTypedVector(val, &Parcel::readFloat);
1519}
1520
Casey Dahlin451ff582015-10-19 18:12:18 -07001521status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001522 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001523}
1524
Casey Dahlinb9872622015-11-25 15:09:45 -08001525status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1526 return readNullableTypedVector(val, &Parcel::readDouble);
1527}
1528
Casey Dahlin451ff582015-10-19 18:12:18 -07001529status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001530 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001531}
1532
Casey Dahlinb9872622015-11-25 15:09:45 -08001533status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1534 const int32_t start = dataPosition();
1535 int32_t size;
1536 status_t status = readInt32(&size);
1537 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001538
Casey Dahlinb9872622015-11-25 15:09:45 -08001539 if (status != OK || size < 0) {
1540 return status;
1541 }
1542
1543 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001544 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001545
1546 status = readBoolVector(val->get());
1547
1548 if (status != OK) {
1549 val->reset();
1550 }
1551
1552 return status;
1553}
1554
1555status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001556 int32_t size;
1557 status_t status = readInt32(&size);
1558
1559 if (status != OK) {
1560 return status;
1561 }
1562
1563 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001564 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001565 }
1566
1567 val->resize(size);
1568
1569 /* C++ bool handling means a vector of bools isn't necessarily addressable
1570 * (we might use individual bits)
1571 */
Christopher Wiley97887982015-10-27 16:33:47 -07001572 bool data;
1573 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001574 status = readBool(&data);
1575 (*val)[i] = data;
1576
1577 if (status != OK) {
1578 return status;
1579 }
1580 }
1581
1582 return OK;
1583}
1584
Casey Dahlinb9872622015-11-25 15:09:45 -08001585status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1586 return readNullableTypedVector(val, &Parcel::readChar);
1587}
1588
Casey Dahlin451ff582015-10-19 18:12:18 -07001589status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001590 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001591}
1592
Casey Dahlinb9872622015-11-25 15:09:45 -08001593status_t Parcel::readString16Vector(
1594 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1595 return readNullableTypedVector(val, &Parcel::readString16);
1596}
1597
Casey Dahlin451ff582015-10-19 18:12:18 -07001598status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001599 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001600}
1601
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001602status_t Parcel::readUtf8VectorFromUtf16Vector(
1603 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1604 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1605}
1606
1607status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1608 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1609}
Casey Dahlin451ff582015-10-19 18:12:18 -07001610
Andreas Huber84a6d042009-08-17 13:33:27 -07001611status_t Parcel::readInt32(int32_t *pArg) const
1612{
1613 return readAligned(pArg);
1614}
1615
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001616int32_t Parcel::readInt32() const
1617{
Andreas Huber84a6d042009-08-17 13:33:27 -07001618 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001619}
1620
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001621status_t Parcel::readUint32(uint32_t *pArg) const
1622{
1623 return readAligned(pArg);
1624}
1625
1626uint32_t Parcel::readUint32() const
1627{
1628 return readAligned<uint32_t>();
1629}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001630
1631status_t Parcel::readInt64(int64_t *pArg) const
1632{
Andreas Huber84a6d042009-08-17 13:33:27 -07001633 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001634}
1635
1636
1637int64_t Parcel::readInt64() const
1638{
Andreas Huber84a6d042009-08-17 13:33:27 -07001639 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001640}
1641
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001642status_t Parcel::readUint64(uint64_t *pArg) const
1643{
1644 return readAligned(pArg);
1645}
1646
1647uint64_t Parcel::readUint64() const
1648{
1649 return readAligned<uint64_t>();
1650}
1651
Serban Constantinescuf683e012013-11-05 16:53:55 +00001652status_t Parcel::readPointer(uintptr_t *pArg) const
1653{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001654 status_t ret;
1655 binder_uintptr_t ptr;
1656 ret = readAligned(&ptr);
1657 if (!ret)
1658 *pArg = ptr;
1659 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001660}
1661
1662uintptr_t Parcel::readPointer() const
1663{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001664 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001665}
1666
1667
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001668status_t Parcel::readFloat(float *pArg) const
1669{
Andreas Huber84a6d042009-08-17 13:33:27 -07001670 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001671}
1672
1673
1674float Parcel::readFloat() const
1675{
Andreas Huber84a6d042009-08-17 13:33:27 -07001676 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001677}
1678
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001679#if defined(__mips__) && defined(__mips_hard_float)
1680
1681status_t Parcel::readDouble(double *pArg) const
1682{
1683 union {
1684 double d;
1685 unsigned long long ll;
1686 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001687 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001688 status_t status;
1689 status = readAligned(&u.ll);
1690 *pArg = u.d;
1691 return status;
1692}
1693
1694double Parcel::readDouble() const
1695{
1696 union {
1697 double d;
1698 unsigned long long ll;
1699 } u;
1700 u.ll = readAligned<unsigned long long>();
1701 return u.d;
1702}
1703
1704#else
1705
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001706status_t Parcel::readDouble(double *pArg) const
1707{
Andreas Huber84a6d042009-08-17 13:33:27 -07001708 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001709}
1710
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001711double Parcel::readDouble() const
1712{
Andreas Huber84a6d042009-08-17 13:33:27 -07001713 return readAligned<double>();
1714}
1715
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001716#endif
1717
Andreas Huber84a6d042009-08-17 13:33:27 -07001718status_t Parcel::readIntPtr(intptr_t *pArg) const
1719{
1720 return readAligned(pArg);
1721}
1722
1723
1724intptr_t Parcel::readIntPtr() const
1725{
1726 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001727}
1728
Casey Dahlind6848f52015-10-15 15:44:59 -07001729status_t Parcel::readBool(bool *pArg) const
1730{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001731 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001732 status_t ret = readInt32(&tmp);
1733 *pArg = (tmp != 0);
1734 return ret;
1735}
1736
1737bool Parcel::readBool() const
1738{
1739 return readInt32() != 0;
1740}
1741
1742status_t Parcel::readChar(char16_t *pArg) const
1743{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001744 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001745 status_t ret = readInt32(&tmp);
1746 *pArg = char16_t(tmp);
1747 return ret;
1748}
1749
1750char16_t Parcel::readChar() const
1751{
1752 return char16_t(readInt32());
1753}
1754
1755status_t Parcel::readByte(int8_t *pArg) const
1756{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001757 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001758 status_t ret = readInt32(&tmp);
1759 *pArg = int8_t(tmp);
1760 return ret;
1761}
1762
1763int8_t Parcel::readByte() const
1764{
1765 return int8_t(readInt32());
1766}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001767
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001768status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1769 size_t utf16Size = 0;
1770 const char16_t* src = readString16Inplace(&utf16Size);
1771 if (!src) {
1772 return UNEXPECTED_NULL;
1773 }
1774
1775 // Save ourselves the trouble, we're done.
1776 if (utf16Size == 0u) {
1777 str->clear();
1778 return NO_ERROR;
1779 }
1780
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001781 // Allow for closing '\0'
1782 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1783 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001784 return BAD_VALUE;
1785 }
1786 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001787 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001788 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001789 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1790 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001791 return NO_ERROR;
1792}
1793
1794status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1795 const int32_t start = dataPosition();
1796 int32_t size;
1797 status_t status = readInt32(&size);
1798 str->reset();
1799
1800 if (status != OK || size < 0) {
1801 return status;
1802 }
1803
1804 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001805 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001806 return readUtf8FromUtf16(str->get());
1807}
1808
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001809const char* Parcel::readCString() const
1810{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001811 if (mDataPos < mDataSize) {
1812 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001813 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1814 // is the string's trailing NUL within the parcel's valid bounds?
1815 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1816 if (eos) {
1817 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001818 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001819 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001820 return str;
1821 }
1822 }
Yi Kong91635562018-06-07 14:38:36 -07001823 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001824}
1825
1826String8 Parcel::readString8() const
1827{
Roshan Pius87b64d22016-07-18 12:51:02 -07001828 String8 retString;
1829 status_t status = readString8(&retString);
1830 if (status != OK) {
1831 // We don't care about errors here, so just return an empty string.
1832 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001833 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001834 return retString;
1835}
1836
1837status_t Parcel::readString8(String8* pArg) const
1838{
1839 int32_t size;
1840 status_t status = readInt32(&size);
1841 if (status != OK) {
1842 return status;
1843 }
1844 // watch for potential int overflow from size+1
1845 if (size < 0 || size >= INT32_MAX) {
1846 return BAD_VALUE;
1847 }
1848 // |writeString8| writes nothing for empty string.
1849 if (size == 0) {
1850 *pArg = String8();
1851 return OK;
1852 }
1853 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07001854 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07001855 return BAD_VALUE;
1856 }
1857 pArg->setTo(str, size);
1858 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001859}
1860
1861String16 Parcel::readString16() const
1862{
1863 size_t len;
1864 const char16_t* str = readString16Inplace(&len);
1865 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001866 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001867 return String16();
1868}
1869
Casey Dahlinb9872622015-11-25 15:09:45 -08001870status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1871{
1872 const int32_t start = dataPosition();
1873 int32_t size;
1874 status_t status = readInt32(&size);
1875 pArg->reset();
1876
1877 if (status != OK || size < 0) {
1878 return status;
1879 }
1880
1881 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001882 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001883
1884 status = readString16(pArg->get());
1885
1886 if (status != OK) {
1887 pArg->reset();
1888 }
1889
1890 return status;
1891}
1892
Casey Dahlin451ff582015-10-19 18:12:18 -07001893status_t Parcel::readString16(String16* pArg) const
1894{
1895 size_t len;
1896 const char16_t* str = readString16Inplace(&len);
1897 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001898 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001899 return 0;
1900 } else {
1901 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001902 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001903 }
1904}
1905
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001906const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1907{
1908 int32_t size = readInt32();
1909 // watch for potential int overflow from size+1
1910 if (size >= 0 && size < INT32_MAX) {
1911 *outLen = size;
1912 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001913 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001914 return str;
1915 }
1916 }
1917 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001918 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001919}
1920
Casey Dahlinf0c13772015-10-27 18:33:56 -07001921status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1922{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001923 status_t status = readNullableStrongBinder(val);
1924 if (status == OK && !val->get()) {
1925 status = UNEXPECTED_NULL;
1926 }
1927 return status;
1928}
1929
1930status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1931{
Steven Morelanda86a3562019-08-01 23:28:34 +00001932 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001933}
1934
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001935sp<IBinder> Parcel::readStrongBinder() const
1936{
1937 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001938 // Note that a lot of code in Android reads binders by hand with this
1939 // method, and that code has historically been ok with getting nullptr
1940 // back (while ignoring error codes).
1941 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001942 return val;
1943}
1944
Christopher Wiley97f048d2015-11-19 06:49:05 -08001945status_t Parcel::readParcelable(Parcelable* parcelable) const {
1946 int32_t have_parcelable = 0;
1947 status_t status = readInt32(&have_parcelable);
1948 if (status != OK) {
1949 return status;
1950 }
1951 if (!have_parcelable) {
1952 return UNEXPECTED_NULL;
1953 }
1954 return parcelable->readFromParcel(this);
1955}
1956
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001957int32_t Parcel::readExceptionCode() const
1958{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001959 binder::Status status;
1960 status.readFromParcel(*this);
1961 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001962}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001963
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001964native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001965{
1966 int numFds, numInts;
1967 status_t err;
1968 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001969 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001970 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001971 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001972
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001973 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001974 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001975 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001976 }
1977
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001978 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001979 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001980 if (h->data[i] < 0) {
1981 for (int j = 0; j < i; j++) {
1982 close(h->data[j]);
1983 }
1984 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001985 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001986 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001987 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001988 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001989 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001990 native_handle_close(h);
1991 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001992 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001993 }
1994 return h;
1995}
1996
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001997int Parcel::readFileDescriptor() const
1998{
1999 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002000
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002001 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002002 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002003 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002004
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002005 return BAD_TYPE;
2006}
2007
Dianne Hackborn1941a402016-08-29 12:30:43 -07002008int Parcel::readParcelFileDescriptor() const
2009{
2010 int32_t hasComm = readInt32();
2011 int fd = readFileDescriptor();
2012 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002013 // detach (owned by the binder driver)
2014 int comm = readFileDescriptor();
2015
2016 // warning: this must be kept in sync with:
2017 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2018 enum ParcelFileDescriptorStatus {
2019 DETACHED = 2,
2020 };
2021
2022#if BYTE_ORDER == BIG_ENDIAN
2023 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2024#endif
2025#if BYTE_ORDER == LITTLE_ENDIAN
2026 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2027#endif
2028
2029 ssize_t written = TEMP_FAILURE_RETRY(
2030 ::write(comm, &message, sizeof(message)));
2031
2032 if (written == -1 || written != sizeof(message)) {
2033 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2034 written, strerror(errno));
2035 return BAD_TYPE;
2036 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002037 }
2038 return fd;
2039}
2040
Christopher Wiley2cf19952016-04-11 11:09:37 -07002041status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002042{
2043 int got = readFileDescriptor();
2044
2045 if (got == BAD_TYPE) {
2046 return BAD_TYPE;
2047 }
2048
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002049 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002050
2051 if (val->get() < 0) {
2052 return BAD_VALUE;
2053 }
2054
2055 return OK;
2056}
2057
Ryo Hashimotobf551892018-05-31 16:58:35 +09002058status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2059{
2060 int got = readParcelFileDescriptor();
2061
2062 if (got == BAD_TYPE) {
2063 return BAD_TYPE;
2064 }
2065
2066 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2067
2068 if (val->get() < 0) {
2069 return BAD_VALUE;
2070 }
2071
2072 return OK;
2073}
Casey Dahlin06673e32015-11-23 13:24:23 -08002074
Christopher Wiley2cf19952016-04-11 11:09:37 -07002075status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002076 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2077}
2078
Christopher Wiley2cf19952016-04-11 11:09:37 -07002079status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002080 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2081}
2082
Jeff Brown5707dbf2011-09-23 21:17:56 -07002083status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2084{
Jeff Brown13b16042014-11-11 16:44:25 -08002085 int32_t blobType;
2086 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002087 if (status) return status;
2088
Jeff Brown13b16042014-11-11 16:44:25 -08002089 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002090 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002091 const void* ptr = readInplace(len);
2092 if (!ptr) return BAD_VALUE;
2093
Jeff Brown13b16042014-11-11 16:44:25 -08002094 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002095 return NO_ERROR;
2096 }
2097
Steve Block6807e592011-10-20 11:56:00 +01002098 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002099 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002100 int fd = readFileDescriptor();
2101 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2102
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002103 if (!ashmem_valid(fd)) {
2104 ALOGE("invalid fd");
2105 return BAD_VALUE;
2106 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002107 int size = ashmem_get_size_region(fd);
2108 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002109 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002110 return BAD_VALUE;
2111 }
Yi Kong91635562018-06-07 14:38:36 -07002112 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002113 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002114 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002115
Jeff Brown13b16042014-11-11 16:44:25 -08002116 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002117 return NO_ERROR;
2118}
2119
Mathias Agopiane1424282013-07-29 21:24:40 -07002120status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002121{
2122 // size
2123 const size_t len = this->readInt32();
2124 const size_t fd_count = this->readInt32();
2125
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002126 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002127 // don't accept size_t values which may have come from an
2128 // inadvertent conversion from a negative int.
2129 return BAD_VALUE;
2130 }
2131
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002132 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002133 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002134 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002135 return BAD_VALUE;
2136
Yi Kong91635562018-06-07 14:38:36 -07002137 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002138 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002139 fds = new (std::nothrow) int[fd_count];
2140 if (fds == nullptr) {
2141 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2142 return BAD_VALUE;
2143 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002144 }
2145
2146 status_t err = NO_ERROR;
2147 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002148 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002149 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002150 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002151 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 -07002152 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2153 // Close all the file descriptors that were dup-ed.
2154 for (size_t j=0; j<i ;j++) {
2155 close(fds[j]);
2156 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002157 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002158 }
2159
2160 if (err == NO_ERROR) {
2161 err = val.unflatten(buf, len, fds, fd_count);
2162 }
2163
2164 if (fd_count) {
2165 delete [] fds;
2166 }
2167
2168 return err;
2169}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002170const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2171{
2172 const size_t DPOS = mDataPos;
2173 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2174 const flat_binder_object* obj
2175 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2176 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002177 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002178 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002179 // the object list, so we don't want to check for it when
2180 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002181 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002182 return obj;
2183 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002184
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002185 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002186 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002187 const size_t N = mObjectsSize;
2188 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002189
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002190 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002191 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002192 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002193
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002194 // Start at the current hint position, looking for an object at
2195 // the current data position.
2196 if (opos < N) {
2197 while (opos < (N-1) && OBJS[opos] < DPOS) {
2198 opos++;
2199 }
2200 } else {
2201 opos = N-1;
2202 }
2203 if (OBJS[opos] == DPOS) {
2204 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002205 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002206 this, DPOS, opos);
2207 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002208 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002209 return obj;
2210 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002211
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002212 // Look backwards for it...
2213 while (opos > 0 && OBJS[opos] > DPOS) {
2214 opos--;
2215 }
2216 if (OBJS[opos] == DPOS) {
2217 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002218 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002219 this, DPOS, opos);
2220 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002221 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002222 return obj;
2223 }
2224 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002225 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 -07002226 this, DPOS);
2227 }
Yi Kong91635562018-06-07 14:38:36 -07002228 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002229}
2230
2231void Parcel::closeFileDescriptors()
2232{
2233 size_t i = mObjectsSize;
2234 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002235 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002236 }
2237 while (i > 0) {
2238 i--;
2239 const flat_binder_object* flat
2240 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002241 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002242 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002243 close(flat->handle);
2244 }
2245 }
2246}
2247
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002248uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002249{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002250 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002251}
2252
2253size_t Parcel::ipcDataSize() const
2254{
2255 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2256}
2257
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002258uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002259{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002260 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002261}
2262
2263size_t Parcel::ipcObjectsCount() const
2264{
2265 return mObjectsSize;
2266}
2267
2268void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002269 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002270{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002271 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002272 freeDataNoInit();
2273 mError = NO_ERROR;
2274 mData = const_cast<uint8_t*>(data);
2275 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002276 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002277 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002278 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002279 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002280 mObjectsSize = mObjectsCapacity = objectsCount;
2281 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002282 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002283 mOwner = relFunc;
2284 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002285 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002286 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002287 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002288 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002289 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002290 mObjectsSize = 0;
2291 break;
2292 }
2293 minOffset = offset + sizeof(flat_binder_object);
2294 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002295 scanForFds();
2296}
2297
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002298void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002299{
2300 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002301
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002302 if (errorCheck() != NO_ERROR) {
2303 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002304 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002305 } else if (dataSize() > 0) {
2306 const uint8_t* DATA = data();
2307 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002308 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002309 const size_t N = objectsCount();
2310 for (size_t i=0; i<N; i++) {
2311 const flat_binder_object* flat
2312 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2313 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002314 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002315 << " = " << flat->binder;
2316 }
2317 } else {
2318 to << "NULL";
2319 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002320
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002321 to << ")";
2322}
2323
2324void Parcel::releaseObjects()
2325{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002326 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002327 if (i == 0) {
2328 return;
2329 }
2330 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002331 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002332 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002333 while (i > 0) {
2334 i--;
2335 const flat_binder_object* flat
2336 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002337 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002338 }
2339}
2340
2341void Parcel::acquireObjects()
2342{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002343 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002344 if (i == 0) {
2345 return;
2346 }
2347 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002348 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002349 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002350 while (i > 0) {
2351 i--;
2352 const flat_binder_object* flat
2353 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002354 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002355 }
2356}
2357
2358void Parcel::freeData()
2359{
2360 freeDataNoInit();
2361 initState();
2362}
2363
2364void Parcel::freeDataNoInit()
2365{
2366 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002367 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002368 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002369 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2370 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002371 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002372 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002373 if (mData) {
2374 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002375 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002376 if (mDataCapacity <= gParcelGlobalAllocSize) {
2377 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2378 } else {
2379 gParcelGlobalAllocSize = 0;
2380 }
2381 if (gParcelGlobalAllocCount > 0) {
2382 gParcelGlobalAllocCount--;
2383 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002384 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002385 free(mData);
2386 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002387 if (mObjects) free(mObjects);
2388 }
2389}
2390
2391status_t Parcel::growData(size_t len)
2392{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002393 if (len > INT32_MAX) {
2394 // don't accept size_t values which may have come from an
2395 // inadvertent conversion from a negative int.
2396 return BAD_VALUE;
2397 }
2398
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002399 size_t newSize = ((mDataSize+len)*3)/2;
2400 return (newSize <= mDataSize)
2401 ? (status_t) NO_MEMORY
2402 : continueWrite(newSize);
2403}
2404
2405status_t Parcel::restartWrite(size_t desired)
2406{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002407 if (desired > INT32_MAX) {
2408 // don't accept size_t values which may have come from an
2409 // inadvertent conversion from a negative int.
2410 return BAD_VALUE;
2411 }
2412
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002413 if (mOwner) {
2414 freeData();
2415 return continueWrite(desired);
2416 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002417
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002418 uint8_t* data = (uint8_t*)realloc(mData, desired);
2419 if (!data && desired > mDataCapacity) {
2420 mError = NO_MEMORY;
2421 return NO_MEMORY;
2422 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002423
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002424 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002425
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002426 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002427 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002428 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002429 gParcelGlobalAllocSize += desired;
2430 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002431 if (!mData) {
2432 gParcelGlobalAllocCount++;
2433 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002434 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435 mData = data;
2436 mDataCapacity = desired;
2437 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002438
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002439 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002440 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2441 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2442
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002443 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002444 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002445 mObjectsSize = mObjectsCapacity = 0;
2446 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002447 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002448 mHasFds = false;
2449 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002450 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002451
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002452 return NO_ERROR;
2453}
2454
2455status_t Parcel::continueWrite(size_t desired)
2456{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002457 if (desired > INT32_MAX) {
2458 // don't accept size_t values which may have come from an
2459 // inadvertent conversion from a negative int.
2460 return BAD_VALUE;
2461 }
2462
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002463 // If shrinking, first adjust for any objects that appear
2464 // after the new data size.
2465 size_t objectsSize = mObjectsSize;
2466 if (desired < mDataSize) {
2467 if (desired == 0) {
2468 objectsSize = 0;
2469 } else {
2470 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002471 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002472 break;
2473 objectsSize--;
2474 }
2475 }
2476 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002477
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002478 if (mOwner) {
2479 // If the size is going to zero, just release the owner's data.
2480 if (desired == 0) {
2481 freeData();
2482 return NO_ERROR;
2483 }
2484
2485 // If there is a different owner, we need to take
2486 // posession.
2487 uint8_t* data = (uint8_t*)malloc(desired);
2488 if (!data) {
2489 mError = NO_MEMORY;
2490 return NO_MEMORY;
2491 }
Yi Kong91635562018-06-07 14:38:36 -07002492 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002493
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002494 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002495 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002496 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002497 free(data);
2498
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002499 mError = NO_MEMORY;
2500 return NO_MEMORY;
2501 }
2502
2503 // Little hack to only acquire references on objects
2504 // we will be keeping.
2505 size_t oldObjectsSize = mObjectsSize;
2506 mObjectsSize = objectsSize;
2507 acquireObjects();
2508 mObjectsSize = oldObjectsSize;
2509 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002510
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002511 if (mData) {
2512 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2513 }
2514 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002515 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002517 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002518 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002519 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002520
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002521 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002522 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002523 gParcelGlobalAllocSize += desired;
2524 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002525 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002526
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002527 mData = data;
2528 mObjects = objects;
2529 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002530 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002531 mDataCapacity = desired;
2532 mObjectsSize = mObjectsCapacity = objectsSize;
2533 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002534 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002535
2536 } else if (mData) {
2537 if (objectsSize < mObjectsSize) {
2538 // Need to release refs on any objects we are dropping.
2539 const sp<ProcessState> proc(ProcessState::self());
2540 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2541 const flat_binder_object* flat
2542 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002543 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544 // will need to rescan because we may have lopped off the only FDs
2545 mFdsKnown = false;
2546 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002547 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002548 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002549
2550 if (objectsSize == 0) {
2551 free(mObjects);
2552 mObjects = nullptr;
2553 } else {
2554 binder_size_t* objects =
2555 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2556 if (objects) {
2557 mObjects = objects;
2558 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002559 }
2560 mObjectsSize = objectsSize;
2561 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002562 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002563 }
2564
2565 // We own the data, so we can just do a realloc().
2566 if (desired > mDataCapacity) {
2567 uint8_t* data = (uint8_t*)realloc(mData, desired);
2568 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002569 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2570 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002571 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002572 gParcelGlobalAllocSize += desired;
2573 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002574 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002575 mData = data;
2576 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002577 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002578 mError = NO_MEMORY;
2579 return NO_MEMORY;
2580 }
2581 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002582 if (mDataSize > desired) {
2583 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002584 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002585 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002586 if (mDataPos > desired) {
2587 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002588 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002589 }
2590 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002591
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002592 } else {
2593 // This is the first data. Easy!
2594 uint8_t* data = (uint8_t*)malloc(desired);
2595 if (!data) {
2596 mError = NO_MEMORY;
2597 return NO_MEMORY;
2598 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002599
Yi Kong91635562018-06-07 14:38:36 -07002600 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002601 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002602 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002603 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002604
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002605 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002606 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002607 gParcelGlobalAllocSize += desired;
2608 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002609 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002610
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002611 mData = data;
2612 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002613 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2614 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002615 mDataCapacity = desired;
2616 }
2617
2618 return NO_ERROR;
2619}
2620
2621void Parcel::initState()
2622{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002623 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002624 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002625 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002626 mDataSize = 0;
2627 mDataCapacity = 0;
2628 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002629 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2630 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002631 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002632 mObjectsSize = 0;
2633 mObjectsCapacity = 0;
2634 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002635 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002636 mHasFds = false;
2637 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002638 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002639 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002640 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002641 mWorkSourceRequestHeaderPosition = 0;
2642 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002643
2644 // racing multiple init leads only to multiple identical write
2645 if (gMaxFds == 0) {
2646 struct rlimit result;
2647 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2648 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002649 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002650 } else {
2651 ALOGW("Unable to getrlimit: %s", strerror(errno));
2652 gMaxFds = 1024;
2653 }
2654 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002655}
2656
2657void Parcel::scanForFds() const
2658{
2659 bool hasFds = false;
2660 for (size_t i=0; i<mObjectsSize; i++) {
2661 const flat_binder_object* flat
2662 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002663 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002664 hasFds = true;
2665 break;
2666 }
2667 }
2668 mHasFds = hasFds;
2669 mFdsKnown = true;
2670}
2671
Dan Sandleraa5c2342015-04-10 10:08:45 -04002672size_t Parcel::getBlobAshmemSize() const
2673{
Adrian Roos6bb31142015-10-22 16:46:12 -07002674 // This used to return the size of all blobs that were written to ashmem, now we're returning
2675 // the ashmem currently referenced by this Parcel, which should be equivalent.
2676 // TODO: Remove method once ABI can be changed.
2677 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002678}
2679
Adrian Rooscbf37262015-10-22 16:12:53 -07002680size_t Parcel::getOpenAshmemSize() const
2681{
2682 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002683}
2684
2685// --- Parcel::Blob ---
2686
2687Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002688 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002689}
2690
2691Parcel::Blob::~Blob() {
2692 release();
2693}
2694
2695void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002696 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002697 ::munmap(mData, mSize);
2698 }
2699 clear();
2700}
2701
Jeff Brown13b16042014-11-11 16:44:25 -08002702void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2703 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002704 mData = data;
2705 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002706 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002707}
2708
2709void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002710 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002711 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002712 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002713 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002714}
2715
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002716}; // namespace android