blob: a2333ae07e5f0ecc34311fa6cebb599c16c8fb15 [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>
Christopher Wiley09eb7492015-11-09 15:06:15 -080038#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070039#include <binder/TextOutput.h>
40
Mark Salyzynabed7f72016-01-27 08:02:48 -080041#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070042#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080043#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070044#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080045#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#include <utils/String8.h>
47#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070048
Mathias Agopian208059f2009-05-18 15:08:03 -070049#include <private/binder/binder_module.h>
Steven Morelanda4853cd2019-07-12 15:44:37 -070050#include "Static.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070051
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052#ifndef INT32_MAX
53#define INT32_MAX ((int32_t)(2147483647))
54#endif
55
56#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080057//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080058#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080059//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070060
61// ---------------------------------------------------------------------------
62
Nick Kralevichb6b14232015-04-02 09:36:02 -070063// This macro should never be used at runtime, as a too large value
64// of s could cause an integer overflow. Instead, you should always
65// use the wrapper function pad_size()
66#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
67
68static size_t pad_size(size_t s) {
69 if (s > (SIZE_T_MAX - 3)) {
70 abort();
71 }
72 return PAD_SIZE_UNSAFE(s);
73}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070074
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070075// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080076#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070077
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070078namespace android {
79
Dianne Hackborna4cff882014-11-13 17:07:40 -080080static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
81static size_t gParcelGlobalAllocSize = 0;
82static size_t gParcelGlobalAllocCount = 0;
83
Christopher Tatee4e0ae82016-03-24 16:03:44 -070084static size_t gMaxFds = 0;
85
Jeff Brown13b16042014-11-11 16:44:25 -080086// Maximum size of a blob to transfer in-place.
87static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
88
89enum {
90 BLOB_INPLACE = 0,
91 BLOB_ASHMEM_IMMUTABLE = 1,
92 BLOB_ASHMEM_MUTABLE = 2,
93};
94
Steven Morelandb1c81202019-04-05 18:49:55 -070095static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070096 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070097{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -070098 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070099 case BINDER_TYPE_BINDER:
100 if (obj.binder) {
101 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800102 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700103 }
104 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700105 case BINDER_TYPE_HANDLE: {
106 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kongfdd8da92018-06-07 17:52:27 -0700107 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
109 b->incStrong(who);
110 }
111 return;
112 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700113 case BINDER_TYPE_FD: {
Yi Kongfdd8da92018-06-07 17:52:27 -0700114 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700115 // If we own an ashmem fd, keep track of how much memory it refers to.
116 int size = ashmem_get_size_region(obj.handle);
117 if (size > 0) {
118 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700119 }
120 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121 return;
122 }
123 }
124
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700125 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700126}
127
Adrian Roos6bb31142015-10-22 16:46:12 -0700128static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700129 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700130{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700131 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700132 case BINDER_TYPE_BINDER:
133 if (obj.binder) {
134 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800135 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136 }
137 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700138 case BINDER_TYPE_HANDLE: {
139 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kongfdd8da92018-06-07 17:52:27 -0700140 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700141 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
142 b->decStrong(who);
143 }
144 return;
145 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700146 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800147 if (obj.cookie != 0) { // owned
Yi Kongfdd8da92018-06-07 17:52:27 -0700148 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700149 int size = ashmem_get_size_region(obj.handle);
150 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800151 // ashmem size might have changed since last time it was accounted for, e.g.
152 // in acquire_object(). Value of *outAshmemSize is not critical since we are
153 // releasing the object anyway. Check for integer overflow condition.
154 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700155 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700156 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800157
158 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700159 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 return;
161 }
162 }
163
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700164 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165}
166
Steven Morelanddea3cf92019-07-16 18:06:55 -0700167status_t Parcel::finishFlattenBinder(
168 const sp<IBinder>& /*binder*/, const flat_binder_object& flat)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169{
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700170 // internal::Stability::tryMarkCompilationUnit(binder.get());
171
Steven Morelanddea3cf92019-07-16 18:06:55 -0700172 status_t status = writeObject(flat, false);
173 if (status != OK) return status;
174
175 // 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 // if (binder != nullptr && !internal::Stability::check(stability, mRequiredStability)) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700189 // return BAD_TYPE;
190 // }
191
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700192 // status = internal::Stability::set(binder.get(), stability, true /*log*/);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700193 // if (status != OK) return status;
194
195 *out = binder;
196 return OK;
197}
198
199status_t Parcel::flattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700200{
201 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700202
Martijn Coenen2b631742017-05-05 11:16:59 -0700203 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
204 /* minimum priority for all nodes is nice 0 */
205 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
206 } else {
207 /* minimum priority for all nodes is MAX_NICE(19) */
208 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
209 }
210
Yi Kongfdd8da92018-06-07 17:52:27 -0700211 if (binder != nullptr) {
Steven Moreland3085a472018-12-26 13:59:23 -0800212 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700213 if (!local) {
214 BpBinder *proxy = binder->remoteBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700215 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000216 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700217 }
218 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700219 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800220 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700221 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800222 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 } else {
Steven Moreland3085a472018-12-26 13:59:23 -0800224 if (local->isRequestingSid()) {
225 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
226 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700227 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800228 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
229 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230 }
231 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700232 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800233 obj.binder = 0;
234 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700236
Steven Morelanddea3cf92019-07-16 18:06:55 -0700237 return finishFlattenBinder(binder, obj);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238}
239
Steven Morelanddea3cf92019-07-16 18:06:55 -0700240status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700241{
Steven Morelanddea3cf92019-07-16 18:06:55 -0700242 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700243
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700244 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700245 switch (flat->hdr.type) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700246 case BINDER_TYPE_BINDER: {
247 sp<IBinder> binder = reinterpret_cast<IBinder*>(flat->cookie);
248 return finishUnflattenBinder(binder, out);
249 }
250 case BINDER_TYPE_HANDLE: {
251 sp<IBinder> binder =
252 ProcessState::self()->getStrongProxyForHandle(flat->handle);
253 return finishUnflattenBinder(binder, out);
254 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700255 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700256 }
257 return BAD_TYPE;
258}
259
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700260// ---------------------------------------------------------------------------
261
262Parcel::Parcel()
263{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800264 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700265 initState();
266}
267
268Parcel::~Parcel()
269{
270 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800271 LOG_ALLOC("Parcel %p: destroyed", this);
272}
273
274size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800275 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
276 size_t size = gParcelGlobalAllocSize;
277 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
278 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800279}
280
281size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800282 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
283 size_t count = gParcelGlobalAllocCount;
284 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
285 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700286}
287
288const uint8_t* Parcel::data() const
289{
290 return mData;
291}
292
293size_t Parcel::dataSize() const
294{
295 return (mDataSize > mDataPos ? mDataSize : mDataPos);
296}
297
298size_t Parcel::dataAvail() const
299{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700300 size_t result = dataSize() - dataPosition();
301 if (result > INT32_MAX) {
302 abort();
303 }
304 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700305}
306
307size_t Parcel::dataPosition() const
308{
309 return mDataPos;
310}
311
312size_t Parcel::dataCapacity() const
313{
314 return mDataCapacity;
315}
316
317status_t Parcel::setDataSize(size_t size)
318{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700319 if (size > INT32_MAX) {
320 // don't accept size_t values which may have come from an
321 // inadvertent conversion from a negative int.
322 return BAD_VALUE;
323 }
324
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700325 status_t err;
326 err = continueWrite(size);
327 if (err == NO_ERROR) {
328 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700329 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700330 }
331 return err;
332}
333
334void Parcel::setDataPosition(size_t pos) const
335{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700336 if (pos > INT32_MAX) {
337 // don't accept size_t values which may have come from an
338 // inadvertent conversion from a negative int.
339 abort();
340 }
341
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700342 mDataPos = pos;
343 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800344 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700345}
346
347status_t Parcel::setDataCapacity(size_t size)
348{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700349 if (size > INT32_MAX) {
350 // don't accept size_t values which may have come from an
351 // inadvertent conversion from a negative int.
352 return BAD_VALUE;
353 }
354
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700355 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700356 return NO_ERROR;
357}
358
Steven Morelanddea3cf92019-07-16 18:06:55 -0700359void Parcel::setTransactingBinder(const sp<IBinder>& binder) const {
360 mRequiredStability = internal::Stability::get(binder.get());
361}
362
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700363status_t Parcel::setData(const uint8_t* buffer, size_t len)
364{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700365 if (len > INT32_MAX) {
366 // don't accept size_t values which may have come from an
367 // inadvertent conversion from a negative int.
368 return BAD_VALUE;
369 }
370
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700371 status_t err = restartWrite(len);
372 if (err == NO_ERROR) {
373 memcpy(const_cast<uint8_t*>(data()), buffer, len);
374 mDataSize = len;
375 mFdsKnown = false;
376 }
377 return err;
378}
379
Andreas Huber51faf462011-04-13 10:21:56 -0700380status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700381{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700382 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700383 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800384 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700385 size_t size = parcel->mObjectsSize;
386 int startPos = mDataPos;
387 int firstIndex = -1, lastIndex = -2;
388
389 if (len == 0) {
390 return NO_ERROR;
391 }
392
Nick Kralevichb6b14232015-04-02 09:36:02 -0700393 if (len > INT32_MAX) {
394 // don't accept size_t values which may have come from an
395 // inadvertent conversion from a negative int.
396 return BAD_VALUE;
397 }
398
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700399 // range checks against the source parcel size
400 if ((offset > parcel->mDataSize)
401 || (len > parcel->mDataSize)
402 || (offset + len > parcel->mDataSize)) {
403 return BAD_VALUE;
404 }
405
406 // Count objects in range
407 for (int i = 0; i < (int) size; i++) {
408 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700409 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700410 if (firstIndex == -1) {
411 firstIndex = i;
412 }
413 lastIndex = i;
414 }
415 }
416 int numObjects = lastIndex - firstIndex + 1;
417
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700418 if ((mDataSize+len) > mDataCapacity) {
419 // grow data
420 err = growData(len);
421 if (err != NO_ERROR) {
422 return err;
423 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700424 }
425
426 // append data
427 memcpy(mData + mDataPos, data + offset, len);
428 mDataPos += len;
429 mDataSize += len;
430
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400431 err = NO_ERROR;
432
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700433 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200434 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700435 // grow objects
436 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700437 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700438 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800439 binder_size_t *objects =
440 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kongfdd8da92018-06-07 17:52:27 -0700441 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700442 return NO_MEMORY;
443 }
444 mObjects = objects;
445 mObjectsCapacity = newSize;
446 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700447
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 // append and acquire objects
449 int idx = mObjectsSize;
450 for (int i = firstIndex; i <= lastIndex; i++) {
451 size_t off = objects[i] - offset + startPos;
452 mObjects[idx++] = off;
453 mObjectsSize++;
454
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700455 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700456 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700457 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700458
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700459 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700460 // If this is a file descriptor, we need to dup it so the
461 // new Parcel now owns its own fd, and can declare that we
462 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800463 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800464 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700465 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400466 if (!mAllowFds) {
467 err = FDS_NOT_ALLOWED;
468 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700469 }
470 }
471 }
472
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400473 return err;
474}
475
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700476int Parcel::compareData(const Parcel& other) {
477 size_t size = dataSize();
478 if (size != other.dataSize()) {
479 return size < other.dataSize() ? -1 : 1;
480 }
481 return memcmp(data(), other.data(), size);
482}
483
Jeff Brown13b16042014-11-11 16:44:25 -0800484bool Parcel::allowFds() const
485{
486 return mAllowFds;
487}
488
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700489bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400490{
491 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700492 if (!allowFds) {
493 mAllowFds = false;
494 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400495 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700496}
497
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700498void Parcel::restoreAllowFds(bool lastValue)
499{
500 mAllowFds = lastValue;
501}
502
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700503bool Parcel::hasFileDescriptors() const
504{
505 if (!mFdsKnown) {
506 scanForFds();
507 }
508 return mHasFds;
509}
510
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700511// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700512status_t Parcel::writeInterfaceToken(const String16& interface)
513{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700514 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
515 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700516 // currently the interface identification token is just its name as a string
517 return writeString16(interface);
518}
519
Mathias Agopian83c04462009-05-22 19:00:22 -0700520bool Parcel::checkInterface(IBinder* binder) const
521{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700522 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700523}
524
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700525bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700526 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700527{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700528 int32_t strictPolicy = readInt32();
Yi Kongfdd8da92018-06-07 17:52:27 -0700529 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700530 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700531 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700532 if ((threadState->getLastTransactionBinderFlags() &
533 IBinder::FLAG_ONEWAY) != 0) {
534 // For one-way calls, the callee is running entirely
535 // disconnected from the caller, so disable StrictMode entirely.
536 // Not only does disk/network usage not impact the caller, but
537 // there's no way to commuicate back any violations anyway.
538 threadState->setStrictModePolicy(0);
539 } else {
540 threadState->setStrictModePolicy(strictPolicy);
541 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700542 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543 if (str == interface) {
544 return true;
545 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700546 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700547 String8(interface).string(), String8(str).string());
548 return false;
549 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700550}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700551
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700552size_t Parcel::objectsCount() const
553{
554 return mObjectsSize;
555}
556
557status_t Parcel::errorCheck() const
558{
559 return mError;
560}
561
562void Parcel::setError(status_t err)
563{
564 mError = err;
565}
566
567status_t Parcel::finishWrite(size_t len)
568{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700569 if (len > INT32_MAX) {
570 // don't accept size_t values which may have come from an
571 // inadvertent conversion from a negative int.
572 return BAD_VALUE;
573 }
574
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700575 //printf("Finish write of %d\n", len);
576 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700577 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700578 if (mDataPos > mDataSize) {
579 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700580 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700581 }
582 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
583 return NO_ERROR;
584}
585
586status_t Parcel::writeUnpadded(const void* data, size_t len)
587{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700588 if (len > INT32_MAX) {
589 // don't accept size_t values which may have come from an
590 // inadvertent conversion from a negative int.
591 return BAD_VALUE;
592 }
593
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700594 size_t end = mDataPos + len;
595 if (end < mDataPos) {
596 // integer overflow
597 return BAD_VALUE;
598 }
599
600 if (end <= mDataCapacity) {
601restart_write:
602 memcpy(mData+mDataPos, data, len);
603 return finishWrite(len);
604 }
605
606 status_t err = growData(len);
607 if (err == NO_ERROR) goto restart_write;
608 return err;
609}
610
611status_t Parcel::write(const void* data, size_t len)
612{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700613 if (len > INT32_MAX) {
614 // don't accept size_t values which may have come from an
615 // inadvertent conversion from a negative int.
616 return BAD_VALUE;
617 }
618
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700619 void* const d = writeInplace(len);
620 if (d) {
621 memcpy(d, data, len);
622 return NO_ERROR;
623 }
624 return mError;
625}
626
627void* Parcel::writeInplace(size_t len)
628{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700629 if (len > INT32_MAX) {
630 // don't accept size_t values which may have come from an
631 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -0700632 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700633 }
634
635 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700636
637 // sanity check for integer overflow
638 if (mDataPos+padded < mDataPos) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700639 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700640 }
641
642 if ((mDataPos+padded) <= mDataCapacity) {
643restart_write:
644 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
645 uint8_t* const data = mData+mDataPos;
646
647 // Need to pad at end?
648 if (padded != len) {
649#if BYTE_ORDER == BIG_ENDIAN
650 static const uint32_t mask[4] = {
651 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
652 };
653#endif
654#if BYTE_ORDER == LITTLE_ENDIAN
655 static const uint32_t mask[4] = {
656 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
657 };
658#endif
659 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
660 // *reinterpret_cast<void**>(data+padded-4));
661 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
662 }
663
664 finishWrite(padded);
665 return data;
666 }
667
668 status_t err = growData(padded);
669 if (err == NO_ERROR) goto restart_write;
Yi Kongfdd8da92018-06-07 17:52:27 -0700670 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700671}
672
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800673status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
674 const uint8_t* strData = (uint8_t*)str.data();
675 const size_t strLen= str.length();
676 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100677 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800678 return BAD_VALUE;
679 }
680
681 status_t err = writeInt32(utf16Len);
682 if (err) {
683 return err;
684 }
685
686 // Allocate enough bytes to hold our converted string and its terminating NULL.
687 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
688 if (!dst) {
689 return NO_MEMORY;
690 }
691
Sergio Girof4607432016-07-21 14:46:35 +0100692 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800693
694 return NO_ERROR;
695}
696
697status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
698 if (!str) {
699 return writeInt32(-1);
700 }
701 return writeUtf8AsUtf16(*str);
702}
703
Casey Dahlin185d3442016-02-09 11:08:35 -0800704namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800705
Casey Dahlin185d3442016-02-09 11:08:35 -0800706template<typename T>
707status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700708{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700709 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700710 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700711 status = BAD_VALUE;
712 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700713 }
714
Casey Dahlin185d3442016-02-09 11:08:35 -0800715 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700716 if (status != OK) {
717 return status;
718 }
719
Casey Dahlin185d3442016-02-09 11:08:35 -0800720 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700721 if (!data) {
722 status = BAD_VALUE;
723 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700724 }
725
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700726 memcpy(data, val.data(), val.size());
727 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700728}
729
Casey Dahlin185d3442016-02-09 11:08:35 -0800730template<typename T>
731status_t writeByteVectorInternalPtr(Parcel* parcel,
732 const std::unique_ptr<std::vector<T>>& val)
733{
734 if (!val) {
735 return parcel->writeInt32(-1);
736 }
737
738 return writeByteVectorInternal(parcel, *val);
739}
740
741} // namespace
742
743status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
744 return writeByteVectorInternal(this, val);
745}
746
747status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
748{
749 return writeByteVectorInternalPtr(this, val);
750}
751
752status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
753 return writeByteVectorInternal(this, val);
754}
755
756status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
757{
758 return writeByteVectorInternalPtr(this, val);
759}
760
Casey Dahlin451ff582015-10-19 18:12:18 -0700761status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
762{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800763 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700764}
765
Casey Dahlinb9872622015-11-25 15:09:45 -0800766status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
767{
768 return writeNullableTypedVector(val, &Parcel::writeInt32);
769}
770
Casey Dahlin451ff582015-10-19 18:12:18 -0700771status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
772{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800773 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700774}
775
Casey Dahlinb9872622015-11-25 15:09:45 -0800776status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
777{
778 return writeNullableTypedVector(val, &Parcel::writeInt64);
779}
780
Casey Dahlin451ff582015-10-19 18:12:18 -0700781status_t Parcel::writeFloatVector(const std::vector<float>& val)
782{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800783 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700784}
785
Casey Dahlinb9872622015-11-25 15:09:45 -0800786status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
787{
788 return writeNullableTypedVector(val, &Parcel::writeFloat);
789}
790
Casey Dahlin451ff582015-10-19 18:12:18 -0700791status_t Parcel::writeDoubleVector(const std::vector<double>& val)
792{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800793 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700794}
795
Casey Dahlinb9872622015-11-25 15:09:45 -0800796status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
797{
798 return writeNullableTypedVector(val, &Parcel::writeDouble);
799}
800
Casey Dahlin451ff582015-10-19 18:12:18 -0700801status_t Parcel::writeBoolVector(const std::vector<bool>& val)
802{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800803 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700804}
805
Casey Dahlinb9872622015-11-25 15:09:45 -0800806status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
807{
808 return writeNullableTypedVector(val, &Parcel::writeBool);
809}
810
Casey Dahlin451ff582015-10-19 18:12:18 -0700811status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
812{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800813 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700814}
815
Casey Dahlinb9872622015-11-25 15:09:45 -0800816status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
817{
818 return writeNullableTypedVector(val, &Parcel::writeChar);
819}
820
Casey Dahlin451ff582015-10-19 18:12:18 -0700821status_t Parcel::writeString16Vector(const std::vector<String16>& val)
822{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800823 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700824}
825
Casey Dahlinb9872622015-11-25 15:09:45 -0800826status_t Parcel::writeString16Vector(
827 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
828{
829 return writeNullableTypedVector(val, &Parcel::writeString16);
830}
831
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800832status_t Parcel::writeUtf8VectorAsUtf16Vector(
833 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
834 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
835}
836
837status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
838 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
839}
840
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700841status_t Parcel::writeInt32(int32_t val)
842{
Andreas Huber84a6d042009-08-17 13:33:27 -0700843 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700844}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800845
846status_t Parcel::writeUint32(uint32_t val)
847{
848 return writeAligned(val);
849}
850
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700851status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700852 if (len > INT32_MAX) {
853 // don't accept size_t values which may have come from an
854 // inadvertent conversion from a negative int.
855 return BAD_VALUE;
856 }
857
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700858 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700859 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700860 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700861 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700862 if (ret == NO_ERROR) {
863 ret = write(val, len * sizeof(*val));
864 }
865 return ret;
866}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700867status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700868 if (len > INT32_MAX) {
869 // don't accept size_t values which may have come from an
870 // inadvertent conversion from a negative int.
871 return BAD_VALUE;
872 }
873
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700874 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700875 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700876 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700877 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700878 if (ret == NO_ERROR) {
879 ret = write(val, len * sizeof(*val));
880 }
881 return ret;
882}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700883
Casey Dahlind6848f52015-10-15 15:44:59 -0700884status_t Parcel::writeBool(bool val)
885{
886 return writeInt32(int32_t(val));
887}
888
889status_t Parcel::writeChar(char16_t val)
890{
891 return writeInt32(int32_t(val));
892}
893
894status_t Parcel::writeByte(int8_t val)
895{
896 return writeInt32(int32_t(val));
897}
898
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700899status_t Parcel::writeInt64(int64_t val)
900{
Andreas Huber84a6d042009-08-17 13:33:27 -0700901 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700902}
903
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700904status_t Parcel::writeUint64(uint64_t val)
905{
906 return writeAligned(val);
907}
908
Serban Constantinescuf683e012013-11-05 16:53:55 +0000909status_t Parcel::writePointer(uintptr_t val)
910{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800911 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000912}
913
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700914status_t Parcel::writeFloat(float val)
915{
Andreas Huber84a6d042009-08-17 13:33:27 -0700916 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700917}
918
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800919#if defined(__mips__) && defined(__mips_hard_float)
920
921status_t Parcel::writeDouble(double val)
922{
923 union {
924 double d;
925 unsigned long long ll;
926 } u;
927 u.d = val;
928 return writeAligned(u.ll);
929}
930
931#else
932
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700933status_t Parcel::writeDouble(double val)
934{
Andreas Huber84a6d042009-08-17 13:33:27 -0700935 return writeAligned(val);
936}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700937
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800938#endif
939
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700940status_t Parcel::writeCString(const char* str)
941{
942 return write(str, strlen(str)+1);
943}
944
945status_t Parcel::writeString8(const String8& str)
946{
947 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100948 // only write string if its length is more than zero characters,
949 // as readString8 will only read if the length field is non-zero.
950 // this is slightly different from how writeString16 works.
951 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700952 err = write(str.string(), str.bytes()+1);
953 }
954 return err;
955}
956
Casey Dahlinb9872622015-11-25 15:09:45 -0800957status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
958{
959 if (!str) {
960 return writeInt32(-1);
961 }
962
963 return writeString16(*str);
964}
965
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700966status_t Parcel::writeString16(const String16& str)
967{
968 return writeString16(str.string(), str.size());
969}
970
971status_t Parcel::writeString16(const char16_t* str, size_t len)
972{
Yi Kongfdd8da92018-06-07 17:52:27 -0700973 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700974
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700975 status_t err = writeInt32(len);
976 if (err == NO_ERROR) {
977 len *= sizeof(char16_t);
978 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
979 if (data) {
980 memcpy(data, str, len);
981 *reinterpret_cast<char16_t*>(data+len) = 0;
982 return NO_ERROR;
983 }
984 err = mError;
985 }
986 return err;
987}
988
989status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
990{
Steven Morelanddea3cf92019-07-16 18:06:55 -0700991 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700992}
993
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800994status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
995{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800996 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800997}
998
Casey Dahlinb9872622015-11-25 15:09:45 -0800999status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1000{
1001 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1002}
1003
1004status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001005 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001006}
1007
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001008status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001009 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001010}
1011
Casey Dahlinb9872622015-11-25 15:09:45 -08001012status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1013 if (!parcelable) {
1014 return writeInt32(0);
1015 }
1016
1017 return writeParcelable(*parcelable);
1018}
1019
Christopher Wiley97f048d2015-11-19 06:49:05 -08001020status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1021 status_t status = writeInt32(1); // parcelable is not null.
1022 if (status != OK) {
1023 return status;
1024 }
1025 return parcelable.writeToParcel(this);
1026}
1027
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001028status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001029{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001030 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001031 return BAD_TYPE;
1032
1033 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001034 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001035 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001037 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001038 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001040 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1041 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042
1043 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001044 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045 return err;
1046 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001047 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001048 return err;
1049}
1050
Jeff Brown93ff1f92011-11-04 19:01:44 -07001051status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001052{
1053 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001054 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001055 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001056 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001057 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001058 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001059 return writeObject(obj, true);
1060}
1061
1062status_t Parcel::writeDupFileDescriptor(int fd)
1063{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001064 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001065 if (dupFd < 0) {
1066 return -errno;
1067 }
1068 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001069 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001070 close(dupFd);
1071 }
1072 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001073}
1074
Dianne Hackborn1941a402016-08-29 12:30:43 -07001075status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1076{
1077 writeInt32(0);
1078 return writeFileDescriptor(fd, takeOwnership);
1079}
1080
Ryo Hashimotobf551892018-05-31 16:58:35 +09001081status_t Parcel::writeDupParcelFileDescriptor(int fd)
1082{
1083 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1084 if (dupFd < 0) {
1085 return -errno;
1086 }
1087 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1088 if (err != OK) {
1089 close(dupFd);
1090 }
1091 return err;
1092}
1093
Christopher Wiley2cf19952016-04-11 11:09:37 -07001094status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001095 return writeDupFileDescriptor(fd.get());
1096}
1097
Christopher Wiley2cf19952016-04-11 11:09:37 -07001098status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001099 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1100}
1101
Christopher Wiley2cf19952016-04-11 11:09:37 -07001102status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001103 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1104}
1105
Jeff Brown13b16042014-11-11 16:44:25 -08001106status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001107{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001108 if (len > INT32_MAX) {
1109 // don't accept size_t values which may have come from an
1110 // inadvertent conversion from a negative int.
1111 return BAD_VALUE;
1112 }
1113
Jeff Brown13b16042014-11-11 16:44:25 -08001114 status_t status;
1115 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001116 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001117 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001118 if (status) return status;
1119
1120 void* ptr = writeInplace(len);
1121 if (!ptr) return NO_MEMORY;
1122
Jeff Brown13b16042014-11-11 16:44:25 -08001123 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001124 return NO_ERROR;
1125 }
1126
Steve Block6807e592011-10-20 11:56:00 +01001127 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001128 int fd = ashmem_create_region("Parcel Blob", len);
1129 if (fd < 0) return NO_MEMORY;
1130
1131 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1132 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001133 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001134 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -07001135 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001136 if (ptr == MAP_FAILED) {
1137 status = -errno;
1138 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001139 if (!mutableCopy) {
1140 result = ashmem_set_prot_region(fd, PROT_READ);
1141 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001142 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001143 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001144 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001145 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001146 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001147 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001148 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001149 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001150 return NO_ERROR;
1151 }
1152 }
1153 }
1154 }
1155 ::munmap(ptr, len);
1156 }
1157 ::close(fd);
1158 return status;
1159}
1160
Jeff Brown13b16042014-11-11 16:44:25 -08001161status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1162{
1163 // Must match up with what's done in writeBlob.
1164 if (!mAllowFds) return FDS_NOT_ALLOWED;
1165 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1166 if (status) return status;
1167 return writeDupFileDescriptor(fd);
1168}
1169
Mathias Agopiane1424282013-07-29 21:24:40 -07001170status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001171{
1172 status_t err;
1173
1174 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001175 const size_t len = val.getFlattenedSize();
1176 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001177
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001178 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001179 // don't accept size_t values which may have come from an
1180 // inadvertent conversion from a negative int.
1181 return BAD_VALUE;
1182 }
1183
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001184 err = this->writeInt32(len);
1185 if (err) return err;
1186
1187 err = this->writeInt32(fd_count);
1188 if (err) return err;
1189
1190 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001191 void* const buf = this->writeInplace(len);
Yi Kongfdd8da92018-06-07 17:52:27 -07001192 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001193 return BAD_VALUE;
1194
Yi Kongfdd8da92018-06-07 17:52:27 -07001195 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001196 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001197 fds = new (std::nothrow) int[fd_count];
1198 if (fds == nullptr) {
1199 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1200 return BAD_VALUE;
1201 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001202 }
1203
1204 err = val.flatten(buf, len, fds, fd_count);
1205 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1206 err = this->writeDupFileDescriptor( fds[i] );
1207 }
1208
1209 if (fd_count) {
1210 delete [] fds;
1211 }
1212
1213 return err;
1214}
1215
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001216status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1217{
1218 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1219 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1220 if (enoughData && enoughObjects) {
1221restart_write:
1222 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001223
Christopher Tate98e67d32015-06-03 18:44:15 -07001224 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001225 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001226 if (!mAllowFds) {
1227 // fail before modifying our object index
1228 return FDS_NOT_ALLOWED;
1229 }
1230 mHasFds = mFdsKnown = true;
1231 }
1232
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001233 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001234 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001235 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001236 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001237 mObjectsSize++;
1238 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001239
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001240 return finishWrite(sizeof(flat_binder_object));
1241 }
1242
1243 if (!enoughData) {
1244 const status_t err = growData(sizeof(val));
1245 if (err != NO_ERROR) return err;
1246 }
1247 if (!enoughObjects) {
1248 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001249 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001250 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07001251 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001252 mObjects = objects;
1253 mObjectsCapacity = newSize;
1254 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001255
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001256 goto restart_write;
1257}
1258
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001259status_t Parcel::writeNoException()
1260{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001261 binder::Status status;
1262 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001263}
1264
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001265status_t Parcel::validateReadData(size_t upperBound) const
1266{
1267 // Don't allow non-object reads on object data
1268 if (mObjectsSorted || mObjectsSize <= 1) {
1269data_sorted:
1270 // Expect to check only against the next object
1271 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1272 // For some reason the current read position is greater than the next object
1273 // hint. Iterate until we find the right object
1274 size_t nextObject = mNextObjectHint;
1275 do {
1276 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1277 // Requested info overlaps with an object
1278 ALOGE("Attempt to read from protected data in Parcel %p", this);
1279 return PERMISSION_DENIED;
1280 }
1281 nextObject++;
1282 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1283 mNextObjectHint = nextObject;
1284 }
1285 return NO_ERROR;
1286 }
1287 // Quickly determine if mObjects is sorted.
1288 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1289 binder_size_t* prevObj = currObj;
1290 while (currObj > mObjects) {
1291 prevObj--;
1292 if(*prevObj > *currObj) {
1293 goto data_unsorted;
1294 }
1295 currObj--;
1296 }
1297 mObjectsSorted = true;
1298 goto data_sorted;
1299
1300data_unsorted:
1301 // Insertion Sort mObjects
1302 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1303 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1304 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1305 binder_size_t temp = *iter0;
1306 binder_size_t* iter1 = iter0 - 1;
1307 while (iter1 >= mObjects && *iter1 > temp) {
1308 *(iter1 + 1) = *iter1;
1309 iter1--;
1310 }
1311 *(iter1 + 1) = temp;
1312 }
1313 mNextObjectHint = 0;
1314 mObjectsSorted = true;
1315 goto data_sorted;
1316}
1317
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001318status_t Parcel::read(void* outData, size_t len) const
1319{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001320 if (len > INT32_MAX) {
1321 // don't accept size_t values which may have come from an
1322 // inadvertent conversion from a negative int.
1323 return BAD_VALUE;
1324 }
1325
1326 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1327 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001328 if (mObjectsSize > 0) {
1329 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001330 if(err != NO_ERROR) {
1331 // Still increment the data position by the expected length
1332 mDataPos += pad_size(len);
1333 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1334 return err;
1335 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001336 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001337 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001338 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001339 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001340 return NO_ERROR;
1341 }
1342 return NOT_ENOUGH_DATA;
1343}
1344
1345const void* Parcel::readInplace(size_t len) const
1346{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001347 if (len > INT32_MAX) {
1348 // don't accept size_t values which may have come from an
1349 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -07001350 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001351 }
1352
1353 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1354 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001355 if (mObjectsSize > 0) {
1356 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001357 if(err != NO_ERROR) {
1358 // Still increment the data position by the expected length
1359 mDataPos += pad_size(len);
1360 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Xin Lif11e2bd2018-06-08 15:11:57 -07001361 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001362 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001363 }
1364
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001365 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001366 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001367 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001368 return data;
1369 }
Yi Kongfdd8da92018-06-07 17:52:27 -07001370 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001371}
1372
Andreas Huber84a6d042009-08-17 13:33:27 -07001373template<class T>
1374status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001375 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001376
1377 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001378 if (mObjectsSize > 0) {
1379 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001380 if(err != NO_ERROR) {
1381 // Still increment the data position by the expected length
1382 mDataPos += sizeof(T);
1383 return err;
1384 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001385 }
1386
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001387 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001388 mDataPos += sizeof(T);
1389 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001390 return NO_ERROR;
1391 } else {
1392 return NOT_ENOUGH_DATA;
1393 }
1394}
1395
Andreas Huber84a6d042009-08-17 13:33:27 -07001396template<class T>
1397T Parcel::readAligned() const {
1398 T result;
1399 if (readAligned(&result) != NO_ERROR) {
1400 result = 0;
1401 }
1402
1403 return result;
1404}
1405
1406template<class T>
1407status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001408 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001409
1410 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1411restart_write:
1412 *reinterpret_cast<T*>(mData+mDataPos) = val;
1413 return finishWrite(sizeof(val));
1414 }
1415
1416 status_t err = growData(sizeof(val));
1417 if (err == NO_ERROR) goto restart_write;
1418 return err;
1419}
1420
Casey Dahlin185d3442016-02-09 11:08:35 -08001421namespace {
1422
1423template<typename T>
1424status_t readByteVectorInternal(const Parcel* parcel,
1425 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001426 val->clear();
1427
1428 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001429 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001430
1431 if (status != OK) {
1432 return status;
1433 }
1434
Christopher Wiley4db672d2015-11-10 09:44:30 -08001435 if (size < 0) {
1436 status = UNEXPECTED_NULL;
1437 return status;
1438 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001439 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001440 status = BAD_VALUE;
1441 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001442 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001443
Paul Lietar433e87b2016-09-16 10:39:32 -07001444 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001445 if (!data) {
1446 status = BAD_VALUE;
1447 return status;
1448 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001449 val->reserve(size);
1450 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001451
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001452 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001453}
1454
Casey Dahlin185d3442016-02-09 11:08:35 -08001455template<typename T>
1456status_t readByteVectorInternalPtr(
1457 const Parcel* parcel,
1458 std::unique_ptr<std::vector<T>>* val) {
1459 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001460 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001461 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001462 val->reset();
1463
1464 if (status != OK || size < 0) {
1465 return status;
1466 }
1467
Casey Dahlin185d3442016-02-09 11:08:35 -08001468 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001469 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001470
Casey Dahlin185d3442016-02-09 11:08:35 -08001471 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001472
1473 if (status != OK) {
1474 val->reset();
1475 }
1476
1477 return status;
1478}
1479
Casey Dahlin185d3442016-02-09 11:08:35 -08001480} // namespace
1481
1482status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1483 return readByteVectorInternal(this, val);
1484}
1485
1486status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1487 return readByteVectorInternal(this, val);
1488}
1489
1490status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1491 return readByteVectorInternalPtr(this, val);
1492}
1493
1494status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1495 return readByteVectorInternalPtr(this, val);
1496}
1497
Casey Dahlinb9872622015-11-25 15:09:45 -08001498status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1499 return readNullableTypedVector(val, &Parcel::readInt32);
1500}
1501
Casey Dahlin451ff582015-10-19 18:12:18 -07001502status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001503 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001504}
1505
Casey Dahlinb9872622015-11-25 15:09:45 -08001506status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1507 return readNullableTypedVector(val, &Parcel::readInt64);
1508}
1509
Casey Dahlin451ff582015-10-19 18:12:18 -07001510status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001511 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001512}
1513
Casey Dahlinb9872622015-11-25 15:09:45 -08001514status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1515 return readNullableTypedVector(val, &Parcel::readFloat);
1516}
1517
Casey Dahlin451ff582015-10-19 18:12:18 -07001518status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001519 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001520}
1521
Casey Dahlinb9872622015-11-25 15:09:45 -08001522status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1523 return readNullableTypedVector(val, &Parcel::readDouble);
1524}
1525
Casey Dahlin451ff582015-10-19 18:12:18 -07001526status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001527 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001528}
1529
Casey Dahlinb9872622015-11-25 15:09:45 -08001530status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1531 const int32_t start = dataPosition();
1532 int32_t size;
1533 status_t status = readInt32(&size);
1534 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001535
Casey Dahlinb9872622015-11-25 15:09:45 -08001536 if (status != OK || size < 0) {
1537 return status;
1538 }
1539
1540 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001541 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001542
1543 status = readBoolVector(val->get());
1544
1545 if (status != OK) {
1546 val->reset();
1547 }
1548
1549 return status;
1550}
1551
1552status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001553 int32_t size;
1554 status_t status = readInt32(&size);
1555
1556 if (status != OK) {
1557 return status;
1558 }
1559
1560 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001561 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001562 }
1563
1564 val->resize(size);
1565
1566 /* C++ bool handling means a vector of bools isn't necessarily addressable
1567 * (we might use individual bits)
1568 */
Christopher Wiley97887982015-10-27 16:33:47 -07001569 bool data;
1570 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001571 status = readBool(&data);
1572 (*val)[i] = data;
1573
1574 if (status != OK) {
1575 return status;
1576 }
1577 }
1578
1579 return OK;
1580}
1581
Casey Dahlinb9872622015-11-25 15:09:45 -08001582status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1583 return readNullableTypedVector(val, &Parcel::readChar);
1584}
1585
Casey Dahlin451ff582015-10-19 18:12:18 -07001586status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001587 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001588}
1589
Casey Dahlinb9872622015-11-25 15:09:45 -08001590status_t Parcel::readString16Vector(
1591 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1592 return readNullableTypedVector(val, &Parcel::readString16);
1593}
1594
Casey Dahlin451ff582015-10-19 18:12:18 -07001595status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001596 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001597}
1598
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001599status_t Parcel::readUtf8VectorFromUtf16Vector(
1600 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1601 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1602}
1603
1604status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1605 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1606}
Casey Dahlin451ff582015-10-19 18:12:18 -07001607
Andreas Huber84a6d042009-08-17 13:33:27 -07001608status_t Parcel::readInt32(int32_t *pArg) const
1609{
1610 return readAligned(pArg);
1611}
1612
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001613int32_t Parcel::readInt32() const
1614{
Andreas Huber84a6d042009-08-17 13:33:27 -07001615 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001616}
1617
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001618status_t Parcel::readUint32(uint32_t *pArg) const
1619{
1620 return readAligned(pArg);
1621}
1622
1623uint32_t Parcel::readUint32() const
1624{
1625 return readAligned<uint32_t>();
1626}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001627
1628status_t Parcel::readInt64(int64_t *pArg) const
1629{
Andreas Huber84a6d042009-08-17 13:33:27 -07001630 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001631}
1632
1633
1634int64_t Parcel::readInt64() const
1635{
Andreas Huber84a6d042009-08-17 13:33:27 -07001636 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001637}
1638
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001639status_t Parcel::readUint64(uint64_t *pArg) const
1640{
1641 return readAligned(pArg);
1642}
1643
1644uint64_t Parcel::readUint64() const
1645{
1646 return readAligned<uint64_t>();
1647}
1648
Serban Constantinescuf683e012013-11-05 16:53:55 +00001649status_t Parcel::readPointer(uintptr_t *pArg) const
1650{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001651 status_t ret;
1652 binder_uintptr_t ptr;
1653 ret = readAligned(&ptr);
1654 if (!ret)
1655 *pArg = ptr;
1656 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001657}
1658
1659uintptr_t Parcel::readPointer() const
1660{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001661 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001662}
1663
1664
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001665status_t Parcel::readFloat(float *pArg) const
1666{
Andreas Huber84a6d042009-08-17 13:33:27 -07001667 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001668}
1669
1670
1671float Parcel::readFloat() const
1672{
Andreas Huber84a6d042009-08-17 13:33:27 -07001673 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001674}
1675
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001676#if defined(__mips__) && defined(__mips_hard_float)
1677
1678status_t Parcel::readDouble(double *pArg) const
1679{
1680 union {
1681 double d;
1682 unsigned long long ll;
1683 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001684 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001685 status_t status;
1686 status = readAligned(&u.ll);
1687 *pArg = u.d;
1688 return status;
1689}
1690
1691double Parcel::readDouble() const
1692{
1693 union {
1694 double d;
1695 unsigned long long ll;
1696 } u;
1697 u.ll = readAligned<unsigned long long>();
1698 return u.d;
1699}
1700
1701#else
1702
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001703status_t Parcel::readDouble(double *pArg) const
1704{
Andreas Huber84a6d042009-08-17 13:33:27 -07001705 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001706}
1707
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001708double Parcel::readDouble() const
1709{
Andreas Huber84a6d042009-08-17 13:33:27 -07001710 return readAligned<double>();
1711}
1712
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001713#endif
1714
Andreas Huber84a6d042009-08-17 13:33:27 -07001715status_t Parcel::readIntPtr(intptr_t *pArg) const
1716{
1717 return readAligned(pArg);
1718}
1719
1720
1721intptr_t Parcel::readIntPtr() const
1722{
1723 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001724}
1725
Casey Dahlind6848f52015-10-15 15:44:59 -07001726status_t Parcel::readBool(bool *pArg) const
1727{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001728 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001729 status_t ret = readInt32(&tmp);
1730 *pArg = (tmp != 0);
1731 return ret;
1732}
1733
1734bool Parcel::readBool() const
1735{
1736 return readInt32() != 0;
1737}
1738
1739status_t Parcel::readChar(char16_t *pArg) const
1740{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001741 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001742 status_t ret = readInt32(&tmp);
1743 *pArg = char16_t(tmp);
1744 return ret;
1745}
1746
1747char16_t Parcel::readChar() const
1748{
1749 return char16_t(readInt32());
1750}
1751
1752status_t Parcel::readByte(int8_t *pArg) const
1753{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001754 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001755 status_t ret = readInt32(&tmp);
1756 *pArg = int8_t(tmp);
1757 return ret;
1758}
1759
1760int8_t Parcel::readByte() const
1761{
1762 return int8_t(readInt32());
1763}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001764
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001765status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1766 size_t utf16Size = 0;
1767 const char16_t* src = readString16Inplace(&utf16Size);
1768 if (!src) {
1769 return UNEXPECTED_NULL;
1770 }
1771
1772 // Save ourselves the trouble, we're done.
1773 if (utf16Size == 0u) {
1774 str->clear();
1775 return NO_ERROR;
1776 }
1777
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001778 // Allow for closing '\0'
1779 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1780 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001781 return BAD_VALUE;
1782 }
1783 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001784 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001785 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001786 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1787 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001788 return NO_ERROR;
1789}
1790
1791status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1792 const int32_t start = dataPosition();
1793 int32_t size;
1794 status_t status = readInt32(&size);
1795 str->reset();
1796
1797 if (status != OK || size < 0) {
1798 return status;
1799 }
1800
1801 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001802 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001803 return readUtf8FromUtf16(str->get());
1804}
1805
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001806const char* Parcel::readCString() const
1807{
1808 const size_t avail = mDataSize-mDataPos;
1809 if (avail > 0) {
1810 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1811 // is the string's trailing NUL within the parcel's valid bounds?
1812 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1813 if (eos) {
1814 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001815 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001816 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001817 return str;
1818 }
1819 }
Yi Kongfdd8da92018-06-07 17:52:27 -07001820 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001821}
1822
1823String8 Parcel::readString8() const
1824{
Roshan Pius87b64d22016-07-18 12:51:02 -07001825 String8 retString;
1826 status_t status = readString8(&retString);
1827 if (status != OK) {
1828 // We don't care about errors here, so just return an empty string.
1829 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001830 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001831 return retString;
1832}
1833
1834status_t Parcel::readString8(String8* pArg) const
1835{
1836 int32_t size;
1837 status_t status = readInt32(&size);
1838 if (status != OK) {
1839 return status;
1840 }
1841 // watch for potential int overflow from size+1
1842 if (size < 0 || size >= INT32_MAX) {
1843 return BAD_VALUE;
1844 }
1845 // |writeString8| writes nothing for empty string.
1846 if (size == 0) {
1847 *pArg = String8();
1848 return OK;
1849 }
1850 const char* str = (const char*)readInplace(size + 1);
Yi Kongfdd8da92018-06-07 17:52:27 -07001851 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07001852 return BAD_VALUE;
1853 }
1854 pArg->setTo(str, size);
1855 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001856}
1857
1858String16 Parcel::readString16() const
1859{
1860 size_t len;
1861 const char16_t* str = readString16Inplace(&len);
1862 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001863 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001864 return String16();
1865}
1866
Casey Dahlinb9872622015-11-25 15:09:45 -08001867status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1868{
1869 const int32_t start = dataPosition();
1870 int32_t size;
1871 status_t status = readInt32(&size);
1872 pArg->reset();
1873
1874 if (status != OK || size < 0) {
1875 return status;
1876 }
1877
1878 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001879 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001880
1881 status = readString16(pArg->get());
1882
1883 if (status != OK) {
1884 pArg->reset();
1885 }
1886
1887 return status;
1888}
1889
Casey Dahlin451ff582015-10-19 18:12:18 -07001890status_t Parcel::readString16(String16* pArg) const
1891{
1892 size_t len;
1893 const char16_t* str = readString16Inplace(&len);
1894 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001895 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001896 return 0;
1897 } else {
1898 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001899 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001900 }
1901}
1902
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001903const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1904{
1905 int32_t size = readInt32();
1906 // watch for potential int overflow from size+1
1907 if (size >= 0 && size < INT32_MAX) {
1908 *outLen = size;
1909 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07001910 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001911 return str;
1912 }
1913 }
1914 *outLen = 0;
Yi Kongfdd8da92018-06-07 17:52:27 -07001915 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001916}
1917
Casey Dahlinf0c13772015-10-27 18:33:56 -07001918status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1919{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001920 status_t status = readNullableStrongBinder(val);
1921 if (status == OK && !val->get()) {
1922 status = UNEXPECTED_NULL;
1923 }
1924 return status;
1925}
1926
1927status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1928{
Steven Morelanddea3cf92019-07-16 18:06:55 -07001929 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001930}
1931
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001932sp<IBinder> Parcel::readStrongBinder() const
1933{
1934 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001935 // Note that a lot of code in Android reads binders by hand with this
1936 // method, and that code has historically been ok with getting nullptr
1937 // back (while ignoring error codes).
1938 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001939 return val;
1940}
1941
Christopher Wiley97f048d2015-11-19 06:49:05 -08001942status_t Parcel::readParcelable(Parcelable* parcelable) const {
1943 int32_t have_parcelable = 0;
1944 status_t status = readInt32(&have_parcelable);
1945 if (status != OK) {
1946 return status;
1947 }
1948 if (!have_parcelable) {
1949 return UNEXPECTED_NULL;
1950 }
1951 return parcelable->readFromParcel(this);
1952}
1953
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001954int32_t Parcel::readExceptionCode() const
1955{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001956 binder::Status status;
1957 status.readFromParcel(*this);
1958 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001959}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001960
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001961native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001962{
1963 int numFds, numInts;
1964 status_t err;
1965 err = readInt32(&numFds);
Yi Kongfdd8da92018-06-07 17:52:27 -07001966 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001967 err = readInt32(&numInts);
Yi Kongfdd8da92018-06-07 17:52:27 -07001968 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001969
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001970 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001971 if (!h) {
Yi Kongfdd8da92018-06-07 17:52:27 -07001972 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001973 }
1974
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001975 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001976 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001977 if (h->data[i] < 0) {
1978 for (int j = 0; j < i; j++) {
1979 close(h->data[j]);
1980 }
1981 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07001982 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001983 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001984 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001985 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001986 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001987 native_handle_close(h);
1988 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07001989 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001990 }
1991 return h;
1992}
1993
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001994int Parcel::readFileDescriptor() const
1995{
1996 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001997
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001998 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001999 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002000 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002001
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002002 return BAD_TYPE;
2003}
2004
Dianne Hackborn1941a402016-08-29 12:30:43 -07002005int Parcel::readParcelFileDescriptor() const
2006{
2007 int32_t hasComm = readInt32();
2008 int fd = readFileDescriptor();
2009 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002010 // detach (owned by the binder driver)
2011 int comm = readFileDescriptor();
2012
2013 // warning: this must be kept in sync with:
2014 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2015 enum ParcelFileDescriptorStatus {
2016 DETACHED = 2,
2017 };
2018
2019#if BYTE_ORDER == BIG_ENDIAN
2020 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2021#endif
2022#if BYTE_ORDER == LITTLE_ENDIAN
2023 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2024#endif
2025
2026 ssize_t written = TEMP_FAILURE_RETRY(
2027 ::write(comm, &message, sizeof(message)));
2028
2029 if (written == -1 || written != sizeof(message)) {
2030 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2031 written, strerror(errno));
2032 return BAD_TYPE;
2033 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002034 }
2035 return fd;
2036}
2037
Christopher Wiley2cf19952016-04-11 11:09:37 -07002038status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002039{
2040 int got = readFileDescriptor();
2041
2042 if (got == BAD_TYPE) {
2043 return BAD_TYPE;
2044 }
2045
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002046 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002047
2048 if (val->get() < 0) {
2049 return BAD_VALUE;
2050 }
2051
2052 return OK;
2053}
2054
Ryo Hashimotobf551892018-05-31 16:58:35 +09002055status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2056{
2057 int got = readParcelFileDescriptor();
2058
2059 if (got == BAD_TYPE) {
2060 return BAD_TYPE;
2061 }
2062
2063 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2064
2065 if (val->get() < 0) {
2066 return BAD_VALUE;
2067 }
2068
2069 return OK;
2070}
Casey Dahlin06673e32015-11-23 13:24:23 -08002071
Christopher Wiley2cf19952016-04-11 11:09:37 -07002072status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002073 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2074}
2075
Christopher Wiley2cf19952016-04-11 11:09:37 -07002076status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002077 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2078}
2079
Jeff Brown5707dbf2011-09-23 21:17:56 -07002080status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2081{
Jeff Brown13b16042014-11-11 16:44:25 -08002082 int32_t blobType;
2083 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002084 if (status) return status;
2085
Jeff Brown13b16042014-11-11 16:44:25 -08002086 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002087 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002088 const void* ptr = readInplace(len);
2089 if (!ptr) return BAD_VALUE;
2090
Jeff Brown13b16042014-11-11 16:44:25 -08002091 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002092 return NO_ERROR;
2093 }
2094
Steve Block6807e592011-10-20 11:56:00 +01002095 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002096 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002097 int fd = readFileDescriptor();
2098 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2099
Yi Kongfdd8da92018-06-07 17:52:27 -07002100 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002101 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002102 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002103
Jeff Brown13b16042014-11-11 16:44:25 -08002104 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002105 return NO_ERROR;
2106}
2107
Mathias Agopiane1424282013-07-29 21:24:40 -07002108status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002109{
2110 // size
2111 const size_t len = this->readInt32();
2112 const size_t fd_count = this->readInt32();
2113
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002114 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002115 // don't accept size_t values which may have come from an
2116 // inadvertent conversion from a negative int.
2117 return BAD_VALUE;
2118 }
2119
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002120 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002121 void const* const buf = this->readInplace(pad_size(len));
Yi Kongfdd8da92018-06-07 17:52:27 -07002122 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002123 return BAD_VALUE;
2124
Yi Kongfdd8da92018-06-07 17:52:27 -07002125 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002126 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002127 fds = new (std::nothrow) int[fd_count];
2128 if (fds == nullptr) {
2129 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2130 return BAD_VALUE;
2131 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002132 }
2133
2134 status_t err = NO_ERROR;
2135 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002136 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002137 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002138 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002139 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 -07002140 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2141 // Close all the file descriptors that were dup-ed.
2142 for (size_t j=0; j<i ;j++) {
2143 close(fds[j]);
2144 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002145 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002146 }
2147
2148 if (err == NO_ERROR) {
2149 err = val.unflatten(buf, len, fds, fd_count);
2150 }
2151
2152 if (fd_count) {
2153 delete [] fds;
2154 }
2155
2156 return err;
2157}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002158const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2159{
2160 const size_t DPOS = mDataPos;
2161 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2162 const flat_binder_object* obj
2163 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2164 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002165 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002166 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002167 // the object list, so we don't want to check for it when
2168 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002169 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002170 return obj;
2171 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002172
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002173 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002174 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002175 const size_t N = mObjectsSize;
2176 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002177
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002178 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002179 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002180 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002181
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002182 // Start at the current hint position, looking for an object at
2183 // the current data position.
2184 if (opos < N) {
2185 while (opos < (N-1) && OBJS[opos] < DPOS) {
2186 opos++;
2187 }
2188 } else {
2189 opos = N-1;
2190 }
2191 if (OBJS[opos] == DPOS) {
2192 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002193 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002194 this, DPOS, opos);
2195 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002196 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002197 return obj;
2198 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002199
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002200 // Look backwards for it...
2201 while (opos > 0 && OBJS[opos] > DPOS) {
2202 opos--;
2203 }
2204 if (OBJS[opos] == DPOS) {
2205 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002206 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002207 this, DPOS, opos);
2208 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002209 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002210 return obj;
2211 }
2212 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002213 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 -07002214 this, DPOS);
2215 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002216 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002217}
2218
2219void Parcel::closeFileDescriptors()
2220{
2221 size_t i = mObjectsSize;
2222 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002223 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002224 }
2225 while (i > 0) {
2226 i--;
2227 const flat_binder_object* flat
2228 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002229 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002230 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231 close(flat->handle);
2232 }
2233 }
2234}
2235
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002236uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002237{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002238 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002239}
2240
2241size_t Parcel::ipcDataSize() const
2242{
2243 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2244}
2245
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002246uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002247{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002248 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002249}
2250
2251size_t Parcel::ipcObjectsCount() const
2252{
2253 return mObjectsSize;
2254}
2255
2256void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002257 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002258{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002259 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002260 freeDataNoInit();
2261 mError = NO_ERROR;
2262 mData = const_cast<uint8_t*>(data);
2263 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002264 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002265 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002266 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002267 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002268 mObjectsSize = mObjectsCapacity = objectsCount;
2269 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002270 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002271 mOwner = relFunc;
2272 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002273 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002274 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002275 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002276 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002277 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002278 mObjectsSize = 0;
2279 break;
2280 }
2281 minOffset = offset + sizeof(flat_binder_object);
2282 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002283 scanForFds();
2284}
2285
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002286void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002287{
2288 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002289
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002290 if (errorCheck() != NO_ERROR) {
2291 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002292 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002293 } else if (dataSize() > 0) {
2294 const uint8_t* DATA = data();
2295 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002296 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002297 const size_t N = objectsCount();
2298 for (size_t i=0; i<N; i++) {
2299 const flat_binder_object* flat
2300 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2301 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002302 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002303 << " = " << flat->binder;
2304 }
2305 } else {
2306 to << "NULL";
2307 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002308
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002309 to << ")";
2310}
2311
2312void Parcel::releaseObjects()
2313{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002314 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002315 if (i == 0) {
2316 return;
2317 }
2318 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002319 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002320 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002321 while (i > 0) {
2322 i--;
2323 const flat_binder_object* flat
2324 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002325 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002326 }
2327}
2328
2329void Parcel::acquireObjects()
2330{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002331 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002332 if (i == 0) {
2333 return;
2334 }
2335 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002336 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002337 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002338 while (i > 0) {
2339 i--;
2340 const flat_binder_object* flat
2341 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002342 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002343 }
2344}
2345
2346void Parcel::freeData()
2347{
2348 freeDataNoInit();
2349 initState();
2350}
2351
2352void Parcel::freeDataNoInit()
2353{
2354 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002355 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002356 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002357 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2358 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002359 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002360 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002361 if (mData) {
2362 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002363 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002364 if (mDataCapacity <= gParcelGlobalAllocSize) {
2365 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2366 } else {
2367 gParcelGlobalAllocSize = 0;
2368 }
2369 if (gParcelGlobalAllocCount > 0) {
2370 gParcelGlobalAllocCount--;
2371 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002372 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002373 free(mData);
2374 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002375 if (mObjects) free(mObjects);
2376 }
2377}
2378
2379status_t Parcel::growData(size_t len)
2380{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002381 if (len > INT32_MAX) {
2382 // don't accept size_t values which may have come from an
2383 // inadvertent conversion from a negative int.
2384 return BAD_VALUE;
2385 }
2386
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002387 size_t newSize = ((mDataSize+len)*3)/2;
2388 return (newSize <= mDataSize)
2389 ? (status_t) NO_MEMORY
2390 : continueWrite(newSize);
2391}
2392
2393status_t Parcel::restartWrite(size_t desired)
2394{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002395 if (desired > INT32_MAX) {
2396 // don't accept size_t values which may have come from an
2397 // inadvertent conversion from a negative int.
2398 return BAD_VALUE;
2399 }
2400
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002401 if (mOwner) {
2402 freeData();
2403 return continueWrite(desired);
2404 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002405
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002406 uint8_t* data = (uint8_t*)realloc(mData, desired);
2407 if (!data && desired > mDataCapacity) {
2408 mError = NO_MEMORY;
2409 return NO_MEMORY;
2410 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002411
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002412 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002413
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002414 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002415 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002416 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002417 gParcelGlobalAllocSize += desired;
2418 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002419 if (!mData) {
2420 gParcelGlobalAllocCount++;
2421 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002422 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002423 mData = data;
2424 mDataCapacity = desired;
2425 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002426
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002428 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2429 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2430
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002431 free(mObjects);
Yi Kongfdd8da92018-06-07 17:52:27 -07002432 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002433 mObjectsSize = mObjectsCapacity = 0;
2434 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002435 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002436 mHasFds = false;
2437 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002438 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002439
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002440 return NO_ERROR;
2441}
2442
2443status_t Parcel::continueWrite(size_t desired)
2444{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002445 if (desired > INT32_MAX) {
2446 // don't accept size_t values which may have come from an
2447 // inadvertent conversion from a negative int.
2448 return BAD_VALUE;
2449 }
2450
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002451 // If shrinking, first adjust for any objects that appear
2452 // after the new data size.
2453 size_t objectsSize = mObjectsSize;
2454 if (desired < mDataSize) {
2455 if (desired == 0) {
2456 objectsSize = 0;
2457 } else {
2458 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002459 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002460 break;
2461 objectsSize--;
2462 }
2463 }
2464 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002465
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002466 if (mOwner) {
2467 // If the size is going to zero, just release the owner's data.
2468 if (desired == 0) {
2469 freeData();
2470 return NO_ERROR;
2471 }
2472
2473 // If there is a different owner, we need to take
2474 // posession.
2475 uint8_t* data = (uint8_t*)malloc(desired);
2476 if (!data) {
2477 mError = NO_MEMORY;
2478 return NO_MEMORY;
2479 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002480 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002481
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002482 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002483 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002484 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002485 free(data);
2486
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002487 mError = NO_MEMORY;
2488 return NO_MEMORY;
2489 }
2490
2491 // Little hack to only acquire references on objects
2492 // we will be keeping.
2493 size_t oldObjectsSize = mObjectsSize;
2494 mObjectsSize = objectsSize;
2495 acquireObjects();
2496 mObjectsSize = oldObjectsSize;
2497 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002498
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002499 if (mData) {
2500 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2501 }
2502 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002503 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002504 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002505 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002506 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kongfdd8da92018-06-07 17:52:27 -07002507 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002509 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002510 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002511 gParcelGlobalAllocSize += desired;
2512 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002513 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002514
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002515 mData = data;
2516 mObjects = objects;
2517 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002518 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002519 mDataCapacity = desired;
2520 mObjectsSize = mObjectsCapacity = objectsSize;
2521 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002522 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002523
2524 } else if (mData) {
2525 if (objectsSize < mObjectsSize) {
2526 // Need to release refs on any objects we are dropping.
2527 const sp<ProcessState> proc(ProcessState::self());
2528 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2529 const flat_binder_object* flat
2530 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002531 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002532 // will need to rescan because we may have lopped off the only FDs
2533 mFdsKnown = false;
2534 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002535 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002536 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002537 binder_size_t* objects =
2538 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002539 if (objects) {
2540 mObjects = objects;
2541 }
2542 mObjectsSize = objectsSize;
2543 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002544 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002545 }
2546
2547 // We own the data, so we can just do a realloc().
2548 if (desired > mDataCapacity) {
2549 uint8_t* data = (uint8_t*)realloc(mData, desired);
2550 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002551 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2552 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002553 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002554 gParcelGlobalAllocSize += desired;
2555 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002556 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002557 mData = data;
2558 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002559 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002560 mError = NO_MEMORY;
2561 return NO_MEMORY;
2562 }
2563 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002564 if (mDataSize > desired) {
2565 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002566 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002567 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002568 if (mDataPos > desired) {
2569 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002570 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002571 }
2572 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002573
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002574 } else {
2575 // This is the first data. Easy!
2576 uint8_t* data = (uint8_t*)malloc(desired);
2577 if (!data) {
2578 mError = NO_MEMORY;
2579 return NO_MEMORY;
2580 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002581
Yi Kongfdd8da92018-06-07 17:52:27 -07002582 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002583 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002584 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002585 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002586
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002587 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002588 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002589 gParcelGlobalAllocSize += desired;
2590 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002591 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002592
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002593 mData = data;
2594 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002595 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2596 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002597 mDataCapacity = desired;
2598 }
2599
2600 return NO_ERROR;
2601}
2602
2603void Parcel::initState()
2604{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002605 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002606 mError = NO_ERROR;
Yi Kongfdd8da92018-06-07 17:52:27 -07002607 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002608 mDataSize = 0;
2609 mDataCapacity = 0;
2610 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002611 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2612 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kongfdd8da92018-06-07 17:52:27 -07002613 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002614 mObjectsSize = 0;
2615 mObjectsCapacity = 0;
2616 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002617 mObjectsSorted = false;
Steven Morelanddea3cf92019-07-16 18:06:55 -07002618 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002619 mHasFds = false;
2620 mFdsKnown = true;
Steven Morelanddea3cf92019-07-16 18:06:55 -07002621 mRequiredStability = internal::Stability::UNDECLARED;
Yi Kongfdd8da92018-06-07 17:52:27 -07002622 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002623 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002624
2625 // racing multiple init leads only to multiple identical write
2626 if (gMaxFds == 0) {
2627 struct rlimit result;
2628 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2629 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002630 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002631 } else {
2632 ALOGW("Unable to getrlimit: %s", strerror(errno));
2633 gMaxFds = 1024;
2634 }
2635 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002636}
2637
2638void Parcel::scanForFds() const
2639{
2640 bool hasFds = false;
2641 for (size_t i=0; i<mObjectsSize; i++) {
2642 const flat_binder_object* flat
2643 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002644 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002645 hasFds = true;
2646 break;
2647 }
2648 }
2649 mHasFds = hasFds;
2650 mFdsKnown = true;
2651}
2652
Dan Sandleraa5c2342015-04-10 10:08:45 -04002653size_t Parcel::getBlobAshmemSize() const
2654{
Adrian Roos6bb31142015-10-22 16:46:12 -07002655 // This used to return the size of all blobs that were written to ashmem, now we're returning
2656 // the ashmem currently referenced by this Parcel, which should be equivalent.
2657 // TODO: Remove method once ABI can be changed.
2658 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002659}
2660
Adrian Rooscbf37262015-10-22 16:12:53 -07002661size_t Parcel::getOpenAshmemSize() const
2662{
2663 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002664}
2665
2666// --- Parcel::Blob ---
2667
2668Parcel::Blob::Blob() :
Yi Kongfdd8da92018-06-07 17:52:27 -07002669 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002670}
2671
2672Parcel::Blob::~Blob() {
2673 release();
2674}
2675
2676void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002677 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002678 ::munmap(mData, mSize);
2679 }
2680 clear();
2681}
2682
Jeff Brown13b16042014-11-11 16:44:25 -08002683void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2684 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002685 mData = data;
2686 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002687 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002688}
2689
2690void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002691 mFd = -1;
Yi Kongfdd8da92018-06-07 17:52:27 -07002692 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002693 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002694 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002695}
2696
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002697}; // namespace android