blob: 82053ad2caf3473665b92cfd77f43cff8c8d33b0 [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 Sharkey05827be2018-06-26 10:52:38 -060076#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070077
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070078namespace android {
79
Steven Moreland7b102262019-08-01 15:48:43 -070080// many things compile this into prebuilts on the stack
81static_assert(sizeof(Parcel) == 60 || sizeof(Parcel) == 120);
82
Dianne Hackborna4cff882014-11-13 17:07:40 -080083static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
84static size_t gParcelGlobalAllocSize = 0;
85static size_t gParcelGlobalAllocCount = 0;
86
Christopher Tatee4e0ae82016-03-24 16:03:44 -070087static size_t gMaxFds = 0;
88
Jeff Brown13b16042014-11-11 16:44:25 -080089// Maximum size of a blob to transfer in-place.
90static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
91
92enum {
93 BLOB_INPLACE = 0,
94 BLOB_ASHMEM_IMMUTABLE = 1,
95 BLOB_ASHMEM_MUTABLE = 2,
96};
97
Steven Morelandb1c81202019-04-05 18:49:55 -070098static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070099 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700100{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700101 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700102 case BINDER_TYPE_BINDER:
103 if (obj.binder) {
104 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800105 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106 }
107 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108 case BINDER_TYPE_HANDLE: {
109 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700110 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700111 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
112 b->incStrong(who);
113 }
114 return;
115 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700116 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000117 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700118 // If we own an ashmem fd, keep track of how much memory it refers to.
119 int size = ashmem_get_size_region(obj.handle);
120 if (size > 0) {
121 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700122 }
123 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700124 return;
125 }
126 }
127
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700128 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700129}
130
Adrian Roos6bb31142015-10-22 16:46:12 -0700131static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700132 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700133{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700134 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700135 case BINDER_TYPE_BINDER:
136 if (obj.binder) {
137 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800138 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 }
140 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700141 case BINDER_TYPE_HANDLE: {
142 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700143 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
145 b->decStrong(who);
146 }
147 return;
148 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700149 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800150 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000151 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700152 int size = ashmem_get_size_region(obj.handle);
153 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800154 // ashmem size might have changed since last time it was accounted for, e.g.
155 // in acquire_object(). Value of *outAshmemSize is not critical since we are
156 // releasing the object anyway. Check for integer overflow condition.
157 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700158 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700159 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800160
161 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700162 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700163 return;
164 }
165 }
166
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700167 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700168}
169
Steven Morelanda86a3562019-08-01 23:28:34 +0000170status_t Parcel::finishFlattenBinder(
171 const sp<IBinder>& binder, const flat_binder_object& flat)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700172{
Steven Moreland05929552019-07-31 17:51:25 -0700173 internal::Stability::tryMarkCompilationUnit(binder.get());
174
Steven Morelanda86a3562019-08-01 23:28:34 +0000175 status_t status = writeObject(flat, false);
176 if (status != OK) return status;
177
178 return writeInt32(internal::Stability::get(binder.get()));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700179}
180
Steven Morelanda86a3562019-08-01 23:28:34 +0000181status_t Parcel::finishUnflattenBinder(
182 const sp<IBinder>& binder, sp<IBinder>* out) const
183{
184 int32_t stability;
185 status_t status = readInt32(&stability);
186 if (status != OK) return status;
187
Steven Moreland05929552019-07-31 17:51:25 -0700188 if (binder != nullptr && !internal::Stability::check(stability, mRequiredStability)) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000189 return BAD_TYPE;
190 }
191
Steven Moreland05929552019-07-31 17:51:25 -0700192 status = internal::Stability::set(binder.get(), stability, true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000193 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 Kong91635562018-06-07 14:38:36 -0700211 if (binder != nullptr) {
Steven Morelandf0212002018-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 Kong91635562018-06-07 14:38:36 -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 Morelandf0212002018-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 Morelanda86a3562019-08-01 23:28:34 +0000237 return finishFlattenBinder(binder, obj);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238}
239
Steven Morelanda86a3562019-08-01 23:28:34 +0000240status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700241{
Steven Morelanda86a3562019-08-01 23:28:34 +0000242 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 Morelanda86a3562019-08-01 23:28:34 +0000246 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 Morelanda86a3562019-08-01 23:28:34 +0000359void 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 Kong91635562018-06-07 14:38:36 -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
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000511void Parcel::updateWorkSourceRequestHeaderPosition() const {
512 // Only update the request headers once. We only want to point
513 // to the first headers read/written.
514 if (!mRequestHeaderPresent) {
515 mWorkSourceRequestHeaderPosition = dataPosition();
516 mRequestHeaderPresent = true;
517 }
518}
519
Steven Morelandd70160f2019-07-23 10:20:38 -0700520#ifdef __ANDROID_VNDK__
521constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
522#else
523constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
524#endif
525
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700526// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700527status_t Parcel::writeInterfaceToken(const String16& interface)
528{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000529 const IPCThreadState* threadState = IPCThreadState::self();
530 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000531 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000532 writeInt32(threadState->shouldPropagateWorkSource() ?
533 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700534 writeInt32(kHeader);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700535 // currently the interface identification token is just its name as a string
536 return writeString16(interface);
537}
538
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000539bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
540{
541 if (!mRequestHeaderPresent) {
542 return false;
543 }
544
545 const size_t initialPosition = dataPosition();
546 setDataPosition(mWorkSourceRequestHeaderPosition);
547 status_t err = writeInt32(uid);
548 setDataPosition(initialPosition);
549 return err == NO_ERROR;
550}
551
552uid_t Parcel::readCallingWorkSourceUid()
553{
554 if (!mRequestHeaderPresent) {
555 return IPCThreadState::kUnsetWorkSource;
556 }
557
558 const size_t initialPosition = dataPosition();
559 setDataPosition(mWorkSourceRequestHeaderPosition);
560 uid_t uid = readInt32();
561 setDataPosition(initialPosition);
562 return uid;
563}
564
Mathias Agopian83c04462009-05-22 19:00:22 -0700565bool Parcel::checkInterface(IBinder* binder) const
566{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700567 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700568}
569
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700570bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700571 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700572{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100573 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700574 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700575 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700576 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700577 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700578 if ((threadState->getLastTransactionBinderFlags() &
579 IBinder::FLAG_ONEWAY) != 0) {
580 // For one-way calls, the callee is running entirely
581 // disconnected from the caller, so disable StrictMode entirely.
582 // Not only does disk/network usage not impact the caller, but
583 // there's no way to commuicate back any violations anyway.
584 threadState->setStrictModePolicy(0);
585 } else {
586 threadState->setStrictModePolicy(strictPolicy);
587 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100588 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000589 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100590 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000591 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700592 // vendor header
593 int32_t header = readInt32();
594 if (header != kHeader) {
595 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
596 return false;
597 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100598 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700599 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700600 if (str == interface) {
601 return true;
602 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700603 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700604 String8(interface).string(), String8(str).string());
605 return false;
606 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700607}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700608
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700609size_t Parcel::objectsCount() const
610{
611 return mObjectsSize;
612}
613
614status_t Parcel::errorCheck() const
615{
616 return mError;
617}
618
619void Parcel::setError(status_t err)
620{
621 mError = err;
622}
623
624status_t Parcel::finishWrite(size_t len)
625{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700626 if (len > INT32_MAX) {
627 // don't accept size_t values which may have come from an
628 // inadvertent conversion from a negative int.
629 return BAD_VALUE;
630 }
631
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700632 //printf("Finish write of %d\n", len);
633 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700634 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700635 if (mDataPos > mDataSize) {
636 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700637 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700638 }
639 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
640 return NO_ERROR;
641}
642
643status_t Parcel::writeUnpadded(const void* data, size_t len)
644{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700645 if (len > INT32_MAX) {
646 // don't accept size_t values which may have come from an
647 // inadvertent conversion from a negative int.
648 return BAD_VALUE;
649 }
650
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700651 size_t end = mDataPos + len;
652 if (end < mDataPos) {
653 // integer overflow
654 return BAD_VALUE;
655 }
656
657 if (end <= mDataCapacity) {
658restart_write:
659 memcpy(mData+mDataPos, data, len);
660 return finishWrite(len);
661 }
662
663 status_t err = growData(len);
664 if (err == NO_ERROR) goto restart_write;
665 return err;
666}
667
668status_t Parcel::write(const void* data, size_t len)
669{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700670 if (len > INT32_MAX) {
671 // don't accept size_t values which may have come from an
672 // inadvertent conversion from a negative int.
673 return BAD_VALUE;
674 }
675
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700676 void* const d = writeInplace(len);
677 if (d) {
678 memcpy(d, data, len);
679 return NO_ERROR;
680 }
681 return mError;
682}
683
684void* Parcel::writeInplace(size_t len)
685{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700686 if (len > INT32_MAX) {
687 // don't accept size_t values which may have come from an
688 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700689 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700690 }
691
692 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700693
694 // sanity check for integer overflow
695 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700696 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700697 }
698
699 if ((mDataPos+padded) <= mDataCapacity) {
700restart_write:
701 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
702 uint8_t* const data = mData+mDataPos;
703
704 // Need to pad at end?
705 if (padded != len) {
706#if BYTE_ORDER == BIG_ENDIAN
707 static const uint32_t mask[4] = {
708 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
709 };
710#endif
711#if BYTE_ORDER == LITTLE_ENDIAN
712 static const uint32_t mask[4] = {
713 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
714 };
715#endif
716 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
717 // *reinterpret_cast<void**>(data+padded-4));
718 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
719 }
720
721 finishWrite(padded);
722 return data;
723 }
724
725 status_t err = growData(padded);
726 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700727 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700728}
729
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800730status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
731 const uint8_t* strData = (uint8_t*)str.data();
732 const size_t strLen= str.length();
733 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100734 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800735 return BAD_VALUE;
736 }
737
738 status_t err = writeInt32(utf16Len);
739 if (err) {
740 return err;
741 }
742
743 // Allocate enough bytes to hold our converted string and its terminating NULL.
744 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
745 if (!dst) {
746 return NO_MEMORY;
747 }
748
Sergio Girof4607432016-07-21 14:46:35 +0100749 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800750
751 return NO_ERROR;
752}
753
754status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
755 if (!str) {
756 return writeInt32(-1);
757 }
758 return writeUtf8AsUtf16(*str);
759}
760
Casey Dahlin185d3442016-02-09 11:08:35 -0800761namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800762
Casey Dahlin185d3442016-02-09 11:08:35 -0800763template<typename T>
764status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700765{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700766 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700767 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700768 status = BAD_VALUE;
769 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700770 }
771
Casey Dahlin185d3442016-02-09 11:08:35 -0800772 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700773 if (status != OK) {
774 return status;
775 }
776
Casey Dahlin185d3442016-02-09 11:08:35 -0800777 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700778 if (!data) {
779 status = BAD_VALUE;
780 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700781 }
782
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700783 memcpy(data, val.data(), val.size());
784 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700785}
786
Casey Dahlin185d3442016-02-09 11:08:35 -0800787template<typename T>
788status_t writeByteVectorInternalPtr(Parcel* parcel,
789 const std::unique_ptr<std::vector<T>>& val)
790{
791 if (!val) {
792 return parcel->writeInt32(-1);
793 }
794
795 return writeByteVectorInternal(parcel, *val);
796}
797
798} // namespace
799
800status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
801 return writeByteVectorInternal(this, val);
802}
803
804status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
805{
806 return writeByteVectorInternalPtr(this, val);
807}
808
809status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
810 return writeByteVectorInternal(this, val);
811}
812
813status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
814{
815 return writeByteVectorInternalPtr(this, val);
816}
817
Casey Dahlin451ff582015-10-19 18:12:18 -0700818status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
819{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800820 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700821}
822
Casey Dahlinb9872622015-11-25 15:09:45 -0800823status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
824{
825 return writeNullableTypedVector(val, &Parcel::writeInt32);
826}
827
Casey Dahlin451ff582015-10-19 18:12:18 -0700828status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
829{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800830 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700831}
832
Casey Dahlinb9872622015-11-25 15:09:45 -0800833status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
834{
835 return writeNullableTypedVector(val, &Parcel::writeInt64);
836}
837
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800838status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
839{
840 return writeTypedVector(val, &Parcel::writeUint64);
841}
842
843status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
844{
845 return writeNullableTypedVector(val, &Parcel::writeUint64);
846}
847
Casey Dahlin451ff582015-10-19 18:12:18 -0700848status_t Parcel::writeFloatVector(const std::vector<float>& val)
849{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800850 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700851}
852
Casey Dahlinb9872622015-11-25 15:09:45 -0800853status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
854{
855 return writeNullableTypedVector(val, &Parcel::writeFloat);
856}
857
Casey Dahlin451ff582015-10-19 18:12:18 -0700858status_t Parcel::writeDoubleVector(const std::vector<double>& val)
859{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800860 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700861}
862
Casey Dahlinb9872622015-11-25 15:09:45 -0800863status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
864{
865 return writeNullableTypedVector(val, &Parcel::writeDouble);
866}
867
Casey Dahlin451ff582015-10-19 18:12:18 -0700868status_t Parcel::writeBoolVector(const std::vector<bool>& val)
869{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800870 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700871}
872
Casey Dahlinb9872622015-11-25 15:09:45 -0800873status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
874{
875 return writeNullableTypedVector(val, &Parcel::writeBool);
876}
877
Casey Dahlin451ff582015-10-19 18:12:18 -0700878status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
879{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800880 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700881}
882
Casey Dahlinb9872622015-11-25 15:09:45 -0800883status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
884{
885 return writeNullableTypedVector(val, &Parcel::writeChar);
886}
887
Casey Dahlin451ff582015-10-19 18:12:18 -0700888status_t Parcel::writeString16Vector(const std::vector<String16>& val)
889{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800890 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700891}
892
Casey Dahlinb9872622015-11-25 15:09:45 -0800893status_t Parcel::writeString16Vector(
894 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
895{
896 return writeNullableTypedVector(val, &Parcel::writeString16);
897}
898
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800899status_t Parcel::writeUtf8VectorAsUtf16Vector(
900 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
901 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
902}
903
904status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
905 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
906}
907
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700908status_t Parcel::writeInt32(int32_t val)
909{
Andreas Huber84a6d042009-08-17 13:33:27 -0700910 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700911}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800912
913status_t Parcel::writeUint32(uint32_t val)
914{
915 return writeAligned(val);
916}
917
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700918status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700919 if (len > INT32_MAX) {
920 // don't accept size_t values which may have come from an
921 // inadvertent conversion from a negative int.
922 return BAD_VALUE;
923 }
924
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700925 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700926 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700927 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700928 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700929 if (ret == NO_ERROR) {
930 ret = write(val, len * sizeof(*val));
931 }
932 return ret;
933}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700934status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700935 if (len > INT32_MAX) {
936 // don't accept size_t values which may have come from an
937 // inadvertent conversion from a negative int.
938 return BAD_VALUE;
939 }
940
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700941 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700942 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700943 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700944 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700945 if (ret == NO_ERROR) {
946 ret = write(val, len * sizeof(*val));
947 }
948 return ret;
949}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700950
Casey Dahlind6848f52015-10-15 15:44:59 -0700951status_t Parcel::writeBool(bool val)
952{
953 return writeInt32(int32_t(val));
954}
955
956status_t Parcel::writeChar(char16_t val)
957{
958 return writeInt32(int32_t(val));
959}
960
961status_t Parcel::writeByte(int8_t val)
962{
963 return writeInt32(int32_t(val));
964}
965
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700966status_t Parcel::writeInt64(int64_t val)
967{
Andreas Huber84a6d042009-08-17 13:33:27 -0700968 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700969}
970
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700971status_t Parcel::writeUint64(uint64_t val)
972{
973 return writeAligned(val);
974}
975
Serban Constantinescuf683e012013-11-05 16:53:55 +0000976status_t Parcel::writePointer(uintptr_t val)
977{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800978 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000979}
980
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700981status_t Parcel::writeFloat(float val)
982{
Andreas Huber84a6d042009-08-17 13:33:27 -0700983 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700984}
985
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800986#if defined(__mips__) && defined(__mips_hard_float)
987
988status_t Parcel::writeDouble(double val)
989{
990 union {
991 double d;
992 unsigned long long ll;
993 } u;
994 u.d = val;
995 return writeAligned(u.ll);
996}
997
998#else
999
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001000status_t Parcel::writeDouble(double val)
1001{
Andreas Huber84a6d042009-08-17 13:33:27 -07001002 return writeAligned(val);
1003}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001004
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001005#endif
1006
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001007status_t Parcel::writeCString(const char* str)
1008{
1009 return write(str, strlen(str)+1);
1010}
1011
1012status_t Parcel::writeString8(const String8& str)
1013{
1014 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001015 // only write string if its length is more than zero characters,
1016 // as readString8 will only read if the length field is non-zero.
1017 // this is slightly different from how writeString16 works.
1018 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001019 err = write(str.string(), str.bytes()+1);
1020 }
1021 return err;
1022}
1023
Casey Dahlinb9872622015-11-25 15:09:45 -08001024status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1025{
1026 if (!str) {
1027 return writeInt32(-1);
1028 }
1029
1030 return writeString16(*str);
1031}
1032
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001033status_t Parcel::writeString16(const String16& str)
1034{
1035 return writeString16(str.string(), str.size());
1036}
1037
1038status_t Parcel::writeString16(const char16_t* str, size_t len)
1039{
Yi Kong91635562018-06-07 14:38:36 -07001040 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001041
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001042 status_t err = writeInt32(len);
1043 if (err == NO_ERROR) {
1044 len *= sizeof(char16_t);
1045 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1046 if (data) {
1047 memcpy(data, str, len);
1048 *reinterpret_cast<char16_t*>(data+len) = 0;
1049 return NO_ERROR;
1050 }
1051 err = mError;
1052 }
1053 return err;
1054}
1055
1056status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1057{
Steven Morelanda86a3562019-08-01 23:28:34 +00001058 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001059}
1060
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001061status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1062{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001063 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001064}
1065
Casey Dahlinb9872622015-11-25 15:09:45 -08001066status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1067{
1068 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1069}
1070
1071status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001072 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001073}
1074
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001075status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001076 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001077}
1078
Casey Dahlinb9872622015-11-25 15:09:45 -08001079status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1080 if (!parcelable) {
1081 return writeInt32(0);
1082 }
1083
1084 return writeParcelable(*parcelable);
1085}
1086
Christopher Wiley97f048d2015-11-19 06:49:05 -08001087status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1088 status_t status = writeInt32(1); // parcelable is not null.
1089 if (status != OK) {
1090 return status;
1091 }
1092 return parcelable.writeToParcel(this);
1093}
1094
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001095status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001096{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001097 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001098 return BAD_TYPE;
1099
1100 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001101 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001102 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001104 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001105 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001107 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1108 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109
1110 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001111 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112 return err;
1113 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001114 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001115 return err;
1116}
1117
Jeff Brown93ff1f92011-11-04 19:01:44 -07001118status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001119{
1120 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001121 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001122 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001123 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001124 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001125 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001126 return writeObject(obj, true);
1127}
1128
1129status_t Parcel::writeDupFileDescriptor(int fd)
1130{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001131 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001132 if (dupFd < 0) {
1133 return -errno;
1134 }
1135 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001136 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001137 close(dupFd);
1138 }
1139 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001140}
1141
Dianne Hackborn1941a402016-08-29 12:30:43 -07001142status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1143{
1144 writeInt32(0);
1145 return writeFileDescriptor(fd, takeOwnership);
1146}
1147
Ryo Hashimotobf551892018-05-31 16:58:35 +09001148status_t Parcel::writeDupParcelFileDescriptor(int fd)
1149{
1150 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1151 if (dupFd < 0) {
1152 return -errno;
1153 }
1154 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1155 if (err != OK) {
1156 close(dupFd);
1157 }
1158 return err;
1159}
1160
Christopher Wiley2cf19952016-04-11 11:09:37 -07001161status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001162 return writeDupFileDescriptor(fd.get());
1163}
1164
Christopher Wiley2cf19952016-04-11 11:09:37 -07001165status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001166 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1167}
1168
Christopher Wiley2cf19952016-04-11 11:09:37 -07001169status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001170 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1171}
1172
Jeff Brown13b16042014-11-11 16:44:25 -08001173status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001174{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001175 if (len > INT32_MAX) {
1176 // don't accept size_t values which may have come from an
1177 // inadvertent conversion from a negative int.
1178 return BAD_VALUE;
1179 }
1180
Jeff Brown13b16042014-11-11 16:44:25 -08001181 status_t status;
1182 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001183 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001184 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001185 if (status) return status;
1186
1187 void* ptr = writeInplace(len);
1188 if (!ptr) return NO_MEMORY;
1189
Jeff Brown13b16042014-11-11 16:44:25 -08001190 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001191 return NO_ERROR;
1192 }
1193
Steve Block6807e592011-10-20 11:56:00 +01001194 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001195 int fd = ashmem_create_region("Parcel Blob", len);
1196 if (fd < 0) return NO_MEMORY;
1197
1198 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1199 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001200 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001201 } else {
Yi Kong91635562018-06-07 14:38:36 -07001202 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001203 if (ptr == MAP_FAILED) {
1204 status = -errno;
1205 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001206 if (!mutableCopy) {
1207 result = ashmem_set_prot_region(fd, PROT_READ);
1208 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001209 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001210 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001211 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001212 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001213 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001214 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001215 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001216 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001217 return NO_ERROR;
1218 }
1219 }
1220 }
1221 }
1222 ::munmap(ptr, len);
1223 }
1224 ::close(fd);
1225 return status;
1226}
1227
Jeff Brown13b16042014-11-11 16:44:25 -08001228status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1229{
1230 // Must match up with what's done in writeBlob.
1231 if (!mAllowFds) return FDS_NOT_ALLOWED;
1232 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1233 if (status) return status;
1234 return writeDupFileDescriptor(fd);
1235}
1236
Mathias Agopiane1424282013-07-29 21:24:40 -07001237status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001238{
1239 status_t err;
1240
1241 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001242 const size_t len = val.getFlattenedSize();
1243 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001244
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001245 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001246 // don't accept size_t values which may have come from an
1247 // inadvertent conversion from a negative int.
1248 return BAD_VALUE;
1249 }
1250
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001251 err = this->writeInt32(len);
1252 if (err) return err;
1253
1254 err = this->writeInt32(fd_count);
1255 if (err) return err;
1256
1257 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001258 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001259 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001260 return BAD_VALUE;
1261
Yi Kong91635562018-06-07 14:38:36 -07001262 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001263 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001264 fds = new (std::nothrow) int[fd_count];
1265 if (fds == nullptr) {
1266 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1267 return BAD_VALUE;
1268 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001269 }
1270
1271 err = val.flatten(buf, len, fds, fd_count);
1272 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1273 err = this->writeDupFileDescriptor( fds[i] );
1274 }
1275
1276 if (fd_count) {
1277 delete [] fds;
1278 }
1279
1280 return err;
1281}
1282
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001283status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1284{
1285 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1286 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1287 if (enoughData && enoughObjects) {
1288restart_write:
1289 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001290
Christopher Tate98e67d32015-06-03 18:44:15 -07001291 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001292 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001293 if (!mAllowFds) {
1294 // fail before modifying our object index
1295 return FDS_NOT_ALLOWED;
1296 }
1297 mHasFds = mFdsKnown = true;
1298 }
1299
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001300 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001301 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001302 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001303 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001304 mObjectsSize++;
1305 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001306
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001307 return finishWrite(sizeof(flat_binder_object));
1308 }
1309
1310 if (!enoughData) {
1311 const status_t err = growData(sizeof(val));
1312 if (err != NO_ERROR) return err;
1313 }
1314 if (!enoughObjects) {
1315 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001316 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001317 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001318 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001319 mObjects = objects;
1320 mObjectsCapacity = newSize;
1321 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001322
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001323 goto restart_write;
1324}
1325
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001326status_t Parcel::writeNoException()
1327{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001328 binder::Status status;
1329 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001330}
1331
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001332status_t Parcel::validateReadData(size_t upperBound) const
1333{
1334 // Don't allow non-object reads on object data
1335 if (mObjectsSorted || mObjectsSize <= 1) {
1336data_sorted:
1337 // Expect to check only against the next object
1338 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1339 // For some reason the current read position is greater than the next object
1340 // hint. Iterate until we find the right object
1341 size_t nextObject = mNextObjectHint;
1342 do {
1343 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1344 // Requested info overlaps with an object
1345 ALOGE("Attempt to read from protected data in Parcel %p", this);
1346 return PERMISSION_DENIED;
1347 }
1348 nextObject++;
1349 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1350 mNextObjectHint = nextObject;
1351 }
1352 return NO_ERROR;
1353 }
1354 // Quickly determine if mObjects is sorted.
1355 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1356 binder_size_t* prevObj = currObj;
1357 while (currObj > mObjects) {
1358 prevObj--;
1359 if(*prevObj > *currObj) {
1360 goto data_unsorted;
1361 }
1362 currObj--;
1363 }
1364 mObjectsSorted = true;
1365 goto data_sorted;
1366
1367data_unsorted:
1368 // Insertion Sort mObjects
1369 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1370 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1371 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1372 binder_size_t temp = *iter0;
1373 binder_size_t* iter1 = iter0 - 1;
1374 while (iter1 >= mObjects && *iter1 > temp) {
1375 *(iter1 + 1) = *iter1;
1376 iter1--;
1377 }
1378 *(iter1 + 1) = temp;
1379 }
1380 mNextObjectHint = 0;
1381 mObjectsSorted = true;
1382 goto data_sorted;
1383}
1384
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001385status_t Parcel::read(void* outData, size_t len) const
1386{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001387 if (len > INT32_MAX) {
1388 // don't accept size_t values which may have come from an
1389 // inadvertent conversion from a negative int.
1390 return BAD_VALUE;
1391 }
1392
1393 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1394 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001395 if (mObjectsSize > 0) {
1396 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001397 if(err != NO_ERROR) {
1398 // Still increment the data position by the expected length
1399 mDataPos += pad_size(len);
1400 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1401 return err;
1402 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001403 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001404 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001405 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001406 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001407 return NO_ERROR;
1408 }
1409 return NOT_ENOUGH_DATA;
1410}
1411
1412const void* Parcel::readInplace(size_t len) const
1413{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001414 if (len > INT32_MAX) {
1415 // don't accept size_t values which may have come from an
1416 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001417 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001418 }
1419
1420 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1421 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001422 if (mObjectsSize > 0) {
1423 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001424 if(err != NO_ERROR) {
1425 // Still increment the data position by the expected length
1426 mDataPos += pad_size(len);
1427 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001428 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001429 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001430 }
1431
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001432 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001433 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001434 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001435 return data;
1436 }
Yi Kong91635562018-06-07 14:38:36 -07001437 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001438}
1439
Andreas Huber84a6d042009-08-17 13:33:27 -07001440template<class T>
1441status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001442 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001443
1444 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001445 if (mObjectsSize > 0) {
1446 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001447 if(err != NO_ERROR) {
1448 // Still increment the data position by the expected length
1449 mDataPos += sizeof(T);
1450 return err;
1451 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001452 }
1453
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001454 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001455 mDataPos += sizeof(T);
1456 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001457 return NO_ERROR;
1458 } else {
1459 return NOT_ENOUGH_DATA;
1460 }
1461}
1462
Andreas Huber84a6d042009-08-17 13:33:27 -07001463template<class T>
1464T Parcel::readAligned() const {
1465 T result;
1466 if (readAligned(&result) != NO_ERROR) {
1467 result = 0;
1468 }
1469
1470 return result;
1471}
1472
1473template<class T>
1474status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001475 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001476
1477 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1478restart_write:
1479 *reinterpret_cast<T*>(mData+mDataPos) = val;
1480 return finishWrite(sizeof(val));
1481 }
1482
1483 status_t err = growData(sizeof(val));
1484 if (err == NO_ERROR) goto restart_write;
1485 return err;
1486}
1487
Casey Dahlin185d3442016-02-09 11:08:35 -08001488namespace {
1489
1490template<typename T>
1491status_t readByteVectorInternal(const Parcel* parcel,
1492 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001493 val->clear();
1494
1495 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001496 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001497
1498 if (status != OK) {
1499 return status;
1500 }
1501
Christopher Wiley4db672d2015-11-10 09:44:30 -08001502 if (size < 0) {
1503 status = UNEXPECTED_NULL;
1504 return status;
1505 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001506 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001507 status = BAD_VALUE;
1508 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001509 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001510
Paul Lietar433e87b2016-09-16 10:39:32 -07001511 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001512 if (!data) {
1513 status = BAD_VALUE;
1514 return status;
1515 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001516 val->reserve(size);
1517 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001518
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001519 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001520}
1521
Casey Dahlin185d3442016-02-09 11:08:35 -08001522template<typename T>
1523status_t readByteVectorInternalPtr(
1524 const Parcel* parcel,
1525 std::unique_ptr<std::vector<T>>* val) {
1526 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001527 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001528 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001529 val->reset();
1530
1531 if (status != OK || size < 0) {
1532 return status;
1533 }
1534
Casey Dahlin185d3442016-02-09 11:08:35 -08001535 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001536 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001537
Casey Dahlin185d3442016-02-09 11:08:35 -08001538 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001539
1540 if (status != OK) {
1541 val->reset();
1542 }
1543
1544 return status;
1545}
1546
Casey Dahlin185d3442016-02-09 11:08:35 -08001547} // namespace
1548
1549status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1550 return readByteVectorInternal(this, val);
1551}
1552
1553status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1554 return readByteVectorInternal(this, val);
1555}
1556
1557status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1558 return readByteVectorInternalPtr(this, val);
1559}
1560
1561status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1562 return readByteVectorInternalPtr(this, val);
1563}
1564
Casey Dahlinb9872622015-11-25 15:09:45 -08001565status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1566 return readNullableTypedVector(val, &Parcel::readInt32);
1567}
1568
Casey Dahlin451ff582015-10-19 18:12:18 -07001569status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001570 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001571}
1572
Casey Dahlinb9872622015-11-25 15:09:45 -08001573status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1574 return readNullableTypedVector(val, &Parcel::readInt64);
1575}
1576
Casey Dahlin451ff582015-10-19 18:12:18 -07001577status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001578 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001579}
1580
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001581status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1582 return readNullableTypedVector(val, &Parcel::readUint64);
1583}
1584
1585status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1586 return readTypedVector(val, &Parcel::readUint64);
1587}
1588
Casey Dahlinb9872622015-11-25 15:09:45 -08001589status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1590 return readNullableTypedVector(val, &Parcel::readFloat);
1591}
1592
Casey Dahlin451ff582015-10-19 18:12:18 -07001593status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001594 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001595}
1596
Casey Dahlinb9872622015-11-25 15:09:45 -08001597status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1598 return readNullableTypedVector(val, &Parcel::readDouble);
1599}
1600
Casey Dahlin451ff582015-10-19 18:12:18 -07001601status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001602 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001603}
1604
Casey Dahlinb9872622015-11-25 15:09:45 -08001605status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1606 const int32_t start = dataPosition();
1607 int32_t size;
1608 status_t status = readInt32(&size);
1609 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001610
Casey Dahlinb9872622015-11-25 15:09:45 -08001611 if (status != OK || size < 0) {
1612 return status;
1613 }
1614
1615 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001616 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001617
1618 status = readBoolVector(val->get());
1619
1620 if (status != OK) {
1621 val->reset();
1622 }
1623
1624 return status;
1625}
1626
1627status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001628 int32_t size;
1629 status_t status = readInt32(&size);
1630
1631 if (status != OK) {
1632 return status;
1633 }
1634
1635 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001636 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001637 }
1638
1639 val->resize(size);
1640
1641 /* C++ bool handling means a vector of bools isn't necessarily addressable
1642 * (we might use individual bits)
1643 */
Christopher Wiley97887982015-10-27 16:33:47 -07001644 bool data;
1645 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001646 status = readBool(&data);
1647 (*val)[i] = data;
1648
1649 if (status != OK) {
1650 return status;
1651 }
1652 }
1653
1654 return OK;
1655}
1656
Casey Dahlinb9872622015-11-25 15:09:45 -08001657status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1658 return readNullableTypedVector(val, &Parcel::readChar);
1659}
1660
Casey Dahlin451ff582015-10-19 18:12:18 -07001661status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001662 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001663}
1664
Casey Dahlinb9872622015-11-25 15:09:45 -08001665status_t Parcel::readString16Vector(
1666 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1667 return readNullableTypedVector(val, &Parcel::readString16);
1668}
1669
Casey Dahlin451ff582015-10-19 18:12:18 -07001670status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001671 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001672}
1673
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001674status_t Parcel::readUtf8VectorFromUtf16Vector(
1675 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1676 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1677}
1678
1679status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1680 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1681}
Casey Dahlin451ff582015-10-19 18:12:18 -07001682
Andreas Huber84a6d042009-08-17 13:33:27 -07001683status_t Parcel::readInt32(int32_t *pArg) const
1684{
1685 return readAligned(pArg);
1686}
1687
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001688int32_t Parcel::readInt32() const
1689{
Andreas Huber84a6d042009-08-17 13:33:27 -07001690 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001691}
1692
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001693status_t Parcel::readUint32(uint32_t *pArg) const
1694{
1695 return readAligned(pArg);
1696}
1697
1698uint32_t Parcel::readUint32() const
1699{
1700 return readAligned<uint32_t>();
1701}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001702
1703status_t Parcel::readInt64(int64_t *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
1708
1709int64_t Parcel::readInt64() const
1710{
Andreas Huber84a6d042009-08-17 13:33:27 -07001711 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001712}
1713
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001714status_t Parcel::readUint64(uint64_t *pArg) const
1715{
1716 return readAligned(pArg);
1717}
1718
1719uint64_t Parcel::readUint64() const
1720{
1721 return readAligned<uint64_t>();
1722}
1723
Serban Constantinescuf683e012013-11-05 16:53:55 +00001724status_t Parcel::readPointer(uintptr_t *pArg) const
1725{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001726 status_t ret;
1727 binder_uintptr_t ptr;
1728 ret = readAligned(&ptr);
1729 if (!ret)
1730 *pArg = ptr;
1731 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001732}
1733
1734uintptr_t Parcel::readPointer() const
1735{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001736 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001737}
1738
1739
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001740status_t Parcel::readFloat(float *pArg) const
1741{
Andreas Huber84a6d042009-08-17 13:33:27 -07001742 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001743}
1744
1745
1746float Parcel::readFloat() const
1747{
Andreas Huber84a6d042009-08-17 13:33:27 -07001748 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001749}
1750
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001751#if defined(__mips__) && defined(__mips_hard_float)
1752
1753status_t Parcel::readDouble(double *pArg) const
1754{
1755 union {
1756 double d;
1757 unsigned long long ll;
1758 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001759 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001760 status_t status;
1761 status = readAligned(&u.ll);
1762 *pArg = u.d;
1763 return status;
1764}
1765
1766double Parcel::readDouble() const
1767{
1768 union {
1769 double d;
1770 unsigned long long ll;
1771 } u;
1772 u.ll = readAligned<unsigned long long>();
1773 return u.d;
1774}
1775
1776#else
1777
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001778status_t Parcel::readDouble(double *pArg) const
1779{
Andreas Huber84a6d042009-08-17 13:33:27 -07001780 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001781}
1782
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001783double Parcel::readDouble() const
1784{
Andreas Huber84a6d042009-08-17 13:33:27 -07001785 return readAligned<double>();
1786}
1787
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001788#endif
1789
Andreas Huber84a6d042009-08-17 13:33:27 -07001790status_t Parcel::readIntPtr(intptr_t *pArg) const
1791{
1792 return readAligned(pArg);
1793}
1794
1795
1796intptr_t Parcel::readIntPtr() const
1797{
1798 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001799}
1800
Casey Dahlind6848f52015-10-15 15:44:59 -07001801status_t Parcel::readBool(bool *pArg) const
1802{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001803 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001804 status_t ret = readInt32(&tmp);
1805 *pArg = (tmp != 0);
1806 return ret;
1807}
1808
1809bool Parcel::readBool() const
1810{
1811 return readInt32() != 0;
1812}
1813
1814status_t Parcel::readChar(char16_t *pArg) const
1815{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001816 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001817 status_t ret = readInt32(&tmp);
1818 *pArg = char16_t(tmp);
1819 return ret;
1820}
1821
1822char16_t Parcel::readChar() const
1823{
1824 return char16_t(readInt32());
1825}
1826
1827status_t Parcel::readByte(int8_t *pArg) const
1828{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001829 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001830 status_t ret = readInt32(&tmp);
1831 *pArg = int8_t(tmp);
1832 return ret;
1833}
1834
1835int8_t Parcel::readByte() const
1836{
1837 return int8_t(readInt32());
1838}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001839
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001840status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1841 size_t utf16Size = 0;
1842 const char16_t* src = readString16Inplace(&utf16Size);
1843 if (!src) {
1844 return UNEXPECTED_NULL;
1845 }
1846
1847 // Save ourselves the trouble, we're done.
1848 if (utf16Size == 0u) {
1849 str->clear();
1850 return NO_ERROR;
1851 }
1852
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001853 // Allow for closing '\0'
1854 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1855 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001856 return BAD_VALUE;
1857 }
1858 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001859 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001860 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001861 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1862 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001863 return NO_ERROR;
1864}
1865
1866status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1867 const int32_t start = dataPosition();
1868 int32_t size;
1869 status_t status = readInt32(&size);
1870 str->reset();
1871
1872 if (status != OK || size < 0) {
1873 return status;
1874 }
1875
1876 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001877 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001878 return readUtf8FromUtf16(str->get());
1879}
1880
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001881const char* Parcel::readCString() const
1882{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001883 if (mDataPos < mDataSize) {
1884 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001885 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1886 // is the string's trailing NUL within the parcel's valid bounds?
1887 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1888 if (eos) {
1889 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001890 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001891 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001892 return str;
1893 }
1894 }
Yi Kong91635562018-06-07 14:38:36 -07001895 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001896}
1897
1898String8 Parcel::readString8() const
1899{
Roshan Pius87b64d22016-07-18 12:51:02 -07001900 String8 retString;
1901 status_t status = readString8(&retString);
1902 if (status != OK) {
1903 // We don't care about errors here, so just return an empty string.
1904 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001905 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001906 return retString;
1907}
1908
1909status_t Parcel::readString8(String8* pArg) const
1910{
1911 int32_t size;
1912 status_t status = readInt32(&size);
1913 if (status != OK) {
1914 return status;
1915 }
1916 // watch for potential int overflow from size+1
1917 if (size < 0 || size >= INT32_MAX) {
1918 return BAD_VALUE;
1919 }
1920 // |writeString8| writes nothing for empty string.
1921 if (size == 0) {
1922 *pArg = String8();
1923 return OK;
1924 }
1925 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07001926 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07001927 return BAD_VALUE;
1928 }
1929 pArg->setTo(str, size);
1930 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001931}
1932
1933String16 Parcel::readString16() const
1934{
1935 size_t len;
1936 const char16_t* str = readString16Inplace(&len);
1937 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001938 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001939 return String16();
1940}
1941
Casey Dahlinb9872622015-11-25 15:09:45 -08001942status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1943{
1944 const int32_t start = dataPosition();
1945 int32_t size;
1946 status_t status = readInt32(&size);
1947 pArg->reset();
1948
1949 if (status != OK || size < 0) {
1950 return status;
1951 }
1952
1953 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001954 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001955
1956 status = readString16(pArg->get());
1957
1958 if (status != OK) {
1959 pArg->reset();
1960 }
1961
1962 return status;
1963}
1964
Casey Dahlin451ff582015-10-19 18:12:18 -07001965status_t Parcel::readString16(String16* pArg) const
1966{
1967 size_t len;
1968 const char16_t* str = readString16Inplace(&len);
1969 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001970 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001971 return 0;
1972 } else {
1973 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001974 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001975 }
1976}
1977
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001978const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1979{
1980 int32_t size = readInt32();
1981 // watch for potential int overflow from size+1
1982 if (size >= 0 && size < INT32_MAX) {
1983 *outLen = size;
1984 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001985 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001986 return str;
1987 }
1988 }
1989 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001990 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001991}
1992
Casey Dahlinf0c13772015-10-27 18:33:56 -07001993status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1994{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001995 status_t status = readNullableStrongBinder(val);
1996 if (status == OK && !val->get()) {
1997 status = UNEXPECTED_NULL;
1998 }
1999 return status;
2000}
2001
2002status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2003{
Steven Morelanda86a3562019-08-01 23:28:34 +00002004 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002005}
2006
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002007sp<IBinder> Parcel::readStrongBinder() const
2008{
2009 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002010 // Note that a lot of code in Android reads binders by hand with this
2011 // method, and that code has historically been ok with getting nullptr
2012 // back (while ignoring error codes).
2013 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002014 return val;
2015}
2016
Christopher Wiley97f048d2015-11-19 06:49:05 -08002017status_t Parcel::readParcelable(Parcelable* parcelable) const {
2018 int32_t have_parcelable = 0;
2019 status_t status = readInt32(&have_parcelable);
2020 if (status != OK) {
2021 return status;
2022 }
2023 if (!have_parcelable) {
2024 return UNEXPECTED_NULL;
2025 }
2026 return parcelable->readFromParcel(this);
2027}
2028
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002029int32_t Parcel::readExceptionCode() const
2030{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002031 binder::Status status;
2032 status.readFromParcel(*this);
2033 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002034}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002035
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002036native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002037{
2038 int numFds, numInts;
2039 status_t err;
2040 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002041 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002042 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002043 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002044
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002045 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002046 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002047 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002048 }
2049
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002050 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002051 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002052 if (h->data[i] < 0) {
2053 for (int j = 0; j < i; j++) {
2054 close(h->data[j]);
2055 }
2056 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002057 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002058 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002059 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002060 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002061 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002062 native_handle_close(h);
2063 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002064 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002065 }
2066 return h;
2067}
2068
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002069int Parcel::readFileDescriptor() const
2070{
2071 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002072
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002073 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002074 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002075 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002076
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002077 return BAD_TYPE;
2078}
2079
Dianne Hackborn1941a402016-08-29 12:30:43 -07002080int Parcel::readParcelFileDescriptor() const
2081{
2082 int32_t hasComm = readInt32();
2083 int fd = readFileDescriptor();
2084 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002085 // detach (owned by the binder driver)
2086 int comm = readFileDescriptor();
2087
2088 // warning: this must be kept in sync with:
2089 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2090 enum ParcelFileDescriptorStatus {
2091 DETACHED = 2,
2092 };
2093
2094#if BYTE_ORDER == BIG_ENDIAN
2095 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2096#endif
2097#if BYTE_ORDER == LITTLE_ENDIAN
2098 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2099#endif
2100
2101 ssize_t written = TEMP_FAILURE_RETRY(
2102 ::write(comm, &message, sizeof(message)));
2103
2104 if (written == -1 || written != sizeof(message)) {
2105 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2106 written, strerror(errno));
2107 return BAD_TYPE;
2108 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002109 }
2110 return fd;
2111}
2112
Christopher Wiley2cf19952016-04-11 11:09:37 -07002113status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002114{
2115 int got = readFileDescriptor();
2116
2117 if (got == BAD_TYPE) {
2118 return BAD_TYPE;
2119 }
2120
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002121 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002122
2123 if (val->get() < 0) {
2124 return BAD_VALUE;
2125 }
2126
2127 return OK;
2128}
2129
Ryo Hashimotobf551892018-05-31 16:58:35 +09002130status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2131{
2132 int got = readParcelFileDescriptor();
2133
2134 if (got == BAD_TYPE) {
2135 return BAD_TYPE;
2136 }
2137
2138 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2139
2140 if (val->get() < 0) {
2141 return BAD_VALUE;
2142 }
2143
2144 return OK;
2145}
Casey Dahlin06673e32015-11-23 13:24:23 -08002146
Christopher Wiley2cf19952016-04-11 11:09:37 -07002147status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002148 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2149}
2150
Christopher Wiley2cf19952016-04-11 11:09:37 -07002151status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002152 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2153}
2154
Jeff Brown5707dbf2011-09-23 21:17:56 -07002155status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2156{
Jeff Brown13b16042014-11-11 16:44:25 -08002157 int32_t blobType;
2158 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002159 if (status) return status;
2160
Jeff Brown13b16042014-11-11 16:44:25 -08002161 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002162 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002163 const void* ptr = readInplace(len);
2164 if (!ptr) return BAD_VALUE;
2165
Jeff Brown13b16042014-11-11 16:44:25 -08002166 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002167 return NO_ERROR;
2168 }
2169
Steve Block6807e592011-10-20 11:56:00 +01002170 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002171 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002172 int fd = readFileDescriptor();
2173 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2174
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002175 if (!ashmem_valid(fd)) {
2176 ALOGE("invalid fd");
2177 return BAD_VALUE;
2178 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002179 int size = ashmem_get_size_region(fd);
2180 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002181 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002182 return BAD_VALUE;
2183 }
Yi Kong91635562018-06-07 14:38:36 -07002184 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002185 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002186 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002187
Jeff Brown13b16042014-11-11 16:44:25 -08002188 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002189 return NO_ERROR;
2190}
2191
Mathias Agopiane1424282013-07-29 21:24:40 -07002192status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002193{
2194 // size
2195 const size_t len = this->readInt32();
2196 const size_t fd_count = this->readInt32();
2197
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002198 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002199 // don't accept size_t values which may have come from an
2200 // inadvertent conversion from a negative int.
2201 return BAD_VALUE;
2202 }
2203
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002204 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002205 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002206 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002207 return BAD_VALUE;
2208
Yi Kong91635562018-06-07 14:38:36 -07002209 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002210 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002211 fds = new (std::nothrow) int[fd_count];
2212 if (fds == nullptr) {
2213 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2214 return BAD_VALUE;
2215 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002216 }
2217
2218 status_t err = NO_ERROR;
2219 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002220 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002221 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002222 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002223 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 -07002224 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2225 // Close all the file descriptors that were dup-ed.
2226 for (size_t j=0; j<i ;j++) {
2227 close(fds[j]);
2228 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002229 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002230 }
2231
2232 if (err == NO_ERROR) {
2233 err = val.unflatten(buf, len, fds, fd_count);
2234 }
2235
2236 if (fd_count) {
2237 delete [] fds;
2238 }
2239
2240 return err;
2241}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002242const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2243{
2244 const size_t DPOS = mDataPos;
2245 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2246 const flat_binder_object* obj
2247 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2248 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002249 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002250 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002251 // the object list, so we don't want to check for it when
2252 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002253 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002254 return obj;
2255 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002256
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002257 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002258 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002259 const size_t N = mObjectsSize;
2260 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002261
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002262 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002263 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002264 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002265
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002266 // Start at the current hint position, looking for an object at
2267 // the current data position.
2268 if (opos < N) {
2269 while (opos < (N-1) && OBJS[opos] < DPOS) {
2270 opos++;
2271 }
2272 } else {
2273 opos = N-1;
2274 }
2275 if (OBJS[opos] == DPOS) {
2276 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002277 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002278 this, DPOS, opos);
2279 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002280 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002281 return obj;
2282 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002283
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002284 // Look backwards for it...
2285 while (opos > 0 && OBJS[opos] > DPOS) {
2286 opos--;
2287 }
2288 if (OBJS[opos] == DPOS) {
2289 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002290 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002291 this, DPOS, opos);
2292 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002293 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002294 return obj;
2295 }
2296 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002297 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 -07002298 this, DPOS);
2299 }
Yi Kong91635562018-06-07 14:38:36 -07002300 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002301}
2302
2303void Parcel::closeFileDescriptors()
2304{
2305 size_t i = mObjectsSize;
2306 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002307 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002308 }
2309 while (i > 0) {
2310 i--;
2311 const flat_binder_object* flat
2312 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002313 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002314 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002315 close(flat->handle);
2316 }
2317 }
2318}
2319
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002320uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002321{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002322 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002323}
2324
2325size_t Parcel::ipcDataSize() const
2326{
2327 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2328}
2329
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002330uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002331{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002332 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002333}
2334
2335size_t Parcel::ipcObjectsCount() const
2336{
2337 return mObjectsSize;
2338}
2339
2340void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002341 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002342{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002343 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002344 freeDataNoInit();
2345 mError = NO_ERROR;
2346 mData = const_cast<uint8_t*>(data);
2347 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002348 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002349 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002350 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002351 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002352 mObjectsSize = mObjectsCapacity = objectsCount;
2353 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002354 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002355 mOwner = relFunc;
2356 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002357 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002358 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002359 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002360 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002361 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002362 mObjectsSize = 0;
2363 break;
2364 }
2365 minOffset = offset + sizeof(flat_binder_object);
2366 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002367 scanForFds();
2368}
2369
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002370void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002371{
2372 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002373
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002374 if (errorCheck() != NO_ERROR) {
2375 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002376 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002377 } else if (dataSize() > 0) {
2378 const uint8_t* DATA = data();
2379 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002380 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002381 const size_t N = objectsCount();
2382 for (size_t i=0; i<N; i++) {
2383 const flat_binder_object* flat
2384 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2385 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002386 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002387 << " = " << flat->binder;
2388 }
2389 } else {
2390 to << "NULL";
2391 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002392
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002393 to << ")";
2394}
2395
2396void Parcel::releaseObjects()
2397{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002398 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002399 if (i == 0) {
2400 return;
2401 }
2402 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002403 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002404 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002405 while (i > 0) {
2406 i--;
2407 const flat_binder_object* flat
2408 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002409 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002410 }
2411}
2412
2413void Parcel::acquireObjects()
2414{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002415 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002416 if (i == 0) {
2417 return;
2418 }
2419 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002420 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002421 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002422 while (i > 0) {
2423 i--;
2424 const flat_binder_object* flat
2425 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002426 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427 }
2428}
2429
2430void Parcel::freeData()
2431{
2432 freeDataNoInit();
2433 initState();
2434}
2435
2436void Parcel::freeDataNoInit()
2437{
2438 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002439 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002440 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002441 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2442 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002443 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002444 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002445 if (mData) {
2446 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002447 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002448 if (mDataCapacity <= gParcelGlobalAllocSize) {
2449 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2450 } else {
2451 gParcelGlobalAllocSize = 0;
2452 }
2453 if (gParcelGlobalAllocCount > 0) {
2454 gParcelGlobalAllocCount--;
2455 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002456 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002457 free(mData);
2458 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002459 if (mObjects) free(mObjects);
2460 }
2461}
2462
2463status_t Parcel::growData(size_t len)
2464{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002465 if (len > INT32_MAX) {
2466 // don't accept size_t values which may have come from an
2467 // inadvertent conversion from a negative int.
2468 return BAD_VALUE;
2469 }
2470
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002471 size_t newSize = ((mDataSize+len)*3)/2;
2472 return (newSize <= mDataSize)
2473 ? (status_t) NO_MEMORY
2474 : continueWrite(newSize);
2475}
2476
2477status_t Parcel::restartWrite(size_t desired)
2478{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002479 if (desired > INT32_MAX) {
2480 // don't accept size_t values which may have come from an
2481 // inadvertent conversion from a negative int.
2482 return BAD_VALUE;
2483 }
2484
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002485 if (mOwner) {
2486 freeData();
2487 return continueWrite(desired);
2488 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002489
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002490 uint8_t* data = (uint8_t*)realloc(mData, desired);
2491 if (!data && desired > mDataCapacity) {
2492 mError = NO_MEMORY;
2493 return NO_MEMORY;
2494 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002495
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002496 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002497
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002498 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002499 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002500 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002501 gParcelGlobalAllocSize += desired;
2502 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002503 if (!mData) {
2504 gParcelGlobalAllocCount++;
2505 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002506 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002507 mData = data;
2508 mDataCapacity = desired;
2509 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002510
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002511 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002512 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2513 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2514
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002515 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002516 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002517 mObjectsSize = mObjectsCapacity = 0;
2518 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002519 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002520 mHasFds = false;
2521 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002522 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002523
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002524 return NO_ERROR;
2525}
2526
2527status_t Parcel::continueWrite(size_t desired)
2528{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002529 if (desired > INT32_MAX) {
2530 // don't accept size_t values which may have come from an
2531 // inadvertent conversion from a negative int.
2532 return BAD_VALUE;
2533 }
2534
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002535 // If shrinking, first adjust for any objects that appear
2536 // after the new data size.
2537 size_t objectsSize = mObjectsSize;
2538 if (desired < mDataSize) {
2539 if (desired == 0) {
2540 objectsSize = 0;
2541 } else {
2542 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002543 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544 break;
2545 objectsSize--;
2546 }
2547 }
2548 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002549
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002550 if (mOwner) {
2551 // If the size is going to zero, just release the owner's data.
2552 if (desired == 0) {
2553 freeData();
2554 return NO_ERROR;
2555 }
2556
2557 // If there is a different owner, we need to take
2558 // posession.
2559 uint8_t* data = (uint8_t*)malloc(desired);
2560 if (!data) {
2561 mError = NO_MEMORY;
2562 return NO_MEMORY;
2563 }
Yi Kong91635562018-06-07 14:38:36 -07002564 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002565
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002566 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002567 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002568 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002569 free(data);
2570
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002571 mError = NO_MEMORY;
2572 return NO_MEMORY;
2573 }
2574
2575 // Little hack to only acquire references on objects
2576 // we will be keeping.
2577 size_t oldObjectsSize = mObjectsSize;
2578 mObjectsSize = objectsSize;
2579 acquireObjects();
2580 mObjectsSize = oldObjectsSize;
2581 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002582
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002583 if (mData) {
2584 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2585 }
2586 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002587 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002588 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002589 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002590 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002591 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002592
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002593 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002594 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002595 gParcelGlobalAllocSize += desired;
2596 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002597 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002598
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002599 mData = data;
2600 mObjects = objects;
2601 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002602 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002603 mDataCapacity = desired;
2604 mObjectsSize = mObjectsCapacity = objectsSize;
2605 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002606 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002607
2608 } else if (mData) {
2609 if (objectsSize < mObjectsSize) {
2610 // Need to release refs on any objects we are dropping.
2611 const sp<ProcessState> proc(ProcessState::self());
2612 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2613 const flat_binder_object* flat
2614 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002615 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002616 // will need to rescan because we may have lopped off the only FDs
2617 mFdsKnown = false;
2618 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002619 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002620 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002621
2622 if (objectsSize == 0) {
2623 free(mObjects);
2624 mObjects = nullptr;
2625 } else {
2626 binder_size_t* objects =
2627 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2628 if (objects) {
2629 mObjects = objects;
2630 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002631 }
2632 mObjectsSize = objectsSize;
2633 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002634 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002635 }
2636
2637 // We own the data, so we can just do a realloc().
2638 if (desired > mDataCapacity) {
2639 uint8_t* data = (uint8_t*)realloc(mData, desired);
2640 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002641 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2642 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002643 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002644 gParcelGlobalAllocSize += desired;
2645 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002646 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002647 mData = data;
2648 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002649 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002650 mError = NO_MEMORY;
2651 return NO_MEMORY;
2652 }
2653 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002654 if (mDataSize > desired) {
2655 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002656 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002657 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002658 if (mDataPos > desired) {
2659 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002660 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002661 }
2662 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002663
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002664 } else {
2665 // This is the first data. Easy!
2666 uint8_t* data = (uint8_t*)malloc(desired);
2667 if (!data) {
2668 mError = NO_MEMORY;
2669 return NO_MEMORY;
2670 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002671
Yi Kong91635562018-06-07 14:38:36 -07002672 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002673 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002674 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002675 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002676
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002677 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002678 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002679 gParcelGlobalAllocSize += desired;
2680 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002681 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002682
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002683 mData = data;
2684 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002685 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2686 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002687 mDataCapacity = desired;
2688 }
2689
2690 return NO_ERROR;
2691}
2692
2693void Parcel::initState()
2694{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002695 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002696 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002697 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002698 mDataSize = 0;
2699 mDataCapacity = 0;
2700 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002701 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2702 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002703 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002704 mObjectsSize = 0;
2705 mObjectsCapacity = 0;
2706 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002707 mObjectsSorted = false;
Steven Morelanda86a3562019-08-01 23:28:34 +00002708 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002709 mHasFds = false;
2710 mFdsKnown = true;
Steven Morelanda86a3562019-08-01 23:28:34 +00002711 mRequiredStability = internal::Stability::UNDECLARED;
Yi Kong91635562018-06-07 14:38:36 -07002712 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002713 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002714 mWorkSourceRequestHeaderPosition = 0;
2715 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002716
2717 // racing multiple init leads only to multiple identical write
2718 if (gMaxFds == 0) {
2719 struct rlimit result;
2720 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2721 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002722 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002723 } else {
2724 ALOGW("Unable to getrlimit: %s", strerror(errno));
2725 gMaxFds = 1024;
2726 }
2727 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002728}
2729
2730void Parcel::scanForFds() const
2731{
2732 bool hasFds = false;
2733 for (size_t i=0; i<mObjectsSize; i++) {
2734 const flat_binder_object* flat
2735 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002736 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002737 hasFds = true;
2738 break;
2739 }
2740 }
2741 mHasFds = hasFds;
2742 mFdsKnown = true;
2743}
2744
Dan Sandleraa5c2342015-04-10 10:08:45 -04002745size_t Parcel::getBlobAshmemSize() const
2746{
Adrian Roos6bb31142015-10-22 16:46:12 -07002747 // This used to return the size of all blobs that were written to ashmem, now we're returning
2748 // the ashmem currently referenced by this Parcel, which should be equivalent.
2749 // TODO: Remove method once ABI can be changed.
2750 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002751}
2752
Adrian Rooscbf37262015-10-22 16:12:53 -07002753size_t Parcel::getOpenAshmemSize() const
2754{
2755 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002756}
2757
2758// --- Parcel::Blob ---
2759
2760Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002761 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002762}
2763
2764Parcel::Blob::~Blob() {
2765 release();
2766}
2767
2768void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002769 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002770 ::munmap(mData, mSize);
2771 }
2772 clear();
2773}
2774
Jeff Brown13b16042014-11-11 16:44:25 -08002775void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2776 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002777 mData = data;
2778 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002779 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002780}
2781
2782void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002783 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002784 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002785 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002786 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002787}
2788
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002789}; // namespace android