blob: d0f20fe4a7a4a62019d2144eb26828c800b8663e [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)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070071 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070072 }
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) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700298 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700299 }
300 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700301}
302
303size_t Parcel::dataPosition() const
304{
305 return mDataPos;
306}
307
308size_t Parcel::dataCapacity() const
309{
310 return mDataCapacity;
311}
312
313status_t Parcel::setDataSize(size_t size)
314{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700315 if (size > INT32_MAX) {
316 // don't accept size_t values which may have come from an
317 // inadvertent conversion from a negative int.
318 return BAD_VALUE;
319 }
320
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700321 status_t err;
322 err = continueWrite(size);
323 if (err == NO_ERROR) {
324 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700325 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700326 }
327 return err;
328}
329
330void Parcel::setDataPosition(size_t pos) const
331{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700332 if (pos > INT32_MAX) {
333 // don't accept size_t values which may have come from an
334 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700335 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700336 }
337
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700338 mDataPos = pos;
339 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800340 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700341}
342
343status_t Parcel::setDataCapacity(size_t size)
344{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700345 if (size > INT32_MAX) {
346 // don't accept size_t values which may have come from an
347 // inadvertent conversion from a negative int.
348 return BAD_VALUE;
349 }
350
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700351 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700352 return NO_ERROR;
353}
354
355status_t Parcel::setData(const uint8_t* buffer, size_t len)
356{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700357 if (len > INT32_MAX) {
358 // don't accept size_t values which may have come from an
359 // inadvertent conversion from a negative int.
360 return BAD_VALUE;
361 }
362
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700363 status_t err = restartWrite(len);
364 if (err == NO_ERROR) {
365 memcpy(const_cast<uint8_t*>(data()), buffer, len);
366 mDataSize = len;
367 mFdsKnown = false;
368 }
369 return err;
370}
371
Andreas Huber51faf462011-04-13 10:21:56 -0700372status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700373{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700374 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700375 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800376 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700377 size_t size = parcel->mObjectsSize;
378 int startPos = mDataPos;
379 int firstIndex = -1, lastIndex = -2;
380
381 if (len == 0) {
382 return NO_ERROR;
383 }
384
Nick Kralevichb6b14232015-04-02 09:36:02 -0700385 if (len > INT32_MAX) {
386 // don't accept size_t values which may have come from an
387 // inadvertent conversion from a negative int.
388 return BAD_VALUE;
389 }
390
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700391 // range checks against the source parcel size
392 if ((offset > parcel->mDataSize)
393 || (len > parcel->mDataSize)
394 || (offset + len > parcel->mDataSize)) {
395 return BAD_VALUE;
396 }
397
398 // Count objects in range
399 for (int i = 0; i < (int) size; i++) {
400 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700401 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700402 if (firstIndex == -1) {
403 firstIndex = i;
404 }
405 lastIndex = i;
406 }
407 }
408 int numObjects = lastIndex - firstIndex + 1;
409
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700410 if ((mDataSize+len) > mDataCapacity) {
411 // grow data
412 err = growData(len);
413 if (err != NO_ERROR) {
414 return err;
415 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700416 }
417
418 // append data
419 memcpy(mData + mDataPos, data + offset, len);
420 mDataPos += len;
421 mDataSize += len;
422
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400423 err = NO_ERROR;
424
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700425 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200426 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700427 // grow objects
428 if (mObjectsCapacity < mObjectsSize + numObjects) {
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 Moreland0f452742019-07-31 15:50:51 +0000512#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 Moreland0f452742019-07-31 15:50:51 +0000526 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 Moreland0891c9b2019-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 Moreland0f452742019-07-31 15:50:51 +0000584 // 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
Casey Dahlin185d3442016-02-09 11:08:35 -0800753namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800754
Casey Dahlin185d3442016-02-09 11:08:35 -0800755template<typename T>
756status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700757{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700758 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700759 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700760 status = BAD_VALUE;
761 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700762 }
763
Casey Dahlin185d3442016-02-09 11:08:35 -0800764 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700765 if (status != OK) {
766 return status;
767 }
768
Casey Dahlin185d3442016-02-09 11:08:35 -0800769 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700770 if (!data) {
771 status = BAD_VALUE;
772 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700773 }
774
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700775 memcpy(data, val.data(), val.size());
776 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700777}
778
Casey Dahlin185d3442016-02-09 11:08:35 -0800779template<typename T>
780status_t writeByteVectorInternalPtr(Parcel* parcel,
781 const std::unique_ptr<std::vector<T>>& val)
782{
783 if (!val) {
784 return parcel->writeInt32(-1);
785 }
786
787 return writeByteVectorInternal(parcel, *val);
788}
789
790} // namespace
791
792status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
793 return writeByteVectorInternal(this, val);
794}
795
796status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
797{
798 return writeByteVectorInternalPtr(this, val);
799}
800
801status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
802 return writeByteVectorInternal(this, val);
803}
804
805status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
806{
807 return writeByteVectorInternalPtr(this, val);
808}
809
Casey Dahlin451ff582015-10-19 18:12:18 -0700810status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
811{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800812 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700813}
814
Casey Dahlinb9872622015-11-25 15:09:45 -0800815status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
816{
817 return writeNullableTypedVector(val, &Parcel::writeInt32);
818}
819
Casey Dahlin451ff582015-10-19 18:12:18 -0700820status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
821{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800822 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700823}
824
Casey Dahlinb9872622015-11-25 15:09:45 -0800825status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
826{
827 return writeNullableTypedVector(val, &Parcel::writeInt64);
828}
829
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800830status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
831{
832 return writeTypedVector(val, &Parcel::writeUint64);
833}
834
835status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
836{
837 return writeNullableTypedVector(val, &Parcel::writeUint64);
838}
839
Casey Dahlin451ff582015-10-19 18:12:18 -0700840status_t Parcel::writeFloatVector(const std::vector<float>& val)
841{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800842 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700843}
844
Casey Dahlinb9872622015-11-25 15:09:45 -0800845status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
846{
847 return writeNullableTypedVector(val, &Parcel::writeFloat);
848}
849
Casey Dahlin451ff582015-10-19 18:12:18 -0700850status_t Parcel::writeDoubleVector(const std::vector<double>& val)
851{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800852 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700853}
854
Casey Dahlinb9872622015-11-25 15:09:45 -0800855status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
856{
857 return writeNullableTypedVector(val, &Parcel::writeDouble);
858}
859
Casey Dahlin451ff582015-10-19 18:12:18 -0700860status_t Parcel::writeBoolVector(const std::vector<bool>& val)
861{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800862 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700863}
864
Casey Dahlinb9872622015-11-25 15:09:45 -0800865status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
866{
867 return writeNullableTypedVector(val, &Parcel::writeBool);
868}
869
Casey Dahlin451ff582015-10-19 18:12:18 -0700870status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
871{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800872 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700873}
874
Casey Dahlinb9872622015-11-25 15:09:45 -0800875status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
876{
877 return writeNullableTypedVector(val, &Parcel::writeChar);
878}
879
Casey Dahlin451ff582015-10-19 18:12:18 -0700880status_t Parcel::writeString16Vector(const std::vector<String16>& val)
881{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800882 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700883}
884
Casey Dahlinb9872622015-11-25 15:09:45 -0800885status_t Parcel::writeString16Vector(
886 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
887{
888 return writeNullableTypedVector(val, &Parcel::writeString16);
889}
890
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800891status_t Parcel::writeUtf8VectorAsUtf16Vector(
892 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
893 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
894}
895
896status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
897 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
898}
899
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700900status_t Parcel::writeInt32(int32_t val)
901{
Andreas Huber84a6d042009-08-17 13:33:27 -0700902 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700903}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800904
905status_t Parcel::writeUint32(uint32_t val)
906{
907 return writeAligned(val);
908}
909
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700910status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700911 if (len > INT32_MAX) {
912 // don't accept size_t values which may have come from an
913 // inadvertent conversion from a negative int.
914 return BAD_VALUE;
915 }
916
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700917 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700918 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700919 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700920 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700921 if (ret == NO_ERROR) {
922 ret = write(val, len * sizeof(*val));
923 }
924 return ret;
925}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700926status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700927 if (len > INT32_MAX) {
928 // don't accept size_t values which may have come from an
929 // inadvertent conversion from a negative int.
930 return BAD_VALUE;
931 }
932
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700933 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700934 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700935 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700936 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700937 if (ret == NO_ERROR) {
938 ret = write(val, len * sizeof(*val));
939 }
940 return ret;
941}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700942
Casey Dahlind6848f52015-10-15 15:44:59 -0700943status_t Parcel::writeBool(bool val)
944{
945 return writeInt32(int32_t(val));
946}
947
948status_t Parcel::writeChar(char16_t val)
949{
950 return writeInt32(int32_t(val));
951}
952
953status_t Parcel::writeByte(int8_t val)
954{
955 return writeInt32(int32_t(val));
956}
957
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700958status_t Parcel::writeInt64(int64_t val)
959{
Andreas Huber84a6d042009-08-17 13:33:27 -0700960 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700961}
962
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700963status_t Parcel::writeUint64(uint64_t val)
964{
965 return writeAligned(val);
966}
967
Serban Constantinescuf683e012013-11-05 16:53:55 +0000968status_t Parcel::writePointer(uintptr_t val)
969{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800970 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000971}
972
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700973status_t Parcel::writeFloat(float val)
974{
Andreas Huber84a6d042009-08-17 13:33:27 -0700975 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700976}
977
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800978#if defined(__mips__) && defined(__mips_hard_float)
979
980status_t Parcel::writeDouble(double val)
981{
982 union {
983 double d;
984 unsigned long long ll;
985 } u;
986 u.d = val;
987 return writeAligned(u.ll);
988}
989
990#else
991
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700992status_t Parcel::writeDouble(double val)
993{
Andreas Huber84a6d042009-08-17 13:33:27 -0700994 return writeAligned(val);
995}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700996
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800997#endif
998
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700999status_t Parcel::writeCString(const char* str)
1000{
1001 return write(str, strlen(str)+1);
1002}
1003
1004status_t Parcel::writeString8(const String8& str)
1005{
1006 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001007 // only write string if its length is more than zero characters,
1008 // as readString8 will only read if the length field is non-zero.
1009 // this is slightly different from how writeString16 works.
1010 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001011 err = write(str.string(), str.bytes()+1);
1012 }
1013 return err;
1014}
1015
Casey Dahlinb9872622015-11-25 15:09:45 -08001016status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1017{
1018 if (!str) {
1019 return writeInt32(-1);
1020 }
1021
1022 return writeString16(*str);
1023}
1024
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001025status_t Parcel::writeString16(const String16& str)
1026{
1027 return writeString16(str.string(), str.size());
1028}
1029
1030status_t Parcel::writeString16(const char16_t* str, size_t len)
1031{
Yi Kong91635562018-06-07 14:38:36 -07001032 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001033
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001034 status_t err = writeInt32(len);
1035 if (err == NO_ERROR) {
1036 len *= sizeof(char16_t);
1037 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1038 if (data) {
1039 memcpy(data, str, len);
1040 *reinterpret_cast<char16_t*>(data+len) = 0;
1041 return NO_ERROR;
1042 }
1043 err = mError;
1044 }
1045 return err;
1046}
1047
1048status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1049{
Steven Morelanda86a3562019-08-01 23:28:34 +00001050 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001051}
1052
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001053status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1054{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001055 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001056}
1057
Casey Dahlinb9872622015-11-25 15:09:45 -08001058status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1059{
1060 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1061}
1062
1063status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001064 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001065}
1066
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001067status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001068 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001069}
1070
Casey Dahlinb9872622015-11-25 15:09:45 -08001071status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1072 if (!parcelable) {
1073 return writeInt32(0);
1074 }
1075
1076 return writeParcelable(*parcelable);
1077}
1078
Christopher Wiley97f048d2015-11-19 06:49:05 -08001079status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1080 status_t status = writeInt32(1); // parcelable is not null.
1081 if (status != OK) {
1082 return status;
1083 }
1084 return parcelable.writeToParcel(this);
1085}
1086
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001087status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001088{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001089 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001090 return BAD_TYPE;
1091
1092 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001093 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001094 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001096 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001097 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001099 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1100 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101
1102 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001103 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104 return err;
1105 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001106 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001107 return err;
1108}
1109
Jeff Brown93ff1f92011-11-04 19:01:44 -07001110status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001111{
1112 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001113 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001114 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001115 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001116 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001117 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001118 return writeObject(obj, true);
1119}
1120
1121status_t Parcel::writeDupFileDescriptor(int fd)
1122{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001123 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001124 if (dupFd < 0) {
1125 return -errno;
1126 }
1127 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001128 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001129 close(dupFd);
1130 }
1131 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001132}
1133
Dianne Hackborn1941a402016-08-29 12:30:43 -07001134status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1135{
1136 writeInt32(0);
1137 return writeFileDescriptor(fd, takeOwnership);
1138}
1139
Ryo Hashimotobf551892018-05-31 16:58:35 +09001140status_t Parcel::writeDupParcelFileDescriptor(int fd)
1141{
1142 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1143 if (dupFd < 0) {
1144 return -errno;
1145 }
1146 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1147 if (err != OK) {
1148 close(dupFd);
1149 }
1150 return err;
1151}
1152
Christopher Wiley2cf19952016-04-11 11:09:37 -07001153status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001154 return writeDupFileDescriptor(fd.get());
1155}
1156
Christopher Wiley2cf19952016-04-11 11:09:37 -07001157status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001158 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1159}
1160
Christopher Wiley2cf19952016-04-11 11:09:37 -07001161status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001162 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1163}
1164
Jeff Brown13b16042014-11-11 16:44:25 -08001165status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001166{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001167 if (len > INT32_MAX) {
1168 // don't accept size_t values which may have come from an
1169 // inadvertent conversion from a negative int.
1170 return BAD_VALUE;
1171 }
1172
Jeff Brown13b16042014-11-11 16:44:25 -08001173 status_t status;
1174 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001175 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001176 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001177 if (status) return status;
1178
1179 void* ptr = writeInplace(len);
1180 if (!ptr) return NO_MEMORY;
1181
Jeff Brown13b16042014-11-11 16:44:25 -08001182 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001183 return NO_ERROR;
1184 }
1185
Steve Block6807e592011-10-20 11:56:00 +01001186 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001187 int fd = ashmem_create_region("Parcel Blob", len);
1188 if (fd < 0) return NO_MEMORY;
1189
1190 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1191 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001192 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001193 } else {
Yi Kong91635562018-06-07 14:38:36 -07001194 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001195 if (ptr == MAP_FAILED) {
1196 status = -errno;
1197 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001198 if (!mutableCopy) {
1199 result = ashmem_set_prot_region(fd, PROT_READ);
1200 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001201 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001202 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001203 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001204 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001205 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001206 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001207 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001208 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001209 return NO_ERROR;
1210 }
1211 }
1212 }
1213 }
1214 ::munmap(ptr, len);
1215 }
1216 ::close(fd);
1217 return status;
1218}
1219
Jeff Brown13b16042014-11-11 16:44:25 -08001220status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1221{
1222 // Must match up with what's done in writeBlob.
1223 if (!mAllowFds) return FDS_NOT_ALLOWED;
1224 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1225 if (status) return status;
1226 return writeDupFileDescriptor(fd);
1227}
1228
Mathias Agopiane1424282013-07-29 21:24:40 -07001229status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001230{
1231 status_t err;
1232
1233 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001234 const size_t len = val.getFlattenedSize();
1235 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001236
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001237 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001238 // don't accept size_t values which may have come from an
1239 // inadvertent conversion from a negative int.
1240 return BAD_VALUE;
1241 }
1242
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001243 err = this->writeInt32(len);
1244 if (err) return err;
1245
1246 err = this->writeInt32(fd_count);
1247 if (err) return err;
1248
1249 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001250 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001251 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001252 return BAD_VALUE;
1253
Yi Kong91635562018-06-07 14:38:36 -07001254 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001255 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001256 fds = new (std::nothrow) int[fd_count];
1257 if (fds == nullptr) {
1258 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1259 return BAD_VALUE;
1260 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001261 }
1262
1263 err = val.flatten(buf, len, fds, fd_count);
1264 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1265 err = this->writeDupFileDescriptor( fds[i] );
1266 }
1267
1268 if (fd_count) {
1269 delete [] fds;
1270 }
1271
1272 return err;
1273}
1274
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001275status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1276{
1277 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1278 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1279 if (enoughData && enoughObjects) {
1280restart_write:
1281 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001282
Christopher Tate98e67d32015-06-03 18:44:15 -07001283 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001284 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001285 if (!mAllowFds) {
1286 // fail before modifying our object index
1287 return FDS_NOT_ALLOWED;
1288 }
1289 mHasFds = mFdsKnown = true;
1290 }
1291
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001292 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001293 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001294 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001295 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001296 mObjectsSize++;
1297 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001298
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001299 return finishWrite(sizeof(flat_binder_object));
1300 }
1301
1302 if (!enoughData) {
1303 const status_t err = growData(sizeof(val));
1304 if (err != NO_ERROR) return err;
1305 }
1306 if (!enoughObjects) {
1307 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001308 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001309 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001310 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001311 mObjects = objects;
1312 mObjectsCapacity = newSize;
1313 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001314
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001315 goto restart_write;
1316}
1317
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001318status_t Parcel::writeNoException()
1319{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001320 binder::Status status;
1321 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001322}
1323
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001324status_t Parcel::validateReadData(size_t upperBound) const
1325{
1326 // Don't allow non-object reads on object data
1327 if (mObjectsSorted || mObjectsSize <= 1) {
1328data_sorted:
1329 // Expect to check only against the next object
1330 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1331 // For some reason the current read position is greater than the next object
1332 // hint. Iterate until we find the right object
1333 size_t nextObject = mNextObjectHint;
1334 do {
1335 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1336 // Requested info overlaps with an object
1337 ALOGE("Attempt to read from protected data in Parcel %p", this);
1338 return PERMISSION_DENIED;
1339 }
1340 nextObject++;
1341 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1342 mNextObjectHint = nextObject;
1343 }
1344 return NO_ERROR;
1345 }
1346 // Quickly determine if mObjects is sorted.
1347 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1348 binder_size_t* prevObj = currObj;
1349 while (currObj > mObjects) {
1350 prevObj--;
1351 if(*prevObj > *currObj) {
1352 goto data_unsorted;
1353 }
1354 currObj--;
1355 }
1356 mObjectsSorted = true;
1357 goto data_sorted;
1358
1359data_unsorted:
1360 // Insertion Sort mObjects
1361 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1362 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1363 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1364 binder_size_t temp = *iter0;
1365 binder_size_t* iter1 = iter0 - 1;
1366 while (iter1 >= mObjects && *iter1 > temp) {
1367 *(iter1 + 1) = *iter1;
1368 iter1--;
1369 }
1370 *(iter1 + 1) = temp;
1371 }
1372 mNextObjectHint = 0;
1373 mObjectsSorted = true;
1374 goto data_sorted;
1375}
1376
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001377status_t Parcel::read(void* outData, size_t len) const
1378{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001379 if (len > INT32_MAX) {
1380 // don't accept size_t values which may have come from an
1381 // inadvertent conversion from a negative int.
1382 return BAD_VALUE;
1383 }
1384
1385 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1386 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001387 if (mObjectsSize > 0) {
1388 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001389 if(err != NO_ERROR) {
1390 // Still increment the data position by the expected length
1391 mDataPos += pad_size(len);
1392 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1393 return err;
1394 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001395 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001396 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001397 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001398 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001399 return NO_ERROR;
1400 }
1401 return NOT_ENOUGH_DATA;
1402}
1403
1404const void* Parcel::readInplace(size_t len) const
1405{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001406 if (len > INT32_MAX) {
1407 // don't accept size_t values which may have come from an
1408 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001409 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001410 }
1411
1412 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1413 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001414 if (mObjectsSize > 0) {
1415 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001416 if(err != NO_ERROR) {
1417 // Still increment the data position by the expected length
1418 mDataPos += pad_size(len);
1419 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001420 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001421 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001422 }
1423
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001424 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001425 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001426 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001427 return data;
1428 }
Yi Kong91635562018-06-07 14:38:36 -07001429 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001430}
1431
Andreas Huber84a6d042009-08-17 13:33:27 -07001432template<class T>
1433status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001434 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001435
1436 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001437 if (mObjectsSize > 0) {
1438 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001439 if(err != NO_ERROR) {
1440 // Still increment the data position by the expected length
1441 mDataPos += sizeof(T);
1442 return err;
1443 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001444 }
1445
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001446 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001447 mDataPos += sizeof(T);
1448 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001449 return NO_ERROR;
1450 } else {
1451 return NOT_ENOUGH_DATA;
1452 }
1453}
1454
Andreas Huber84a6d042009-08-17 13:33:27 -07001455template<class T>
1456T Parcel::readAligned() const {
1457 T result;
1458 if (readAligned(&result) != NO_ERROR) {
1459 result = 0;
1460 }
1461
1462 return result;
1463}
1464
1465template<class T>
1466status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001467 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001468
1469 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1470restart_write:
1471 *reinterpret_cast<T*>(mData+mDataPos) = val;
1472 return finishWrite(sizeof(val));
1473 }
1474
1475 status_t err = growData(sizeof(val));
1476 if (err == NO_ERROR) goto restart_write;
1477 return err;
1478}
1479
Casey Dahlin185d3442016-02-09 11:08:35 -08001480namespace {
1481
1482template<typename T>
1483status_t readByteVectorInternal(const Parcel* parcel,
1484 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001485 val->clear();
1486
1487 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001488 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001489
1490 if (status != OK) {
1491 return status;
1492 }
1493
Christopher Wiley4db672d2015-11-10 09:44:30 -08001494 if (size < 0) {
1495 status = UNEXPECTED_NULL;
1496 return status;
1497 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001498 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001499 status = BAD_VALUE;
1500 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001501 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001502
Paul Lietar433e87b2016-09-16 10:39:32 -07001503 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001504 if (!data) {
1505 status = BAD_VALUE;
1506 return status;
1507 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001508 val->reserve(size);
1509 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001510
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001511 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001512}
1513
Casey Dahlin185d3442016-02-09 11:08:35 -08001514template<typename T>
1515status_t readByteVectorInternalPtr(
1516 const Parcel* parcel,
1517 std::unique_ptr<std::vector<T>>* val) {
1518 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001519 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001520 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001521 val->reset();
1522
1523 if (status != OK || size < 0) {
1524 return status;
1525 }
1526
Casey Dahlin185d3442016-02-09 11:08:35 -08001527 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001528 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001529
Casey Dahlin185d3442016-02-09 11:08:35 -08001530 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001531
1532 if (status != OK) {
1533 val->reset();
1534 }
1535
1536 return status;
1537}
1538
Casey Dahlin185d3442016-02-09 11:08:35 -08001539} // namespace
1540
1541status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1542 return readByteVectorInternal(this, val);
1543}
1544
1545status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1546 return readByteVectorInternal(this, val);
1547}
1548
1549status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1550 return readByteVectorInternalPtr(this, val);
1551}
1552
1553status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1554 return readByteVectorInternalPtr(this, val);
1555}
1556
Casey Dahlinb9872622015-11-25 15:09:45 -08001557status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1558 return readNullableTypedVector(val, &Parcel::readInt32);
1559}
1560
Casey Dahlin451ff582015-10-19 18:12:18 -07001561status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001562 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001563}
1564
Casey Dahlinb9872622015-11-25 15:09:45 -08001565status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1566 return readNullableTypedVector(val, &Parcel::readInt64);
1567}
1568
Casey Dahlin451ff582015-10-19 18:12:18 -07001569status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001570 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001571}
1572
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001573status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1574 return readNullableTypedVector(val, &Parcel::readUint64);
1575}
1576
1577status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1578 return readTypedVector(val, &Parcel::readUint64);
1579}
1580
Casey Dahlinb9872622015-11-25 15:09:45 -08001581status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1582 return readNullableTypedVector(val, &Parcel::readFloat);
1583}
1584
Casey Dahlin451ff582015-10-19 18:12:18 -07001585status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001586 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001587}
1588
Casey Dahlinb9872622015-11-25 15:09:45 -08001589status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1590 return readNullableTypedVector(val, &Parcel::readDouble);
1591}
1592
Casey Dahlin451ff582015-10-19 18:12:18 -07001593status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001594 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001595}
1596
Casey Dahlinb9872622015-11-25 15:09:45 -08001597status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1598 const int32_t start = dataPosition();
1599 int32_t size;
1600 status_t status = readInt32(&size);
1601 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001602
Casey Dahlinb9872622015-11-25 15:09:45 -08001603 if (status != OK || size < 0) {
1604 return status;
1605 }
1606
1607 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001608 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001609
1610 status = readBoolVector(val->get());
1611
1612 if (status != OK) {
1613 val->reset();
1614 }
1615
1616 return status;
1617}
1618
1619status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001620 int32_t size;
1621 status_t status = readInt32(&size);
1622
1623 if (status != OK) {
1624 return status;
1625 }
1626
1627 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001628 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001629 }
1630
1631 val->resize(size);
1632
1633 /* C++ bool handling means a vector of bools isn't necessarily addressable
1634 * (we might use individual bits)
1635 */
Christopher Wiley97887982015-10-27 16:33:47 -07001636 bool data;
1637 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001638 status = readBool(&data);
1639 (*val)[i] = data;
1640
1641 if (status != OK) {
1642 return status;
1643 }
1644 }
1645
1646 return OK;
1647}
1648
Casey Dahlinb9872622015-11-25 15:09:45 -08001649status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1650 return readNullableTypedVector(val, &Parcel::readChar);
1651}
1652
Casey Dahlin451ff582015-10-19 18:12:18 -07001653status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001654 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001655}
1656
Casey Dahlinb9872622015-11-25 15:09:45 -08001657status_t Parcel::readString16Vector(
1658 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1659 return readNullableTypedVector(val, &Parcel::readString16);
1660}
1661
Casey Dahlin451ff582015-10-19 18:12:18 -07001662status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001663 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001664}
1665
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001666status_t Parcel::readUtf8VectorFromUtf16Vector(
1667 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1668 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1669}
1670
1671status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1672 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1673}
Casey Dahlin451ff582015-10-19 18:12:18 -07001674
Andreas Huber84a6d042009-08-17 13:33:27 -07001675status_t Parcel::readInt32(int32_t *pArg) const
1676{
1677 return readAligned(pArg);
1678}
1679
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001680int32_t Parcel::readInt32() const
1681{
Andreas Huber84a6d042009-08-17 13:33:27 -07001682 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001683}
1684
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001685status_t Parcel::readUint32(uint32_t *pArg) const
1686{
1687 return readAligned(pArg);
1688}
1689
1690uint32_t Parcel::readUint32() const
1691{
1692 return readAligned<uint32_t>();
1693}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001694
1695status_t Parcel::readInt64(int64_t *pArg) const
1696{
Andreas Huber84a6d042009-08-17 13:33:27 -07001697 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001698}
1699
1700
1701int64_t Parcel::readInt64() const
1702{
Andreas Huber84a6d042009-08-17 13:33:27 -07001703 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001704}
1705
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001706status_t Parcel::readUint64(uint64_t *pArg) const
1707{
1708 return readAligned(pArg);
1709}
1710
1711uint64_t Parcel::readUint64() const
1712{
1713 return readAligned<uint64_t>();
1714}
1715
Serban Constantinescuf683e012013-11-05 16:53:55 +00001716status_t Parcel::readPointer(uintptr_t *pArg) const
1717{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001718 status_t ret;
1719 binder_uintptr_t ptr;
1720 ret = readAligned(&ptr);
1721 if (!ret)
1722 *pArg = ptr;
1723 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001724}
1725
1726uintptr_t Parcel::readPointer() const
1727{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001728 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001729}
1730
1731
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001732status_t Parcel::readFloat(float *pArg) const
1733{
Andreas Huber84a6d042009-08-17 13:33:27 -07001734 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001735}
1736
1737
1738float Parcel::readFloat() const
1739{
Andreas Huber84a6d042009-08-17 13:33:27 -07001740 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001741}
1742
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001743#if defined(__mips__) && defined(__mips_hard_float)
1744
1745status_t Parcel::readDouble(double *pArg) const
1746{
1747 union {
1748 double d;
1749 unsigned long long ll;
1750 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001751 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001752 status_t status;
1753 status = readAligned(&u.ll);
1754 *pArg = u.d;
1755 return status;
1756}
1757
1758double Parcel::readDouble() const
1759{
1760 union {
1761 double d;
1762 unsigned long long ll;
1763 } u;
1764 u.ll = readAligned<unsigned long long>();
1765 return u.d;
1766}
1767
1768#else
1769
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001770status_t Parcel::readDouble(double *pArg) const
1771{
Andreas Huber84a6d042009-08-17 13:33:27 -07001772 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001773}
1774
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001775double Parcel::readDouble() const
1776{
Andreas Huber84a6d042009-08-17 13:33:27 -07001777 return readAligned<double>();
1778}
1779
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001780#endif
1781
Andreas Huber84a6d042009-08-17 13:33:27 -07001782status_t Parcel::readIntPtr(intptr_t *pArg) const
1783{
1784 return readAligned(pArg);
1785}
1786
1787
1788intptr_t Parcel::readIntPtr() const
1789{
1790 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001791}
1792
Casey Dahlind6848f52015-10-15 15:44:59 -07001793status_t Parcel::readBool(bool *pArg) const
1794{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001795 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001796 status_t ret = readInt32(&tmp);
1797 *pArg = (tmp != 0);
1798 return ret;
1799}
1800
1801bool Parcel::readBool() const
1802{
1803 return readInt32() != 0;
1804}
1805
1806status_t Parcel::readChar(char16_t *pArg) const
1807{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001808 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001809 status_t ret = readInt32(&tmp);
1810 *pArg = char16_t(tmp);
1811 return ret;
1812}
1813
1814char16_t Parcel::readChar() const
1815{
1816 return char16_t(readInt32());
1817}
1818
1819status_t Parcel::readByte(int8_t *pArg) const
1820{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001821 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001822 status_t ret = readInt32(&tmp);
1823 *pArg = int8_t(tmp);
1824 return ret;
1825}
1826
1827int8_t Parcel::readByte() const
1828{
1829 return int8_t(readInt32());
1830}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001831
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001832status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1833 size_t utf16Size = 0;
1834 const char16_t* src = readString16Inplace(&utf16Size);
1835 if (!src) {
1836 return UNEXPECTED_NULL;
1837 }
1838
1839 // Save ourselves the trouble, we're done.
1840 if (utf16Size == 0u) {
1841 str->clear();
1842 return NO_ERROR;
1843 }
1844
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001845 // Allow for closing '\0'
1846 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1847 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001848 return BAD_VALUE;
1849 }
1850 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001851 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001852 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001853 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1854 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001855 return NO_ERROR;
1856}
1857
1858status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1859 const int32_t start = dataPosition();
1860 int32_t size;
1861 status_t status = readInt32(&size);
1862 str->reset();
1863
1864 if (status != OK || size < 0) {
1865 return status;
1866 }
1867
1868 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001869 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001870 return readUtf8FromUtf16(str->get());
1871}
1872
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001873const char* Parcel::readCString() const
1874{
Steven Morelandf5e6c7e2019-05-17 13:14:06 -07001875 if (mDataPos < mDataSize) {
1876 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001877 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1878 // is the string's trailing NUL within the parcel's valid bounds?
1879 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1880 if (eos) {
1881 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001882 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001883 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001884 return str;
1885 }
1886 }
Yi Kong91635562018-06-07 14:38:36 -07001887 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001888}
1889
1890String8 Parcel::readString8() const
1891{
Roshan Pius87b64d22016-07-18 12:51:02 -07001892 String8 retString;
1893 status_t status = readString8(&retString);
1894 if (status != OK) {
1895 // We don't care about errors here, so just return an empty string.
1896 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001897 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001898 return retString;
1899}
1900
1901status_t Parcel::readString8(String8* pArg) const
1902{
1903 int32_t size;
1904 status_t status = readInt32(&size);
1905 if (status != OK) {
1906 return status;
1907 }
1908 // watch for potential int overflow from size+1
1909 if (size < 0 || size >= INT32_MAX) {
1910 return BAD_VALUE;
1911 }
1912 // |writeString8| writes nothing for empty string.
1913 if (size == 0) {
1914 *pArg = String8();
1915 return OK;
1916 }
1917 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07001918 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07001919 return BAD_VALUE;
1920 }
1921 pArg->setTo(str, size);
1922 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001923}
1924
1925String16 Parcel::readString16() const
1926{
1927 size_t len;
1928 const char16_t* str = readString16Inplace(&len);
1929 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001930 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001931 return String16();
1932}
1933
Casey Dahlinb9872622015-11-25 15:09:45 -08001934status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1935{
1936 const int32_t start = dataPosition();
1937 int32_t size;
1938 status_t status = readInt32(&size);
1939 pArg->reset();
1940
1941 if (status != OK || size < 0) {
1942 return status;
1943 }
1944
1945 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001946 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001947
1948 status = readString16(pArg->get());
1949
1950 if (status != OK) {
1951 pArg->reset();
1952 }
1953
1954 return status;
1955}
1956
Casey Dahlin451ff582015-10-19 18:12:18 -07001957status_t Parcel::readString16(String16* pArg) const
1958{
1959 size_t len;
1960 const char16_t* str = readString16Inplace(&len);
1961 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001962 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001963 return 0;
1964 } else {
1965 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001966 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001967 }
1968}
1969
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001970const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1971{
1972 int32_t size = readInt32();
1973 // watch for potential int overflow from size+1
1974 if (size >= 0 && size < INT32_MAX) {
1975 *outLen = size;
1976 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001977 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001978 return str;
1979 }
1980 }
1981 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001982 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001983}
1984
Casey Dahlinf0c13772015-10-27 18:33:56 -07001985status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1986{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001987 status_t status = readNullableStrongBinder(val);
1988 if (status == OK && !val->get()) {
1989 status = UNEXPECTED_NULL;
1990 }
1991 return status;
1992}
1993
1994status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1995{
Steven Morelanda86a3562019-08-01 23:28:34 +00001996 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001997}
1998
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001999sp<IBinder> Parcel::readStrongBinder() const
2000{
2001 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002002 // Note that a lot of code in Android reads binders by hand with this
2003 // method, and that code has historically been ok with getting nullptr
2004 // back (while ignoring error codes).
2005 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002006 return val;
2007}
2008
Christopher Wiley97f048d2015-11-19 06:49:05 -08002009status_t Parcel::readParcelable(Parcelable* parcelable) const {
2010 int32_t have_parcelable = 0;
2011 status_t status = readInt32(&have_parcelable);
2012 if (status != OK) {
2013 return status;
2014 }
2015 if (!have_parcelable) {
2016 return UNEXPECTED_NULL;
2017 }
2018 return parcelable->readFromParcel(this);
2019}
2020
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002021int32_t Parcel::readExceptionCode() const
2022{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002023 binder::Status status;
2024 status.readFromParcel(*this);
2025 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002026}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002027
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002028native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002029{
2030 int numFds, numInts;
2031 status_t err;
2032 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002033 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002034 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002035 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002036
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002037 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002038 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002039 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002040 }
2041
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002042 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002043 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002044 if (h->data[i] < 0) {
2045 for (int j = 0; j < i; j++) {
2046 close(h->data[j]);
2047 }
2048 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002049 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002050 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002051 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002052 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002053 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002054 native_handle_close(h);
2055 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002056 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002057 }
2058 return h;
2059}
2060
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002061int Parcel::readFileDescriptor() const
2062{
2063 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002064
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002065 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002066 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002067 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002068
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002069 return BAD_TYPE;
2070}
2071
Dianne Hackborn1941a402016-08-29 12:30:43 -07002072int Parcel::readParcelFileDescriptor() const
2073{
2074 int32_t hasComm = readInt32();
2075 int fd = readFileDescriptor();
2076 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002077 // detach (owned by the binder driver)
2078 int comm = readFileDescriptor();
2079
2080 // warning: this must be kept in sync with:
2081 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2082 enum ParcelFileDescriptorStatus {
2083 DETACHED = 2,
2084 };
2085
2086#if BYTE_ORDER == BIG_ENDIAN
2087 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2088#endif
2089#if BYTE_ORDER == LITTLE_ENDIAN
2090 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2091#endif
2092
2093 ssize_t written = TEMP_FAILURE_RETRY(
2094 ::write(comm, &message, sizeof(message)));
2095
2096 if (written == -1 || written != sizeof(message)) {
2097 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2098 written, strerror(errno));
2099 return BAD_TYPE;
2100 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002101 }
2102 return fd;
2103}
2104
Christopher Wiley2cf19952016-04-11 11:09:37 -07002105status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002106{
2107 int got = readFileDescriptor();
2108
2109 if (got == BAD_TYPE) {
2110 return BAD_TYPE;
2111 }
2112
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002113 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002114
2115 if (val->get() < 0) {
2116 return BAD_VALUE;
2117 }
2118
2119 return OK;
2120}
2121
Ryo Hashimotobf551892018-05-31 16:58:35 +09002122status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2123{
2124 int got = readParcelFileDescriptor();
2125
2126 if (got == BAD_TYPE) {
2127 return BAD_TYPE;
2128 }
2129
2130 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2131
2132 if (val->get() < 0) {
2133 return BAD_VALUE;
2134 }
2135
2136 return OK;
2137}
Casey Dahlin06673e32015-11-23 13:24:23 -08002138
Christopher Wiley2cf19952016-04-11 11:09:37 -07002139status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002140 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2141}
2142
Christopher Wiley2cf19952016-04-11 11:09:37 -07002143status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002144 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2145}
2146
Jeff Brown5707dbf2011-09-23 21:17:56 -07002147status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2148{
Jeff Brown13b16042014-11-11 16:44:25 -08002149 int32_t blobType;
2150 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002151 if (status) return status;
2152
Jeff Brown13b16042014-11-11 16:44:25 -08002153 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002154 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002155 const void* ptr = readInplace(len);
2156 if (!ptr) return BAD_VALUE;
2157
Jeff Brown13b16042014-11-11 16:44:25 -08002158 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002159 return NO_ERROR;
2160 }
2161
Steve Block6807e592011-10-20 11:56:00 +01002162 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002163 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002164 int fd = readFileDescriptor();
2165 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2166
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002167 if (!ashmem_valid(fd)) {
2168 ALOGE("invalid fd");
2169 return BAD_VALUE;
2170 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002171 int size = ashmem_get_size_region(fd);
2172 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002173 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002174 return BAD_VALUE;
2175 }
Yi Kong91635562018-06-07 14:38:36 -07002176 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002177 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002178 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002179
Jeff Brown13b16042014-11-11 16:44:25 -08002180 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002181 return NO_ERROR;
2182}
2183
Mathias Agopiane1424282013-07-29 21:24:40 -07002184status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002185{
2186 // size
2187 const size_t len = this->readInt32();
2188 const size_t fd_count = this->readInt32();
2189
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002190 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002191 // don't accept size_t values which may have come from an
2192 // inadvertent conversion from a negative int.
2193 return BAD_VALUE;
2194 }
2195
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002196 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002197 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002198 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002199 return BAD_VALUE;
2200
Yi Kong91635562018-06-07 14:38:36 -07002201 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002202 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002203 fds = new (std::nothrow) int[fd_count];
2204 if (fds == nullptr) {
2205 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2206 return BAD_VALUE;
2207 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002208 }
2209
2210 status_t err = NO_ERROR;
2211 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002212 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002213 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002214 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002215 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 -07002216 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2217 // Close all the file descriptors that were dup-ed.
2218 for (size_t j=0; j<i ;j++) {
2219 close(fds[j]);
2220 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002221 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002222 }
2223
2224 if (err == NO_ERROR) {
2225 err = val.unflatten(buf, len, fds, fd_count);
2226 }
2227
2228 if (fd_count) {
2229 delete [] fds;
2230 }
2231
2232 return err;
2233}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002234const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2235{
2236 const size_t DPOS = mDataPos;
2237 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2238 const flat_binder_object* obj
2239 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2240 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002241 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002242 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002243 // the object list, so we don't want to check for it when
2244 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002245 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002246 return obj;
2247 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002248
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002249 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002250 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002251 const size_t N = mObjectsSize;
2252 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002253
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002254 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002255 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002256 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002257
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002258 // Start at the current hint position, looking for an object at
2259 // the current data position.
2260 if (opos < N) {
2261 while (opos < (N-1) && OBJS[opos] < DPOS) {
2262 opos++;
2263 }
2264 } else {
2265 opos = N-1;
2266 }
2267 if (OBJS[opos] == DPOS) {
2268 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002269 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002270 this, DPOS, opos);
2271 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002272 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002273 return obj;
2274 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002275
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002276 // Look backwards for it...
2277 while (opos > 0 && OBJS[opos] > DPOS) {
2278 opos--;
2279 }
2280 if (OBJS[opos] == DPOS) {
2281 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002282 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002283 this, DPOS, opos);
2284 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002285 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002286 return obj;
2287 }
2288 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002289 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 -07002290 this, DPOS);
2291 }
Yi Kong91635562018-06-07 14:38:36 -07002292 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002293}
2294
2295void Parcel::closeFileDescriptors()
2296{
2297 size_t i = mObjectsSize;
2298 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002299 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002300 }
2301 while (i > 0) {
2302 i--;
2303 const flat_binder_object* flat
2304 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002305 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002306 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002307 close(flat->handle);
2308 }
2309 }
2310}
2311
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002312uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002313{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002314 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002315}
2316
2317size_t Parcel::ipcDataSize() const
2318{
2319 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2320}
2321
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002322uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002323{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002324 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002325}
2326
2327size_t Parcel::ipcObjectsCount() const
2328{
2329 return mObjectsSize;
2330}
2331
2332void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002333 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002334{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002335 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002336 freeDataNoInit();
2337 mError = NO_ERROR;
2338 mData = const_cast<uint8_t*>(data);
2339 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002340 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002341 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002342 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002343 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002344 mObjectsSize = mObjectsCapacity = objectsCount;
2345 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002346 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002347 mOwner = relFunc;
2348 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002349 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002350 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002351 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002352 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002353 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002354 mObjectsSize = 0;
2355 break;
2356 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002357 const flat_binder_object* flat
2358 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2359 uint32_t type = flat->hdr.type;
2360 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2361 type == BINDER_TYPE_FD)) {
2362 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2363 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2364 // recover gracefully by clearing out the objects, and releasing the objects we do
2365 // know about.
2366 android_errorWriteLog(0x534e4554, "135930648");
2367 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2368 __func__, type, (uint64_t)offset);
2369 releaseObjects();
2370 mObjectsSize = 0;
2371 break;
2372 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002373 minOffset = offset + sizeof(flat_binder_object);
2374 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002375 scanForFds();
2376}
2377
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002378void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002379{
2380 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002381
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002382 if (errorCheck() != NO_ERROR) {
2383 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002384 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002385 } else if (dataSize() > 0) {
2386 const uint8_t* DATA = data();
2387 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002388 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002389 const size_t N = objectsCount();
2390 for (size_t i=0; i<N; i++) {
2391 const flat_binder_object* flat
2392 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2393 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002394 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002395 << " = " << flat->binder;
2396 }
2397 } else {
2398 to << "NULL";
2399 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002400
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002401 to << ")";
2402}
2403
2404void Parcel::releaseObjects()
2405{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002406 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002407 if (i == 0) {
2408 return;
2409 }
2410 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002411 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002412 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002413 while (i > 0) {
2414 i--;
2415 const flat_binder_object* flat
2416 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002417 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002418 }
2419}
2420
2421void Parcel::acquireObjects()
2422{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002423 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002424 if (i == 0) {
2425 return;
2426 }
2427 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002428 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002429 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002430 while (i > 0) {
2431 i--;
2432 const flat_binder_object* flat
2433 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002434 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435 }
2436}
2437
2438void Parcel::freeData()
2439{
2440 freeDataNoInit();
2441 initState();
2442}
2443
2444void Parcel::freeDataNoInit()
2445{
2446 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002447 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002448 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002449 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2450 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002451 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002452 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002453 if (mData) {
2454 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002455 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002456 if (mDataCapacity <= gParcelGlobalAllocSize) {
2457 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2458 } else {
2459 gParcelGlobalAllocSize = 0;
2460 }
2461 if (gParcelGlobalAllocCount > 0) {
2462 gParcelGlobalAllocCount--;
2463 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002464 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002465 free(mData);
2466 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002467 if (mObjects) free(mObjects);
2468 }
2469}
2470
2471status_t Parcel::growData(size_t len)
2472{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002473 if (len > INT32_MAX) {
2474 // don't accept size_t values which may have come from an
2475 // inadvertent conversion from a negative int.
2476 return BAD_VALUE;
2477 }
2478
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479 size_t newSize = ((mDataSize+len)*3)/2;
2480 return (newSize <= mDataSize)
2481 ? (status_t) NO_MEMORY
2482 : continueWrite(newSize);
2483}
2484
2485status_t Parcel::restartWrite(size_t desired)
2486{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002487 if (desired > INT32_MAX) {
2488 // don't accept size_t values which may have come from an
2489 // inadvertent conversion from a negative int.
2490 return BAD_VALUE;
2491 }
2492
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002493 if (mOwner) {
2494 freeData();
2495 return continueWrite(desired);
2496 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002497
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002498 uint8_t* data = (uint8_t*)realloc(mData, desired);
2499 if (!data && desired > mDataCapacity) {
2500 mError = NO_MEMORY;
2501 return NO_MEMORY;
2502 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002503
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002504 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002505
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002506 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002507 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002508 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002509 gParcelGlobalAllocSize += desired;
2510 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002511 if (!mData) {
2512 gParcelGlobalAllocCount++;
2513 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002514 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002515 mData = data;
2516 mDataCapacity = desired;
2517 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002518
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002519 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002520 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2521 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2522
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002523 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002524 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002525 mObjectsSize = mObjectsCapacity = 0;
2526 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002527 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002528 mHasFds = false;
2529 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002530 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002531
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002532 return NO_ERROR;
2533}
2534
2535status_t Parcel::continueWrite(size_t desired)
2536{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002537 if (desired > INT32_MAX) {
2538 // don't accept size_t values which may have come from an
2539 // inadvertent conversion from a negative int.
2540 return BAD_VALUE;
2541 }
2542
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002543 // If shrinking, first adjust for any objects that appear
2544 // after the new data size.
2545 size_t objectsSize = mObjectsSize;
2546 if (desired < mDataSize) {
2547 if (desired == 0) {
2548 objectsSize = 0;
2549 } else {
2550 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002551 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002552 break;
2553 objectsSize--;
2554 }
2555 }
2556 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002557
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002558 if (mOwner) {
2559 // If the size is going to zero, just release the owner's data.
2560 if (desired == 0) {
2561 freeData();
2562 return NO_ERROR;
2563 }
2564
2565 // If there is a different owner, we need to take
2566 // posession.
2567 uint8_t* data = (uint8_t*)malloc(desired);
2568 if (!data) {
2569 mError = NO_MEMORY;
2570 return NO_MEMORY;
2571 }
Yi Kong91635562018-06-07 14:38:36 -07002572 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002573
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002574 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002575 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002576 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002577 free(data);
2578
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002579 mError = NO_MEMORY;
2580 return NO_MEMORY;
2581 }
2582
2583 // Little hack to only acquire references on objects
2584 // we will be keeping.
2585 size_t oldObjectsSize = mObjectsSize;
2586 mObjectsSize = objectsSize;
2587 acquireObjects();
2588 mObjectsSize = oldObjectsSize;
2589 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002590
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002591 if (mData) {
2592 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2593 }
2594 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002595 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002596 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002597 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002598 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002599 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002600
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002601 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002602 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002603 gParcelGlobalAllocSize += desired;
2604 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002605 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002606
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002607 mData = data;
2608 mObjects = objects;
2609 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002610 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002611 mDataCapacity = desired;
2612 mObjectsSize = mObjectsCapacity = objectsSize;
2613 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002614 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002615
2616 } else if (mData) {
2617 if (objectsSize < mObjectsSize) {
2618 // Need to release refs on any objects we are dropping.
2619 const sp<ProcessState> proc(ProcessState::self());
2620 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2621 const flat_binder_object* flat
2622 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002623 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002624 // will need to rescan because we may have lopped off the only FDs
2625 mFdsKnown = false;
2626 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002627 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002628 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002629
2630 if (objectsSize == 0) {
2631 free(mObjects);
2632 mObjects = nullptr;
2633 } else {
2634 binder_size_t* objects =
2635 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2636 if (objects) {
2637 mObjects = objects;
2638 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002639 }
2640 mObjectsSize = objectsSize;
2641 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002642 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002643 }
2644
2645 // We own the data, so we can just do a realloc().
2646 if (desired > mDataCapacity) {
2647 uint8_t* data = (uint8_t*)realloc(mData, desired);
2648 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002649 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2650 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002651 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002652 gParcelGlobalAllocSize += desired;
2653 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002654 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002655 mData = data;
2656 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002657 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002658 mError = NO_MEMORY;
2659 return NO_MEMORY;
2660 }
2661 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002662 if (mDataSize > desired) {
2663 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002664 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002665 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002666 if (mDataPos > desired) {
2667 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002668 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002669 }
2670 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002671
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002672 } else {
2673 // This is the first data. Easy!
2674 uint8_t* data = (uint8_t*)malloc(desired);
2675 if (!data) {
2676 mError = NO_MEMORY;
2677 return NO_MEMORY;
2678 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002679
Yi Kong91635562018-06-07 14:38:36 -07002680 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002681 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002682 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002683 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002684
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002685 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002686 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002687 gParcelGlobalAllocSize += desired;
2688 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002689 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002690
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002691 mData = data;
2692 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002693 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2694 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002695 mDataCapacity = desired;
2696 }
2697
2698 return NO_ERROR;
2699}
2700
2701void Parcel::initState()
2702{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002703 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002704 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002705 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002706 mDataSize = 0;
2707 mDataCapacity = 0;
2708 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002709 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2710 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002711 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002712 mObjectsSize = 0;
2713 mObjectsCapacity = 0;
2714 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002715 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002716 mHasFds = false;
2717 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002718 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002719 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002720 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002721 mWorkSourceRequestHeaderPosition = 0;
2722 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002723
2724 // racing multiple init leads only to multiple identical write
2725 if (gMaxFds == 0) {
2726 struct rlimit result;
2727 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2728 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002729 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002730 } else {
2731 ALOGW("Unable to getrlimit: %s", strerror(errno));
2732 gMaxFds = 1024;
2733 }
2734 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002735}
2736
2737void Parcel::scanForFds() const
2738{
2739 bool hasFds = false;
2740 for (size_t i=0; i<mObjectsSize; i++) {
2741 const flat_binder_object* flat
2742 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002743 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002744 hasFds = true;
2745 break;
2746 }
2747 }
2748 mHasFds = hasFds;
2749 mFdsKnown = true;
2750}
2751
Dan Sandleraa5c2342015-04-10 10:08:45 -04002752size_t Parcel::getBlobAshmemSize() const
2753{
Adrian Roos6bb31142015-10-22 16:46:12 -07002754 // This used to return the size of all blobs that were written to ashmem, now we're returning
2755 // the ashmem currently referenced by this Parcel, which should be equivalent.
2756 // TODO: Remove method once ABI can be changed.
2757 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002758}
2759
Adrian Rooscbf37262015-10-22 16:12:53 -07002760size_t Parcel::getOpenAshmemSize() const
2761{
2762 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002763}
2764
2765// --- Parcel::Blob ---
2766
2767Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002768 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002769}
2770
2771Parcel::Blob::~Blob() {
2772 release();
2773}
2774
2775void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002776 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002777 ::munmap(mData, mSize);
2778 }
2779 clear();
2780}
2781
Jeff Brown13b16042014-11-11 16:44:25 -08002782void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2783 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002784 mData = data;
2785 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002786 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002787}
2788
2789void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002790 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002791 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002792 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002793 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002794}
2795
Steven Moreland6511af52019-09-26 16:05:45 -07002796} // namespace android