blob: 9b47f80f1d9af834066fff0b56353401e0124b48 [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) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700425 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700426 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800427 binder_size_t *objects =
428 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700429 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700430 return NO_MEMORY;
431 }
432 mObjects = objects;
433 mObjectsCapacity = newSize;
434 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700435
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700436 // append and acquire objects
437 int idx = mObjectsSize;
438 for (int i = firstIndex; i <= lastIndex; i++) {
439 size_t off = objects[i] - offset + startPos;
440 mObjects[idx++] = off;
441 mObjectsSize++;
442
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700443 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700444 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700445 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700446
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700447 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700448 // If this is a file descriptor, we need to dup it so the
449 // new Parcel now owns its own fd, and can declare that we
450 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800451 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800452 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700453 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400454 if (!mAllowFds) {
455 err = FDS_NOT_ALLOWED;
456 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700457 }
458 }
459 }
460
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400461 return err;
462}
463
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700464int Parcel::compareData(const Parcel& other) {
465 size_t size = dataSize();
466 if (size != other.dataSize()) {
467 return size < other.dataSize() ? -1 : 1;
468 }
469 return memcmp(data(), other.data(), size);
470}
471
Jeff Brown13b16042014-11-11 16:44:25 -0800472bool Parcel::allowFds() const
473{
474 return mAllowFds;
475}
476
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700477bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400478{
479 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700480 if (!allowFds) {
481 mAllowFds = false;
482 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400483 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700484}
485
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700486void Parcel::restoreAllowFds(bool lastValue)
487{
488 mAllowFds = lastValue;
489}
490
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700491bool Parcel::hasFileDescriptors() const
492{
493 if (!mFdsKnown) {
494 scanForFds();
495 }
496 return mHasFds;
497}
498
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000499void Parcel::updateWorkSourceRequestHeaderPosition() const {
500 // Only update the request headers once. We only want to point
501 // to the first headers read/written.
502 if (!mRequestHeaderPresent) {
503 mWorkSourceRequestHeaderPosition = dataPosition();
504 mRequestHeaderPresent = true;
505 }
506}
507
Jooyung Han4df8e2d2019-10-18 16:30:14 +0900508#if defined(__ANDROID_APEX_COM_ANDROID_VNDK_CURRENT__) || (defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__))
Steven Morelandd70160f2019-07-23 10:20:38 -0700509constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
510#else
511constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
512#endif
513
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700514// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700515status_t Parcel::writeInterfaceToken(const String16& interface)
516{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000517 const IPCThreadState* threadState = IPCThreadState::self();
518 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000519 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000520 writeInt32(threadState->shouldPropagateWorkSource() ?
521 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700522 writeInt32(kHeader);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700523 // currently the interface identification token is just its name as a string
524 return writeString16(interface);
525}
526
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000527bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
528{
529 if (!mRequestHeaderPresent) {
530 return false;
531 }
532
533 const size_t initialPosition = dataPosition();
534 setDataPosition(mWorkSourceRequestHeaderPosition);
535 status_t err = writeInt32(uid);
536 setDataPosition(initialPosition);
537 return err == NO_ERROR;
538}
539
Steven Morelandf1b1e492019-05-06 15:05:13 -0700540uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000541{
542 if (!mRequestHeaderPresent) {
543 return IPCThreadState::kUnsetWorkSource;
544 }
545
546 const size_t initialPosition = dataPosition();
547 setDataPosition(mWorkSourceRequestHeaderPosition);
548 uid_t uid = readInt32();
549 setDataPosition(initialPosition);
550 return uid;
551}
552
Mathias Agopian83c04462009-05-22 19:00:22 -0700553bool Parcel::checkInterface(IBinder* binder) const
554{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700555 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700556}
557
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700558bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700559 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700560{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100561 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700562 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700563 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700564 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700565 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700566 if ((threadState->getLastTransactionBinderFlags() &
567 IBinder::FLAG_ONEWAY) != 0) {
568 // For one-way calls, the callee is running entirely
569 // disconnected from the caller, so disable StrictMode entirely.
570 // Not only does disk/network usage not impact the caller, but
571 // there's no way to commuicate back any violations anyway.
572 threadState->setStrictModePolicy(0);
573 } else {
574 threadState->setStrictModePolicy(strictPolicy);
575 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100576 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000577 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100578 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000579 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700580 // vendor header
581 int32_t header = readInt32();
582 if (header != kHeader) {
583 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
584 return false;
585 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100586 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700587 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700588 if (str == interface) {
589 return true;
590 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700591 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700592 String8(interface).string(), String8(str).string());
593 return false;
594 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700595}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700596
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700597size_t Parcel::objectsCount() const
598{
599 return mObjectsSize;
600}
601
602status_t Parcel::errorCheck() const
603{
604 return mError;
605}
606
607void Parcel::setError(status_t err)
608{
609 mError = err;
610}
611
612status_t Parcel::finishWrite(size_t len)
613{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700614 if (len > INT32_MAX) {
615 // don't accept size_t values which may have come from an
616 // inadvertent conversion from a negative int.
617 return BAD_VALUE;
618 }
619
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620 //printf("Finish write of %d\n", len);
621 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700622 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700623 if (mDataPos > mDataSize) {
624 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700625 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700626 }
627 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
628 return NO_ERROR;
629}
630
631status_t Parcel::writeUnpadded(const void* data, size_t len)
632{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700633 if (len > INT32_MAX) {
634 // don't accept size_t values which may have come from an
635 // inadvertent conversion from a negative int.
636 return BAD_VALUE;
637 }
638
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700639 size_t end = mDataPos + len;
640 if (end < mDataPos) {
641 // integer overflow
642 return BAD_VALUE;
643 }
644
645 if (end <= mDataCapacity) {
646restart_write:
647 memcpy(mData+mDataPos, data, len);
648 return finishWrite(len);
649 }
650
651 status_t err = growData(len);
652 if (err == NO_ERROR) goto restart_write;
653 return err;
654}
655
656status_t Parcel::write(const void* data, size_t len)
657{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700658 if (len > INT32_MAX) {
659 // don't accept size_t values which may have come from an
660 // inadvertent conversion from a negative int.
661 return BAD_VALUE;
662 }
663
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700664 void* const d = writeInplace(len);
665 if (d) {
666 memcpy(d, data, len);
667 return NO_ERROR;
668 }
669 return mError;
670}
671
672void* Parcel::writeInplace(size_t len)
673{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700674 if (len > INT32_MAX) {
675 // don't accept size_t values which may have come from an
676 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700677 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700678 }
679
680 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700681
682 // sanity check for integer overflow
683 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700684 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700685 }
686
687 if ((mDataPos+padded) <= mDataCapacity) {
688restart_write:
689 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
690 uint8_t* const data = mData+mDataPos;
691
692 // Need to pad at end?
693 if (padded != len) {
694#if BYTE_ORDER == BIG_ENDIAN
695 static const uint32_t mask[4] = {
696 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
697 };
698#endif
699#if BYTE_ORDER == LITTLE_ENDIAN
700 static const uint32_t mask[4] = {
701 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
702 };
703#endif
704 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
705 // *reinterpret_cast<void**>(data+padded-4));
706 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
707 }
708
709 finishWrite(padded);
710 return data;
711 }
712
713 status_t err = growData(padded);
714 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700715 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700716}
717
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800718status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
719 const uint8_t* strData = (uint8_t*)str.data();
720 const size_t strLen= str.length();
721 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100722 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800723 return BAD_VALUE;
724 }
725
726 status_t err = writeInt32(utf16Len);
727 if (err) {
728 return err;
729 }
730
731 // Allocate enough bytes to hold our converted string and its terminating NULL.
732 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
733 if (!dst) {
734 return NO_MEMORY;
735 }
736
Sergio Girof4607432016-07-21 14:46:35 +0100737 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800738
739 return NO_ERROR;
740}
741
742status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
743 if (!str) {
744 return writeInt32(-1);
745 }
746 return writeUtf8AsUtf16(*str);
747}
748
Daniel Normand0337ef2019-09-20 15:46:03 -0700749status_t Parcel::writeByteVectorInternal(const int8_t* data, size_t size) {
750 if (size > std::numeric_limits<int32_t>::max()) {
751 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -0700752 }
753
Daniel Normand0337ef2019-09-20 15:46:03 -0700754 status_t status = writeInt32(size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700755 if (status != OK) {
756 return status;
757 }
758
Daniel Normand0337ef2019-09-20 15:46:03 -0700759 return write(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700760}
761
Casey Dahlin185d3442016-02-09 11:08:35 -0800762status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700763 return writeByteVectorInternal(val.data(), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800764}
765
766status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
767{
Daniel Normand0337ef2019-09-20 15:46:03 -0700768 if (!val) return writeInt32(-1);
769 return writeByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800770}
771
772status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700773 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val.data()), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800774}
775
776status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
777{
Daniel Normand0337ef2019-09-20 15:46:03 -0700778 if (!val) return writeInt32(-1);
779 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800780}
781
Casey Dahlin451ff582015-10-19 18:12:18 -0700782status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
783{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800784 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700785}
786
Casey Dahlinb9872622015-11-25 15:09:45 -0800787status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
788{
789 return writeNullableTypedVector(val, &Parcel::writeInt32);
790}
791
Casey Dahlin451ff582015-10-19 18:12:18 -0700792status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
793{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800794 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700795}
796
Casey Dahlinb9872622015-11-25 15:09:45 -0800797status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
798{
799 return writeNullableTypedVector(val, &Parcel::writeInt64);
800}
801
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800802status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
803{
804 return writeTypedVector(val, &Parcel::writeUint64);
805}
806
807status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
808{
809 return writeNullableTypedVector(val, &Parcel::writeUint64);
810}
811
Casey Dahlin451ff582015-10-19 18:12:18 -0700812status_t Parcel::writeFloatVector(const std::vector<float>& val)
813{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800814 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700815}
816
Casey Dahlinb9872622015-11-25 15:09:45 -0800817status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
818{
819 return writeNullableTypedVector(val, &Parcel::writeFloat);
820}
821
Casey Dahlin451ff582015-10-19 18:12:18 -0700822status_t Parcel::writeDoubleVector(const std::vector<double>& val)
823{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800824 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700825}
826
Casey Dahlinb9872622015-11-25 15:09:45 -0800827status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
828{
829 return writeNullableTypedVector(val, &Parcel::writeDouble);
830}
831
Casey Dahlin451ff582015-10-19 18:12:18 -0700832status_t Parcel::writeBoolVector(const std::vector<bool>& val)
833{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800834 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700835}
836
Casey Dahlinb9872622015-11-25 15:09:45 -0800837status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
838{
839 return writeNullableTypedVector(val, &Parcel::writeBool);
840}
841
Casey Dahlin451ff582015-10-19 18:12:18 -0700842status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
843{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800844 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700845}
846
Casey Dahlinb9872622015-11-25 15:09:45 -0800847status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
848{
849 return writeNullableTypedVector(val, &Parcel::writeChar);
850}
851
Casey Dahlin451ff582015-10-19 18:12:18 -0700852status_t Parcel::writeString16Vector(const std::vector<String16>& val)
853{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800854 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700855}
856
Casey Dahlinb9872622015-11-25 15:09:45 -0800857status_t Parcel::writeString16Vector(
858 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
859{
860 return writeNullableTypedVector(val, &Parcel::writeString16);
861}
862
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800863status_t Parcel::writeUtf8VectorAsUtf16Vector(
864 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
865 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
866}
867
868status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
869 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
870}
871
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700872status_t Parcel::writeInt32(int32_t val)
873{
Andreas Huber84a6d042009-08-17 13:33:27 -0700874 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700875}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800876
877status_t Parcel::writeUint32(uint32_t val)
878{
879 return writeAligned(val);
880}
881
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700882status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700883 if (len > INT32_MAX) {
884 // don't accept size_t values which may have come from an
885 // inadvertent conversion from a negative int.
886 return BAD_VALUE;
887 }
888
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700889 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700890 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700891 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700892 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700893 if (ret == NO_ERROR) {
894 ret = write(val, len * sizeof(*val));
895 }
896 return ret;
897}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700898status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700899 if (len > INT32_MAX) {
900 // don't accept size_t values which may have come from an
901 // inadvertent conversion from a negative int.
902 return BAD_VALUE;
903 }
904
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700905 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700906 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700907 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700908 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700909 if (ret == NO_ERROR) {
910 ret = write(val, len * sizeof(*val));
911 }
912 return ret;
913}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700914
Casey Dahlind6848f52015-10-15 15:44:59 -0700915status_t Parcel::writeBool(bool val)
916{
917 return writeInt32(int32_t(val));
918}
919
920status_t Parcel::writeChar(char16_t val)
921{
922 return writeInt32(int32_t(val));
923}
924
925status_t Parcel::writeByte(int8_t val)
926{
927 return writeInt32(int32_t(val));
928}
929
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700930status_t Parcel::writeInt64(int64_t val)
931{
Andreas Huber84a6d042009-08-17 13:33:27 -0700932 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700933}
934
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700935status_t Parcel::writeUint64(uint64_t val)
936{
937 return writeAligned(val);
938}
939
Serban Constantinescuf683e012013-11-05 16:53:55 +0000940status_t Parcel::writePointer(uintptr_t val)
941{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800942 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000943}
944
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700945status_t Parcel::writeFloat(float val)
946{
Andreas Huber84a6d042009-08-17 13:33:27 -0700947 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700948}
949
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800950#if defined(__mips__) && defined(__mips_hard_float)
951
952status_t Parcel::writeDouble(double val)
953{
954 union {
955 double d;
956 unsigned long long ll;
957 } u;
958 u.d = val;
959 return writeAligned(u.ll);
960}
961
962#else
963
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700964status_t Parcel::writeDouble(double val)
965{
Andreas Huber84a6d042009-08-17 13:33:27 -0700966 return writeAligned(val);
967}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700968
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800969#endif
970
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700971status_t Parcel::writeCString(const char* str)
972{
973 return write(str, strlen(str)+1);
974}
975
976status_t Parcel::writeString8(const String8& str)
977{
978 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100979 // only write string if its length is more than zero characters,
980 // as readString8 will only read if the length field is non-zero.
981 // this is slightly different from how writeString16 works.
982 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700983 err = write(str.string(), str.bytes()+1);
984 }
985 return err;
986}
987
Casey Dahlinb9872622015-11-25 15:09:45 -0800988status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
989{
990 if (!str) {
991 return writeInt32(-1);
992 }
993
994 return writeString16(*str);
995}
996
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700997status_t Parcel::writeString16(const String16& str)
998{
999 return writeString16(str.string(), str.size());
1000}
1001
1002status_t Parcel::writeString16(const char16_t* str, size_t len)
1003{
Yi Kong91635562018-06-07 14:38:36 -07001004 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001005
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001006 status_t err = writeInt32(len);
1007 if (err == NO_ERROR) {
1008 len *= sizeof(char16_t);
1009 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1010 if (data) {
1011 memcpy(data, str, len);
1012 *reinterpret_cast<char16_t*>(data+len) = 0;
1013 return NO_ERROR;
1014 }
1015 err = mError;
1016 }
1017 return err;
1018}
1019
1020status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1021{
Steven Morelanda86a3562019-08-01 23:28:34 +00001022 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001023}
1024
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001025status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1026{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001027 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001028}
1029
Casey Dahlinb9872622015-11-25 15:09:45 -08001030status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1031{
1032 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1033}
1034
1035status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001036 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001037}
1038
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001039status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001040 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001041}
1042
Casey Dahlinb9872622015-11-25 15:09:45 -08001043status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1044 if (!parcelable) {
1045 return writeInt32(0);
1046 }
1047
1048 return writeParcelable(*parcelable);
1049}
1050
Christopher Wiley97f048d2015-11-19 06:49:05 -08001051status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1052 status_t status = writeInt32(1); // parcelable is not null.
1053 if (status != OK) {
1054 return status;
1055 }
1056 return parcelable.writeToParcel(this);
1057}
1058
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001059status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001060{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001061 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001062 return BAD_TYPE;
1063
1064 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001065 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001066 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001068 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001069 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001071 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1072 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073
1074 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001075 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076 return err;
1077 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001078 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001079 return err;
1080}
1081
Jeff Brown93ff1f92011-11-04 19:01:44 -07001082status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001083{
1084 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001085 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001086 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001087 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001088 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001089 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001090 return writeObject(obj, true);
1091}
1092
1093status_t Parcel::writeDupFileDescriptor(int fd)
1094{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001095 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001096 if (dupFd < 0) {
1097 return -errno;
1098 }
1099 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001100 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001101 close(dupFd);
1102 }
1103 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001104}
1105
Dianne Hackborn1941a402016-08-29 12:30:43 -07001106status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1107{
1108 writeInt32(0);
1109 return writeFileDescriptor(fd, takeOwnership);
1110}
1111
Ryo Hashimotobf551892018-05-31 16:58:35 +09001112status_t Parcel::writeDupParcelFileDescriptor(int fd)
1113{
1114 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1115 if (dupFd < 0) {
1116 return -errno;
1117 }
1118 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1119 if (err != OK) {
1120 close(dupFd);
1121 }
1122 return err;
1123}
1124
Christopher Wiley2cf19952016-04-11 11:09:37 -07001125status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001126 return writeDupFileDescriptor(fd.get());
1127}
1128
Christopher Wiley2cf19952016-04-11 11:09:37 -07001129status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001130 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1131}
1132
Christopher Wiley2cf19952016-04-11 11:09:37 -07001133status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001134 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1135}
1136
Jeff Brown13b16042014-11-11 16:44:25 -08001137status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001138{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001139 if (len > INT32_MAX) {
1140 // don't accept size_t values which may have come from an
1141 // inadvertent conversion from a negative int.
1142 return BAD_VALUE;
1143 }
1144
Jeff Brown13b16042014-11-11 16:44:25 -08001145 status_t status;
1146 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001147 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001148 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001149 if (status) return status;
1150
1151 void* ptr = writeInplace(len);
1152 if (!ptr) return NO_MEMORY;
1153
Jeff Brown13b16042014-11-11 16:44:25 -08001154 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001155 return NO_ERROR;
1156 }
1157
Steve Block6807e592011-10-20 11:56:00 +01001158 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001159 int fd = ashmem_create_region("Parcel Blob", len);
1160 if (fd < 0) return NO_MEMORY;
1161
1162 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1163 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001164 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001165 } else {
Yi Kong91635562018-06-07 14:38:36 -07001166 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001167 if (ptr == MAP_FAILED) {
1168 status = -errno;
1169 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001170 if (!mutableCopy) {
1171 result = ashmem_set_prot_region(fd, PROT_READ);
1172 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001173 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001174 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001175 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001176 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001177 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001178 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001179 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001180 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001181 return NO_ERROR;
1182 }
1183 }
1184 }
1185 }
1186 ::munmap(ptr, len);
1187 }
1188 ::close(fd);
1189 return status;
1190}
1191
Jeff Brown13b16042014-11-11 16:44:25 -08001192status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1193{
1194 // Must match up with what's done in writeBlob.
1195 if (!mAllowFds) return FDS_NOT_ALLOWED;
1196 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1197 if (status) return status;
1198 return writeDupFileDescriptor(fd);
1199}
1200
Mathias Agopiane1424282013-07-29 21:24:40 -07001201status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001202{
1203 status_t err;
1204
1205 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001206 const size_t len = val.getFlattenedSize();
1207 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001208
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001209 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001210 // don't accept size_t values which may have come from an
1211 // inadvertent conversion from a negative int.
1212 return BAD_VALUE;
1213 }
1214
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001215 err = this->writeInt32(len);
1216 if (err) return err;
1217
1218 err = this->writeInt32(fd_count);
1219 if (err) return err;
1220
1221 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001222 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001223 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001224 return BAD_VALUE;
1225
Yi Kong91635562018-06-07 14:38:36 -07001226 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001227 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001228 fds = new (std::nothrow) int[fd_count];
1229 if (fds == nullptr) {
1230 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1231 return BAD_VALUE;
1232 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001233 }
1234
1235 err = val.flatten(buf, len, fds, fd_count);
1236 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1237 err = this->writeDupFileDescriptor( fds[i] );
1238 }
1239
1240 if (fd_count) {
1241 delete [] fds;
1242 }
1243
1244 return err;
1245}
1246
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001247status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1248{
1249 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1250 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1251 if (enoughData && enoughObjects) {
1252restart_write:
1253 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001254
Christopher Tate98e67d32015-06-03 18:44:15 -07001255 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001256 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001257 if (!mAllowFds) {
1258 // fail before modifying our object index
1259 return FDS_NOT_ALLOWED;
1260 }
1261 mHasFds = mFdsKnown = true;
1262 }
1263
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001264 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001265 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001266 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001267 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001268 mObjectsSize++;
1269 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001270
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001271 return finishWrite(sizeof(flat_binder_object));
1272 }
1273
1274 if (!enoughData) {
1275 const status_t err = growData(sizeof(val));
1276 if (err != NO_ERROR) return err;
1277 }
1278 if (!enoughObjects) {
1279 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001280 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001281 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001282 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001283 mObjects = objects;
1284 mObjectsCapacity = newSize;
1285 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001286
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001287 goto restart_write;
1288}
1289
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001290status_t Parcel::writeNoException()
1291{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001292 binder::Status status;
1293 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001294}
1295
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001296status_t Parcel::validateReadData(size_t upperBound) const
1297{
1298 // Don't allow non-object reads on object data
1299 if (mObjectsSorted || mObjectsSize <= 1) {
1300data_sorted:
1301 // Expect to check only against the next object
1302 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1303 // For some reason the current read position is greater than the next object
1304 // hint. Iterate until we find the right object
1305 size_t nextObject = mNextObjectHint;
1306 do {
1307 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1308 // Requested info overlaps with an object
1309 ALOGE("Attempt to read from protected data in Parcel %p", this);
1310 return PERMISSION_DENIED;
1311 }
1312 nextObject++;
1313 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1314 mNextObjectHint = nextObject;
1315 }
1316 return NO_ERROR;
1317 }
1318 // Quickly determine if mObjects is sorted.
1319 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1320 binder_size_t* prevObj = currObj;
1321 while (currObj > mObjects) {
1322 prevObj--;
1323 if(*prevObj > *currObj) {
1324 goto data_unsorted;
1325 }
1326 currObj--;
1327 }
1328 mObjectsSorted = true;
1329 goto data_sorted;
1330
1331data_unsorted:
1332 // Insertion Sort mObjects
1333 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1334 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1335 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1336 binder_size_t temp = *iter0;
1337 binder_size_t* iter1 = iter0 - 1;
1338 while (iter1 >= mObjects && *iter1 > temp) {
1339 *(iter1 + 1) = *iter1;
1340 iter1--;
1341 }
1342 *(iter1 + 1) = temp;
1343 }
1344 mNextObjectHint = 0;
1345 mObjectsSorted = true;
1346 goto data_sorted;
1347}
1348
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001349status_t Parcel::read(void* outData, size_t len) const
1350{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001351 if (len > INT32_MAX) {
1352 // don't accept size_t values which may have come from an
1353 // inadvertent conversion from a negative int.
1354 return BAD_VALUE;
1355 }
1356
1357 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1358 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001359 if (mObjectsSize > 0) {
1360 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001361 if(err != NO_ERROR) {
1362 // Still increment the data position by the expected length
1363 mDataPos += pad_size(len);
1364 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1365 return err;
1366 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001367 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001368 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001369 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001370 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001371 return NO_ERROR;
1372 }
1373 return NOT_ENOUGH_DATA;
1374}
1375
1376const void* Parcel::readInplace(size_t len) const
1377{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001378 if (len > INT32_MAX) {
1379 // don't accept size_t values which may have come from an
1380 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001381 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001382 }
1383
1384 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1385 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001386 if (mObjectsSize > 0) {
1387 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001388 if(err != NO_ERROR) {
1389 // Still increment the data position by the expected length
1390 mDataPos += pad_size(len);
1391 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001392 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001393 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001394 }
1395
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001396 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001397 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001398 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001399 return data;
1400 }
Yi Kong91635562018-06-07 14:38:36 -07001401 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001402}
1403
Andreas Huber84a6d042009-08-17 13:33:27 -07001404template<class T>
1405status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001406 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001407
1408 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001409 if (mObjectsSize > 0) {
1410 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001411 if(err != NO_ERROR) {
1412 // Still increment the data position by the expected length
1413 mDataPos += sizeof(T);
1414 return err;
1415 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001416 }
1417
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001418 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001419 mDataPos += sizeof(T);
1420 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001421 return NO_ERROR;
1422 } else {
1423 return NOT_ENOUGH_DATA;
1424 }
1425}
1426
Andreas Huber84a6d042009-08-17 13:33:27 -07001427template<class T>
1428T Parcel::readAligned() const {
1429 T result;
1430 if (readAligned(&result) != NO_ERROR) {
1431 result = 0;
1432 }
1433
1434 return result;
1435}
1436
1437template<class T>
1438status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001439 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001440
1441 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1442restart_write:
1443 *reinterpret_cast<T*>(mData+mDataPos) = val;
1444 return finishWrite(sizeof(val));
1445 }
1446
1447 status_t err = growData(sizeof(val));
1448 if (err == NO_ERROR) goto restart_write;
1449 return err;
1450}
1451
Daniel Normand0337ef2019-09-20 15:46:03 -07001452status_t Parcel::readByteVectorInternal(int8_t* data, size_t size) const {
1453 if (size_t(size) > dataAvail()) {
1454 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -07001455 }
Daniel Normand0337ef2019-09-20 15:46:03 -07001456 return read(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001457}
1458
Casey Dahlin185d3442016-02-09 11:08:35 -08001459status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
Daniel Normand0337ef2019-09-20 15:46:03 -07001460 if (status_t status = resizeOutVector(val); status != OK) return status;
1461 return readByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -08001462}
1463
1464status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
Daniel Normand0337ef2019-09-20 15:46:03 -07001465 if (status_t status = resizeOutVector(val); status != OK) return status;
1466 return readByteVectorInternal(reinterpret_cast<int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -08001467}
1468
1469status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
Daniel Normand0337ef2019-09-20 15:46:03 -07001470 if (status_t status = resizeOutVector(val); status != OK) return status;
1471 if (val->get() == nullptr) {
1472 // resizeOutVector does not create the out vector if size is < 0.
1473 // This occurs when writing a null byte vector.
1474 return OK;
1475 }
1476 return readByteVectorInternal((*val)->data(), (*val)->size());
Casey Dahlin185d3442016-02-09 11:08:35 -08001477}
1478
1479status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
Daniel Normand0337ef2019-09-20 15:46:03 -07001480 if (status_t status = resizeOutVector(val); status != OK) return status;
1481 if (val->get() == nullptr) {
1482 // resizeOutVector does not create the out vector if size is < 0.
1483 // This occurs when writing a null byte vector.
1484 return OK;
1485 }
1486 return readByteVectorInternal(reinterpret_cast<int8_t*>((*val)->data()), (*val)->size());
Casey Dahlin185d3442016-02-09 11:08:35 -08001487}
1488
Casey Dahlinb9872622015-11-25 15:09:45 -08001489status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1490 return readNullableTypedVector(val, &Parcel::readInt32);
1491}
1492
Casey Dahlin451ff582015-10-19 18:12:18 -07001493status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001494 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001495}
1496
Casey Dahlinb9872622015-11-25 15:09:45 -08001497status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1498 return readNullableTypedVector(val, &Parcel::readInt64);
1499}
1500
Casey Dahlin451ff582015-10-19 18:12:18 -07001501status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001502 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001503}
1504
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001505status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1506 return readNullableTypedVector(val, &Parcel::readUint64);
1507}
1508
1509status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1510 return readTypedVector(val, &Parcel::readUint64);
1511}
1512
Casey Dahlinb9872622015-11-25 15:09:45 -08001513status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1514 return readNullableTypedVector(val, &Parcel::readFloat);
1515}
1516
Casey Dahlin451ff582015-10-19 18:12:18 -07001517status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001518 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001519}
1520
Casey Dahlinb9872622015-11-25 15:09:45 -08001521status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1522 return readNullableTypedVector(val, &Parcel::readDouble);
1523}
1524
Casey Dahlin451ff582015-10-19 18:12:18 -07001525status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001526 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001527}
1528
Casey Dahlinb9872622015-11-25 15:09:45 -08001529status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1530 const int32_t start = dataPosition();
1531 int32_t size;
1532 status_t status = readInt32(&size);
1533 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001534
Casey Dahlinb9872622015-11-25 15:09:45 -08001535 if (status != OK || size < 0) {
1536 return status;
1537 }
1538
1539 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001540 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001541
1542 status = readBoolVector(val->get());
1543
1544 if (status != OK) {
1545 val->reset();
1546 }
1547
1548 return status;
1549}
1550
1551status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001552 int32_t size;
1553 status_t status = readInt32(&size);
1554
1555 if (status != OK) {
1556 return status;
1557 }
1558
1559 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001560 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001561 }
1562
1563 val->resize(size);
1564
1565 /* C++ bool handling means a vector of bools isn't necessarily addressable
1566 * (we might use individual bits)
1567 */
Christopher Wiley97887982015-10-27 16:33:47 -07001568 bool data;
1569 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001570 status = readBool(&data);
1571 (*val)[i] = data;
1572
1573 if (status != OK) {
1574 return status;
1575 }
1576 }
1577
1578 return OK;
1579}
1580
Casey Dahlinb9872622015-11-25 15:09:45 -08001581status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1582 return readNullableTypedVector(val, &Parcel::readChar);
1583}
1584
Casey Dahlin451ff582015-10-19 18:12:18 -07001585status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001586 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001587}
1588
Casey Dahlinb9872622015-11-25 15:09:45 -08001589status_t Parcel::readString16Vector(
1590 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1591 return readNullableTypedVector(val, &Parcel::readString16);
1592}
1593
Casey Dahlin451ff582015-10-19 18:12:18 -07001594status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001595 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001596}
1597
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001598status_t Parcel::readUtf8VectorFromUtf16Vector(
1599 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1600 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1601}
1602
1603status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1604 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1605}
Casey Dahlin451ff582015-10-19 18:12:18 -07001606
Andreas Huber84a6d042009-08-17 13:33:27 -07001607status_t Parcel::readInt32(int32_t *pArg) const
1608{
1609 return readAligned(pArg);
1610}
1611
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001612int32_t Parcel::readInt32() const
1613{
Andreas Huber84a6d042009-08-17 13:33:27 -07001614 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001615}
1616
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001617status_t Parcel::readUint32(uint32_t *pArg) const
1618{
1619 return readAligned(pArg);
1620}
1621
1622uint32_t Parcel::readUint32() const
1623{
1624 return readAligned<uint32_t>();
1625}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001626
1627status_t Parcel::readInt64(int64_t *pArg) const
1628{
Andreas Huber84a6d042009-08-17 13:33:27 -07001629 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001630}
1631
1632
1633int64_t Parcel::readInt64() const
1634{
Andreas Huber84a6d042009-08-17 13:33:27 -07001635 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001636}
1637
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001638status_t Parcel::readUint64(uint64_t *pArg) const
1639{
1640 return readAligned(pArg);
1641}
1642
1643uint64_t Parcel::readUint64() const
1644{
1645 return readAligned<uint64_t>();
1646}
1647
Serban Constantinescuf683e012013-11-05 16:53:55 +00001648status_t Parcel::readPointer(uintptr_t *pArg) const
1649{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001650 status_t ret;
1651 binder_uintptr_t ptr;
1652 ret = readAligned(&ptr);
1653 if (!ret)
1654 *pArg = ptr;
1655 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001656}
1657
1658uintptr_t Parcel::readPointer() const
1659{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001660 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001661}
1662
1663
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001664status_t Parcel::readFloat(float *pArg) const
1665{
Andreas Huber84a6d042009-08-17 13:33:27 -07001666 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001667}
1668
1669
1670float Parcel::readFloat() const
1671{
Andreas Huber84a6d042009-08-17 13:33:27 -07001672 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001673}
1674
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001675#if defined(__mips__) && defined(__mips_hard_float)
1676
1677status_t Parcel::readDouble(double *pArg) const
1678{
1679 union {
1680 double d;
1681 unsigned long long ll;
1682 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001683 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001684 status_t status;
1685 status = readAligned(&u.ll);
1686 *pArg = u.d;
1687 return status;
1688}
1689
1690double Parcel::readDouble() const
1691{
1692 union {
1693 double d;
1694 unsigned long long ll;
1695 } u;
1696 u.ll = readAligned<unsigned long long>();
1697 return u.d;
1698}
1699
1700#else
1701
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001702status_t Parcel::readDouble(double *pArg) const
1703{
Andreas Huber84a6d042009-08-17 13:33:27 -07001704 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001705}
1706
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001707double Parcel::readDouble() const
1708{
Andreas Huber84a6d042009-08-17 13:33:27 -07001709 return readAligned<double>();
1710}
1711
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001712#endif
1713
Andreas Huber84a6d042009-08-17 13:33:27 -07001714status_t Parcel::readIntPtr(intptr_t *pArg) const
1715{
1716 return readAligned(pArg);
1717}
1718
1719
1720intptr_t Parcel::readIntPtr() const
1721{
1722 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001723}
1724
Casey Dahlind6848f52015-10-15 15:44:59 -07001725status_t Parcel::readBool(bool *pArg) const
1726{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001727 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001728 status_t ret = readInt32(&tmp);
1729 *pArg = (tmp != 0);
1730 return ret;
1731}
1732
1733bool Parcel::readBool() const
1734{
1735 return readInt32() != 0;
1736}
1737
1738status_t Parcel::readChar(char16_t *pArg) const
1739{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001740 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001741 status_t ret = readInt32(&tmp);
1742 *pArg = char16_t(tmp);
1743 return ret;
1744}
1745
1746char16_t Parcel::readChar() const
1747{
1748 return char16_t(readInt32());
1749}
1750
1751status_t Parcel::readByte(int8_t *pArg) const
1752{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001753 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001754 status_t ret = readInt32(&tmp);
1755 *pArg = int8_t(tmp);
1756 return ret;
1757}
1758
1759int8_t Parcel::readByte() const
1760{
1761 return int8_t(readInt32());
1762}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001763
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001764status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1765 size_t utf16Size = 0;
1766 const char16_t* src = readString16Inplace(&utf16Size);
1767 if (!src) {
1768 return UNEXPECTED_NULL;
1769 }
1770
1771 // Save ourselves the trouble, we're done.
1772 if (utf16Size == 0u) {
1773 str->clear();
1774 return NO_ERROR;
1775 }
1776
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001777 // Allow for closing '\0'
1778 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1779 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001780 return BAD_VALUE;
1781 }
1782 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001783 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001784 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001785 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1786 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001787 return NO_ERROR;
1788}
1789
1790status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1791 const int32_t start = dataPosition();
1792 int32_t size;
1793 status_t status = readInt32(&size);
1794 str->reset();
1795
1796 if (status != OK || size < 0) {
1797 return status;
1798 }
1799
1800 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001801 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001802 return readUtf8FromUtf16(str->get());
1803}
1804
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001805const char* Parcel::readCString() const
1806{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001807 if (mDataPos < mDataSize) {
1808 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001809 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1810 // is the string's trailing NUL within the parcel's valid bounds?
1811 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1812 if (eos) {
1813 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001814 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001815 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001816 return str;
1817 }
1818 }
Yi Kong91635562018-06-07 14:38:36 -07001819 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001820}
1821
1822String8 Parcel::readString8() const
1823{
Roshan Pius87b64d22016-07-18 12:51:02 -07001824 String8 retString;
1825 status_t status = readString8(&retString);
1826 if (status != OK) {
1827 // We don't care about errors here, so just return an empty string.
1828 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001829 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001830 return retString;
1831}
1832
1833status_t Parcel::readString8(String8* pArg) const
1834{
1835 int32_t size;
1836 status_t status = readInt32(&size);
1837 if (status != OK) {
1838 return status;
1839 }
1840 // watch for potential int overflow from size+1
1841 if (size < 0 || size >= INT32_MAX) {
1842 return BAD_VALUE;
1843 }
1844 // |writeString8| writes nothing for empty string.
1845 if (size == 0) {
1846 *pArg = String8();
1847 return OK;
1848 }
1849 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07001850 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07001851 return BAD_VALUE;
1852 }
1853 pArg->setTo(str, size);
1854 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001855}
1856
1857String16 Parcel::readString16() const
1858{
1859 size_t len;
1860 const char16_t* str = readString16Inplace(&len);
1861 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001862 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001863 return String16();
1864}
1865
Casey Dahlinb9872622015-11-25 15:09:45 -08001866status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1867{
1868 const int32_t start = dataPosition();
1869 int32_t size;
1870 status_t status = readInt32(&size);
1871 pArg->reset();
1872
1873 if (status != OK || size < 0) {
1874 return status;
1875 }
1876
1877 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001878 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001879
1880 status = readString16(pArg->get());
1881
1882 if (status != OK) {
1883 pArg->reset();
1884 }
1885
1886 return status;
1887}
1888
Casey Dahlin451ff582015-10-19 18:12:18 -07001889status_t Parcel::readString16(String16* pArg) const
1890{
1891 size_t len;
1892 const char16_t* str = readString16Inplace(&len);
1893 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001894 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001895 return 0;
1896 } else {
1897 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001898 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001899 }
1900}
1901
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001902const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1903{
1904 int32_t size = readInt32();
1905 // watch for potential int overflow from size+1
1906 if (size >= 0 && size < INT32_MAX) {
1907 *outLen = size;
1908 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001909 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001910 return str;
1911 }
1912 }
1913 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001914 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001915}
1916
Casey Dahlinf0c13772015-10-27 18:33:56 -07001917status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1918{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001919 status_t status = readNullableStrongBinder(val);
1920 if (status == OK && !val->get()) {
1921 status = UNEXPECTED_NULL;
1922 }
1923 return status;
1924}
1925
1926status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1927{
Steven Morelanda86a3562019-08-01 23:28:34 +00001928 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001929}
1930
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001931sp<IBinder> Parcel::readStrongBinder() const
1932{
1933 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001934 // Note that a lot of code in Android reads binders by hand with this
1935 // method, and that code has historically been ok with getting nullptr
1936 // back (while ignoring error codes).
1937 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001938 return val;
1939}
1940
Christopher Wiley97f048d2015-11-19 06:49:05 -08001941status_t Parcel::readParcelable(Parcelable* parcelable) const {
1942 int32_t have_parcelable = 0;
1943 status_t status = readInt32(&have_parcelable);
1944 if (status != OK) {
1945 return status;
1946 }
1947 if (!have_parcelable) {
1948 return UNEXPECTED_NULL;
1949 }
1950 return parcelable->readFromParcel(this);
1951}
1952
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001953int32_t Parcel::readExceptionCode() const
1954{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001955 binder::Status status;
1956 status.readFromParcel(*this);
1957 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001958}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001959
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001960native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001961{
1962 int numFds, numInts;
1963 status_t err;
1964 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001965 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001966 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001967 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001968
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001969 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001970 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001971 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001972 }
1973
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001974 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001975 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001976 if (h->data[i] < 0) {
1977 for (int j = 0; j < i; j++) {
1978 close(h->data[j]);
1979 }
1980 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001981 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001982 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001983 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001984 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001985 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001986 native_handle_close(h);
1987 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001988 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001989 }
1990 return h;
1991}
1992
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001993int Parcel::readFileDescriptor() const
1994{
1995 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001996
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001997 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001998 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001999 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002000
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002001 return BAD_TYPE;
2002}
2003
Dianne Hackborn1941a402016-08-29 12:30:43 -07002004int Parcel::readParcelFileDescriptor() const
2005{
2006 int32_t hasComm = readInt32();
2007 int fd = readFileDescriptor();
2008 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002009 // detach (owned by the binder driver)
2010 int comm = readFileDescriptor();
2011
2012 // warning: this must be kept in sync with:
2013 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2014 enum ParcelFileDescriptorStatus {
2015 DETACHED = 2,
2016 };
2017
2018#if BYTE_ORDER == BIG_ENDIAN
2019 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2020#endif
2021#if BYTE_ORDER == LITTLE_ENDIAN
2022 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2023#endif
2024
2025 ssize_t written = TEMP_FAILURE_RETRY(
2026 ::write(comm, &message, sizeof(message)));
2027
2028 if (written == -1 || written != sizeof(message)) {
2029 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2030 written, strerror(errno));
2031 return BAD_TYPE;
2032 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002033 }
2034 return fd;
2035}
2036
Christopher Wiley2cf19952016-04-11 11:09:37 -07002037status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002038{
2039 int got = readFileDescriptor();
2040
2041 if (got == BAD_TYPE) {
2042 return BAD_TYPE;
2043 }
2044
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002045 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002046
2047 if (val->get() < 0) {
2048 return BAD_VALUE;
2049 }
2050
2051 return OK;
2052}
2053
Ryo Hashimotobf551892018-05-31 16:58:35 +09002054status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2055{
2056 int got = readParcelFileDescriptor();
2057
2058 if (got == BAD_TYPE) {
2059 return BAD_TYPE;
2060 }
2061
2062 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2063
2064 if (val->get() < 0) {
2065 return BAD_VALUE;
2066 }
2067
2068 return OK;
2069}
Casey Dahlin06673e32015-11-23 13:24:23 -08002070
Christopher Wiley2cf19952016-04-11 11:09:37 -07002071status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002072 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2073}
2074
Christopher Wiley2cf19952016-04-11 11:09:37 -07002075status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002076 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2077}
2078
Jeff Brown5707dbf2011-09-23 21:17:56 -07002079status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2080{
Jeff Brown13b16042014-11-11 16:44:25 -08002081 int32_t blobType;
2082 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002083 if (status) return status;
2084
Jeff Brown13b16042014-11-11 16:44:25 -08002085 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002086 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002087 const void* ptr = readInplace(len);
2088 if (!ptr) return BAD_VALUE;
2089
Jeff Brown13b16042014-11-11 16:44:25 -08002090 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002091 return NO_ERROR;
2092 }
2093
Steve Block6807e592011-10-20 11:56:00 +01002094 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002095 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002096 int fd = readFileDescriptor();
2097 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2098
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002099 if (!ashmem_valid(fd)) {
2100 ALOGE("invalid fd");
2101 return BAD_VALUE;
2102 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002103 int size = ashmem_get_size_region(fd);
2104 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002105 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002106 return BAD_VALUE;
2107 }
Yi Kong91635562018-06-07 14:38:36 -07002108 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002109 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002110 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002111
Jeff Brown13b16042014-11-11 16:44:25 -08002112 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002113 return NO_ERROR;
2114}
2115
Mathias Agopiane1424282013-07-29 21:24:40 -07002116status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002117{
2118 // size
2119 const size_t len = this->readInt32();
2120 const size_t fd_count = this->readInt32();
2121
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002122 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002123 // don't accept size_t values which may have come from an
2124 // inadvertent conversion from a negative int.
2125 return BAD_VALUE;
2126 }
2127
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002128 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002129 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002130 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002131 return BAD_VALUE;
2132
Yi Kong91635562018-06-07 14:38:36 -07002133 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002134 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002135 fds = new (std::nothrow) int[fd_count];
2136 if (fds == nullptr) {
2137 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2138 return BAD_VALUE;
2139 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002140 }
2141
2142 status_t err = NO_ERROR;
2143 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002144 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002145 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002146 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002147 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 -07002148 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2149 // Close all the file descriptors that were dup-ed.
2150 for (size_t j=0; j<i ;j++) {
2151 close(fds[j]);
2152 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002153 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002154 }
2155
2156 if (err == NO_ERROR) {
2157 err = val.unflatten(buf, len, fds, fd_count);
2158 }
2159
2160 if (fd_count) {
2161 delete [] fds;
2162 }
2163
2164 return err;
2165}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002166const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2167{
2168 const size_t DPOS = mDataPos;
2169 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2170 const flat_binder_object* obj
2171 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2172 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002173 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002174 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002175 // the object list, so we don't want to check for it when
2176 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002177 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002178 return obj;
2179 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002180
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002181 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002182 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002183 const size_t N = mObjectsSize;
2184 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002185
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002186 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002187 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002188 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002189
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002190 // Start at the current hint position, looking for an object at
2191 // the current data position.
2192 if (opos < N) {
2193 while (opos < (N-1) && OBJS[opos] < DPOS) {
2194 opos++;
2195 }
2196 } else {
2197 opos = N-1;
2198 }
2199 if (OBJS[opos] == DPOS) {
2200 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002201 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002202 this, DPOS, opos);
2203 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002204 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002205 return obj;
2206 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002207
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002208 // Look backwards for it...
2209 while (opos > 0 && OBJS[opos] > DPOS) {
2210 opos--;
2211 }
2212 if (OBJS[opos] == DPOS) {
2213 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002214 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002215 this, DPOS, opos);
2216 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002217 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002218 return obj;
2219 }
2220 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002221 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 -07002222 this, DPOS);
2223 }
Yi Kong91635562018-06-07 14:38:36 -07002224 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002225}
2226
2227void Parcel::closeFileDescriptors()
2228{
2229 size_t i = mObjectsSize;
2230 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002231 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002232 }
2233 while (i > 0) {
2234 i--;
2235 const flat_binder_object* flat
2236 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002237 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002238 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002239 close(flat->handle);
2240 }
2241 }
2242}
2243
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002244uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002245{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002246 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002247}
2248
2249size_t Parcel::ipcDataSize() const
2250{
2251 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2252}
2253
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002254uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002255{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002256 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002257}
2258
2259size_t Parcel::ipcObjectsCount() const
2260{
2261 return mObjectsSize;
2262}
2263
2264void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002265 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002266{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002267 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002268 freeDataNoInit();
2269 mError = NO_ERROR;
2270 mData = const_cast<uint8_t*>(data);
2271 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002272 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002273 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002274 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002275 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002276 mObjectsSize = mObjectsCapacity = objectsCount;
2277 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002278 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002279 mOwner = relFunc;
2280 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002281 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002282 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002283 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002284 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002285 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002286 mObjectsSize = 0;
2287 break;
2288 }
2289 minOffset = offset + sizeof(flat_binder_object);
2290 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002291 scanForFds();
2292}
2293
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002294void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002295{
2296 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002297
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002298 if (errorCheck() != NO_ERROR) {
2299 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002300 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002301 } else if (dataSize() > 0) {
2302 const uint8_t* DATA = data();
2303 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002304 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002305 const size_t N = objectsCount();
2306 for (size_t i=0; i<N; i++) {
2307 const flat_binder_object* flat
2308 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2309 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002310 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002311 << " = " << flat->binder;
2312 }
2313 } else {
2314 to << "NULL";
2315 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002316
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002317 to << ")";
2318}
2319
2320void Parcel::releaseObjects()
2321{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002322 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002323 if (i == 0) {
2324 return;
2325 }
2326 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002327 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002328 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002329 while (i > 0) {
2330 i--;
2331 const flat_binder_object* flat
2332 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002333 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002334 }
2335}
2336
2337void Parcel::acquireObjects()
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 const 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 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002351 }
2352}
2353
2354void Parcel::freeData()
2355{
2356 freeDataNoInit();
2357 initState();
2358}
2359
2360void Parcel::freeDataNoInit()
2361{
2362 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002363 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002364 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002365 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2366 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002367 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002368 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002369 if (mData) {
2370 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002371 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002372 if (mDataCapacity <= gParcelGlobalAllocSize) {
2373 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2374 } else {
2375 gParcelGlobalAllocSize = 0;
2376 }
2377 if (gParcelGlobalAllocCount > 0) {
2378 gParcelGlobalAllocCount--;
2379 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002380 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002381 free(mData);
2382 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002383 if (mObjects) free(mObjects);
2384 }
2385}
2386
2387status_t Parcel::growData(size_t len)
2388{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002389 if (len > INT32_MAX) {
2390 // don't accept size_t values which may have come from an
2391 // inadvertent conversion from a negative int.
2392 return BAD_VALUE;
2393 }
2394
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002395 size_t newSize = ((mDataSize+len)*3)/2;
2396 return (newSize <= mDataSize)
2397 ? (status_t) NO_MEMORY
2398 : continueWrite(newSize);
2399}
2400
2401status_t Parcel::restartWrite(size_t desired)
2402{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002403 if (desired > INT32_MAX) {
2404 // don't accept size_t values which may have come from an
2405 // inadvertent conversion from a negative int.
2406 return BAD_VALUE;
2407 }
2408
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002409 if (mOwner) {
2410 freeData();
2411 return continueWrite(desired);
2412 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002413
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002414 uint8_t* data = (uint8_t*)realloc(mData, desired);
2415 if (!data && desired > mDataCapacity) {
2416 mError = NO_MEMORY;
2417 return NO_MEMORY;
2418 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002419
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002420 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002421
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002422 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002423 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002424 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002425 gParcelGlobalAllocSize += desired;
2426 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002427 if (!mData) {
2428 gParcelGlobalAllocCount++;
2429 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002430 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002431 mData = data;
2432 mDataCapacity = desired;
2433 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002434
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002436 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2437 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2438
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002439 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002440 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002441 mObjectsSize = mObjectsCapacity = 0;
2442 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002443 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002444 mHasFds = false;
2445 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002446 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002447
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002448 return NO_ERROR;
2449}
2450
2451status_t Parcel::continueWrite(size_t desired)
2452{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002453 if (desired > INT32_MAX) {
2454 // don't accept size_t values which may have come from an
2455 // inadvertent conversion from a negative int.
2456 return BAD_VALUE;
2457 }
2458
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002459 // If shrinking, first adjust for any objects that appear
2460 // after the new data size.
2461 size_t objectsSize = mObjectsSize;
2462 if (desired < mDataSize) {
2463 if (desired == 0) {
2464 objectsSize = 0;
2465 } else {
2466 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002467 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002468 break;
2469 objectsSize--;
2470 }
2471 }
2472 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002473
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002474 if (mOwner) {
2475 // If the size is going to zero, just release the owner's data.
2476 if (desired == 0) {
2477 freeData();
2478 return NO_ERROR;
2479 }
2480
2481 // If there is a different owner, we need to take
2482 // posession.
2483 uint8_t* data = (uint8_t*)malloc(desired);
2484 if (!data) {
2485 mError = NO_MEMORY;
2486 return NO_MEMORY;
2487 }
Yi Kong91635562018-06-07 14:38:36 -07002488 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002489
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002490 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002491 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002492 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002493 free(data);
2494
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002495 mError = NO_MEMORY;
2496 return NO_MEMORY;
2497 }
2498
2499 // Little hack to only acquire references on objects
2500 // we will be keeping.
2501 size_t oldObjectsSize = mObjectsSize;
2502 mObjectsSize = objectsSize;
2503 acquireObjects();
2504 mObjectsSize = oldObjectsSize;
2505 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002506
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002507 if (mData) {
2508 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2509 }
2510 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002511 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002512 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002513 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002514 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002515 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002517 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002518 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002519 gParcelGlobalAllocSize += desired;
2520 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002521 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002522
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002523 mData = data;
2524 mObjects = objects;
2525 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002526 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002527 mDataCapacity = desired;
2528 mObjectsSize = mObjectsCapacity = objectsSize;
2529 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002530 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002531
2532 } else if (mData) {
2533 if (objectsSize < mObjectsSize) {
2534 // Need to release refs on any objects we are dropping.
2535 const sp<ProcessState> proc(ProcessState::self());
2536 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2537 const flat_binder_object* flat
2538 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002539 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002540 // will need to rescan because we may have lopped off the only FDs
2541 mFdsKnown = false;
2542 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002543 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002545
2546 if (objectsSize == 0) {
2547 free(mObjects);
2548 mObjects = nullptr;
2549 } else {
2550 binder_size_t* objects =
2551 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2552 if (objects) {
2553 mObjects = objects;
2554 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002555 }
2556 mObjectsSize = objectsSize;
2557 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002558 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002559 }
2560
2561 // We own the data, so we can just do a realloc().
2562 if (desired > mDataCapacity) {
2563 uint8_t* data = (uint8_t*)realloc(mData, desired);
2564 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002565 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2566 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002567 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002568 gParcelGlobalAllocSize += desired;
2569 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002570 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002571 mData = data;
2572 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002573 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002574 mError = NO_MEMORY;
2575 return NO_MEMORY;
2576 }
2577 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002578 if (mDataSize > desired) {
2579 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002580 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002581 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002582 if (mDataPos > desired) {
2583 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002584 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002585 }
2586 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002587
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002588 } else {
2589 // This is the first data. Easy!
2590 uint8_t* data = (uint8_t*)malloc(desired);
2591 if (!data) {
2592 mError = NO_MEMORY;
2593 return NO_MEMORY;
2594 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002595
Yi Kong91635562018-06-07 14:38:36 -07002596 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002597 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002598 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002599 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002600
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002601 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002602 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002603 gParcelGlobalAllocSize += desired;
2604 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002605 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002606
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002607 mData = data;
2608 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002609 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2610 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002611 mDataCapacity = desired;
2612 }
2613
2614 return NO_ERROR;
2615}
2616
2617void Parcel::initState()
2618{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002619 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002620 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002621 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002622 mDataSize = 0;
2623 mDataCapacity = 0;
2624 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002625 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2626 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002627 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002628 mObjectsSize = 0;
2629 mObjectsCapacity = 0;
2630 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002631 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002632 mHasFds = false;
2633 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002634 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002635 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002636 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002637 mWorkSourceRequestHeaderPosition = 0;
2638 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002639
2640 // racing multiple init leads only to multiple identical write
2641 if (gMaxFds == 0) {
2642 struct rlimit result;
2643 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2644 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002645 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002646 } else {
2647 ALOGW("Unable to getrlimit: %s", strerror(errno));
2648 gMaxFds = 1024;
2649 }
2650 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002651}
2652
2653void Parcel::scanForFds() const
2654{
2655 bool hasFds = false;
2656 for (size_t i=0; i<mObjectsSize; i++) {
2657 const flat_binder_object* flat
2658 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002659 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002660 hasFds = true;
2661 break;
2662 }
2663 }
2664 mHasFds = hasFds;
2665 mFdsKnown = true;
2666}
2667
Dan Sandleraa5c2342015-04-10 10:08:45 -04002668size_t Parcel::getBlobAshmemSize() const
2669{
Adrian Roos6bb31142015-10-22 16:46:12 -07002670 // This used to return the size of all blobs that were written to ashmem, now we're returning
2671 // the ashmem currently referenced by this Parcel, which should be equivalent.
2672 // TODO: Remove method once ABI can be changed.
2673 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002674}
2675
Adrian Rooscbf37262015-10-22 16:12:53 -07002676size_t Parcel::getOpenAshmemSize() const
2677{
2678 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002679}
2680
2681// --- Parcel::Blob ---
2682
2683Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002684 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002685}
2686
2687Parcel::Blob::~Blob() {
2688 release();
2689}
2690
2691void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002692 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002693 ::munmap(mData, mSize);
2694 }
2695 clear();
2696}
2697
Jeff Brown13b16042014-11-11 16:44:25 -08002698void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2699 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002700 mData = data;
2701 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002702 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002703}
2704
2705void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002706 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002707 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002708 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002709 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002710}
2711
Steven Moreland61ff8492019-09-26 16:05:45 -07002712} // namespace android