blob: 994e3b9ba3e7ff48cc76d1e90ad258207717fb6f [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080023#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080028#include <sys/stat.h>
29#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070030#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080031#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070032
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/Binder.h>
34#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080035#include <binder/IPCThreadState.h>
36#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070037#include <binder/ProcessState.h>
Steven Moreland6e5a7752019-08-05 20:30:14 -070038#include <binder/Stability.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080039#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070040#include <binder/TextOutput.h>
41
Mark Salyzynabed7f72016-01-27 08:02:48 -080042#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070043#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080044#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080046#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String8.h>
48#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049
Mathias Agopian208059f2009-05-18 15:08:03 -070050#include <private/binder/binder_module.h>
Steven Morelanda4853cd2019-07-12 15:44:37 -070051#include "Static.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080054//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080055#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080056//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070057
58// ---------------------------------------------------------------------------
59
Nick Kralevichb6b14232015-04-02 09:36:02 -070060// This macro should never be used at runtime, as a too large value
61// of s could cause an integer overflow. Instead, you should always
62// use the wrapper function pad_size()
63#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
64
65static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070066 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070067 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070068 }
69 return PAD_SIZE_UNSAFE(s);
70}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070071
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070072// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060073#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070074
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075namespace android {
76
Steven Moreland7b102262019-08-01 15:48:43 -070077// many things compile this into prebuilts on the stack
78static_assert(sizeof(Parcel) == 60 || sizeof(Parcel) == 120);
79
Dianne Hackborna4cff882014-11-13 17:07:40 -080080static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
81static size_t gParcelGlobalAllocSize = 0;
82static size_t gParcelGlobalAllocCount = 0;
83
Christopher Tatee4e0ae82016-03-24 16:03:44 -070084static size_t gMaxFds = 0;
85
Jeff Brown13b16042014-11-11 16:44:25 -080086// Maximum size of a blob to transfer in-place.
87static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
88
89enum {
90 BLOB_INPLACE = 0,
91 BLOB_ASHMEM_IMMUTABLE = 1,
92 BLOB_ASHMEM_MUTABLE = 2,
93};
94
Steven Morelandb1c81202019-04-05 18:49:55 -070095static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070096 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070097{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -070098 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070099 case BINDER_TYPE_BINDER:
100 if (obj.binder) {
101 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800102 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700103 }
104 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700105 case BINDER_TYPE_HANDLE: {
106 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700107 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
109 b->incStrong(who);
110 }
111 return;
112 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700113 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000114 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700115 // If we own an ashmem fd, keep track of how much memory it refers to.
116 int size = ashmem_get_size_region(obj.handle);
117 if (size > 0) {
118 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700119 }
120 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121 return;
122 }
123 }
124
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700125 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700126}
127
Adrian Roos6bb31142015-10-22 16:46:12 -0700128static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700129 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700130{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700131 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700132 case BINDER_TYPE_BINDER:
133 if (obj.binder) {
134 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800135 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136 }
137 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700138 case BINDER_TYPE_HANDLE: {
139 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700140 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700141 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
142 b->decStrong(who);
143 }
144 return;
145 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700146 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800147 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000148 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700149 int size = ashmem_get_size_region(obj.handle);
150 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800151 // ashmem size might have changed since last time it was accounted for, e.g.
152 // in acquire_object(). Value of *outAshmemSize is not critical since we are
153 // releasing the object anyway. Check for integer overflow condition.
154 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700155 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700156 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800157
158 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700159 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 return;
161 }
162 }
163
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700164 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165}
166
Steven Morelanda86a3562019-08-01 23:28:34 +0000167status_t Parcel::finishFlattenBinder(
168 const sp<IBinder>& binder, const flat_binder_object& flat)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169{
Steven Morelanda86a3562019-08-01 23:28:34 +0000170 status_t status = writeObject(flat, false);
171 if (status != OK) return status;
172
Steven Moreland6e5a7752019-08-05 20:30:14 -0700173 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Morelanda86a3562019-08-01 23:28:34 +0000174 return writeInt32(internal::Stability::get(binder.get()));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700175}
176
Steven Morelanda86a3562019-08-01 23:28:34 +0000177status_t Parcel::finishUnflattenBinder(
178 const sp<IBinder>& binder, sp<IBinder>* out) const
179{
180 int32_t stability;
181 status_t status = readInt32(&stability);
182 if (status != OK) return status;
183
Steven Moreland05929552019-07-31 17:51:25 -0700184 status = internal::Stability::set(binder.get(), stability, true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000185 if (status != OK) return status;
186
187 *out = binder;
188 return OK;
189}
190
191status_t Parcel::flattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700192{
193 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700194
Martijn Coenen2b631742017-05-05 11:16:59 -0700195 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
196 /* minimum priority for all nodes is nice 0 */
197 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
198 } else {
199 /* minimum priority for all nodes is MAX_NICE(19) */
200 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
201 }
202
Yi Kong91635562018-06-07 14:38:36 -0700203 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800204 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205 if (!local) {
206 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700207 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000208 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700209 }
210 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700211 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800212 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700213 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800214 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700215 } else {
Steven Morelandf0212002018-12-26 13:59:23 -0800216 if (local->isRequestingSid()) {
217 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
218 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700219 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800220 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
221 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700222 }
223 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700224 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800225 obj.binder = 0;
226 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700228
Steven Morelanda86a3562019-08-01 23:28:34 +0000229 return finishFlattenBinder(binder, obj);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230}
231
Steven Morelanda86a3562019-08-01 23:28:34 +0000232status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700233{
Steven Morelanda86a3562019-08-01 23:28:34 +0000234 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700235
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700236 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700237 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000238 case BINDER_TYPE_BINDER: {
239 sp<IBinder> binder = reinterpret_cast<IBinder*>(flat->cookie);
240 return finishUnflattenBinder(binder, out);
241 }
242 case BINDER_TYPE_HANDLE: {
243 sp<IBinder> binder =
244 ProcessState::self()->getStrongProxyForHandle(flat->handle);
245 return finishUnflattenBinder(binder, out);
246 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700247 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 }
249 return BAD_TYPE;
250}
251
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700252// ---------------------------------------------------------------------------
253
254Parcel::Parcel()
255{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800256 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 initState();
258}
259
260Parcel::~Parcel()
261{
262 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800263 LOG_ALLOC("Parcel %p: destroyed", this);
264}
265
266size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800267 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
268 size_t size = gParcelGlobalAllocSize;
269 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
270 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800271}
272
273size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800274 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
275 size_t count = gParcelGlobalAllocCount;
276 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
277 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700278}
279
280const uint8_t* Parcel::data() const
281{
282 return mData;
283}
284
285size_t Parcel::dataSize() const
286{
287 return (mDataSize > mDataPos ? mDataSize : mDataPos);
288}
289
290size_t Parcel::dataAvail() const
291{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700292 size_t result = dataSize() - dataPosition();
293 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700294 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700295 }
296 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700297}
298
299size_t Parcel::dataPosition() const
300{
301 return mDataPos;
302}
303
304size_t Parcel::dataCapacity() const
305{
306 return mDataCapacity;
307}
308
309status_t Parcel::setDataSize(size_t size)
310{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700311 if (size > INT32_MAX) {
312 // don't accept size_t values which may have come from an
313 // inadvertent conversion from a negative int.
314 return BAD_VALUE;
315 }
316
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700317 status_t err;
318 err = continueWrite(size);
319 if (err == NO_ERROR) {
320 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700321 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700322 }
323 return err;
324}
325
326void Parcel::setDataPosition(size_t pos) const
327{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700328 if (pos > INT32_MAX) {
329 // don't accept size_t values which may have come from an
330 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700331 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700332 }
333
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700334 mDataPos = pos;
335 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800336 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700337}
338
339status_t Parcel::setDataCapacity(size_t size)
340{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700341 if (size > INT32_MAX) {
342 // don't accept size_t values which may have come from an
343 // inadvertent conversion from a negative int.
344 return BAD_VALUE;
345 }
346
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700347 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700348 return NO_ERROR;
349}
350
351status_t Parcel::setData(const uint8_t* buffer, size_t len)
352{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700353 if (len > INT32_MAX) {
354 // don't accept size_t values which may have come from an
355 // inadvertent conversion from a negative int.
356 return BAD_VALUE;
357 }
358
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700359 status_t err = restartWrite(len);
360 if (err == NO_ERROR) {
361 memcpy(const_cast<uint8_t*>(data()), buffer, len);
362 mDataSize = len;
363 mFdsKnown = false;
364 }
365 return err;
366}
367
Andreas Huber51faf462011-04-13 10:21:56 -0700368status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700369{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700370 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700371 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800372 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700373 size_t size = parcel->mObjectsSize;
374 int startPos = mDataPos;
375 int firstIndex = -1, lastIndex = -2;
376
377 if (len == 0) {
378 return NO_ERROR;
379 }
380
Nick Kralevichb6b14232015-04-02 09:36:02 -0700381 if (len > INT32_MAX) {
382 // don't accept size_t values which may have come from an
383 // inadvertent conversion from a negative int.
384 return BAD_VALUE;
385 }
386
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700387 // range checks against the source parcel size
388 if ((offset > parcel->mDataSize)
389 || (len > parcel->mDataSize)
390 || (offset + len > parcel->mDataSize)) {
391 return BAD_VALUE;
392 }
393
394 // Count objects in range
395 for (int i = 0; i < (int) size; i++) {
396 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700397 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700398 if (firstIndex == -1) {
399 firstIndex = i;
400 }
401 lastIndex = i;
402 }
403 }
404 int numObjects = lastIndex - firstIndex + 1;
405
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700406 if ((mDataSize+len) > mDataCapacity) {
407 // grow data
408 err = growData(len);
409 if (err != NO_ERROR) {
410 return err;
411 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700412 }
413
414 // append data
415 memcpy(mData + mDataPos, data + offset, len);
416 mDataPos += len;
417 mDataSize += len;
418
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400419 err = NO_ERROR;
420
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700421 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200422 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700423 // grow objects
424 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenenda2f2fd2020-01-22 10:46:25 +0100425 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
426 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700427 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenenda2f2fd2020-01-22 10:46:25 +0100428 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800429 binder_size_t *objects =
430 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700431 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700432 return NO_MEMORY;
433 }
434 mObjects = objects;
435 mObjectsCapacity = newSize;
436 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700437
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700438 // append and acquire objects
439 int idx = mObjectsSize;
440 for (int i = firstIndex; i <= lastIndex; i++) {
441 size_t off = objects[i] - offset + startPos;
442 mObjects[idx++] = off;
443 mObjectsSize++;
444
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700445 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700446 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700447 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700449 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700450 // If this is a file descriptor, we need to dup it so the
451 // new Parcel now owns its own fd, and can declare that we
452 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800453 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800454 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700455 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400456 if (!mAllowFds) {
457 err = FDS_NOT_ALLOWED;
458 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 }
460 }
461 }
462
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400463 return err;
464}
465
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700466int Parcel::compareData(const Parcel& other) {
467 size_t size = dataSize();
468 if (size != other.dataSize()) {
469 return size < other.dataSize() ? -1 : 1;
470 }
471 return memcmp(data(), other.data(), size);
472}
473
Jeff Brown13b16042014-11-11 16:44:25 -0800474bool Parcel::allowFds() const
475{
476 return mAllowFds;
477}
478
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700479bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400480{
481 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700482 if (!allowFds) {
483 mAllowFds = false;
484 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400485 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700486}
487
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700488void Parcel::restoreAllowFds(bool lastValue)
489{
490 mAllowFds = lastValue;
491}
492
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700493bool Parcel::hasFileDescriptors() const
494{
495 if (!mFdsKnown) {
496 scanForFds();
497 }
498 return mHasFds;
499}
500
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000501void Parcel::updateWorkSourceRequestHeaderPosition() const {
502 // Only update the request headers once. We only want to point
503 // to the first headers read/written.
504 if (!mRequestHeaderPresent) {
505 mWorkSourceRequestHeaderPosition = dataPosition();
506 mRequestHeaderPresent = true;
507 }
508}
509
Jooyung Han4df8e2d2019-10-18 16:30:14 +0900510#if defined(__ANDROID_APEX_COM_ANDROID_VNDK_CURRENT__) || (defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__))
Steven Moreland0f452742019-07-31 15:50:51 +0000511constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
512#else
513constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
514#endif
515
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700516// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700517status_t Parcel::writeInterfaceToken(const String16& interface)
518{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000519 const IPCThreadState* threadState = IPCThreadState::self();
520 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000521 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000522 writeInt32(threadState->shouldPropagateWorkSource() ?
523 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Moreland0f452742019-07-31 15:50:51 +0000524 writeInt32(kHeader);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700525 // currently the interface identification token is just its name as a string
526 return writeString16(interface);
527}
528
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000529bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
530{
531 if (!mRequestHeaderPresent) {
532 return false;
533 }
534
535 const size_t initialPosition = dataPosition();
536 setDataPosition(mWorkSourceRequestHeaderPosition);
537 status_t err = writeInt32(uid);
538 setDataPosition(initialPosition);
539 return err == NO_ERROR;
540}
541
Steven Moreland0891c9b2019-05-06 15:05:13 -0700542uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000543{
544 if (!mRequestHeaderPresent) {
545 return IPCThreadState::kUnsetWorkSource;
546 }
547
548 const size_t initialPosition = dataPosition();
549 setDataPosition(mWorkSourceRequestHeaderPosition);
550 uid_t uid = readInt32();
551 setDataPosition(initialPosition);
552 return uid;
553}
554
Mathias Agopian83c04462009-05-22 19:00:22 -0700555bool Parcel::checkInterface(IBinder* binder) const
556{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700557 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700558}
559
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700560bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700561 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700562{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100563 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700564 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700565 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700566 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700567 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700568 if ((threadState->getLastTransactionBinderFlags() &
569 IBinder::FLAG_ONEWAY) != 0) {
570 // For one-way calls, the callee is running entirely
571 // disconnected from the caller, so disable StrictMode entirely.
572 // Not only does disk/network usage not impact the caller, but
573 // there's no way to commuicate back any violations anyway.
574 threadState->setStrictModePolicy(0);
575 } else {
576 threadState->setStrictModePolicy(strictPolicy);
577 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100578 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000579 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100580 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000581 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Moreland0f452742019-07-31 15:50:51 +0000582 // vendor header
583 int32_t header = readInt32();
584 if (header != kHeader) {
585 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
586 return false;
587 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100588 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700589 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700590 if (str == interface) {
591 return true;
592 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700593 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700594 String8(interface).string(), String8(str).string());
595 return false;
596 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700597}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700598
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700599size_t Parcel::objectsCount() const
600{
601 return mObjectsSize;
602}
603
604status_t Parcel::errorCheck() const
605{
606 return mError;
607}
608
609void Parcel::setError(status_t err)
610{
611 mError = err;
612}
613
614status_t Parcel::finishWrite(size_t len)
615{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700616 if (len > INT32_MAX) {
617 // don't accept size_t values which may have come from an
618 // inadvertent conversion from a negative int.
619 return BAD_VALUE;
620 }
621
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700622 //printf("Finish write of %d\n", len);
623 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700624 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700625 if (mDataPos > mDataSize) {
626 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700627 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700628 }
629 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
630 return NO_ERROR;
631}
632
633status_t Parcel::writeUnpadded(const void* data, size_t len)
634{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700635 if (len > INT32_MAX) {
636 // don't accept size_t values which may have come from an
637 // inadvertent conversion from a negative int.
638 return BAD_VALUE;
639 }
640
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700641 size_t end = mDataPos + len;
642 if (end < mDataPos) {
643 // integer overflow
644 return BAD_VALUE;
645 }
646
647 if (end <= mDataCapacity) {
648restart_write:
649 memcpy(mData+mDataPos, data, len);
650 return finishWrite(len);
651 }
652
653 status_t err = growData(len);
654 if (err == NO_ERROR) goto restart_write;
655 return err;
656}
657
658status_t Parcel::write(const void* data, size_t len)
659{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700660 if (len > INT32_MAX) {
661 // don't accept size_t values which may have come from an
662 // inadvertent conversion from a negative int.
663 return BAD_VALUE;
664 }
665
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700666 void* const d = writeInplace(len);
667 if (d) {
668 memcpy(d, data, len);
669 return NO_ERROR;
670 }
671 return mError;
672}
673
674void* Parcel::writeInplace(size_t len)
675{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700676 if (len > INT32_MAX) {
677 // don't accept size_t values which may have come from an
678 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700679 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700680 }
681
682 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700683
684 // sanity check for integer overflow
685 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700686 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700687 }
688
689 if ((mDataPos+padded) <= mDataCapacity) {
690restart_write:
691 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
692 uint8_t* const data = mData+mDataPos;
693
694 // Need to pad at end?
695 if (padded != len) {
696#if BYTE_ORDER == BIG_ENDIAN
697 static const uint32_t mask[4] = {
698 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
699 };
700#endif
701#if BYTE_ORDER == LITTLE_ENDIAN
702 static const uint32_t mask[4] = {
703 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
704 };
705#endif
706 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
707 // *reinterpret_cast<void**>(data+padded-4));
708 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
709 }
710
711 finishWrite(padded);
712 return data;
713 }
714
715 status_t err = growData(padded);
716 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700717 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700718}
719
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800720status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
721 const uint8_t* strData = (uint8_t*)str.data();
722 const size_t strLen= str.length();
723 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100724 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800725 return BAD_VALUE;
726 }
727
728 status_t err = writeInt32(utf16Len);
729 if (err) {
730 return err;
731 }
732
733 // Allocate enough bytes to hold our converted string and its terminating NULL.
734 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
735 if (!dst) {
736 return NO_MEMORY;
737 }
738
Sergio Girof4607432016-07-21 14:46:35 +0100739 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800740
741 return NO_ERROR;
742}
743
744status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
745 if (!str) {
746 return writeInt32(-1);
747 }
748 return writeUtf8AsUtf16(*str);
749}
750
Daniel Normand0337ef2019-09-20 15:46:03 -0700751status_t Parcel::writeByteVectorInternal(const int8_t* data, size_t size) {
752 if (size > std::numeric_limits<int32_t>::max()) {
753 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -0700754 }
755
Daniel Normand0337ef2019-09-20 15:46:03 -0700756 status_t status = writeInt32(size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700757 if (status != OK) {
758 return status;
759 }
760
Daniel Normand0337ef2019-09-20 15:46:03 -0700761 return write(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700762}
763
Casey Dahlin185d3442016-02-09 11:08:35 -0800764status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700765 return writeByteVectorInternal(val.data(), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800766}
767
768status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
769{
Daniel Normand0337ef2019-09-20 15:46:03 -0700770 if (!val) return writeInt32(-1);
771 return writeByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800772}
773
774status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700775 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val.data()), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800776}
777
778status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
779{
Daniel Normand0337ef2019-09-20 15:46:03 -0700780 if (!val) return writeInt32(-1);
781 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800782}
783
Casey Dahlin451ff582015-10-19 18:12:18 -0700784status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
785{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800786 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700787}
788
Casey Dahlinb9872622015-11-25 15:09:45 -0800789status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
790{
791 return writeNullableTypedVector(val, &Parcel::writeInt32);
792}
793
Casey Dahlin451ff582015-10-19 18:12:18 -0700794status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
795{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800796 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700797}
798
Casey Dahlinb9872622015-11-25 15:09:45 -0800799status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
800{
801 return writeNullableTypedVector(val, &Parcel::writeInt64);
802}
803
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800804status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
805{
806 return writeTypedVector(val, &Parcel::writeUint64);
807}
808
809status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
810{
811 return writeNullableTypedVector(val, &Parcel::writeUint64);
812}
813
Casey Dahlin451ff582015-10-19 18:12:18 -0700814status_t Parcel::writeFloatVector(const std::vector<float>& val)
815{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800816 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700817}
818
Casey Dahlinb9872622015-11-25 15:09:45 -0800819status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
820{
821 return writeNullableTypedVector(val, &Parcel::writeFloat);
822}
823
Casey Dahlin451ff582015-10-19 18:12:18 -0700824status_t Parcel::writeDoubleVector(const std::vector<double>& val)
825{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800826 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700827}
828
Casey Dahlinb9872622015-11-25 15:09:45 -0800829status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
830{
831 return writeNullableTypedVector(val, &Parcel::writeDouble);
832}
833
Casey Dahlin451ff582015-10-19 18:12:18 -0700834status_t Parcel::writeBoolVector(const std::vector<bool>& val)
835{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800836 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700837}
838
Casey Dahlinb9872622015-11-25 15:09:45 -0800839status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
840{
841 return writeNullableTypedVector(val, &Parcel::writeBool);
842}
843
Casey Dahlin451ff582015-10-19 18:12:18 -0700844status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
845{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800846 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700847}
848
Casey Dahlinb9872622015-11-25 15:09:45 -0800849status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
850{
851 return writeNullableTypedVector(val, &Parcel::writeChar);
852}
853
Casey Dahlin451ff582015-10-19 18:12:18 -0700854status_t Parcel::writeString16Vector(const std::vector<String16>& val)
855{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800856 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700857}
858
Casey Dahlinb9872622015-11-25 15:09:45 -0800859status_t Parcel::writeString16Vector(
860 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
861{
862 return writeNullableTypedVector(val, &Parcel::writeString16);
863}
864
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800865status_t Parcel::writeUtf8VectorAsUtf16Vector(
866 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
867 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
868}
869
870status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
871 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
872}
873
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700874status_t Parcel::writeInt32(int32_t val)
875{
Andreas Huber84a6d042009-08-17 13:33:27 -0700876 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700877}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800878
879status_t Parcel::writeUint32(uint32_t val)
880{
881 return writeAligned(val);
882}
883
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700884status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700885 if (len > INT32_MAX) {
886 // don't accept size_t values which may have come from an
887 // inadvertent conversion from a negative int.
888 return BAD_VALUE;
889 }
890
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700891 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700892 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700893 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700894 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700895 if (ret == NO_ERROR) {
896 ret = write(val, len * sizeof(*val));
897 }
898 return ret;
899}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700900status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700901 if (len > INT32_MAX) {
902 // don't accept size_t values which may have come from an
903 // inadvertent conversion from a negative int.
904 return BAD_VALUE;
905 }
906
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700907 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700908 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700909 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700910 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700911 if (ret == NO_ERROR) {
912 ret = write(val, len * sizeof(*val));
913 }
914 return ret;
915}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700916
Casey Dahlind6848f52015-10-15 15:44:59 -0700917status_t Parcel::writeBool(bool val)
918{
919 return writeInt32(int32_t(val));
920}
921
922status_t Parcel::writeChar(char16_t val)
923{
924 return writeInt32(int32_t(val));
925}
926
927status_t Parcel::writeByte(int8_t val)
928{
929 return writeInt32(int32_t(val));
930}
931
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700932status_t Parcel::writeInt64(int64_t val)
933{
Andreas Huber84a6d042009-08-17 13:33:27 -0700934 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700935}
936
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700937status_t Parcel::writeUint64(uint64_t val)
938{
939 return writeAligned(val);
940}
941
Serban Constantinescuf683e012013-11-05 16:53:55 +0000942status_t Parcel::writePointer(uintptr_t val)
943{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800944 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000945}
946
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700947status_t Parcel::writeFloat(float val)
948{
Andreas Huber84a6d042009-08-17 13:33:27 -0700949 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700950}
951
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800952#if defined(__mips__) && defined(__mips_hard_float)
953
954status_t Parcel::writeDouble(double val)
955{
956 union {
957 double d;
958 unsigned long long ll;
959 } u;
960 u.d = val;
961 return writeAligned(u.ll);
962}
963
964#else
965
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700966status_t Parcel::writeDouble(double val)
967{
Andreas Huber84a6d042009-08-17 13:33:27 -0700968 return writeAligned(val);
969}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700970
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800971#endif
972
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700973status_t Parcel::writeCString(const char* str)
974{
975 return write(str, strlen(str)+1);
976}
977
978status_t Parcel::writeString8(const String8& str)
979{
980 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100981 // only write string if its length is more than zero characters,
982 // as readString8 will only read if the length field is non-zero.
983 // this is slightly different from how writeString16 works.
984 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700985 err = write(str.string(), str.bytes()+1);
986 }
987 return err;
988}
989
Casey Dahlinb9872622015-11-25 15:09:45 -0800990status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
991{
992 if (!str) {
993 return writeInt32(-1);
994 }
995
996 return writeString16(*str);
997}
998
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700999status_t Parcel::writeString16(const String16& str)
1000{
1001 return writeString16(str.string(), str.size());
1002}
1003
1004status_t Parcel::writeString16(const char16_t* str, size_t len)
1005{
Yi Kong91635562018-06-07 14:38:36 -07001006 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001007
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001008 status_t err = writeInt32(len);
1009 if (err == NO_ERROR) {
1010 len *= sizeof(char16_t);
1011 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1012 if (data) {
1013 memcpy(data, str, len);
1014 *reinterpret_cast<char16_t*>(data+len) = 0;
1015 return NO_ERROR;
1016 }
1017 err = mError;
1018 }
1019 return err;
1020}
1021
1022status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1023{
Steven Morelanda86a3562019-08-01 23:28:34 +00001024 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001025}
1026
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001027status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1028{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001029 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001030}
1031
Casey Dahlinb9872622015-11-25 15:09:45 -08001032status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1033{
1034 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1035}
1036
1037status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001038 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001039}
1040
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001041status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001042 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001043}
1044
Casey Dahlinb9872622015-11-25 15:09:45 -08001045status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1046 if (!parcelable) {
1047 return writeInt32(0);
1048 }
1049
1050 return writeParcelable(*parcelable);
1051}
1052
Christopher Wiley97f048d2015-11-19 06:49:05 -08001053status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1054 status_t status = writeInt32(1); // parcelable is not null.
1055 if (status != OK) {
1056 return status;
1057 }
1058 return parcelable.writeToParcel(this);
1059}
1060
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001061status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001062{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001063 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001064 return BAD_TYPE;
1065
1066 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001067 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001068 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001070 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001071 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001073 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1074 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075
1076 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001077 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078 return err;
1079 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001080 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001081 return err;
1082}
1083
Jeff Brown93ff1f92011-11-04 19:01:44 -07001084status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001085{
1086 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001087 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001088 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001089 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001090 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001091 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001092 return writeObject(obj, true);
1093}
1094
1095status_t Parcel::writeDupFileDescriptor(int fd)
1096{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001097 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001098 if (dupFd < 0) {
1099 return -errno;
1100 }
1101 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001102 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001103 close(dupFd);
1104 }
1105 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001106}
1107
Dianne Hackborn1941a402016-08-29 12:30:43 -07001108status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1109{
1110 writeInt32(0);
1111 return writeFileDescriptor(fd, takeOwnership);
1112}
1113
Ryo Hashimotobf551892018-05-31 16:58:35 +09001114status_t Parcel::writeDupParcelFileDescriptor(int fd)
1115{
1116 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1117 if (dupFd < 0) {
1118 return -errno;
1119 }
1120 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1121 if (err != OK) {
1122 close(dupFd);
1123 }
1124 return err;
1125}
1126
Christopher Wiley2cf19952016-04-11 11:09:37 -07001127status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001128 return writeDupFileDescriptor(fd.get());
1129}
1130
Christopher Wiley2cf19952016-04-11 11:09:37 -07001131status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001132 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1133}
1134
Christopher Wiley2cf19952016-04-11 11:09:37 -07001135status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001136 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1137}
1138
Jeff Brown13b16042014-11-11 16:44:25 -08001139status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001140{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001141 if (len > INT32_MAX) {
1142 // don't accept size_t values which may have come from an
1143 // inadvertent conversion from a negative int.
1144 return BAD_VALUE;
1145 }
1146
Jeff Brown13b16042014-11-11 16:44:25 -08001147 status_t status;
1148 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001149 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001150 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001151 if (status) return status;
1152
1153 void* ptr = writeInplace(len);
1154 if (!ptr) return NO_MEMORY;
1155
Jeff Brown13b16042014-11-11 16:44:25 -08001156 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001157 return NO_ERROR;
1158 }
1159
Steve Block6807e592011-10-20 11:56:00 +01001160 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001161 int fd = ashmem_create_region("Parcel Blob", len);
1162 if (fd < 0) return NO_MEMORY;
1163
1164 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1165 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001166 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001167 } else {
Yi Kong91635562018-06-07 14:38:36 -07001168 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001169 if (ptr == MAP_FAILED) {
1170 status = -errno;
1171 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001172 if (!mutableCopy) {
1173 result = ashmem_set_prot_region(fd, PROT_READ);
1174 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001175 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001176 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001177 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001178 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001179 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001180 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001181 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001182 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001183 return NO_ERROR;
1184 }
1185 }
1186 }
1187 }
1188 ::munmap(ptr, len);
1189 }
1190 ::close(fd);
1191 return status;
1192}
1193
Jeff Brown13b16042014-11-11 16:44:25 -08001194status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1195{
1196 // Must match up with what's done in writeBlob.
1197 if (!mAllowFds) return FDS_NOT_ALLOWED;
1198 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1199 if (status) return status;
1200 return writeDupFileDescriptor(fd);
1201}
1202
Mathias Agopiane1424282013-07-29 21:24:40 -07001203status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001204{
1205 status_t err;
1206
1207 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001208 const size_t len = val.getFlattenedSize();
1209 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001210
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001211 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001212 // don't accept size_t values which may have come from an
1213 // inadvertent conversion from a negative int.
1214 return BAD_VALUE;
1215 }
1216
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001217 err = this->writeInt32(len);
1218 if (err) return err;
1219
1220 err = this->writeInt32(fd_count);
1221 if (err) return err;
1222
1223 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001224 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001225 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001226 return BAD_VALUE;
1227
Yi Kong91635562018-06-07 14:38:36 -07001228 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001229 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001230 fds = new (std::nothrow) int[fd_count];
1231 if (fds == nullptr) {
1232 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1233 return BAD_VALUE;
1234 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001235 }
1236
1237 err = val.flatten(buf, len, fds, fd_count);
1238 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1239 err = this->writeDupFileDescriptor( fds[i] );
1240 }
1241
1242 if (fd_count) {
1243 delete [] fds;
1244 }
1245
1246 return err;
1247}
1248
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001249status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1250{
1251 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1252 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1253 if (enoughData && enoughObjects) {
1254restart_write:
1255 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001256
Christopher Tate98e67d32015-06-03 18:44:15 -07001257 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001258 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001259 if (!mAllowFds) {
1260 // fail before modifying our object index
1261 return FDS_NOT_ALLOWED;
1262 }
1263 mHasFds = mFdsKnown = true;
1264 }
1265
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001266 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001267 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001268 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001269 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001270 mObjectsSize++;
1271 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001272
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001273 return finishWrite(sizeof(flat_binder_object));
1274 }
1275
1276 if (!enoughData) {
1277 const status_t err = growData(sizeof(val));
1278 if (err != NO_ERROR) return err;
1279 }
1280 if (!enoughObjects) {
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01001281 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1282 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001283 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01001284 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001285 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001286 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001287 mObjects = objects;
1288 mObjectsCapacity = newSize;
1289 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001290
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001291 goto restart_write;
1292}
1293
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001294status_t Parcel::writeNoException()
1295{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001296 binder::Status status;
1297 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001298}
1299
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001300status_t Parcel::validateReadData(size_t upperBound) const
1301{
1302 // Don't allow non-object reads on object data
1303 if (mObjectsSorted || mObjectsSize <= 1) {
1304data_sorted:
1305 // Expect to check only against the next object
1306 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1307 // For some reason the current read position is greater than the next object
1308 // hint. Iterate until we find the right object
1309 size_t nextObject = mNextObjectHint;
1310 do {
1311 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1312 // Requested info overlaps with an object
1313 ALOGE("Attempt to read from protected data in Parcel %p", this);
1314 return PERMISSION_DENIED;
1315 }
1316 nextObject++;
1317 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1318 mNextObjectHint = nextObject;
1319 }
1320 return NO_ERROR;
1321 }
1322 // Quickly determine if mObjects is sorted.
1323 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1324 binder_size_t* prevObj = currObj;
1325 while (currObj > mObjects) {
1326 prevObj--;
1327 if(*prevObj > *currObj) {
1328 goto data_unsorted;
1329 }
1330 currObj--;
1331 }
1332 mObjectsSorted = true;
1333 goto data_sorted;
1334
1335data_unsorted:
1336 // Insertion Sort mObjects
1337 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1338 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1339 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1340 binder_size_t temp = *iter0;
1341 binder_size_t* iter1 = iter0 - 1;
1342 while (iter1 >= mObjects && *iter1 > temp) {
1343 *(iter1 + 1) = *iter1;
1344 iter1--;
1345 }
1346 *(iter1 + 1) = temp;
1347 }
1348 mNextObjectHint = 0;
1349 mObjectsSorted = true;
1350 goto data_sorted;
1351}
1352
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001353status_t Parcel::read(void* outData, size_t len) const
1354{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001355 if (len > INT32_MAX) {
1356 // don't accept size_t values which may have come from an
1357 // inadvertent conversion from a negative int.
1358 return BAD_VALUE;
1359 }
1360
1361 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1362 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001363 if (mObjectsSize > 0) {
1364 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001365 if(err != NO_ERROR) {
1366 // Still increment the data position by the expected length
1367 mDataPos += pad_size(len);
1368 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1369 return err;
1370 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001371 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001372 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001373 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001374 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001375 return NO_ERROR;
1376 }
1377 return NOT_ENOUGH_DATA;
1378}
1379
1380const void* Parcel::readInplace(size_t len) const
1381{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001382 if (len > INT32_MAX) {
1383 // don't accept size_t values which may have come from an
1384 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001385 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001386 }
1387
1388 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1389 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001390 if (mObjectsSize > 0) {
1391 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001392 if(err != NO_ERROR) {
1393 // Still increment the data position by the expected length
1394 mDataPos += pad_size(len);
1395 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001396 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001397 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001398 }
1399
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001400 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001401 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001402 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001403 return data;
1404 }
Yi Kong91635562018-06-07 14:38:36 -07001405 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001406}
1407
Andreas Huber84a6d042009-08-17 13:33:27 -07001408template<class T>
1409status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001410 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001411
1412 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001413 if (mObjectsSize > 0) {
1414 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001415 if(err != NO_ERROR) {
1416 // Still increment the data position by the expected length
1417 mDataPos += sizeof(T);
1418 return err;
1419 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001420 }
1421
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001422 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001423 mDataPos += sizeof(T);
1424 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001425 return NO_ERROR;
1426 } else {
1427 return NOT_ENOUGH_DATA;
1428 }
1429}
1430
Andreas Huber84a6d042009-08-17 13:33:27 -07001431template<class T>
1432T Parcel::readAligned() const {
1433 T result;
1434 if (readAligned(&result) != NO_ERROR) {
1435 result = 0;
1436 }
1437
1438 return result;
1439}
1440
1441template<class T>
1442status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001443 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001444
1445 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1446restart_write:
1447 *reinterpret_cast<T*>(mData+mDataPos) = val;
1448 return finishWrite(sizeof(val));
1449 }
1450
1451 status_t err = growData(sizeof(val));
1452 if (err == NO_ERROR) goto restart_write;
1453 return err;
1454}
1455
Casey Dahlin185d3442016-02-09 11:08:35 -08001456status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001457 size_t size;
1458 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1459 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001460}
1461
1462status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001463 size_t size;
1464 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1465 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001466}
1467
1468status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001469 size_t size;
1470 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001471 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001472 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001473 // This occurs when writing a null byte vector.
1474 return OK;
1475 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001476 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001477}
1478
1479status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001480 size_t size;
1481 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001482 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001483 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001484 // This occurs when writing a null byte vector.
1485 return OK;
1486 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001487 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001488}
1489
Casey Dahlinb9872622015-11-25 15:09:45 -08001490status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1491 return readNullableTypedVector(val, &Parcel::readInt32);
1492}
1493
Casey Dahlin451ff582015-10-19 18:12:18 -07001494status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001495 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001496}
1497
Casey Dahlinb9872622015-11-25 15:09:45 -08001498status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1499 return readNullableTypedVector(val, &Parcel::readInt64);
1500}
1501
Casey Dahlin451ff582015-10-19 18:12:18 -07001502status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001503 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001504}
1505
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001506status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1507 return readNullableTypedVector(val, &Parcel::readUint64);
1508}
1509
1510status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1511 return readTypedVector(val, &Parcel::readUint64);
1512}
1513
Casey Dahlinb9872622015-11-25 15:09:45 -08001514status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1515 return readNullableTypedVector(val, &Parcel::readFloat);
1516}
1517
Casey Dahlin451ff582015-10-19 18:12:18 -07001518status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001519 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001520}
1521
Casey Dahlinb9872622015-11-25 15:09:45 -08001522status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1523 return readNullableTypedVector(val, &Parcel::readDouble);
1524}
1525
Casey Dahlin451ff582015-10-19 18:12:18 -07001526status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001527 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001528}
1529
Casey Dahlinb9872622015-11-25 15:09:45 -08001530status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1531 const int32_t start = dataPosition();
1532 int32_t size;
1533 status_t status = readInt32(&size);
1534 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001535
Casey Dahlinb9872622015-11-25 15:09:45 -08001536 if (status != OK || size < 0) {
1537 return status;
1538 }
1539
1540 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001541 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001542
1543 status = readBoolVector(val->get());
1544
1545 if (status != OK) {
1546 val->reset();
1547 }
1548
1549 return status;
1550}
1551
1552status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001553 int32_t size;
1554 status_t status = readInt32(&size);
1555
1556 if (status != OK) {
1557 return status;
1558 }
1559
1560 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001561 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001562 }
1563
1564 val->resize(size);
1565
1566 /* C++ bool handling means a vector of bools isn't necessarily addressable
1567 * (we might use individual bits)
1568 */
Christopher Wiley97887982015-10-27 16:33:47 -07001569 bool data;
1570 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001571 status = readBool(&data);
1572 (*val)[i] = data;
1573
1574 if (status != OK) {
1575 return status;
1576 }
1577 }
1578
1579 return OK;
1580}
1581
Casey Dahlinb9872622015-11-25 15:09:45 -08001582status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1583 return readNullableTypedVector(val, &Parcel::readChar);
1584}
1585
Casey Dahlin451ff582015-10-19 18:12:18 -07001586status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001587 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001588}
1589
Casey Dahlinb9872622015-11-25 15:09:45 -08001590status_t Parcel::readString16Vector(
1591 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1592 return readNullableTypedVector(val, &Parcel::readString16);
1593}
1594
Casey Dahlin451ff582015-10-19 18:12:18 -07001595status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001596 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001597}
1598
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001599status_t Parcel::readUtf8VectorFromUtf16Vector(
1600 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1601 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1602}
1603
1604status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1605 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1606}
Casey Dahlin451ff582015-10-19 18:12:18 -07001607
Andreas Huber84a6d042009-08-17 13:33:27 -07001608status_t Parcel::readInt32(int32_t *pArg) const
1609{
1610 return readAligned(pArg);
1611}
1612
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001613int32_t Parcel::readInt32() const
1614{
Andreas Huber84a6d042009-08-17 13:33:27 -07001615 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001616}
1617
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001618status_t Parcel::readUint32(uint32_t *pArg) const
1619{
1620 return readAligned(pArg);
1621}
1622
1623uint32_t Parcel::readUint32() const
1624{
1625 return readAligned<uint32_t>();
1626}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001627
1628status_t Parcel::readInt64(int64_t *pArg) const
1629{
Andreas Huber84a6d042009-08-17 13:33:27 -07001630 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001631}
1632
1633
1634int64_t Parcel::readInt64() const
1635{
Andreas Huber84a6d042009-08-17 13:33:27 -07001636 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001637}
1638
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001639status_t Parcel::readUint64(uint64_t *pArg) const
1640{
1641 return readAligned(pArg);
1642}
1643
1644uint64_t Parcel::readUint64() const
1645{
1646 return readAligned<uint64_t>();
1647}
1648
Serban Constantinescuf683e012013-11-05 16:53:55 +00001649status_t Parcel::readPointer(uintptr_t *pArg) const
1650{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001651 status_t ret;
1652 binder_uintptr_t ptr;
1653 ret = readAligned(&ptr);
1654 if (!ret)
1655 *pArg = ptr;
1656 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001657}
1658
1659uintptr_t Parcel::readPointer() const
1660{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001661 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001662}
1663
1664
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001665status_t Parcel::readFloat(float *pArg) const
1666{
Andreas Huber84a6d042009-08-17 13:33:27 -07001667 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001668}
1669
1670
1671float Parcel::readFloat() const
1672{
Andreas Huber84a6d042009-08-17 13:33:27 -07001673 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001674}
1675
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001676#if defined(__mips__) && defined(__mips_hard_float)
1677
1678status_t Parcel::readDouble(double *pArg) const
1679{
1680 union {
1681 double d;
1682 unsigned long long ll;
1683 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001684 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001685 status_t status;
1686 status = readAligned(&u.ll);
1687 *pArg = u.d;
1688 return status;
1689}
1690
1691double Parcel::readDouble() const
1692{
1693 union {
1694 double d;
1695 unsigned long long ll;
1696 } u;
1697 u.ll = readAligned<unsigned long long>();
1698 return u.d;
1699}
1700
1701#else
1702
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001703status_t Parcel::readDouble(double *pArg) const
1704{
Andreas Huber84a6d042009-08-17 13:33:27 -07001705 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001706}
1707
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001708double Parcel::readDouble() const
1709{
Andreas Huber84a6d042009-08-17 13:33:27 -07001710 return readAligned<double>();
1711}
1712
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001713#endif
1714
Andreas Huber84a6d042009-08-17 13:33:27 -07001715status_t Parcel::readIntPtr(intptr_t *pArg) const
1716{
1717 return readAligned(pArg);
1718}
1719
1720
1721intptr_t Parcel::readIntPtr() const
1722{
1723 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001724}
1725
Casey Dahlind6848f52015-10-15 15:44:59 -07001726status_t Parcel::readBool(bool *pArg) const
1727{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001728 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001729 status_t ret = readInt32(&tmp);
1730 *pArg = (tmp != 0);
1731 return ret;
1732}
1733
1734bool Parcel::readBool() const
1735{
1736 return readInt32() != 0;
1737}
1738
1739status_t Parcel::readChar(char16_t *pArg) const
1740{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001741 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001742 status_t ret = readInt32(&tmp);
1743 *pArg = char16_t(tmp);
1744 return ret;
1745}
1746
1747char16_t Parcel::readChar() const
1748{
1749 return char16_t(readInt32());
1750}
1751
1752status_t Parcel::readByte(int8_t *pArg) const
1753{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001754 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001755 status_t ret = readInt32(&tmp);
1756 *pArg = int8_t(tmp);
1757 return ret;
1758}
1759
1760int8_t Parcel::readByte() const
1761{
1762 return int8_t(readInt32());
1763}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001764
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001765status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1766 size_t utf16Size = 0;
1767 const char16_t* src = readString16Inplace(&utf16Size);
1768 if (!src) {
1769 return UNEXPECTED_NULL;
1770 }
1771
1772 // Save ourselves the trouble, we're done.
1773 if (utf16Size == 0u) {
1774 str->clear();
1775 return NO_ERROR;
1776 }
1777
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001778 // Allow for closing '\0'
1779 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1780 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001781 return BAD_VALUE;
1782 }
1783 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001784 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001785 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001786 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1787 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001788 return NO_ERROR;
1789}
1790
1791status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1792 const int32_t start = dataPosition();
1793 int32_t size;
1794 status_t status = readInt32(&size);
1795 str->reset();
1796
1797 if (status != OK || size < 0) {
1798 return status;
1799 }
1800
1801 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001802 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001803 return readUtf8FromUtf16(str->get());
1804}
1805
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001806const char* Parcel::readCString() const
1807{
Steven Morelandf5e6c7e2019-05-17 13:14:06 -07001808 if (mDataPos < mDataSize) {
1809 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001810 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1811 // is the string's trailing NUL within the parcel's valid bounds?
1812 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1813 if (eos) {
1814 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001815 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001816 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001817 return str;
1818 }
1819 }
Yi Kong91635562018-06-07 14:38:36 -07001820 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001821}
1822
1823String8 Parcel::readString8() const
1824{
Roshan Pius87b64d22016-07-18 12:51:02 -07001825 String8 retString;
1826 status_t status = readString8(&retString);
1827 if (status != OK) {
1828 // We don't care about errors here, so just return an empty string.
1829 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001830 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001831 return retString;
1832}
1833
1834status_t Parcel::readString8(String8* pArg) const
1835{
1836 int32_t size;
1837 status_t status = readInt32(&size);
1838 if (status != OK) {
1839 return status;
1840 }
1841 // watch for potential int overflow from size+1
1842 if (size < 0 || size >= INT32_MAX) {
1843 return BAD_VALUE;
1844 }
1845 // |writeString8| writes nothing for empty string.
1846 if (size == 0) {
1847 *pArg = String8();
1848 return OK;
1849 }
1850 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07001851 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07001852 return BAD_VALUE;
1853 }
1854 pArg->setTo(str, size);
1855 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001856}
1857
1858String16 Parcel::readString16() const
1859{
1860 size_t len;
1861 const char16_t* str = readString16Inplace(&len);
1862 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001863 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001864 return String16();
1865}
1866
Casey Dahlinb9872622015-11-25 15:09:45 -08001867status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1868{
1869 const int32_t start = dataPosition();
1870 int32_t size;
1871 status_t status = readInt32(&size);
1872 pArg->reset();
1873
1874 if (status != OK || size < 0) {
1875 return status;
1876 }
1877
1878 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001879 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001880
1881 status = readString16(pArg->get());
1882
1883 if (status != OK) {
1884 pArg->reset();
1885 }
1886
1887 return status;
1888}
1889
Casey Dahlin451ff582015-10-19 18:12:18 -07001890status_t Parcel::readString16(String16* pArg) const
1891{
1892 size_t len;
1893 const char16_t* str = readString16Inplace(&len);
1894 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001895 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001896 return 0;
1897 } else {
1898 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001899 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001900 }
1901}
1902
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001903const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1904{
1905 int32_t size = readInt32();
1906 // watch for potential int overflow from size+1
1907 if (size >= 0 && size < INT32_MAX) {
1908 *outLen = size;
1909 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001910 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001911 return str;
1912 }
1913 }
1914 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001915 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001916}
1917
Casey Dahlinf0c13772015-10-27 18:33:56 -07001918status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1919{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001920 status_t status = readNullableStrongBinder(val);
1921 if (status == OK && !val->get()) {
1922 status = UNEXPECTED_NULL;
1923 }
1924 return status;
1925}
1926
1927status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1928{
Steven Morelanda86a3562019-08-01 23:28:34 +00001929 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001930}
1931
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001932sp<IBinder> Parcel::readStrongBinder() const
1933{
1934 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001935 // Note that a lot of code in Android reads binders by hand with this
1936 // method, and that code has historically been ok with getting nullptr
1937 // back (while ignoring error codes).
1938 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001939 return val;
1940}
1941
Christopher Wiley97f048d2015-11-19 06:49:05 -08001942status_t Parcel::readParcelable(Parcelable* parcelable) const {
1943 int32_t have_parcelable = 0;
1944 status_t status = readInt32(&have_parcelable);
1945 if (status != OK) {
1946 return status;
1947 }
1948 if (!have_parcelable) {
1949 return UNEXPECTED_NULL;
1950 }
1951 return parcelable->readFromParcel(this);
1952}
1953
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001954int32_t Parcel::readExceptionCode() const
1955{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001956 binder::Status status;
1957 status.readFromParcel(*this);
1958 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001959}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001960
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001961native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001962{
1963 int numFds, numInts;
1964 status_t err;
1965 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001966 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001967 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001968 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001969
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001970 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001971 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001972 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001973 }
1974
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001975 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001976 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001977 if (h->data[i] < 0) {
1978 for (int j = 0; j < i; j++) {
1979 close(h->data[j]);
1980 }
1981 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001982 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001983 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001984 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001985 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001986 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001987 native_handle_close(h);
1988 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001989 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001990 }
1991 return h;
1992}
1993
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001994int Parcel::readFileDescriptor() const
1995{
1996 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001997
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001998 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001999 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002000 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002001
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002002 return BAD_TYPE;
2003}
2004
Dianne Hackborn1941a402016-08-29 12:30:43 -07002005int Parcel::readParcelFileDescriptor() const
2006{
2007 int32_t hasComm = readInt32();
2008 int fd = readFileDescriptor();
2009 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002010 // detach (owned by the binder driver)
2011 int comm = readFileDescriptor();
2012
2013 // warning: this must be kept in sync with:
2014 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2015 enum ParcelFileDescriptorStatus {
2016 DETACHED = 2,
2017 };
2018
2019#if BYTE_ORDER == BIG_ENDIAN
2020 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2021#endif
2022#if BYTE_ORDER == LITTLE_ENDIAN
2023 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2024#endif
2025
2026 ssize_t written = TEMP_FAILURE_RETRY(
2027 ::write(comm, &message, sizeof(message)));
2028
2029 if (written == -1 || written != sizeof(message)) {
2030 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2031 written, strerror(errno));
2032 return BAD_TYPE;
2033 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002034 }
2035 return fd;
2036}
2037
Christopher Wiley2cf19952016-04-11 11:09:37 -07002038status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002039{
2040 int got = readFileDescriptor();
2041
2042 if (got == BAD_TYPE) {
2043 return BAD_TYPE;
2044 }
2045
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002046 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002047
2048 if (val->get() < 0) {
2049 return BAD_VALUE;
2050 }
2051
2052 return OK;
2053}
2054
Ryo Hashimotobf551892018-05-31 16:58:35 +09002055status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2056{
2057 int got = readParcelFileDescriptor();
2058
2059 if (got == BAD_TYPE) {
2060 return BAD_TYPE;
2061 }
2062
2063 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2064
2065 if (val->get() < 0) {
2066 return BAD_VALUE;
2067 }
2068
2069 return OK;
2070}
Casey Dahlin06673e32015-11-23 13:24:23 -08002071
Christopher Wiley2cf19952016-04-11 11:09:37 -07002072status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002073 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2074}
2075
Christopher Wiley2cf19952016-04-11 11:09:37 -07002076status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002077 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2078}
2079
Jeff Brown5707dbf2011-09-23 21:17:56 -07002080status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2081{
Jeff Brown13b16042014-11-11 16:44:25 -08002082 int32_t blobType;
2083 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002084 if (status) return status;
2085
Jeff Brown13b16042014-11-11 16:44:25 -08002086 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002087 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002088 const void* ptr = readInplace(len);
2089 if (!ptr) return BAD_VALUE;
2090
Jeff Brown13b16042014-11-11 16:44:25 -08002091 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002092 return NO_ERROR;
2093 }
2094
Steve Block6807e592011-10-20 11:56:00 +01002095 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002096 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002097 int fd = readFileDescriptor();
2098 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2099
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002100 if (!ashmem_valid(fd)) {
2101 ALOGE("invalid fd");
2102 return BAD_VALUE;
2103 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002104 int size = ashmem_get_size_region(fd);
2105 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002106 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002107 return BAD_VALUE;
2108 }
Yi Kong91635562018-06-07 14:38:36 -07002109 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002110 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002111 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002112
Jeff Brown13b16042014-11-11 16:44:25 -08002113 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002114 return NO_ERROR;
2115}
2116
Mathias Agopiane1424282013-07-29 21:24:40 -07002117status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002118{
2119 // size
2120 const size_t len = this->readInt32();
2121 const size_t fd_count = this->readInt32();
2122
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002123 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002124 // don't accept size_t values which may have come from an
2125 // inadvertent conversion from a negative int.
2126 return BAD_VALUE;
2127 }
2128
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002129 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002130 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002131 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002132 return BAD_VALUE;
2133
Yi Kong91635562018-06-07 14:38:36 -07002134 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002135 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002136 fds = new (std::nothrow) int[fd_count];
2137 if (fds == nullptr) {
2138 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2139 return BAD_VALUE;
2140 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002141 }
2142
2143 status_t err = NO_ERROR;
2144 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002145 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002146 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002147 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002148 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 -07002149 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2150 // Close all the file descriptors that were dup-ed.
2151 for (size_t j=0; j<i ;j++) {
2152 close(fds[j]);
2153 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002154 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002155 }
2156
2157 if (err == NO_ERROR) {
2158 err = val.unflatten(buf, len, fds, fd_count);
2159 }
2160
2161 if (fd_count) {
2162 delete [] fds;
2163 }
2164
2165 return err;
2166}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002167const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2168{
2169 const size_t DPOS = mDataPos;
2170 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2171 const flat_binder_object* obj
2172 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2173 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002174 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002175 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002176 // the object list, so we don't want to check for it when
2177 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002178 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002179 return obj;
2180 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002181
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002182 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002183 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002184 const size_t N = mObjectsSize;
2185 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002186
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002187 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002188 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002189 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002190
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002191 // Start at the current hint position, looking for an object at
2192 // the current data position.
2193 if (opos < N) {
2194 while (opos < (N-1) && OBJS[opos] < DPOS) {
2195 opos++;
2196 }
2197 } else {
2198 opos = N-1;
2199 }
2200 if (OBJS[opos] == DPOS) {
2201 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002202 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002203 this, DPOS, opos);
2204 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002205 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002206 return obj;
2207 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002208
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002209 // Look backwards for it...
2210 while (opos > 0 && OBJS[opos] > DPOS) {
2211 opos--;
2212 }
2213 if (OBJS[opos] == DPOS) {
2214 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002215 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002216 this, DPOS, opos);
2217 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002218 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002219 return obj;
2220 }
2221 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002222 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 -07002223 this, DPOS);
2224 }
Yi Kong91635562018-06-07 14:38:36 -07002225 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002226}
2227
2228void Parcel::closeFileDescriptors()
2229{
2230 size_t i = mObjectsSize;
2231 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002232 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002233 }
2234 while (i > 0) {
2235 i--;
2236 const flat_binder_object* flat
2237 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002238 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002239 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002240 close(flat->handle);
2241 }
2242 }
2243}
2244
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002245uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002246{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002247 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002248}
2249
2250size_t Parcel::ipcDataSize() const
2251{
2252 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2253}
2254
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002255uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002256{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002257 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002258}
2259
2260size_t Parcel::ipcObjectsCount() const
2261{
2262 return mObjectsSize;
2263}
2264
2265void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002266 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002267{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002268 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002269 freeDataNoInit();
2270 mError = NO_ERROR;
2271 mData = const_cast<uint8_t*>(data);
2272 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002273 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002274 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002275 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002276 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002277 mObjectsSize = mObjectsCapacity = objectsCount;
2278 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002279 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002280 mOwner = relFunc;
2281 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002282 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002283 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002284 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002285 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002286 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002287 mObjectsSize = 0;
2288 break;
2289 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002290 const flat_binder_object* flat
2291 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2292 uint32_t type = flat->hdr.type;
2293 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2294 type == BINDER_TYPE_FD)) {
2295 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2296 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2297 // recover gracefully by clearing out the objects, and releasing the objects we do
2298 // know about.
2299 android_errorWriteLog(0x534e4554, "135930648");
2300 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2301 __func__, type, (uint64_t)offset);
2302 releaseObjects();
2303 mObjectsSize = 0;
2304 break;
2305 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002306 minOffset = offset + sizeof(flat_binder_object);
2307 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002308 scanForFds();
2309}
2310
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002311void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002312{
2313 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002314
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002315 if (errorCheck() != NO_ERROR) {
2316 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002317 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002318 } else if (dataSize() > 0) {
2319 const uint8_t* DATA = data();
2320 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002321 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002322 const size_t N = objectsCount();
2323 for (size_t i=0; i<N; i++) {
2324 const flat_binder_object* flat
2325 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2326 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002327 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002328 << " = " << flat->binder;
2329 }
2330 } else {
2331 to << "NULL";
2332 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002333
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002334 to << ")";
2335}
2336
2337void Parcel::releaseObjects()
2338{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002339 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002340 if (i == 0) {
2341 return;
2342 }
2343 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002344 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002345 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002346 while (i > 0) {
2347 i--;
2348 const flat_binder_object* flat
2349 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002350 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002351 }
2352}
2353
2354void Parcel::acquireObjects()
2355{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002356 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002357 if (i == 0) {
2358 return;
2359 }
2360 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002361 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002362 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002363 while (i > 0) {
2364 i--;
2365 const flat_binder_object* flat
2366 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002367 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002368 }
2369}
2370
2371void Parcel::freeData()
2372{
2373 freeDataNoInit();
2374 initState();
2375}
2376
2377void Parcel::freeDataNoInit()
2378{
2379 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002380 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002381 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002382 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2383 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002384 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002385 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002386 if (mData) {
2387 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002388 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002389 if (mDataCapacity <= gParcelGlobalAllocSize) {
2390 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2391 } else {
2392 gParcelGlobalAllocSize = 0;
2393 }
2394 if (gParcelGlobalAllocCount > 0) {
2395 gParcelGlobalAllocCount--;
2396 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002397 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002398 free(mData);
2399 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002400 if (mObjects) free(mObjects);
2401 }
2402}
2403
2404status_t Parcel::growData(size_t len)
2405{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002406 if (len > INT32_MAX) {
2407 // don't accept size_t values which may have come from an
2408 // inadvertent conversion from a negative int.
2409 return BAD_VALUE;
2410 }
2411
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01002412 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2413 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002414 size_t newSize = ((mDataSize+len)*3)/2;
2415 return (newSize <= mDataSize)
2416 ? (status_t) NO_MEMORY
2417 : continueWrite(newSize);
2418}
2419
2420status_t Parcel::restartWrite(size_t desired)
2421{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002422 if (desired > INT32_MAX) {
2423 // don't accept size_t values which may have come from an
2424 // inadvertent conversion from a negative int.
2425 return BAD_VALUE;
2426 }
2427
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002428 if (mOwner) {
2429 freeData();
2430 return continueWrite(desired);
2431 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002432
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002433 uint8_t* data = (uint8_t*)realloc(mData, desired);
2434 if (!data && desired > mDataCapacity) {
2435 mError = NO_MEMORY;
2436 return NO_MEMORY;
2437 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002438
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002439 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002440
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002441 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002442 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002443 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002444 gParcelGlobalAllocSize += desired;
2445 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002446 if (!mData) {
2447 gParcelGlobalAllocCount++;
2448 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002449 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002450 mData = data;
2451 mDataCapacity = desired;
2452 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002453
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002454 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002455 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2456 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2457
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002458 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002459 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002460 mObjectsSize = mObjectsCapacity = 0;
2461 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002462 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002463 mHasFds = false;
2464 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002465 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002466
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002467 return NO_ERROR;
2468}
2469
2470status_t Parcel::continueWrite(size_t desired)
2471{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002472 if (desired > INT32_MAX) {
2473 // don't accept size_t values which may have come from an
2474 // inadvertent conversion from a negative int.
2475 return BAD_VALUE;
2476 }
2477
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002478 // If shrinking, first adjust for any objects that appear
2479 // after the new data size.
2480 size_t objectsSize = mObjectsSize;
2481 if (desired < mDataSize) {
2482 if (desired == 0) {
2483 objectsSize = 0;
2484 } else {
2485 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002486 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002487 break;
2488 objectsSize--;
2489 }
2490 }
2491 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002492
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002493 if (mOwner) {
2494 // If the size is going to zero, just release the owner's data.
2495 if (desired == 0) {
2496 freeData();
2497 return NO_ERROR;
2498 }
2499
2500 // If there is a different owner, we need to take
2501 // posession.
2502 uint8_t* data = (uint8_t*)malloc(desired);
2503 if (!data) {
2504 mError = NO_MEMORY;
2505 return NO_MEMORY;
2506 }
Yi Kong91635562018-06-07 14:38:36 -07002507 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002508
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002509 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002510 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002511 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002512 free(data);
2513
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002514 mError = NO_MEMORY;
2515 return NO_MEMORY;
2516 }
2517
2518 // Little hack to only acquire references on objects
2519 // we will be keeping.
2520 size_t oldObjectsSize = mObjectsSize;
2521 mObjectsSize = objectsSize;
2522 acquireObjects();
2523 mObjectsSize = oldObjectsSize;
2524 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002525
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002526 if (mData) {
2527 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2528 }
2529 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002530 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002531 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002532 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002533 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002534 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002535
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002536 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002537 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002538 gParcelGlobalAllocSize += desired;
2539 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002540 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002541
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002542 mData = data;
2543 mObjects = objects;
2544 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002545 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002546 mDataCapacity = desired;
2547 mObjectsSize = mObjectsCapacity = objectsSize;
2548 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002549 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002550
2551 } else if (mData) {
2552 if (objectsSize < mObjectsSize) {
2553 // Need to release refs on any objects we are dropping.
2554 const sp<ProcessState> proc(ProcessState::self());
2555 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2556 const flat_binder_object* flat
2557 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002558 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002559 // will need to rescan because we may have lopped off the only FDs
2560 mFdsKnown = false;
2561 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002562 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002563 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002564
2565 if (objectsSize == 0) {
2566 free(mObjects);
2567 mObjects = nullptr;
Michael Wachenschwanzc67d9f32019-10-15 11:49:22 -07002568 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002569 } else {
2570 binder_size_t* objects =
2571 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2572 if (objects) {
2573 mObjects = objects;
Michael Wachenschwanzc67d9f32019-10-15 11:49:22 -07002574 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002575 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002576 }
2577 mObjectsSize = objectsSize;
2578 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002579 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002580 }
2581
2582 // We own the data, so we can just do a realloc().
2583 if (desired > mDataCapacity) {
2584 uint8_t* data = (uint8_t*)realloc(mData, desired);
2585 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002586 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2587 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002588 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002589 gParcelGlobalAllocSize += desired;
2590 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002591 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002592 mData = data;
2593 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002594 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002595 mError = NO_MEMORY;
2596 return NO_MEMORY;
2597 }
2598 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002599 if (mDataSize > desired) {
2600 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002601 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002602 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002603 if (mDataPos > desired) {
2604 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002605 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002606 }
2607 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002608
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002609 } else {
2610 // This is the first data. Easy!
2611 uint8_t* data = (uint8_t*)malloc(desired);
2612 if (!data) {
2613 mError = NO_MEMORY;
2614 return NO_MEMORY;
2615 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002616
Yi Kong91635562018-06-07 14:38:36 -07002617 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002618 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002619 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002620 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002621
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002622 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002623 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002624 gParcelGlobalAllocSize += desired;
2625 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002626 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002627
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002628 mData = data;
2629 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002630 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2631 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002632 mDataCapacity = desired;
2633 }
2634
2635 return NO_ERROR;
2636}
2637
2638void Parcel::initState()
2639{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002640 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002641 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002642 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002643 mDataSize = 0;
2644 mDataCapacity = 0;
2645 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002646 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2647 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002648 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002649 mObjectsSize = 0;
2650 mObjectsCapacity = 0;
2651 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002652 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002653 mHasFds = false;
2654 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002655 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002656 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002657 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002658 mWorkSourceRequestHeaderPosition = 0;
2659 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002660
2661 // racing multiple init leads only to multiple identical write
2662 if (gMaxFds == 0) {
2663 struct rlimit result;
2664 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2665 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002666 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002667 } else {
2668 ALOGW("Unable to getrlimit: %s", strerror(errno));
2669 gMaxFds = 1024;
2670 }
2671 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002672}
2673
2674void Parcel::scanForFds() const
2675{
2676 bool hasFds = false;
2677 for (size_t i=0; i<mObjectsSize; i++) {
2678 const flat_binder_object* flat
2679 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002680 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002681 hasFds = true;
2682 break;
2683 }
2684 }
2685 mHasFds = hasFds;
2686 mFdsKnown = true;
2687}
2688
Dan Sandleraa5c2342015-04-10 10:08:45 -04002689size_t Parcel::getBlobAshmemSize() const
2690{
Adrian Roos6bb31142015-10-22 16:46:12 -07002691 // This used to return the size of all blobs that were written to ashmem, now we're returning
2692 // the ashmem currently referenced by this Parcel, which should be equivalent.
2693 // TODO: Remove method once ABI can be changed.
2694 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002695}
2696
Adrian Rooscbf37262015-10-22 16:12:53 -07002697size_t Parcel::getOpenAshmemSize() const
2698{
2699 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002700}
2701
2702// --- Parcel::Blob ---
2703
2704Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002705 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002706}
2707
2708Parcel::Blob::~Blob() {
2709 release();
2710}
2711
2712void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002713 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002714 ::munmap(mData, mSize);
2715 }
2716 clear();
2717}
2718
Jeff Brown13b16042014-11-11 16:44:25 -08002719void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2720 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002721 mData = data;
2722 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002723 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002724}
2725
2726void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002727 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002728 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002729 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002730 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002731}
2732
Steven Moreland6511af52019-09-26 16:05:45 -07002733} // namespace android