blob: 485869e535d5f47d960e32f74bcf80d11987395a [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 Morelandc709dd82019-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) {
70 if (s > (SIZE_T_MAX - 3)) {
71 abort();
72 }
73 return PAD_SIZE_UNSAFE(s);
74}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070076// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080077#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070078
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070079namespace android {
80
Dianne Hackborna4cff882014-11-13 17:07:40 -080081static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
82static size_t gParcelGlobalAllocSize = 0;
83static size_t gParcelGlobalAllocCount = 0;
84
Christopher Tatee4e0ae82016-03-24 16:03:44 -070085static size_t gMaxFds = 0;
86
Jeff Brown13b16042014-11-11 16:44:25 -080087// Maximum size of a blob to transfer in-place.
88static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
89
90enum {
91 BLOB_INPLACE = 0,
92 BLOB_ASHMEM_IMMUTABLE = 1,
93 BLOB_ASHMEM_MUTABLE = 2,
94};
95
Steven Morelandb1c81202019-04-05 18:49:55 -070096static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070097 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -070099 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700100 case BINDER_TYPE_BINDER:
101 if (obj.binder) {
102 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800103 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104 }
105 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106 case BINDER_TYPE_HANDLE: {
107 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kongfdd8da92018-06-07 17:52:27 -0700108 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
110 b->incStrong(who);
111 }
112 return;
113 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700114 case BINDER_TYPE_FD: {
Yi Kongfdd8da92018-06-07 17:52:27 -0700115 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700116 // If we own an ashmem fd, keep track of how much memory it refers to.
117 int size = ashmem_get_size_region(obj.handle);
118 if (size > 0) {
119 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700120 }
121 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700122 return;
123 }
124 }
125
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700126 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700127}
128
Adrian Roos6bb31142015-10-22 16:46:12 -0700129static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700130 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700131{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700132 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700133 case BINDER_TYPE_BINDER:
134 if (obj.binder) {
135 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800136 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700137 }
138 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 case BINDER_TYPE_HANDLE: {
140 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kongfdd8da92018-06-07 17:52:27 -0700141 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
143 b->decStrong(who);
144 }
145 return;
146 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800148 if (obj.cookie != 0) { // owned
Yi Kongfdd8da92018-06-07 17:52:27 -0700149 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700150 int size = ashmem_get_size_region(obj.handle);
151 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800152 // ashmem size might have changed since last time it was accounted for, e.g.
153 // in acquire_object(). Value of *outAshmemSize is not critical since we are
154 // releasing the object anyway. Check for integer overflow condition.
155 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700156 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700157 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800158
159 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700160 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700161 return;
162 }
163 }
164
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700165 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700166}
167
Steven Morelanddea3cf92019-07-16 18:06:55 -0700168status_t Parcel::finishFlattenBinder(
169 const sp<IBinder>& /*binder*/, const flat_binder_object& flat)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700170{
Steven Morelanddea3cf92019-07-16 18:06:55 -0700171 status_t status = writeObject(flat, false);
172 if (status != OK) return status;
173
Steven Morelandc709dd82019-08-05 20:30:14 -0700174 // internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700175 // Cannot change wire protocol w/o SM update
176 // return writeInt32(internal::Stability::get(binder.get()));
177 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700178}
179
Steven Morelanddea3cf92019-07-16 18:06:55 -0700180status_t Parcel::finishUnflattenBinder(
181 const sp<IBinder>& binder, sp<IBinder>* out) const
182{
183 // int32_t stability;
184 // Cannot change wire protocol w/o SM update
185 // status_t status = readInt32(&stability);
186 // if (status != OK) return status;
187
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700188 // status = internal::Stability::set(binder.get(), stability, true /*log*/);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700189 // 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 Kongfdd8da92018-06-07 17:52:27 -0700207 if (binder != nullptr) {
Steven Moreland3085a472018-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 Kongfdd8da92018-06-07 17:52:27 -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 Moreland3085a472018-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 Morelanddea3cf92019-07-16 18:06:55 -0700233 return finishFlattenBinder(binder, obj);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700234}
235
Steven Morelanddea3cf92019-07-16 18:06:55 -0700236status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700237{
Steven Morelanddea3cf92019-07-16 18:06:55 -0700238 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 Morelanddea3cf92019-07-16 18:06:55 -0700242 case BINDER_TYPE_BINDER: {
243 sp<IBinder> binder = reinterpret_cast<IBinder*>(flat->cookie);
244 return finishUnflattenBinder(binder, out);
245 }
246 case BINDER_TYPE_HANDLE: {
247 sp<IBinder> binder =
248 ProcessState::self()->getStrongProxyForHandle(flat->handle);
249 return finishUnflattenBinder(binder, out);
250 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700251 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700252 }
253 return BAD_TYPE;
254}
255
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700256// ---------------------------------------------------------------------------
257
258Parcel::Parcel()
259{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800260 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261 initState();
262}
263
264Parcel::~Parcel()
265{
266 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800267 LOG_ALLOC("Parcel %p: destroyed", this);
268}
269
270size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800271 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
272 size_t size = gParcelGlobalAllocSize;
273 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
274 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800275}
276
277size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800278 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
279 size_t count = gParcelGlobalAllocCount;
280 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
281 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700282}
283
284const uint8_t* Parcel::data() const
285{
286 return mData;
287}
288
289size_t Parcel::dataSize() const
290{
291 return (mDataSize > mDataPos ? mDataSize : mDataPos);
292}
293
294size_t Parcel::dataAvail() const
295{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700296 size_t result = dataSize() - dataPosition();
297 if (result > INT32_MAX) {
298 abort();
299 }
300 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700301}
302
303size_t Parcel::dataPosition() const
304{
305 return mDataPos;
306}
307
308size_t Parcel::dataCapacity() const
309{
310 return mDataCapacity;
311}
312
313status_t Parcel::setDataSize(size_t size)
314{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700315 if (size > INT32_MAX) {
316 // don't accept size_t values which may have come from an
317 // inadvertent conversion from a negative int.
318 return BAD_VALUE;
319 }
320
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700321 status_t err;
322 err = continueWrite(size);
323 if (err == NO_ERROR) {
324 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700325 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700326 }
327 return err;
328}
329
330void Parcel::setDataPosition(size_t pos) const
331{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700332 if (pos > INT32_MAX) {
333 // don't accept size_t values which may have come from an
334 // inadvertent conversion from a negative int.
335 abort();
336 }
337
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700338 mDataPos = pos;
339 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800340 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700341}
342
343status_t Parcel::setDataCapacity(size_t size)
344{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700345 if (size > INT32_MAX) {
346 // don't accept size_t values which may have come from an
347 // inadvertent conversion from a negative int.
348 return BAD_VALUE;
349 }
350
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700351 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700352 return NO_ERROR;
353}
354
355status_t Parcel::setData(const uint8_t* buffer, size_t len)
356{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700357 if (len > INT32_MAX) {
358 // don't accept size_t values which may have come from an
359 // inadvertent conversion from a negative int.
360 return BAD_VALUE;
361 }
362
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700363 status_t err = restartWrite(len);
364 if (err == NO_ERROR) {
365 memcpy(const_cast<uint8_t*>(data()), buffer, len);
366 mDataSize = len;
367 mFdsKnown = false;
368 }
369 return err;
370}
371
Andreas Huber51faf462011-04-13 10:21:56 -0700372status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700373{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700374 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700375 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800376 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700377 size_t size = parcel->mObjectsSize;
378 int startPos = mDataPos;
379 int firstIndex = -1, lastIndex = -2;
380
381 if (len == 0) {
382 return NO_ERROR;
383 }
384
Nick Kralevichb6b14232015-04-02 09:36:02 -0700385 if (len > INT32_MAX) {
386 // don't accept size_t values which may have come from an
387 // inadvertent conversion from a negative int.
388 return BAD_VALUE;
389 }
390
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700391 // range checks against the source parcel size
392 if ((offset > parcel->mDataSize)
393 || (len > parcel->mDataSize)
394 || (offset + len > parcel->mDataSize)) {
395 return BAD_VALUE;
396 }
397
398 // Count objects in range
399 for (int i = 0; i < (int) size; i++) {
400 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700401 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700402 if (firstIndex == -1) {
403 firstIndex = i;
404 }
405 lastIndex = i;
406 }
407 }
408 int numObjects = lastIndex - firstIndex + 1;
409
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700410 if ((mDataSize+len) > mDataCapacity) {
411 // grow data
412 err = growData(len);
413 if (err != NO_ERROR) {
414 return err;
415 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700416 }
417
418 // append data
419 memcpy(mData + mDataPos, data + offset, len);
420 mDataPos += len;
421 mDataSize += len;
422
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400423 err = NO_ERROR;
424
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700425 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200426 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700427 // grow objects
428 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700429 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700430 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800431 binder_size_t *objects =
432 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kongfdd8da92018-06-07 17:52:27 -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
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700503// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700504status_t Parcel::writeInterfaceToken(const String16& interface)
505{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700506 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
507 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700508 // currently the interface identification token is just its name as a string
509 return writeString16(interface);
510}
511
Mathias Agopian83c04462009-05-22 19:00:22 -0700512bool Parcel::checkInterface(IBinder* binder) const
513{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700514 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700515}
516
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700517bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700518 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700519{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700520 int32_t strictPolicy = readInt32();
Yi Kongfdd8da92018-06-07 17:52:27 -0700521 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700522 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700523 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700524 if ((threadState->getLastTransactionBinderFlags() &
525 IBinder::FLAG_ONEWAY) != 0) {
526 // For one-way calls, the callee is running entirely
527 // disconnected from the caller, so disable StrictMode entirely.
528 // Not only does disk/network usage not impact the caller, but
529 // there's no way to commuicate back any violations anyway.
530 threadState->setStrictModePolicy(0);
531 } else {
532 threadState->setStrictModePolicy(strictPolicy);
533 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700534 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700535 if (str == interface) {
536 return true;
537 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700538 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700539 String8(interface).string(), String8(str).string());
540 return false;
541 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700542}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700544size_t Parcel::objectsCount() const
545{
546 return mObjectsSize;
547}
548
549status_t Parcel::errorCheck() const
550{
551 return mError;
552}
553
554void Parcel::setError(status_t err)
555{
556 mError = err;
557}
558
559status_t Parcel::finishWrite(size_t len)
560{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700561 if (len > INT32_MAX) {
562 // don't accept size_t values which may have come from an
563 // inadvertent conversion from a negative int.
564 return BAD_VALUE;
565 }
566
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700567 //printf("Finish write of %d\n", len);
568 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700569 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700570 if (mDataPos > mDataSize) {
571 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700572 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700573 }
574 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
575 return NO_ERROR;
576}
577
578status_t Parcel::writeUnpadded(const void* data, size_t len)
579{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700580 if (len > INT32_MAX) {
581 // don't accept size_t values which may have come from an
582 // inadvertent conversion from a negative int.
583 return BAD_VALUE;
584 }
585
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700586 size_t end = mDataPos + len;
587 if (end < mDataPos) {
588 // integer overflow
589 return BAD_VALUE;
590 }
591
592 if (end <= mDataCapacity) {
593restart_write:
594 memcpy(mData+mDataPos, data, len);
595 return finishWrite(len);
596 }
597
598 status_t err = growData(len);
599 if (err == NO_ERROR) goto restart_write;
600 return err;
601}
602
603status_t Parcel::write(const void* data, size_t len)
604{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700605 if (len > INT32_MAX) {
606 // don't accept size_t values which may have come from an
607 // inadvertent conversion from a negative int.
608 return BAD_VALUE;
609 }
610
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700611 void* const d = writeInplace(len);
612 if (d) {
613 memcpy(d, data, len);
614 return NO_ERROR;
615 }
616 return mError;
617}
618
619void* Parcel::writeInplace(size_t len)
620{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700621 if (len > INT32_MAX) {
622 // don't accept size_t values which may have come from an
623 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -0700624 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700625 }
626
627 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700628
629 // sanity check for integer overflow
630 if (mDataPos+padded < mDataPos) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700631 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700632 }
633
634 if ((mDataPos+padded) <= mDataCapacity) {
635restart_write:
636 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
637 uint8_t* const data = mData+mDataPos;
638
639 // Need to pad at end?
640 if (padded != len) {
641#if BYTE_ORDER == BIG_ENDIAN
642 static const uint32_t mask[4] = {
643 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
644 };
645#endif
646#if BYTE_ORDER == LITTLE_ENDIAN
647 static const uint32_t mask[4] = {
648 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
649 };
650#endif
651 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
652 // *reinterpret_cast<void**>(data+padded-4));
653 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
654 }
655
656 finishWrite(padded);
657 return data;
658 }
659
660 status_t err = growData(padded);
661 if (err == NO_ERROR) goto restart_write;
Yi Kongfdd8da92018-06-07 17:52:27 -0700662 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700663}
664
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800665status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
666 const uint8_t* strData = (uint8_t*)str.data();
667 const size_t strLen= str.length();
668 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100669 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800670 return BAD_VALUE;
671 }
672
673 status_t err = writeInt32(utf16Len);
674 if (err) {
675 return err;
676 }
677
678 // Allocate enough bytes to hold our converted string and its terminating NULL.
679 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
680 if (!dst) {
681 return NO_MEMORY;
682 }
683
Sergio Girof4607432016-07-21 14:46:35 +0100684 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800685
686 return NO_ERROR;
687}
688
689status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
690 if (!str) {
691 return writeInt32(-1);
692 }
693 return writeUtf8AsUtf16(*str);
694}
695
Casey Dahlin185d3442016-02-09 11:08:35 -0800696namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800697
Casey Dahlin185d3442016-02-09 11:08:35 -0800698template<typename T>
699status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700700{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700701 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700702 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700703 status = BAD_VALUE;
704 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700705 }
706
Casey Dahlin185d3442016-02-09 11:08:35 -0800707 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700708 if (status != OK) {
709 return status;
710 }
711
Casey Dahlin185d3442016-02-09 11:08:35 -0800712 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700713 if (!data) {
714 status = BAD_VALUE;
715 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700716 }
717
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700718 memcpy(data, val.data(), val.size());
719 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700720}
721
Casey Dahlin185d3442016-02-09 11:08:35 -0800722template<typename T>
723status_t writeByteVectorInternalPtr(Parcel* parcel,
724 const std::unique_ptr<std::vector<T>>& val)
725{
726 if (!val) {
727 return parcel->writeInt32(-1);
728 }
729
730 return writeByteVectorInternal(parcel, *val);
731}
732
733} // namespace
734
735status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
736 return writeByteVectorInternal(this, val);
737}
738
739status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
740{
741 return writeByteVectorInternalPtr(this, val);
742}
743
744status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
745 return writeByteVectorInternal(this, val);
746}
747
748status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
749{
750 return writeByteVectorInternalPtr(this, val);
751}
752
Casey Dahlin451ff582015-10-19 18:12:18 -0700753status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
754{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800755 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700756}
757
Casey Dahlinb9872622015-11-25 15:09:45 -0800758status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
759{
760 return writeNullableTypedVector(val, &Parcel::writeInt32);
761}
762
Casey Dahlin451ff582015-10-19 18:12:18 -0700763status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
764{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800765 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700766}
767
Casey Dahlinb9872622015-11-25 15:09:45 -0800768status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
769{
770 return writeNullableTypedVector(val, &Parcel::writeInt64);
771}
772
Casey Dahlin451ff582015-10-19 18:12:18 -0700773status_t Parcel::writeFloatVector(const std::vector<float>& val)
774{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800775 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700776}
777
Casey Dahlinb9872622015-11-25 15:09:45 -0800778status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
779{
780 return writeNullableTypedVector(val, &Parcel::writeFloat);
781}
782
Casey Dahlin451ff582015-10-19 18:12:18 -0700783status_t Parcel::writeDoubleVector(const std::vector<double>& val)
784{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800785 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700786}
787
Casey Dahlinb9872622015-11-25 15:09:45 -0800788status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
789{
790 return writeNullableTypedVector(val, &Parcel::writeDouble);
791}
792
Casey Dahlin451ff582015-10-19 18:12:18 -0700793status_t Parcel::writeBoolVector(const std::vector<bool>& val)
794{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800795 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700796}
797
Casey Dahlinb9872622015-11-25 15:09:45 -0800798status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
799{
800 return writeNullableTypedVector(val, &Parcel::writeBool);
801}
802
Casey Dahlin451ff582015-10-19 18:12:18 -0700803status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
804{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800805 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700806}
807
Casey Dahlinb9872622015-11-25 15:09:45 -0800808status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
809{
810 return writeNullableTypedVector(val, &Parcel::writeChar);
811}
812
Casey Dahlin451ff582015-10-19 18:12:18 -0700813status_t Parcel::writeString16Vector(const std::vector<String16>& val)
814{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800815 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700816}
817
Casey Dahlinb9872622015-11-25 15:09:45 -0800818status_t Parcel::writeString16Vector(
819 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
820{
821 return writeNullableTypedVector(val, &Parcel::writeString16);
822}
823
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800824status_t Parcel::writeUtf8VectorAsUtf16Vector(
825 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
826 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
827}
828
829status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
830 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
831}
832
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700833status_t Parcel::writeInt32(int32_t val)
834{
Andreas Huber84a6d042009-08-17 13:33:27 -0700835 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700836}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800837
838status_t Parcel::writeUint32(uint32_t val)
839{
840 return writeAligned(val);
841}
842
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700843status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700844 if (len > INT32_MAX) {
845 // don't accept size_t values which may have come from an
846 // inadvertent conversion from a negative int.
847 return BAD_VALUE;
848 }
849
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700850 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700851 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700852 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700853 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700854 if (ret == NO_ERROR) {
855 ret = write(val, len * sizeof(*val));
856 }
857 return ret;
858}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700859status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700860 if (len > INT32_MAX) {
861 // don't accept size_t values which may have come from an
862 // inadvertent conversion from a negative int.
863 return BAD_VALUE;
864 }
865
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700866 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700867 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700868 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700869 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700870 if (ret == NO_ERROR) {
871 ret = write(val, len * sizeof(*val));
872 }
873 return ret;
874}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700875
Casey Dahlind6848f52015-10-15 15:44:59 -0700876status_t Parcel::writeBool(bool val)
877{
878 return writeInt32(int32_t(val));
879}
880
881status_t Parcel::writeChar(char16_t val)
882{
883 return writeInt32(int32_t(val));
884}
885
886status_t Parcel::writeByte(int8_t val)
887{
888 return writeInt32(int32_t(val));
889}
890
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700891status_t Parcel::writeInt64(int64_t val)
892{
Andreas Huber84a6d042009-08-17 13:33:27 -0700893 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700894}
895
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700896status_t Parcel::writeUint64(uint64_t val)
897{
898 return writeAligned(val);
899}
900
Serban Constantinescuf683e012013-11-05 16:53:55 +0000901status_t Parcel::writePointer(uintptr_t val)
902{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800903 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000904}
905
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700906status_t Parcel::writeFloat(float val)
907{
Andreas Huber84a6d042009-08-17 13:33:27 -0700908 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700909}
910
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800911#if defined(__mips__) && defined(__mips_hard_float)
912
913status_t Parcel::writeDouble(double val)
914{
915 union {
916 double d;
917 unsigned long long ll;
918 } u;
919 u.d = val;
920 return writeAligned(u.ll);
921}
922
923#else
924
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700925status_t Parcel::writeDouble(double val)
926{
Andreas Huber84a6d042009-08-17 13:33:27 -0700927 return writeAligned(val);
928}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700929
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800930#endif
931
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700932status_t Parcel::writeCString(const char* str)
933{
934 return write(str, strlen(str)+1);
935}
936
937status_t Parcel::writeString8(const String8& str)
938{
939 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100940 // only write string if its length is more than zero characters,
941 // as readString8 will only read if the length field is non-zero.
942 // this is slightly different from how writeString16 works.
943 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700944 err = write(str.string(), str.bytes()+1);
945 }
946 return err;
947}
948
Casey Dahlinb9872622015-11-25 15:09:45 -0800949status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
950{
951 if (!str) {
952 return writeInt32(-1);
953 }
954
955 return writeString16(*str);
956}
957
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700958status_t Parcel::writeString16(const String16& str)
959{
960 return writeString16(str.string(), str.size());
961}
962
963status_t Parcel::writeString16(const char16_t* str, size_t len)
964{
Yi Kongfdd8da92018-06-07 17:52:27 -0700965 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700966
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700967 status_t err = writeInt32(len);
968 if (err == NO_ERROR) {
969 len *= sizeof(char16_t);
970 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
971 if (data) {
972 memcpy(data, str, len);
973 *reinterpret_cast<char16_t*>(data+len) = 0;
974 return NO_ERROR;
975 }
976 err = mError;
977 }
978 return err;
979}
980
981status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
982{
Steven Morelanddea3cf92019-07-16 18:06:55 -0700983 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700984}
985
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800986status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
987{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800988 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800989}
990
Casey Dahlinb9872622015-11-25 15:09:45 -0800991status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
992{
993 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
994}
995
996status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -0800997 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -0800998}
999
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001000status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001001 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001002}
1003
Casey Dahlinb9872622015-11-25 15:09:45 -08001004status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1005 if (!parcelable) {
1006 return writeInt32(0);
1007 }
1008
1009 return writeParcelable(*parcelable);
1010}
1011
Christopher Wiley97f048d2015-11-19 06:49:05 -08001012status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1013 status_t status = writeInt32(1); // parcelable is not null.
1014 if (status != OK) {
1015 return status;
1016 }
1017 return parcelable.writeToParcel(this);
1018}
1019
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001020status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001021{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001022 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001023 return BAD_TYPE;
1024
1025 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001026 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001027 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001029 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001030 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001032 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1033 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034
1035 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001036 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037 return err;
1038 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001039 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001040 return err;
1041}
1042
Jeff Brown93ff1f92011-11-04 19:01:44 -07001043status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001044{
1045 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001046 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001047 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001048 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001049 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001050 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001051 return writeObject(obj, true);
1052}
1053
1054status_t Parcel::writeDupFileDescriptor(int fd)
1055{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001056 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001057 if (dupFd < 0) {
1058 return -errno;
1059 }
1060 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001061 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001062 close(dupFd);
1063 }
1064 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001065}
1066
Dianne Hackborn1941a402016-08-29 12:30:43 -07001067status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1068{
1069 writeInt32(0);
1070 return writeFileDescriptor(fd, takeOwnership);
1071}
1072
Ryo Hashimotobf551892018-05-31 16:58:35 +09001073status_t Parcel::writeDupParcelFileDescriptor(int fd)
1074{
1075 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1076 if (dupFd < 0) {
1077 return -errno;
1078 }
1079 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1080 if (err != OK) {
1081 close(dupFd);
1082 }
1083 return err;
1084}
1085
Christopher Wiley2cf19952016-04-11 11:09:37 -07001086status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001087 return writeDupFileDescriptor(fd.get());
1088}
1089
Christopher Wiley2cf19952016-04-11 11:09:37 -07001090status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001091 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1092}
1093
Christopher Wiley2cf19952016-04-11 11:09:37 -07001094status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001095 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1096}
1097
Jeff Brown13b16042014-11-11 16:44:25 -08001098status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001099{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001100 if (len > INT32_MAX) {
1101 // don't accept size_t values which may have come from an
1102 // inadvertent conversion from a negative int.
1103 return BAD_VALUE;
1104 }
1105
Jeff Brown13b16042014-11-11 16:44:25 -08001106 status_t status;
1107 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001108 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001109 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001110 if (status) return status;
1111
1112 void* ptr = writeInplace(len);
1113 if (!ptr) return NO_MEMORY;
1114
Jeff Brown13b16042014-11-11 16:44:25 -08001115 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001116 return NO_ERROR;
1117 }
1118
Steve Block6807e592011-10-20 11:56:00 +01001119 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001120 int fd = ashmem_create_region("Parcel Blob", len);
1121 if (fd < 0) return NO_MEMORY;
1122
1123 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1124 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001125 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001126 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -07001127 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001128 if (ptr == MAP_FAILED) {
1129 status = -errno;
1130 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001131 if (!mutableCopy) {
1132 result = ashmem_set_prot_region(fd, PROT_READ);
1133 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001134 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001135 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001136 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001137 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001138 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001139 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001140 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001141 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001142 return NO_ERROR;
1143 }
1144 }
1145 }
1146 }
1147 ::munmap(ptr, len);
1148 }
1149 ::close(fd);
1150 return status;
1151}
1152
Jeff Brown13b16042014-11-11 16:44:25 -08001153status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1154{
1155 // Must match up with what's done in writeBlob.
1156 if (!mAllowFds) return FDS_NOT_ALLOWED;
1157 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1158 if (status) return status;
1159 return writeDupFileDescriptor(fd);
1160}
1161
Mathias Agopiane1424282013-07-29 21:24:40 -07001162status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001163{
1164 status_t err;
1165
1166 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001167 const size_t len = val.getFlattenedSize();
1168 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001169
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001170 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001171 // don't accept size_t values which may have come from an
1172 // inadvertent conversion from a negative int.
1173 return BAD_VALUE;
1174 }
1175
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001176 err = this->writeInt32(len);
1177 if (err) return err;
1178
1179 err = this->writeInt32(fd_count);
1180 if (err) return err;
1181
1182 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001183 void* const buf = this->writeInplace(len);
Yi Kongfdd8da92018-06-07 17:52:27 -07001184 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001185 return BAD_VALUE;
1186
Yi Kongfdd8da92018-06-07 17:52:27 -07001187 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001188 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001189 fds = new (std::nothrow) int[fd_count];
1190 if (fds == nullptr) {
1191 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1192 return BAD_VALUE;
1193 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001194 }
1195
1196 err = val.flatten(buf, len, fds, fd_count);
1197 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1198 err = this->writeDupFileDescriptor( fds[i] );
1199 }
1200
1201 if (fd_count) {
1202 delete [] fds;
1203 }
1204
1205 return err;
1206}
1207
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001208status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1209{
1210 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1211 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1212 if (enoughData && enoughObjects) {
1213restart_write:
1214 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001215
Christopher Tate98e67d32015-06-03 18:44:15 -07001216 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001217 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001218 if (!mAllowFds) {
1219 // fail before modifying our object index
1220 return FDS_NOT_ALLOWED;
1221 }
1222 mHasFds = mFdsKnown = true;
1223 }
1224
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001225 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001226 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001227 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001228 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001229 mObjectsSize++;
1230 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001231
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001232 return finishWrite(sizeof(flat_binder_object));
1233 }
1234
1235 if (!enoughData) {
1236 const status_t err = growData(sizeof(val));
1237 if (err != NO_ERROR) return err;
1238 }
1239 if (!enoughObjects) {
1240 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001241 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001242 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07001243 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001244 mObjects = objects;
1245 mObjectsCapacity = newSize;
1246 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001247
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001248 goto restart_write;
1249}
1250
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001251status_t Parcel::writeNoException()
1252{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001253 binder::Status status;
1254 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001255}
1256
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001257status_t Parcel::validateReadData(size_t upperBound) const
1258{
1259 // Don't allow non-object reads on object data
1260 if (mObjectsSorted || mObjectsSize <= 1) {
1261data_sorted:
1262 // Expect to check only against the next object
1263 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1264 // For some reason the current read position is greater than the next object
1265 // hint. Iterate until we find the right object
1266 size_t nextObject = mNextObjectHint;
1267 do {
1268 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1269 // Requested info overlaps with an object
1270 ALOGE("Attempt to read from protected data in Parcel %p", this);
1271 return PERMISSION_DENIED;
1272 }
1273 nextObject++;
1274 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1275 mNextObjectHint = nextObject;
1276 }
1277 return NO_ERROR;
1278 }
1279 // Quickly determine if mObjects is sorted.
1280 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1281 binder_size_t* prevObj = currObj;
1282 while (currObj > mObjects) {
1283 prevObj--;
1284 if(*prevObj > *currObj) {
1285 goto data_unsorted;
1286 }
1287 currObj--;
1288 }
1289 mObjectsSorted = true;
1290 goto data_sorted;
1291
1292data_unsorted:
1293 // Insertion Sort mObjects
1294 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1295 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1296 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1297 binder_size_t temp = *iter0;
1298 binder_size_t* iter1 = iter0 - 1;
1299 while (iter1 >= mObjects && *iter1 > temp) {
1300 *(iter1 + 1) = *iter1;
1301 iter1--;
1302 }
1303 *(iter1 + 1) = temp;
1304 }
1305 mNextObjectHint = 0;
1306 mObjectsSorted = true;
1307 goto data_sorted;
1308}
1309
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001310status_t Parcel::read(void* outData, size_t len) const
1311{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001312 if (len > INT32_MAX) {
1313 // don't accept size_t values which may have come from an
1314 // inadvertent conversion from a negative int.
1315 return BAD_VALUE;
1316 }
1317
1318 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1319 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001320 if (mObjectsSize > 0) {
1321 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001322 if(err != NO_ERROR) {
1323 // Still increment the data position by the expected length
1324 mDataPos += pad_size(len);
1325 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1326 return err;
1327 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001328 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001329 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001330 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001331 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001332 return NO_ERROR;
1333 }
1334 return NOT_ENOUGH_DATA;
1335}
1336
1337const void* Parcel::readInplace(size_t len) const
1338{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001339 if (len > INT32_MAX) {
1340 // don't accept size_t values which may have come from an
1341 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -07001342 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001343 }
1344
1345 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1346 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001347 if (mObjectsSize > 0) {
1348 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001349 if(err != NO_ERROR) {
1350 // Still increment the data position by the expected length
1351 mDataPos += pad_size(len);
1352 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Xin Lif11e2bd2018-06-08 15:11:57 -07001353 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001354 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001355 }
1356
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001357 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001358 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001359 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001360 return data;
1361 }
Yi Kongfdd8da92018-06-07 17:52:27 -07001362 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001363}
1364
Andreas Huber84a6d042009-08-17 13:33:27 -07001365template<class T>
1366status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001367 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001368
1369 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001370 if (mObjectsSize > 0) {
1371 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001372 if(err != NO_ERROR) {
1373 // Still increment the data position by the expected length
1374 mDataPos += sizeof(T);
1375 return err;
1376 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001377 }
1378
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001379 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001380 mDataPos += sizeof(T);
1381 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001382 return NO_ERROR;
1383 } else {
1384 return NOT_ENOUGH_DATA;
1385 }
1386}
1387
Andreas Huber84a6d042009-08-17 13:33:27 -07001388template<class T>
1389T Parcel::readAligned() const {
1390 T result;
1391 if (readAligned(&result) != NO_ERROR) {
1392 result = 0;
1393 }
1394
1395 return result;
1396}
1397
1398template<class T>
1399status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001400 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001401
1402 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1403restart_write:
1404 *reinterpret_cast<T*>(mData+mDataPos) = val;
1405 return finishWrite(sizeof(val));
1406 }
1407
1408 status_t err = growData(sizeof(val));
1409 if (err == NO_ERROR) goto restart_write;
1410 return err;
1411}
1412
Casey Dahlin185d3442016-02-09 11:08:35 -08001413namespace {
1414
1415template<typename T>
1416status_t readByteVectorInternal(const Parcel* parcel,
1417 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001418 val->clear();
1419
1420 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001421 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001422
1423 if (status != OK) {
1424 return status;
1425 }
1426
Christopher Wiley4db672d2015-11-10 09:44:30 -08001427 if (size < 0) {
1428 status = UNEXPECTED_NULL;
1429 return status;
1430 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001431 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001432 status = BAD_VALUE;
1433 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001434 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001435
Paul Lietar433e87b2016-09-16 10:39:32 -07001436 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001437 if (!data) {
1438 status = BAD_VALUE;
1439 return status;
1440 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001441 val->reserve(size);
1442 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001443
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001444 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001445}
1446
Casey Dahlin185d3442016-02-09 11:08:35 -08001447template<typename T>
1448status_t readByteVectorInternalPtr(
1449 const Parcel* parcel,
1450 std::unique_ptr<std::vector<T>>* val) {
1451 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001452 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001453 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001454 val->reset();
1455
1456 if (status != OK || size < 0) {
1457 return status;
1458 }
1459
Casey Dahlin185d3442016-02-09 11:08:35 -08001460 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001461 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001462
Casey Dahlin185d3442016-02-09 11:08:35 -08001463 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001464
1465 if (status != OK) {
1466 val->reset();
1467 }
1468
1469 return status;
1470}
1471
Casey Dahlin185d3442016-02-09 11:08:35 -08001472} // namespace
1473
1474status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1475 return readByteVectorInternal(this, val);
1476}
1477
1478status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1479 return readByteVectorInternal(this, val);
1480}
1481
1482status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1483 return readByteVectorInternalPtr(this, val);
1484}
1485
1486status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1487 return readByteVectorInternalPtr(this, val);
1488}
1489
Casey Dahlinb9872622015-11-25 15:09:45 -08001490status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1491 return readNullableTypedVector(val, &Parcel::readInt32);
1492}
1493
Casey Dahlin451ff582015-10-19 18:12:18 -07001494status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001495 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001496}
1497
Casey Dahlinb9872622015-11-25 15:09:45 -08001498status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1499 return readNullableTypedVector(val, &Parcel::readInt64);
1500}
1501
Casey Dahlin451ff582015-10-19 18:12:18 -07001502status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001503 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001504}
1505
Casey Dahlinb9872622015-11-25 15:09:45 -08001506status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1507 return readNullableTypedVector(val, &Parcel::readFloat);
1508}
1509
Casey Dahlin451ff582015-10-19 18:12:18 -07001510status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001511 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001512}
1513
Casey Dahlinb9872622015-11-25 15:09:45 -08001514status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1515 return readNullableTypedVector(val, &Parcel::readDouble);
1516}
1517
Casey Dahlin451ff582015-10-19 18:12:18 -07001518status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001519 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001520}
1521
Casey Dahlinb9872622015-11-25 15:09:45 -08001522status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1523 const int32_t start = dataPosition();
1524 int32_t size;
1525 status_t status = readInt32(&size);
1526 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001527
Casey Dahlinb9872622015-11-25 15:09:45 -08001528 if (status != OK || size < 0) {
1529 return status;
1530 }
1531
1532 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001533 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001534
1535 status = readBoolVector(val->get());
1536
1537 if (status != OK) {
1538 val->reset();
1539 }
1540
1541 return status;
1542}
1543
1544status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001545 int32_t size;
1546 status_t status = readInt32(&size);
1547
1548 if (status != OK) {
1549 return status;
1550 }
1551
1552 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001553 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001554 }
1555
1556 val->resize(size);
1557
1558 /* C++ bool handling means a vector of bools isn't necessarily addressable
1559 * (we might use individual bits)
1560 */
Christopher Wiley97887982015-10-27 16:33:47 -07001561 bool data;
1562 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001563 status = readBool(&data);
1564 (*val)[i] = data;
1565
1566 if (status != OK) {
1567 return status;
1568 }
1569 }
1570
1571 return OK;
1572}
1573
Casey Dahlinb9872622015-11-25 15:09:45 -08001574status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1575 return readNullableTypedVector(val, &Parcel::readChar);
1576}
1577
Casey Dahlin451ff582015-10-19 18:12:18 -07001578status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001579 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001580}
1581
Casey Dahlinb9872622015-11-25 15:09:45 -08001582status_t Parcel::readString16Vector(
1583 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1584 return readNullableTypedVector(val, &Parcel::readString16);
1585}
1586
Casey Dahlin451ff582015-10-19 18:12:18 -07001587status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001588 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001589}
1590
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001591status_t Parcel::readUtf8VectorFromUtf16Vector(
1592 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1593 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1594}
1595
1596status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1597 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1598}
Casey Dahlin451ff582015-10-19 18:12:18 -07001599
Andreas Huber84a6d042009-08-17 13:33:27 -07001600status_t Parcel::readInt32(int32_t *pArg) const
1601{
1602 return readAligned(pArg);
1603}
1604
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001605int32_t Parcel::readInt32() const
1606{
Andreas Huber84a6d042009-08-17 13:33:27 -07001607 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001608}
1609
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001610status_t Parcel::readUint32(uint32_t *pArg) const
1611{
1612 return readAligned(pArg);
1613}
1614
1615uint32_t Parcel::readUint32() const
1616{
1617 return readAligned<uint32_t>();
1618}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001619
1620status_t Parcel::readInt64(int64_t *pArg) const
1621{
Andreas Huber84a6d042009-08-17 13:33:27 -07001622 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001623}
1624
1625
1626int64_t Parcel::readInt64() const
1627{
Andreas Huber84a6d042009-08-17 13:33:27 -07001628 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001629}
1630
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001631status_t Parcel::readUint64(uint64_t *pArg) const
1632{
1633 return readAligned(pArg);
1634}
1635
1636uint64_t Parcel::readUint64() const
1637{
1638 return readAligned<uint64_t>();
1639}
1640
Serban Constantinescuf683e012013-11-05 16:53:55 +00001641status_t Parcel::readPointer(uintptr_t *pArg) const
1642{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001643 status_t ret;
1644 binder_uintptr_t ptr;
1645 ret = readAligned(&ptr);
1646 if (!ret)
1647 *pArg = ptr;
1648 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001649}
1650
1651uintptr_t Parcel::readPointer() const
1652{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001653 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001654}
1655
1656
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001657status_t Parcel::readFloat(float *pArg) const
1658{
Andreas Huber84a6d042009-08-17 13:33:27 -07001659 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001660}
1661
1662
1663float Parcel::readFloat() const
1664{
Andreas Huber84a6d042009-08-17 13:33:27 -07001665 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001666}
1667
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001668#if defined(__mips__) && defined(__mips_hard_float)
1669
1670status_t Parcel::readDouble(double *pArg) const
1671{
1672 union {
1673 double d;
1674 unsigned long long ll;
1675 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001676 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001677 status_t status;
1678 status = readAligned(&u.ll);
1679 *pArg = u.d;
1680 return status;
1681}
1682
1683double Parcel::readDouble() const
1684{
1685 union {
1686 double d;
1687 unsigned long long ll;
1688 } u;
1689 u.ll = readAligned<unsigned long long>();
1690 return u.d;
1691}
1692
1693#else
1694
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001695status_t Parcel::readDouble(double *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
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001700double Parcel::readDouble() const
1701{
Andreas Huber84a6d042009-08-17 13:33:27 -07001702 return readAligned<double>();
1703}
1704
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001705#endif
1706
Andreas Huber84a6d042009-08-17 13:33:27 -07001707status_t Parcel::readIntPtr(intptr_t *pArg) const
1708{
1709 return readAligned(pArg);
1710}
1711
1712
1713intptr_t Parcel::readIntPtr() const
1714{
1715 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001716}
1717
Casey Dahlind6848f52015-10-15 15:44:59 -07001718status_t Parcel::readBool(bool *pArg) const
1719{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001720 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001721 status_t ret = readInt32(&tmp);
1722 *pArg = (tmp != 0);
1723 return ret;
1724}
1725
1726bool Parcel::readBool() const
1727{
1728 return readInt32() != 0;
1729}
1730
1731status_t Parcel::readChar(char16_t *pArg) const
1732{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001733 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001734 status_t ret = readInt32(&tmp);
1735 *pArg = char16_t(tmp);
1736 return ret;
1737}
1738
1739char16_t Parcel::readChar() const
1740{
1741 return char16_t(readInt32());
1742}
1743
1744status_t Parcel::readByte(int8_t *pArg) const
1745{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001746 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001747 status_t ret = readInt32(&tmp);
1748 *pArg = int8_t(tmp);
1749 return ret;
1750}
1751
1752int8_t Parcel::readByte() const
1753{
1754 return int8_t(readInt32());
1755}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001756
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001757status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1758 size_t utf16Size = 0;
1759 const char16_t* src = readString16Inplace(&utf16Size);
1760 if (!src) {
1761 return UNEXPECTED_NULL;
1762 }
1763
1764 // Save ourselves the trouble, we're done.
1765 if (utf16Size == 0u) {
1766 str->clear();
1767 return NO_ERROR;
1768 }
1769
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001770 // Allow for closing '\0'
1771 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1772 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001773 return BAD_VALUE;
1774 }
1775 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001776 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001777 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001778 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1779 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001780 return NO_ERROR;
1781}
1782
1783status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1784 const int32_t start = dataPosition();
1785 int32_t size;
1786 status_t status = readInt32(&size);
1787 str->reset();
1788
1789 if (status != OK || size < 0) {
1790 return status;
1791 }
1792
1793 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001794 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001795 return readUtf8FromUtf16(str->get());
1796}
1797
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001798const char* Parcel::readCString() const
1799{
Steven Morelanddb9a7122019-05-17 13:14:06 -07001800 if (mDataPos < mDataSize) {
1801 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001802 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1803 // is the string's trailing NUL within the parcel's valid bounds?
1804 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1805 if (eos) {
1806 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001807 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001808 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001809 return str;
1810 }
1811 }
Yi Kongfdd8da92018-06-07 17:52:27 -07001812 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001813}
1814
1815String8 Parcel::readString8() const
1816{
Roshan Pius87b64d22016-07-18 12:51:02 -07001817 String8 retString;
1818 status_t status = readString8(&retString);
1819 if (status != OK) {
1820 // We don't care about errors here, so just return an empty string.
1821 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001822 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001823 return retString;
1824}
1825
1826status_t Parcel::readString8(String8* pArg) const
1827{
1828 int32_t size;
1829 status_t status = readInt32(&size);
1830 if (status != OK) {
1831 return status;
1832 }
1833 // watch for potential int overflow from size+1
1834 if (size < 0 || size >= INT32_MAX) {
1835 return BAD_VALUE;
1836 }
1837 // |writeString8| writes nothing for empty string.
1838 if (size == 0) {
1839 *pArg = String8();
1840 return OK;
1841 }
1842 const char* str = (const char*)readInplace(size + 1);
Yi Kongfdd8da92018-06-07 17:52:27 -07001843 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07001844 return BAD_VALUE;
1845 }
1846 pArg->setTo(str, size);
1847 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001848}
1849
1850String16 Parcel::readString16() const
1851{
1852 size_t len;
1853 const char16_t* str = readString16Inplace(&len);
1854 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001855 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001856 return String16();
1857}
1858
Casey Dahlinb9872622015-11-25 15:09:45 -08001859status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1860{
1861 const int32_t start = dataPosition();
1862 int32_t size;
1863 status_t status = readInt32(&size);
1864 pArg->reset();
1865
1866 if (status != OK || size < 0) {
1867 return status;
1868 }
1869
1870 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001871 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001872
1873 status = readString16(pArg->get());
1874
1875 if (status != OK) {
1876 pArg->reset();
1877 }
1878
1879 return status;
1880}
1881
Casey Dahlin451ff582015-10-19 18:12:18 -07001882status_t Parcel::readString16(String16* pArg) const
1883{
1884 size_t len;
1885 const char16_t* str = readString16Inplace(&len);
1886 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001887 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001888 return 0;
1889 } else {
1890 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001891 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001892 }
1893}
1894
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001895const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1896{
1897 int32_t size = readInt32();
1898 // watch for potential int overflow from size+1
1899 if (size >= 0 && size < INT32_MAX) {
1900 *outLen = size;
1901 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07001902 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001903 return str;
1904 }
1905 }
1906 *outLen = 0;
Yi Kongfdd8da92018-06-07 17:52:27 -07001907 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001908}
1909
Casey Dahlinf0c13772015-10-27 18:33:56 -07001910status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1911{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001912 status_t status = readNullableStrongBinder(val);
1913 if (status == OK && !val->get()) {
1914 status = UNEXPECTED_NULL;
1915 }
1916 return status;
1917}
1918
1919status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1920{
Steven Morelanddea3cf92019-07-16 18:06:55 -07001921 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001922}
1923
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001924sp<IBinder> Parcel::readStrongBinder() const
1925{
1926 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001927 // Note that a lot of code in Android reads binders by hand with this
1928 // method, and that code has historically been ok with getting nullptr
1929 // back (while ignoring error codes).
1930 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001931 return val;
1932}
1933
Christopher Wiley97f048d2015-11-19 06:49:05 -08001934status_t Parcel::readParcelable(Parcelable* parcelable) const {
1935 int32_t have_parcelable = 0;
1936 status_t status = readInt32(&have_parcelable);
1937 if (status != OK) {
1938 return status;
1939 }
1940 if (!have_parcelable) {
1941 return UNEXPECTED_NULL;
1942 }
1943 return parcelable->readFromParcel(this);
1944}
1945
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001946int32_t Parcel::readExceptionCode() const
1947{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001948 binder::Status status;
1949 status.readFromParcel(*this);
1950 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001951}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001952
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001953native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001954{
1955 int numFds, numInts;
1956 status_t err;
1957 err = readInt32(&numFds);
Yi Kongfdd8da92018-06-07 17:52:27 -07001958 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001959 err = readInt32(&numInts);
Yi Kongfdd8da92018-06-07 17:52:27 -07001960 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001961
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001962 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001963 if (!h) {
Yi Kongfdd8da92018-06-07 17:52:27 -07001964 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001965 }
1966
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001967 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001968 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001969 if (h->data[i] < 0) {
1970 for (int j = 0; j < i; j++) {
1971 close(h->data[j]);
1972 }
1973 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07001974 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001975 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001976 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001977 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001978 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001979 native_handle_close(h);
1980 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07001981 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001982 }
1983 return h;
1984}
1985
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001986int Parcel::readFileDescriptor() const
1987{
1988 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001989
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001990 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001991 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001992 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001993
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001994 return BAD_TYPE;
1995}
1996
Dianne Hackborn1941a402016-08-29 12:30:43 -07001997int Parcel::readParcelFileDescriptor() const
1998{
1999 int32_t hasComm = readInt32();
2000 int fd = readFileDescriptor();
2001 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002002 // detach (owned by the binder driver)
2003 int comm = readFileDescriptor();
2004
2005 // warning: this must be kept in sync with:
2006 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2007 enum ParcelFileDescriptorStatus {
2008 DETACHED = 2,
2009 };
2010
2011#if BYTE_ORDER == BIG_ENDIAN
2012 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2013#endif
2014#if BYTE_ORDER == LITTLE_ENDIAN
2015 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2016#endif
2017
2018 ssize_t written = TEMP_FAILURE_RETRY(
2019 ::write(comm, &message, sizeof(message)));
2020
2021 if (written == -1 || written != sizeof(message)) {
2022 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2023 written, strerror(errno));
2024 return BAD_TYPE;
2025 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002026 }
2027 return fd;
2028}
2029
Christopher Wiley2cf19952016-04-11 11:09:37 -07002030status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002031{
2032 int got = readFileDescriptor();
2033
2034 if (got == BAD_TYPE) {
2035 return BAD_TYPE;
2036 }
2037
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002038 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002039
2040 if (val->get() < 0) {
2041 return BAD_VALUE;
2042 }
2043
2044 return OK;
2045}
2046
Ryo Hashimotobf551892018-05-31 16:58:35 +09002047status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2048{
2049 int got = readParcelFileDescriptor();
2050
2051 if (got == BAD_TYPE) {
2052 return BAD_TYPE;
2053 }
2054
2055 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2056
2057 if (val->get() < 0) {
2058 return BAD_VALUE;
2059 }
2060
2061 return OK;
2062}
Casey Dahlin06673e32015-11-23 13:24:23 -08002063
Christopher Wiley2cf19952016-04-11 11:09:37 -07002064status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002065 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2066}
2067
Christopher Wiley2cf19952016-04-11 11:09:37 -07002068status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002069 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2070}
2071
Jeff Brown5707dbf2011-09-23 21:17:56 -07002072status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2073{
Jeff Brown13b16042014-11-11 16:44:25 -08002074 int32_t blobType;
2075 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002076 if (status) return status;
2077
Jeff Brown13b16042014-11-11 16:44:25 -08002078 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002079 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002080 const void* ptr = readInplace(len);
2081 if (!ptr) return BAD_VALUE;
2082
Jeff Brown13b16042014-11-11 16:44:25 -08002083 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002084 return NO_ERROR;
2085 }
2086
Steve Block6807e592011-10-20 11:56:00 +01002087 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002088 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002089 int fd = readFileDescriptor();
2090 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2091
Yi Kongfdd8da92018-06-07 17:52:27 -07002092 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002093 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002094 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002095
Jeff Brown13b16042014-11-11 16:44:25 -08002096 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002097 return NO_ERROR;
2098}
2099
Mathias Agopiane1424282013-07-29 21:24:40 -07002100status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002101{
2102 // size
2103 const size_t len = this->readInt32();
2104 const size_t fd_count = this->readInt32();
2105
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002106 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002107 // don't accept size_t values which may have come from an
2108 // inadvertent conversion from a negative int.
2109 return BAD_VALUE;
2110 }
2111
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002112 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002113 void const* const buf = this->readInplace(pad_size(len));
Yi Kongfdd8da92018-06-07 17:52:27 -07002114 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002115 return BAD_VALUE;
2116
Yi Kongfdd8da92018-06-07 17:52:27 -07002117 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002118 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002119 fds = new (std::nothrow) int[fd_count];
2120 if (fds == nullptr) {
2121 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2122 return BAD_VALUE;
2123 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002124 }
2125
2126 status_t err = NO_ERROR;
2127 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002128 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002129 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002130 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002131 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 -07002132 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2133 // Close all the file descriptors that were dup-ed.
2134 for (size_t j=0; j<i ;j++) {
2135 close(fds[j]);
2136 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002137 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002138 }
2139
2140 if (err == NO_ERROR) {
2141 err = val.unflatten(buf, len, fds, fd_count);
2142 }
2143
2144 if (fd_count) {
2145 delete [] fds;
2146 }
2147
2148 return err;
2149}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002150const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2151{
2152 const size_t DPOS = mDataPos;
2153 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2154 const flat_binder_object* obj
2155 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2156 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002157 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002158 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002159 // the object list, so we don't want to check for it when
2160 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002161 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002162 return obj;
2163 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002164
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002165 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002166 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002167 const size_t N = mObjectsSize;
2168 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002169
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002170 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002171 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002172 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002173
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002174 // Start at the current hint position, looking for an object at
2175 // the current data position.
2176 if (opos < N) {
2177 while (opos < (N-1) && OBJS[opos] < DPOS) {
2178 opos++;
2179 }
2180 } else {
2181 opos = N-1;
2182 }
2183 if (OBJS[opos] == DPOS) {
2184 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002185 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002186 this, DPOS, opos);
2187 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002188 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002189 return obj;
2190 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002191
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002192 // Look backwards for it...
2193 while (opos > 0 && OBJS[opos] > DPOS) {
2194 opos--;
2195 }
2196 if (OBJS[opos] == DPOS) {
2197 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002198 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002199 this, DPOS, opos);
2200 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002201 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002202 return obj;
2203 }
2204 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002205 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 -07002206 this, DPOS);
2207 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002208 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002209}
2210
2211void Parcel::closeFileDescriptors()
2212{
2213 size_t i = mObjectsSize;
2214 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002215 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002216 }
2217 while (i > 0) {
2218 i--;
2219 const flat_binder_object* flat
2220 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002221 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002222 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002223 close(flat->handle);
2224 }
2225 }
2226}
2227
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002228uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002229{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002230 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231}
2232
2233size_t Parcel::ipcDataSize() const
2234{
2235 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2236}
2237
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002238uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002239{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002240 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002241}
2242
2243size_t Parcel::ipcObjectsCount() const
2244{
2245 return mObjectsSize;
2246}
2247
2248void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002249 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002250{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002251 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002252 freeDataNoInit();
2253 mError = NO_ERROR;
2254 mData = const_cast<uint8_t*>(data);
2255 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002256 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002257 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002258 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002259 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002260 mObjectsSize = mObjectsCapacity = objectsCount;
2261 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002262 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002263 mOwner = relFunc;
2264 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002265 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002266 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002267 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002268 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002269 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002270 mObjectsSize = 0;
2271 break;
2272 }
2273 minOffset = offset + sizeof(flat_binder_object);
2274 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002275 scanForFds();
2276}
2277
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002278void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002279{
2280 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002281
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002282 if (errorCheck() != NO_ERROR) {
2283 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002284 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002285 } else if (dataSize() > 0) {
2286 const uint8_t* DATA = data();
2287 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002288 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002289 const size_t N = objectsCount();
2290 for (size_t i=0; i<N; i++) {
2291 const flat_binder_object* flat
2292 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2293 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002294 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002295 << " = " << flat->binder;
2296 }
2297 } else {
2298 to << "NULL";
2299 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002300
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002301 to << ")";
2302}
2303
2304void Parcel::releaseObjects()
2305{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002306 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002307 if (i == 0) {
2308 return;
2309 }
2310 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002311 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002312 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002313 while (i > 0) {
2314 i--;
2315 const flat_binder_object* flat
2316 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002317 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002318 }
2319}
2320
2321void Parcel::acquireObjects()
2322{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002323 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002324 if (i == 0) {
2325 return;
2326 }
2327 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002328 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002329 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002330 while (i > 0) {
2331 i--;
2332 const flat_binder_object* flat
2333 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002334 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002335 }
2336}
2337
2338void Parcel::freeData()
2339{
2340 freeDataNoInit();
2341 initState();
2342}
2343
2344void Parcel::freeDataNoInit()
2345{
2346 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002347 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002348 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002349 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2350 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002351 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002352 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002353 if (mData) {
2354 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002355 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002356 if (mDataCapacity <= gParcelGlobalAllocSize) {
2357 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2358 } else {
2359 gParcelGlobalAllocSize = 0;
2360 }
2361 if (gParcelGlobalAllocCount > 0) {
2362 gParcelGlobalAllocCount--;
2363 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002364 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002365 free(mData);
2366 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002367 if (mObjects) free(mObjects);
2368 }
2369}
2370
2371status_t Parcel::growData(size_t len)
2372{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002373 if (len > INT32_MAX) {
2374 // don't accept size_t values which may have come from an
2375 // inadvertent conversion from a negative int.
2376 return BAD_VALUE;
2377 }
2378
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002379 size_t newSize = ((mDataSize+len)*3)/2;
2380 return (newSize <= mDataSize)
2381 ? (status_t) NO_MEMORY
2382 : continueWrite(newSize);
2383}
2384
2385status_t Parcel::restartWrite(size_t desired)
2386{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002387 if (desired > INT32_MAX) {
2388 // don't accept size_t values which may have come from an
2389 // inadvertent conversion from a negative int.
2390 return BAD_VALUE;
2391 }
2392
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002393 if (mOwner) {
2394 freeData();
2395 return continueWrite(desired);
2396 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002397
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002398 uint8_t* data = (uint8_t*)realloc(mData, desired);
2399 if (!data && desired > mDataCapacity) {
2400 mError = NO_MEMORY;
2401 return NO_MEMORY;
2402 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002403
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002404 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002405
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002406 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002407 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002408 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002409 gParcelGlobalAllocSize += desired;
2410 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002411 if (!mData) {
2412 gParcelGlobalAllocCount++;
2413 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002414 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002415 mData = data;
2416 mDataCapacity = desired;
2417 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002418
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002419 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002420 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2421 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2422
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002423 free(mObjects);
Yi Kongfdd8da92018-06-07 17:52:27 -07002424 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002425 mObjectsSize = mObjectsCapacity = 0;
2426 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002427 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002428 mHasFds = false;
2429 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002430 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002431
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002432 return NO_ERROR;
2433}
2434
2435status_t Parcel::continueWrite(size_t desired)
2436{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002437 if (desired > INT32_MAX) {
2438 // don't accept size_t values which may have come from an
2439 // inadvertent conversion from a negative int.
2440 return BAD_VALUE;
2441 }
2442
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002443 // If shrinking, first adjust for any objects that appear
2444 // after the new data size.
2445 size_t objectsSize = mObjectsSize;
2446 if (desired < mDataSize) {
2447 if (desired == 0) {
2448 objectsSize = 0;
2449 } else {
2450 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002451 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002452 break;
2453 objectsSize--;
2454 }
2455 }
2456 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002457
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002458 if (mOwner) {
2459 // If the size is going to zero, just release the owner's data.
2460 if (desired == 0) {
2461 freeData();
2462 return NO_ERROR;
2463 }
2464
2465 // If there is a different owner, we need to take
2466 // posession.
2467 uint8_t* data = (uint8_t*)malloc(desired);
2468 if (!data) {
2469 mError = NO_MEMORY;
2470 return NO_MEMORY;
2471 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002472 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002473
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002474 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002475 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002476 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002477 free(data);
2478
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479 mError = NO_MEMORY;
2480 return NO_MEMORY;
2481 }
2482
2483 // Little hack to only acquire references on objects
2484 // we will be keeping.
2485 size_t oldObjectsSize = mObjectsSize;
2486 mObjectsSize = objectsSize;
2487 acquireObjects();
2488 mObjectsSize = oldObjectsSize;
2489 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002490
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002491 if (mData) {
2492 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2493 }
2494 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002495 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002496 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002497 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002498 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kongfdd8da92018-06-07 17:52:27 -07002499 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002500
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002501 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002502 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002503 gParcelGlobalAllocSize += desired;
2504 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002505 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002506
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002507 mData = data;
2508 mObjects = objects;
2509 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002510 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002511 mDataCapacity = desired;
2512 mObjectsSize = mObjectsCapacity = objectsSize;
2513 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002514 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002515
2516 } else if (mData) {
2517 if (objectsSize < mObjectsSize) {
2518 // Need to release refs on any objects we are dropping.
2519 const sp<ProcessState> proc(ProcessState::self());
2520 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2521 const flat_binder_object* flat
2522 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002523 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002524 // will need to rescan because we may have lopped off the only FDs
2525 mFdsKnown = false;
2526 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002527 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002528 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002529 binder_size_t* objects =
2530 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002531 if (objects) {
2532 mObjects = objects;
2533 }
2534 mObjectsSize = objectsSize;
2535 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002536 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002537 }
2538
2539 // We own the data, so we can just do a realloc().
2540 if (desired > mDataCapacity) {
2541 uint8_t* data = (uint8_t*)realloc(mData, desired);
2542 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002543 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2544 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002545 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002546 gParcelGlobalAllocSize += desired;
2547 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002548 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002549 mData = data;
2550 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002551 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002552 mError = NO_MEMORY;
2553 return NO_MEMORY;
2554 }
2555 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002556 if (mDataSize > desired) {
2557 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002558 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002559 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002560 if (mDataPos > desired) {
2561 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002562 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002563 }
2564 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002565
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002566 } else {
2567 // This is the first data. Easy!
2568 uint8_t* data = (uint8_t*)malloc(desired);
2569 if (!data) {
2570 mError = NO_MEMORY;
2571 return NO_MEMORY;
2572 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002573
Yi Kongfdd8da92018-06-07 17:52:27 -07002574 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002575 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002576 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002577 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002578
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002579 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002580 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002581 gParcelGlobalAllocSize += desired;
2582 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002583 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002584
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002585 mData = data;
2586 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002587 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2588 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002589 mDataCapacity = desired;
2590 }
2591
2592 return NO_ERROR;
2593}
2594
2595void Parcel::initState()
2596{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002597 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002598 mError = NO_ERROR;
Yi Kongfdd8da92018-06-07 17:52:27 -07002599 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002600 mDataSize = 0;
2601 mDataCapacity = 0;
2602 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002603 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2604 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kongfdd8da92018-06-07 17:52:27 -07002605 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002606 mObjectsSize = 0;
2607 mObjectsCapacity = 0;
2608 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002609 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002610 mHasFds = false;
2611 mFdsKnown = true;
Steven Morelandc709dd82019-08-05 20:30:14 -07002612 mAllowFds = true;
Yi Kongfdd8da92018-06-07 17:52:27 -07002613 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002614 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002615
2616 // racing multiple init leads only to multiple identical write
2617 if (gMaxFds == 0) {
2618 struct rlimit result;
2619 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2620 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002621 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002622 } else {
2623 ALOGW("Unable to getrlimit: %s", strerror(errno));
2624 gMaxFds = 1024;
2625 }
2626 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002627}
2628
2629void Parcel::scanForFds() const
2630{
2631 bool hasFds = false;
2632 for (size_t i=0; i<mObjectsSize; i++) {
2633 const flat_binder_object* flat
2634 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002635 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002636 hasFds = true;
2637 break;
2638 }
2639 }
2640 mHasFds = hasFds;
2641 mFdsKnown = true;
2642}
2643
Dan Sandleraa5c2342015-04-10 10:08:45 -04002644size_t Parcel::getBlobAshmemSize() const
2645{
Adrian Roos6bb31142015-10-22 16:46:12 -07002646 // This used to return the size of all blobs that were written to ashmem, now we're returning
2647 // the ashmem currently referenced by this Parcel, which should be equivalent.
2648 // TODO: Remove method once ABI can be changed.
2649 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002650}
2651
Adrian Rooscbf37262015-10-22 16:12:53 -07002652size_t Parcel::getOpenAshmemSize() const
2653{
2654 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002655}
2656
2657// --- Parcel::Blob ---
2658
2659Parcel::Blob::Blob() :
Yi Kongfdd8da92018-06-07 17:52:27 -07002660 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002661}
2662
2663Parcel::Blob::~Blob() {
2664 release();
2665}
2666
2667void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002668 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002669 ::munmap(mData, mSize);
2670 }
2671 clear();
2672}
2673
Jeff Brown13b16042014-11-11 16:44:25 -08002674void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2675 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002676 mData = data;
2677 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002678 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002679}
2680
2681void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002682 mFd = -1;
Yi Kongfdd8da92018-06-07 17:52:27 -07002683 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002684 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002685 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002686}
2687
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002688}; // namespace android