blob: f1077ae1409dc46b8bfa2ed034ba8052a250ac41 [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 Han1b228f42020-01-30 13:41:12 +0900508#if 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{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700561 return enforceInterface(interface.string(), interface.size(), threadState);
562}
563
564bool Parcel::enforceInterface(const char16_t* interface,
565 size_t len,
566 IPCThreadState* threadState) const
567{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100568 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700569 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700570 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700571 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700572 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700573 if ((threadState->getLastTransactionBinderFlags() &
574 IBinder::FLAG_ONEWAY) != 0) {
575 // For one-way calls, the callee is running entirely
576 // disconnected from the caller, so disable StrictMode entirely.
577 // Not only does disk/network usage not impact the caller, but
578 // there's no way to commuicate back any violations anyway.
579 threadState->setStrictModePolicy(0);
580 } else {
581 threadState->setStrictModePolicy(strictPolicy);
582 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100583 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000584 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100585 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000586 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700587 // vendor header
588 int32_t header = readInt32();
589 if (header != kHeader) {
590 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
591 return false;
592 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100593 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700594 size_t parcel_interface_len;
595 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
596 if (len == parcel_interface_len &&
597 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700598 return true;
599 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700600 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700601 String8(interface, len).string(),
602 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700603 return false;
604 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700605}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700606
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700607size_t Parcel::objectsCount() const
608{
609 return mObjectsSize;
610}
611
612status_t Parcel::errorCheck() const
613{
614 return mError;
615}
616
617void Parcel::setError(status_t err)
618{
619 mError = err;
620}
621
622status_t Parcel::finishWrite(size_t len)
623{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700624 if (len > INT32_MAX) {
625 // don't accept size_t values which may have come from an
626 // inadvertent conversion from a negative int.
627 return BAD_VALUE;
628 }
629
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700630 //printf("Finish write of %d\n", len);
631 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700632 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700633 if (mDataPos > mDataSize) {
634 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700635 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700636 }
637 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
638 return NO_ERROR;
639}
640
641status_t Parcel::writeUnpadded(const void* data, size_t len)
642{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700643 if (len > INT32_MAX) {
644 // don't accept size_t values which may have come from an
645 // inadvertent conversion from a negative int.
646 return BAD_VALUE;
647 }
648
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700649 size_t end = mDataPos + len;
650 if (end < mDataPos) {
651 // integer overflow
652 return BAD_VALUE;
653 }
654
655 if (end <= mDataCapacity) {
656restart_write:
657 memcpy(mData+mDataPos, data, len);
658 return finishWrite(len);
659 }
660
661 status_t err = growData(len);
662 if (err == NO_ERROR) goto restart_write;
663 return err;
664}
665
666status_t Parcel::write(const void* data, size_t len)
667{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700668 if (len > INT32_MAX) {
669 // don't accept size_t values which may have come from an
670 // inadvertent conversion from a negative int.
671 return BAD_VALUE;
672 }
673
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700674 void* const d = writeInplace(len);
675 if (d) {
676 memcpy(d, data, len);
677 return NO_ERROR;
678 }
679 return mError;
680}
681
682void* Parcel::writeInplace(size_t len)
683{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700684 if (len > INT32_MAX) {
685 // don't accept size_t values which may have come from an
686 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700687 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700688 }
689
690 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700691
692 // sanity check for integer overflow
693 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700694 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700695 }
696
697 if ((mDataPos+padded) <= mDataCapacity) {
698restart_write:
699 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
700 uint8_t* const data = mData+mDataPos;
701
702 // Need to pad at end?
703 if (padded != len) {
704#if BYTE_ORDER == BIG_ENDIAN
705 static const uint32_t mask[4] = {
706 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
707 };
708#endif
709#if BYTE_ORDER == LITTLE_ENDIAN
710 static const uint32_t mask[4] = {
711 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
712 };
713#endif
714 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
715 // *reinterpret_cast<void**>(data+padded-4));
716 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
717 }
718
719 finishWrite(padded);
720 return data;
721 }
722
723 status_t err = growData(padded);
724 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700725 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700726}
727
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800728status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
729 const uint8_t* strData = (uint8_t*)str.data();
730 const size_t strLen= str.length();
731 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100732 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800733 return BAD_VALUE;
734 }
735
736 status_t err = writeInt32(utf16Len);
737 if (err) {
738 return err;
739 }
740
741 // Allocate enough bytes to hold our converted string and its terminating NULL.
742 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
743 if (!dst) {
744 return NO_MEMORY;
745 }
746
Sergio Girof4607432016-07-21 14:46:35 +0100747 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800748
749 return NO_ERROR;
750}
751
752status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
753 if (!str) {
754 return writeInt32(-1);
755 }
756 return writeUtf8AsUtf16(*str);
757}
758
Daniel Normand0337ef2019-09-20 15:46:03 -0700759status_t Parcel::writeByteVectorInternal(const int8_t* data, size_t size) {
760 if (size > std::numeric_limits<int32_t>::max()) {
761 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -0700762 }
763
Daniel Normand0337ef2019-09-20 15:46:03 -0700764 status_t status = writeInt32(size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700765 if (status != OK) {
766 return status;
767 }
768
Daniel Normand0337ef2019-09-20 15:46:03 -0700769 return write(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700770}
771
Casey Dahlin185d3442016-02-09 11:08:35 -0800772status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700773 return writeByteVectorInternal(val.data(), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800774}
775
776status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
777{
Daniel Normand0337ef2019-09-20 15:46:03 -0700778 if (!val) return writeInt32(-1);
779 return writeByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800780}
781
782status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700783 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val.data()), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800784}
785
786status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
787{
Daniel Normand0337ef2019-09-20 15:46:03 -0700788 if (!val) return writeInt32(-1);
789 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800790}
791
Casey Dahlin451ff582015-10-19 18:12:18 -0700792status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
793{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800794 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700795}
796
Casey Dahlinb9872622015-11-25 15:09:45 -0800797status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
798{
799 return writeNullableTypedVector(val, &Parcel::writeInt32);
800}
801
Casey Dahlin451ff582015-10-19 18:12:18 -0700802status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
803{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800804 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700805}
806
Casey Dahlinb9872622015-11-25 15:09:45 -0800807status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
808{
809 return writeNullableTypedVector(val, &Parcel::writeInt64);
810}
811
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800812status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
813{
814 return writeTypedVector(val, &Parcel::writeUint64);
815}
816
817status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
818{
819 return writeNullableTypedVector(val, &Parcel::writeUint64);
820}
821
Casey Dahlin451ff582015-10-19 18:12:18 -0700822status_t Parcel::writeFloatVector(const std::vector<float>& val)
823{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800824 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700825}
826
Casey Dahlinb9872622015-11-25 15:09:45 -0800827status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
828{
829 return writeNullableTypedVector(val, &Parcel::writeFloat);
830}
831
Casey Dahlin451ff582015-10-19 18:12:18 -0700832status_t Parcel::writeDoubleVector(const std::vector<double>& val)
833{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800834 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700835}
836
Casey Dahlinb9872622015-11-25 15:09:45 -0800837status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
838{
839 return writeNullableTypedVector(val, &Parcel::writeDouble);
840}
841
Casey Dahlin451ff582015-10-19 18:12:18 -0700842status_t Parcel::writeBoolVector(const std::vector<bool>& val)
843{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800844 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700845}
846
Casey Dahlinb9872622015-11-25 15:09:45 -0800847status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
848{
849 return writeNullableTypedVector(val, &Parcel::writeBool);
850}
851
Casey Dahlin451ff582015-10-19 18:12:18 -0700852status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
853{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800854 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700855}
856
Casey Dahlinb9872622015-11-25 15:09:45 -0800857status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
858{
859 return writeNullableTypedVector(val, &Parcel::writeChar);
860}
861
Casey Dahlin451ff582015-10-19 18:12:18 -0700862status_t Parcel::writeString16Vector(const std::vector<String16>& val)
863{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800864 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700865}
866
Casey Dahlinb9872622015-11-25 15:09:45 -0800867status_t Parcel::writeString16Vector(
868 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
869{
870 return writeNullableTypedVector(val, &Parcel::writeString16);
871}
872
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800873status_t Parcel::writeUtf8VectorAsUtf16Vector(
874 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
875 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
876}
877
878status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
879 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
880}
881
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700882status_t Parcel::writeInt32(int32_t val)
883{
Andreas Huber84a6d042009-08-17 13:33:27 -0700884 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700885}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800886
887status_t Parcel::writeUint32(uint32_t val)
888{
889 return writeAligned(val);
890}
891
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700892status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700893 if (len > INT32_MAX) {
894 // don't accept size_t values which may have come from an
895 // inadvertent conversion from a negative int.
896 return BAD_VALUE;
897 }
898
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700899 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700900 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700901 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700902 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700903 if (ret == NO_ERROR) {
904 ret = write(val, len * sizeof(*val));
905 }
906 return ret;
907}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700908status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700909 if (len > INT32_MAX) {
910 // don't accept size_t values which may have come from an
911 // inadvertent conversion from a negative int.
912 return BAD_VALUE;
913 }
914
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700915 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700916 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700917 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700918 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700919 if (ret == NO_ERROR) {
920 ret = write(val, len * sizeof(*val));
921 }
922 return ret;
923}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700924
Casey Dahlind6848f52015-10-15 15:44:59 -0700925status_t Parcel::writeBool(bool val)
926{
927 return writeInt32(int32_t(val));
928}
929
930status_t Parcel::writeChar(char16_t val)
931{
932 return writeInt32(int32_t(val));
933}
934
935status_t Parcel::writeByte(int8_t val)
936{
937 return writeInt32(int32_t(val));
938}
939
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700940status_t Parcel::writeInt64(int64_t val)
941{
Andreas Huber84a6d042009-08-17 13:33:27 -0700942 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700943}
944
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700945status_t Parcel::writeUint64(uint64_t val)
946{
947 return writeAligned(val);
948}
949
Serban Constantinescuf683e012013-11-05 16:53:55 +0000950status_t Parcel::writePointer(uintptr_t val)
951{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800952 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000953}
954
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700955status_t Parcel::writeFloat(float val)
956{
Andreas Huber84a6d042009-08-17 13:33:27 -0700957 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700958}
959
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800960#if defined(__mips__) && defined(__mips_hard_float)
961
962status_t Parcel::writeDouble(double val)
963{
964 union {
965 double d;
966 unsigned long long ll;
967 } u;
968 u.d = val;
969 return writeAligned(u.ll);
970}
971
972#else
973
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700974status_t Parcel::writeDouble(double val)
975{
Andreas Huber84a6d042009-08-17 13:33:27 -0700976 return writeAligned(val);
977}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700978
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800979#endif
980
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700981status_t Parcel::writeCString(const char* str)
982{
983 return write(str, strlen(str)+1);
984}
985
986status_t Parcel::writeString8(const String8& str)
987{
988 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100989 // only write string if its length is more than zero characters,
990 // as readString8 will only read if the length field is non-zero.
991 // this is slightly different from how writeString16 works.
992 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700993 err = write(str.string(), str.bytes()+1);
994 }
995 return err;
996}
997
Casey Dahlinb9872622015-11-25 15:09:45 -0800998status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
999{
1000 if (!str) {
1001 return writeInt32(-1);
1002 }
1003
1004 return writeString16(*str);
1005}
1006
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001007status_t Parcel::writeString16(const String16& str)
1008{
1009 return writeString16(str.string(), str.size());
1010}
1011
1012status_t Parcel::writeString16(const char16_t* str, size_t len)
1013{
Yi Kong91635562018-06-07 14:38:36 -07001014 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001015
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001016 status_t err = writeInt32(len);
1017 if (err == NO_ERROR) {
1018 len *= sizeof(char16_t);
1019 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1020 if (data) {
1021 memcpy(data, str, len);
1022 *reinterpret_cast<char16_t*>(data+len) = 0;
1023 return NO_ERROR;
1024 }
1025 err = mError;
1026 }
1027 return err;
1028}
1029
1030status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1031{
Steven Morelanda86a3562019-08-01 23:28:34 +00001032 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001033}
1034
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001035status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1036{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001037 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001038}
1039
Casey Dahlinb9872622015-11-25 15:09:45 -08001040status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1041{
1042 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1043}
1044
1045status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001046 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001047}
1048
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001049status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001050 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001051}
1052
Casey Dahlinb9872622015-11-25 15:09:45 -08001053status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1054 if (!parcelable) {
1055 return writeInt32(0);
1056 }
1057
1058 return writeParcelable(*parcelable);
1059}
1060
Christopher Wiley97f048d2015-11-19 06:49:05 -08001061status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1062 status_t status = writeInt32(1); // parcelable is not null.
1063 if (status != OK) {
1064 return status;
1065 }
1066 return parcelable.writeToParcel(this);
1067}
1068
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001069status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001070{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001071 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001072 return BAD_TYPE;
1073
1074 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001075 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001076 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001078 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001079 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001081 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1082 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083
1084 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001085 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086 return err;
1087 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001088 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001089 return err;
1090}
1091
Jeff Brown93ff1f92011-11-04 19:01:44 -07001092status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001093{
1094 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001095 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001096 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001097 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001098 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001099 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001100 return writeObject(obj, true);
1101}
1102
1103status_t Parcel::writeDupFileDescriptor(int fd)
1104{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001105 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001106 if (dupFd < 0) {
1107 return -errno;
1108 }
1109 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001110 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001111 close(dupFd);
1112 }
1113 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001114}
1115
Dianne Hackborn1941a402016-08-29 12:30:43 -07001116status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1117{
1118 writeInt32(0);
1119 return writeFileDescriptor(fd, takeOwnership);
1120}
1121
Ryo Hashimotobf551892018-05-31 16:58:35 +09001122status_t Parcel::writeDupParcelFileDescriptor(int fd)
1123{
1124 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1125 if (dupFd < 0) {
1126 return -errno;
1127 }
1128 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1129 if (err != OK) {
1130 close(dupFd);
1131 }
1132 return err;
1133}
1134
Christopher Wiley2cf19952016-04-11 11:09:37 -07001135status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001136 return writeDupFileDescriptor(fd.get());
1137}
1138
Christopher Wiley2cf19952016-04-11 11:09:37 -07001139status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001140 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1141}
1142
Christopher Wiley2cf19952016-04-11 11:09:37 -07001143status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001144 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1145}
1146
Jeff Brown13b16042014-11-11 16:44:25 -08001147status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001148{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001149 if (len > INT32_MAX) {
1150 // don't accept size_t values which may have come from an
1151 // inadvertent conversion from a negative int.
1152 return BAD_VALUE;
1153 }
1154
Jeff Brown13b16042014-11-11 16:44:25 -08001155 status_t status;
1156 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001157 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001158 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001159 if (status) return status;
1160
1161 void* ptr = writeInplace(len);
1162 if (!ptr) return NO_MEMORY;
1163
Jeff Brown13b16042014-11-11 16:44:25 -08001164 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001165 return NO_ERROR;
1166 }
1167
Steve Block6807e592011-10-20 11:56:00 +01001168 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001169 int fd = ashmem_create_region("Parcel Blob", len);
1170 if (fd < 0) return NO_MEMORY;
1171
1172 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1173 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001174 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001175 } else {
Yi Kong91635562018-06-07 14:38:36 -07001176 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001177 if (ptr == MAP_FAILED) {
1178 status = -errno;
1179 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001180 if (!mutableCopy) {
1181 result = ashmem_set_prot_region(fd, PROT_READ);
1182 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001183 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001184 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001185 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001186 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001187 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001188 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001189 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001190 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001191 return NO_ERROR;
1192 }
1193 }
1194 }
1195 }
1196 ::munmap(ptr, len);
1197 }
1198 ::close(fd);
1199 return status;
1200}
1201
Jeff Brown13b16042014-11-11 16:44:25 -08001202status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1203{
1204 // Must match up with what's done in writeBlob.
1205 if (!mAllowFds) return FDS_NOT_ALLOWED;
1206 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1207 if (status) return status;
1208 return writeDupFileDescriptor(fd);
1209}
1210
Mathias Agopiane1424282013-07-29 21:24:40 -07001211status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001212{
1213 status_t err;
1214
1215 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001216 const size_t len = val.getFlattenedSize();
1217 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001218
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001219 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001220 // don't accept size_t values which may have come from an
1221 // inadvertent conversion from a negative int.
1222 return BAD_VALUE;
1223 }
1224
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001225 err = this->writeInt32(len);
1226 if (err) return err;
1227
1228 err = this->writeInt32(fd_count);
1229 if (err) return err;
1230
1231 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001232 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001233 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001234 return BAD_VALUE;
1235
Yi Kong91635562018-06-07 14:38:36 -07001236 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001237 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001238 fds = new (std::nothrow) int[fd_count];
1239 if (fds == nullptr) {
1240 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1241 return BAD_VALUE;
1242 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001243 }
1244
1245 err = val.flatten(buf, len, fds, fd_count);
1246 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1247 err = this->writeDupFileDescriptor( fds[i] );
1248 }
1249
1250 if (fd_count) {
1251 delete [] fds;
1252 }
1253
1254 return err;
1255}
1256
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001257status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1258{
1259 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1260 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1261 if (enoughData && enoughObjects) {
1262restart_write:
1263 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001264
Christopher Tate98e67d32015-06-03 18:44:15 -07001265 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001266 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001267 if (!mAllowFds) {
1268 // fail before modifying our object index
1269 return FDS_NOT_ALLOWED;
1270 }
1271 mHasFds = mFdsKnown = true;
1272 }
1273
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001274 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001275 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001276 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001277 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001278 mObjectsSize++;
1279 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001280
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001281 return finishWrite(sizeof(flat_binder_object));
1282 }
1283
1284 if (!enoughData) {
1285 const status_t err = growData(sizeof(val));
1286 if (err != NO_ERROR) return err;
1287 }
1288 if (!enoughObjects) {
1289 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001290 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001291 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001292 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001293 mObjects = objects;
1294 mObjectsCapacity = newSize;
1295 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001296
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001297 goto restart_write;
1298}
1299
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001300status_t Parcel::writeNoException()
1301{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001302 binder::Status status;
1303 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001304}
1305
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001306status_t Parcel::validateReadData(size_t upperBound) const
1307{
1308 // Don't allow non-object reads on object data
1309 if (mObjectsSorted || mObjectsSize <= 1) {
1310data_sorted:
1311 // Expect to check only against the next object
1312 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1313 // For some reason the current read position is greater than the next object
1314 // hint. Iterate until we find the right object
1315 size_t nextObject = mNextObjectHint;
1316 do {
1317 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1318 // Requested info overlaps with an object
1319 ALOGE("Attempt to read from protected data in Parcel %p", this);
1320 return PERMISSION_DENIED;
1321 }
1322 nextObject++;
1323 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1324 mNextObjectHint = nextObject;
1325 }
1326 return NO_ERROR;
1327 }
1328 // Quickly determine if mObjects is sorted.
1329 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1330 binder_size_t* prevObj = currObj;
1331 while (currObj > mObjects) {
1332 prevObj--;
1333 if(*prevObj > *currObj) {
1334 goto data_unsorted;
1335 }
1336 currObj--;
1337 }
1338 mObjectsSorted = true;
1339 goto data_sorted;
1340
1341data_unsorted:
1342 // Insertion Sort mObjects
1343 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1344 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1345 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1346 binder_size_t temp = *iter0;
1347 binder_size_t* iter1 = iter0 - 1;
1348 while (iter1 >= mObjects && *iter1 > temp) {
1349 *(iter1 + 1) = *iter1;
1350 iter1--;
1351 }
1352 *(iter1 + 1) = temp;
1353 }
1354 mNextObjectHint = 0;
1355 mObjectsSorted = true;
1356 goto data_sorted;
1357}
1358
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001359status_t Parcel::read(void* outData, size_t len) const
1360{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001361 if (len > INT32_MAX) {
1362 // don't accept size_t values which may have come from an
1363 // inadvertent conversion from a negative int.
1364 return BAD_VALUE;
1365 }
1366
1367 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1368 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001369 if (mObjectsSize > 0) {
1370 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001371 if(err != NO_ERROR) {
1372 // Still increment the data position by the expected length
1373 mDataPos += pad_size(len);
1374 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1375 return err;
1376 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001377 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001378 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001379 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001380 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001381 return NO_ERROR;
1382 }
1383 return NOT_ENOUGH_DATA;
1384}
1385
1386const void* Parcel::readInplace(size_t len) const
1387{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001388 if (len > INT32_MAX) {
1389 // don't accept size_t values which may have come from an
1390 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001391 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001392 }
1393
1394 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1395 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001396 if (mObjectsSize > 0) {
1397 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001398 if(err != NO_ERROR) {
1399 // Still increment the data position by the expected length
1400 mDataPos += pad_size(len);
1401 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001402 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001403 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001404 }
1405
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001406 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001407 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001408 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001409 return data;
1410 }
Yi Kong91635562018-06-07 14:38:36 -07001411 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001412}
1413
Andreas Huber84a6d042009-08-17 13:33:27 -07001414template<class T>
1415status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001416 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001417
1418 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001419 if (mObjectsSize > 0) {
1420 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001421 if(err != NO_ERROR) {
1422 // Still increment the data position by the expected length
1423 mDataPos += sizeof(T);
1424 return err;
1425 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001426 }
1427
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001428 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001429 mDataPos += sizeof(T);
1430 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001431 return NO_ERROR;
1432 } else {
1433 return NOT_ENOUGH_DATA;
1434 }
1435}
1436
Andreas Huber84a6d042009-08-17 13:33:27 -07001437template<class T>
1438T Parcel::readAligned() const {
1439 T result;
1440 if (readAligned(&result) != NO_ERROR) {
1441 result = 0;
1442 }
1443
1444 return result;
1445}
1446
1447template<class T>
1448status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001449 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001450
1451 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1452restart_write:
1453 *reinterpret_cast<T*>(mData+mDataPos) = val;
1454 return finishWrite(sizeof(val));
1455 }
1456
1457 status_t err = growData(sizeof(val));
1458 if (err == NO_ERROR) goto restart_write;
1459 return err;
1460}
1461
Casey Dahlin185d3442016-02-09 11:08:35 -08001462status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001463 size_t size;
1464 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1465 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001466}
1467
1468status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001469 size_t size;
1470 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1471 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001472}
1473
1474status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001475 size_t size;
1476 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001477 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001478 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001479 // This occurs when writing a null byte vector.
1480 return OK;
1481 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001482 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001483}
1484
1485status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001486 size_t size;
1487 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001488 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001489 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001490 // This occurs when writing a null byte vector.
1491 return OK;
1492 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001493 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001494}
1495
Casey Dahlinb9872622015-11-25 15:09:45 -08001496status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1497 return readNullableTypedVector(val, &Parcel::readInt32);
1498}
1499
Casey Dahlin451ff582015-10-19 18:12:18 -07001500status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001501 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001502}
1503
Casey Dahlinb9872622015-11-25 15:09:45 -08001504status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1505 return readNullableTypedVector(val, &Parcel::readInt64);
1506}
1507
Casey Dahlin451ff582015-10-19 18:12:18 -07001508status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001509 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001510}
1511
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001512status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1513 return readNullableTypedVector(val, &Parcel::readUint64);
1514}
1515
1516status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1517 return readTypedVector(val, &Parcel::readUint64);
1518}
1519
Casey Dahlinb9872622015-11-25 15:09:45 -08001520status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1521 return readNullableTypedVector(val, &Parcel::readFloat);
1522}
1523
Casey Dahlin451ff582015-10-19 18:12:18 -07001524status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001525 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001526}
1527
Casey Dahlinb9872622015-11-25 15:09:45 -08001528status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1529 return readNullableTypedVector(val, &Parcel::readDouble);
1530}
1531
Casey Dahlin451ff582015-10-19 18:12:18 -07001532status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001533 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001534}
1535
Casey Dahlinb9872622015-11-25 15:09:45 -08001536status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1537 const int32_t start = dataPosition();
1538 int32_t size;
1539 status_t status = readInt32(&size);
1540 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001541
Casey Dahlinb9872622015-11-25 15:09:45 -08001542 if (status != OK || size < 0) {
1543 return status;
1544 }
1545
1546 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001547 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001548
1549 status = readBoolVector(val->get());
1550
1551 if (status != OK) {
1552 val->reset();
1553 }
1554
1555 return status;
1556}
1557
1558status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001559 int32_t size;
1560 status_t status = readInt32(&size);
1561
1562 if (status != OK) {
1563 return status;
1564 }
1565
1566 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001567 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001568 }
1569
1570 val->resize(size);
1571
1572 /* C++ bool handling means a vector of bools isn't necessarily addressable
1573 * (we might use individual bits)
1574 */
Christopher Wiley97887982015-10-27 16:33:47 -07001575 bool data;
1576 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001577 status = readBool(&data);
1578 (*val)[i] = data;
1579
1580 if (status != OK) {
1581 return status;
1582 }
1583 }
1584
1585 return OK;
1586}
1587
Casey Dahlinb9872622015-11-25 15:09:45 -08001588status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1589 return readNullableTypedVector(val, &Parcel::readChar);
1590}
1591
Casey Dahlin451ff582015-10-19 18:12:18 -07001592status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001593 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001594}
1595
Casey Dahlinb9872622015-11-25 15:09:45 -08001596status_t Parcel::readString16Vector(
1597 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1598 return readNullableTypedVector(val, &Parcel::readString16);
1599}
1600
Casey Dahlin451ff582015-10-19 18:12:18 -07001601status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001602 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001603}
1604
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001605status_t Parcel::readUtf8VectorFromUtf16Vector(
1606 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1607 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1608}
1609
1610status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1611 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1612}
Casey Dahlin451ff582015-10-19 18:12:18 -07001613
Andreas Huber84a6d042009-08-17 13:33:27 -07001614status_t Parcel::readInt32(int32_t *pArg) const
1615{
1616 return readAligned(pArg);
1617}
1618
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001619int32_t Parcel::readInt32() const
1620{
Andreas Huber84a6d042009-08-17 13:33:27 -07001621 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001622}
1623
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001624status_t Parcel::readUint32(uint32_t *pArg) const
1625{
1626 return readAligned(pArg);
1627}
1628
1629uint32_t Parcel::readUint32() const
1630{
1631 return readAligned<uint32_t>();
1632}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001633
1634status_t Parcel::readInt64(int64_t *pArg) const
1635{
Andreas Huber84a6d042009-08-17 13:33:27 -07001636 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001637}
1638
1639
1640int64_t Parcel::readInt64() const
1641{
Andreas Huber84a6d042009-08-17 13:33:27 -07001642 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001643}
1644
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001645status_t Parcel::readUint64(uint64_t *pArg) const
1646{
1647 return readAligned(pArg);
1648}
1649
1650uint64_t Parcel::readUint64() const
1651{
1652 return readAligned<uint64_t>();
1653}
1654
Serban Constantinescuf683e012013-11-05 16:53:55 +00001655status_t Parcel::readPointer(uintptr_t *pArg) const
1656{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001657 status_t ret;
1658 binder_uintptr_t ptr;
1659 ret = readAligned(&ptr);
1660 if (!ret)
1661 *pArg = ptr;
1662 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001663}
1664
1665uintptr_t Parcel::readPointer() const
1666{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001667 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001668}
1669
1670
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001671status_t Parcel::readFloat(float *pArg) const
1672{
Andreas Huber84a6d042009-08-17 13:33:27 -07001673 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001674}
1675
1676
1677float Parcel::readFloat() const
1678{
Andreas Huber84a6d042009-08-17 13:33:27 -07001679 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001680}
1681
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001682#if defined(__mips__) && defined(__mips_hard_float)
1683
1684status_t Parcel::readDouble(double *pArg) const
1685{
1686 union {
1687 double d;
1688 unsigned long long ll;
1689 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001690 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001691 status_t status;
1692 status = readAligned(&u.ll);
1693 *pArg = u.d;
1694 return status;
1695}
1696
1697double Parcel::readDouble() const
1698{
1699 union {
1700 double d;
1701 unsigned long long ll;
1702 } u;
1703 u.ll = readAligned<unsigned long long>();
1704 return u.d;
1705}
1706
1707#else
1708
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001709status_t Parcel::readDouble(double *pArg) const
1710{
Andreas Huber84a6d042009-08-17 13:33:27 -07001711 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001712}
1713
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001714double Parcel::readDouble() const
1715{
Andreas Huber84a6d042009-08-17 13:33:27 -07001716 return readAligned<double>();
1717}
1718
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001719#endif
1720
Andreas Huber84a6d042009-08-17 13:33:27 -07001721status_t Parcel::readIntPtr(intptr_t *pArg) const
1722{
1723 return readAligned(pArg);
1724}
1725
1726
1727intptr_t Parcel::readIntPtr() const
1728{
1729 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001730}
1731
Casey Dahlind6848f52015-10-15 15:44:59 -07001732status_t Parcel::readBool(bool *pArg) const
1733{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001734 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001735 status_t ret = readInt32(&tmp);
1736 *pArg = (tmp != 0);
1737 return ret;
1738}
1739
1740bool Parcel::readBool() const
1741{
1742 return readInt32() != 0;
1743}
1744
1745status_t Parcel::readChar(char16_t *pArg) const
1746{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001747 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001748 status_t ret = readInt32(&tmp);
1749 *pArg = char16_t(tmp);
1750 return ret;
1751}
1752
1753char16_t Parcel::readChar() const
1754{
1755 return char16_t(readInt32());
1756}
1757
1758status_t Parcel::readByte(int8_t *pArg) const
1759{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001760 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001761 status_t ret = readInt32(&tmp);
1762 *pArg = int8_t(tmp);
1763 return ret;
1764}
1765
1766int8_t Parcel::readByte() const
1767{
1768 return int8_t(readInt32());
1769}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001770
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001771status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1772 size_t utf16Size = 0;
1773 const char16_t* src = readString16Inplace(&utf16Size);
1774 if (!src) {
1775 return UNEXPECTED_NULL;
1776 }
1777
1778 // Save ourselves the trouble, we're done.
1779 if (utf16Size == 0u) {
1780 str->clear();
1781 return NO_ERROR;
1782 }
1783
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001784 // Allow for closing '\0'
1785 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1786 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001787 return BAD_VALUE;
1788 }
1789 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001790 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001791 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001792 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1793 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001794 return NO_ERROR;
1795}
1796
1797status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1798 const int32_t start = dataPosition();
1799 int32_t size;
1800 status_t status = readInt32(&size);
1801 str->reset();
1802
1803 if (status != OK || size < 0) {
1804 return status;
1805 }
1806
1807 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001808 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001809 return readUtf8FromUtf16(str->get());
1810}
1811
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001812const char* Parcel::readCString() const
1813{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001814 if (mDataPos < mDataSize) {
1815 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001816 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1817 // is the string's trailing NUL within the parcel's valid bounds?
1818 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1819 if (eos) {
1820 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001821 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001822 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001823 return str;
1824 }
1825 }
Yi Kong91635562018-06-07 14:38:36 -07001826 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001827}
1828
1829String8 Parcel::readString8() const
1830{
Roshan Pius87b64d22016-07-18 12:51:02 -07001831 String8 retString;
1832 status_t status = readString8(&retString);
1833 if (status != OK) {
1834 // We don't care about errors here, so just return an empty string.
1835 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001836 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001837 return retString;
1838}
1839
1840status_t Parcel::readString8(String8* pArg) const
1841{
1842 int32_t size;
1843 status_t status = readInt32(&size);
1844 if (status != OK) {
1845 return status;
1846 }
1847 // watch for potential int overflow from size+1
1848 if (size < 0 || size >= INT32_MAX) {
1849 return BAD_VALUE;
1850 }
1851 // |writeString8| writes nothing for empty string.
1852 if (size == 0) {
1853 *pArg = String8();
1854 return OK;
1855 }
1856 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07001857 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07001858 return BAD_VALUE;
1859 }
1860 pArg->setTo(str, size);
1861 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001862}
1863
1864String16 Parcel::readString16() const
1865{
1866 size_t len;
1867 const char16_t* str = readString16Inplace(&len);
1868 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001869 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001870 return String16();
1871}
1872
Casey Dahlinb9872622015-11-25 15:09:45 -08001873status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1874{
1875 const int32_t start = dataPosition();
1876 int32_t size;
1877 status_t status = readInt32(&size);
1878 pArg->reset();
1879
1880 if (status != OK || size < 0) {
1881 return status;
1882 }
1883
1884 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001885 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001886
1887 status = readString16(pArg->get());
1888
1889 if (status != OK) {
1890 pArg->reset();
1891 }
1892
1893 return status;
1894}
1895
Casey Dahlin451ff582015-10-19 18:12:18 -07001896status_t Parcel::readString16(String16* pArg) const
1897{
1898 size_t len;
1899 const char16_t* str = readString16Inplace(&len);
1900 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001901 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001902 return 0;
1903 } else {
1904 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001905 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001906 }
1907}
1908
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001909const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1910{
1911 int32_t size = readInt32();
1912 // watch for potential int overflow from size+1
1913 if (size >= 0 && size < INT32_MAX) {
1914 *outLen = size;
1915 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001916 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001917 return str;
1918 }
1919 }
1920 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001921 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001922}
1923
Casey Dahlinf0c13772015-10-27 18:33:56 -07001924status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1925{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001926 status_t status = readNullableStrongBinder(val);
1927 if (status == OK && !val->get()) {
1928 status = UNEXPECTED_NULL;
1929 }
1930 return status;
1931}
1932
1933status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1934{
Steven Morelanda86a3562019-08-01 23:28:34 +00001935 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001936}
1937
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001938sp<IBinder> Parcel::readStrongBinder() const
1939{
1940 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001941 // Note that a lot of code in Android reads binders by hand with this
1942 // method, and that code has historically been ok with getting nullptr
1943 // back (while ignoring error codes).
1944 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001945 return val;
1946}
1947
Christopher Wiley97f048d2015-11-19 06:49:05 -08001948status_t Parcel::readParcelable(Parcelable* parcelable) const {
1949 int32_t have_parcelable = 0;
1950 status_t status = readInt32(&have_parcelable);
1951 if (status != OK) {
1952 return status;
1953 }
1954 if (!have_parcelable) {
1955 return UNEXPECTED_NULL;
1956 }
1957 return parcelable->readFromParcel(this);
1958}
1959
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001960int32_t Parcel::readExceptionCode() const
1961{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001962 binder::Status status;
1963 status.readFromParcel(*this);
1964 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001965}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001966
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001967native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001968{
1969 int numFds, numInts;
1970 status_t err;
1971 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001972 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001973 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001974 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001975
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001976 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001977 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001978 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001979 }
1980
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001981 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001982 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001983 if (h->data[i] < 0) {
1984 for (int j = 0; j < i; j++) {
1985 close(h->data[j]);
1986 }
1987 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001988 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001989 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001990 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001991 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001992 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001993 native_handle_close(h);
1994 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001995 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001996 }
1997 return h;
1998}
1999
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002000int Parcel::readFileDescriptor() const
2001{
2002 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002003
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002004 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002005 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002006 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002007
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002008 return BAD_TYPE;
2009}
2010
Dianne Hackborn1941a402016-08-29 12:30:43 -07002011int Parcel::readParcelFileDescriptor() const
2012{
2013 int32_t hasComm = readInt32();
2014 int fd = readFileDescriptor();
2015 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002016 // detach (owned by the binder driver)
2017 int comm = readFileDescriptor();
2018
2019 // warning: this must be kept in sync with:
2020 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2021 enum ParcelFileDescriptorStatus {
2022 DETACHED = 2,
2023 };
2024
2025#if BYTE_ORDER == BIG_ENDIAN
2026 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2027#endif
2028#if BYTE_ORDER == LITTLE_ENDIAN
2029 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2030#endif
2031
2032 ssize_t written = TEMP_FAILURE_RETRY(
2033 ::write(comm, &message, sizeof(message)));
2034
2035 if (written == -1 || written != sizeof(message)) {
2036 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2037 written, strerror(errno));
2038 return BAD_TYPE;
2039 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002040 }
2041 return fd;
2042}
2043
Christopher Wiley2cf19952016-04-11 11:09:37 -07002044status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002045{
2046 int got = readFileDescriptor();
2047
2048 if (got == BAD_TYPE) {
2049 return BAD_TYPE;
2050 }
2051
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002052 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002053
2054 if (val->get() < 0) {
2055 return BAD_VALUE;
2056 }
2057
2058 return OK;
2059}
2060
Ryo Hashimotobf551892018-05-31 16:58:35 +09002061status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2062{
2063 int got = readParcelFileDescriptor();
2064
2065 if (got == BAD_TYPE) {
2066 return BAD_TYPE;
2067 }
2068
2069 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2070
2071 if (val->get() < 0) {
2072 return BAD_VALUE;
2073 }
2074
2075 return OK;
2076}
Casey Dahlin06673e32015-11-23 13:24:23 -08002077
Christopher Wiley2cf19952016-04-11 11:09:37 -07002078status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002079 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2080}
2081
Christopher Wiley2cf19952016-04-11 11:09:37 -07002082status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002083 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2084}
2085
Jeff Brown5707dbf2011-09-23 21:17:56 -07002086status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2087{
Jeff Brown13b16042014-11-11 16:44:25 -08002088 int32_t blobType;
2089 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002090 if (status) return status;
2091
Jeff Brown13b16042014-11-11 16:44:25 -08002092 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002093 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002094 const void* ptr = readInplace(len);
2095 if (!ptr) return BAD_VALUE;
2096
Jeff Brown13b16042014-11-11 16:44:25 -08002097 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002098 return NO_ERROR;
2099 }
2100
Steve Block6807e592011-10-20 11:56:00 +01002101 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002102 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002103 int fd = readFileDescriptor();
2104 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2105
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002106 if (!ashmem_valid(fd)) {
2107 ALOGE("invalid fd");
2108 return BAD_VALUE;
2109 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002110 int size = ashmem_get_size_region(fd);
2111 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002112 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002113 return BAD_VALUE;
2114 }
Yi Kong91635562018-06-07 14:38:36 -07002115 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002116 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002117 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002118
Jeff Brown13b16042014-11-11 16:44:25 -08002119 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002120 return NO_ERROR;
2121}
2122
Mathias Agopiane1424282013-07-29 21:24:40 -07002123status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002124{
2125 // size
2126 const size_t len = this->readInt32();
2127 const size_t fd_count = this->readInt32();
2128
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002129 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002130 // don't accept size_t values which may have come from an
2131 // inadvertent conversion from a negative int.
2132 return BAD_VALUE;
2133 }
2134
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002135 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002136 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002137 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002138 return BAD_VALUE;
2139
Yi Kong91635562018-06-07 14:38:36 -07002140 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002141 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002142 fds = new (std::nothrow) int[fd_count];
2143 if (fds == nullptr) {
2144 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2145 return BAD_VALUE;
2146 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002147 }
2148
2149 status_t err = NO_ERROR;
2150 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002151 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002152 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002153 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002154 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 -07002155 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2156 // Close all the file descriptors that were dup-ed.
2157 for (size_t j=0; j<i ;j++) {
2158 close(fds[j]);
2159 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002160 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002161 }
2162
2163 if (err == NO_ERROR) {
2164 err = val.unflatten(buf, len, fds, fd_count);
2165 }
2166
2167 if (fd_count) {
2168 delete [] fds;
2169 }
2170
2171 return err;
2172}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002173const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2174{
2175 const size_t DPOS = mDataPos;
2176 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2177 const flat_binder_object* obj
2178 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2179 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002180 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002181 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002182 // the object list, so we don't want to check for it when
2183 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002184 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002185 return obj;
2186 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002187
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002188 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002189 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002190 const size_t N = mObjectsSize;
2191 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002192
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002193 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002194 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002195 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002196
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002197 // Start at the current hint position, looking for an object at
2198 // the current data position.
2199 if (opos < N) {
2200 while (opos < (N-1) && OBJS[opos] < DPOS) {
2201 opos++;
2202 }
2203 } else {
2204 opos = N-1;
2205 }
2206 if (OBJS[opos] == DPOS) {
2207 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002208 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002209 this, DPOS, opos);
2210 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002211 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002212 return obj;
2213 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002214
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002215 // Look backwards for it...
2216 while (opos > 0 && OBJS[opos] > DPOS) {
2217 opos--;
2218 }
2219 if (OBJS[opos] == DPOS) {
2220 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002221 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002222 this, DPOS, opos);
2223 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002224 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002225 return obj;
2226 }
2227 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002228 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 -07002229 this, DPOS);
2230 }
Yi Kong91635562018-06-07 14:38:36 -07002231 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002232}
2233
2234void Parcel::closeFileDescriptors()
2235{
2236 size_t i = mObjectsSize;
2237 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002238 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002239 }
2240 while (i > 0) {
2241 i--;
2242 const flat_binder_object* flat
2243 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002244 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002245 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002246 close(flat->handle);
2247 }
2248 }
2249}
2250
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002251uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002252{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002253 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002254}
2255
2256size_t Parcel::ipcDataSize() const
2257{
2258 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2259}
2260
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002261uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002262{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002263 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002264}
2265
2266size_t Parcel::ipcObjectsCount() const
2267{
2268 return mObjectsSize;
2269}
2270
2271void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002272 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002273{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002274 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002275 freeDataNoInit();
2276 mError = NO_ERROR;
2277 mData = const_cast<uint8_t*>(data);
2278 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002279 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002280 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002281 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002282 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002283 mObjectsSize = mObjectsCapacity = objectsCount;
2284 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002285 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002286 mOwner = relFunc;
2287 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002288 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002289 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002290 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002291 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002292 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002293 mObjectsSize = 0;
2294 break;
2295 }
2296 minOffset = offset + sizeof(flat_binder_object);
2297 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002298 scanForFds();
2299}
2300
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002301void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002302{
2303 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002304
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002305 if (errorCheck() != NO_ERROR) {
2306 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002307 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002308 } else if (dataSize() > 0) {
2309 const uint8_t* DATA = data();
2310 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002311 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002312 const size_t N = objectsCount();
2313 for (size_t i=0; i<N; i++) {
2314 const flat_binder_object* flat
2315 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2316 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002317 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002318 << " = " << flat->binder;
2319 }
2320 } else {
2321 to << "NULL";
2322 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002323
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002324 to << ")";
2325}
2326
2327void Parcel::releaseObjects()
2328{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002329 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002330 if (i == 0) {
2331 return;
2332 }
2333 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002334 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002335 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002336 while (i > 0) {
2337 i--;
2338 const flat_binder_object* flat
2339 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002340 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002341 }
2342}
2343
2344void Parcel::acquireObjects()
2345{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002346 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002347 if (i == 0) {
2348 return;
2349 }
2350 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002351 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002352 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002353 while (i > 0) {
2354 i--;
2355 const flat_binder_object* flat
2356 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002357 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002358 }
2359}
2360
2361void Parcel::freeData()
2362{
2363 freeDataNoInit();
2364 initState();
2365}
2366
2367void Parcel::freeDataNoInit()
2368{
2369 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002370 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002371 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002372 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2373 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002374 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002375 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002376 if (mData) {
2377 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002378 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002379 if (mDataCapacity <= gParcelGlobalAllocSize) {
2380 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2381 } else {
2382 gParcelGlobalAllocSize = 0;
2383 }
2384 if (gParcelGlobalAllocCount > 0) {
2385 gParcelGlobalAllocCount--;
2386 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002387 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002388 free(mData);
2389 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002390 if (mObjects) free(mObjects);
2391 }
2392}
2393
2394status_t Parcel::growData(size_t len)
2395{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002396 if (len > INT32_MAX) {
2397 // don't accept size_t values which may have come from an
2398 // inadvertent conversion from a negative int.
2399 return BAD_VALUE;
2400 }
2401
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002402 size_t newSize = ((mDataSize+len)*3)/2;
2403 return (newSize <= mDataSize)
2404 ? (status_t) NO_MEMORY
2405 : continueWrite(newSize);
2406}
2407
2408status_t Parcel::restartWrite(size_t desired)
2409{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002410 if (desired > INT32_MAX) {
2411 // don't accept size_t values which may have come from an
2412 // inadvertent conversion from a negative int.
2413 return BAD_VALUE;
2414 }
2415
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002416 if (mOwner) {
2417 freeData();
2418 return continueWrite(desired);
2419 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002420
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421 uint8_t* data = (uint8_t*)realloc(mData, desired);
2422 if (!data && desired > mDataCapacity) {
2423 mError = NO_MEMORY;
2424 return NO_MEMORY;
2425 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002426
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002428
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002429 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002430 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002431 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002432 gParcelGlobalAllocSize += desired;
2433 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002434 if (!mData) {
2435 gParcelGlobalAllocCount++;
2436 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002437 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002438 mData = data;
2439 mDataCapacity = desired;
2440 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002441
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002442 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002443 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2444 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2445
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002446 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002447 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002448 mObjectsSize = mObjectsCapacity = 0;
2449 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002450 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002451 mHasFds = false;
2452 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002453 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002454
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002455 return NO_ERROR;
2456}
2457
2458status_t Parcel::continueWrite(size_t desired)
2459{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002460 if (desired > INT32_MAX) {
2461 // don't accept size_t values which may have come from an
2462 // inadvertent conversion from a negative int.
2463 return BAD_VALUE;
2464 }
2465
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002466 // If shrinking, first adjust for any objects that appear
2467 // after the new data size.
2468 size_t objectsSize = mObjectsSize;
2469 if (desired < mDataSize) {
2470 if (desired == 0) {
2471 objectsSize = 0;
2472 } else {
2473 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002474 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002475 break;
2476 objectsSize--;
2477 }
2478 }
2479 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002480
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002481 if (mOwner) {
2482 // If the size is going to zero, just release the owner's data.
2483 if (desired == 0) {
2484 freeData();
2485 return NO_ERROR;
2486 }
2487
2488 // If there is a different owner, we need to take
2489 // posession.
2490 uint8_t* data = (uint8_t*)malloc(desired);
2491 if (!data) {
2492 mError = NO_MEMORY;
2493 return NO_MEMORY;
2494 }
Yi Kong91635562018-06-07 14:38:36 -07002495 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002496
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002497 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002498 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002499 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002500 free(data);
2501
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002502 mError = NO_MEMORY;
2503 return NO_MEMORY;
2504 }
2505
2506 // Little hack to only acquire references on objects
2507 // we will be keeping.
2508 size_t oldObjectsSize = mObjectsSize;
2509 mObjectsSize = objectsSize;
2510 acquireObjects();
2511 mObjectsSize = oldObjectsSize;
2512 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002513
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002514 if (mData) {
2515 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2516 }
2517 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002518 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002519 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002520 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002521 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002522 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002523
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002524 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002525 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002526 gParcelGlobalAllocSize += desired;
2527 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002528 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002529
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002530 mData = data;
2531 mObjects = objects;
2532 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002533 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002534 mDataCapacity = desired;
2535 mObjectsSize = mObjectsCapacity = objectsSize;
2536 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002537 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002538
2539 } else if (mData) {
2540 if (objectsSize < mObjectsSize) {
2541 // Need to release refs on any objects we are dropping.
2542 const sp<ProcessState> proc(ProcessState::self());
2543 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2544 const flat_binder_object* flat
2545 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002546 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002547 // will need to rescan because we may have lopped off the only FDs
2548 mFdsKnown = false;
2549 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002550 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002551 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002552
2553 if (objectsSize == 0) {
2554 free(mObjects);
2555 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002556 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002557 } else {
2558 binder_size_t* objects =
2559 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2560 if (objects) {
2561 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002562 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002563 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002564 }
2565 mObjectsSize = objectsSize;
2566 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002567 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002568 }
2569
2570 // We own the data, so we can just do a realloc().
2571 if (desired > mDataCapacity) {
2572 uint8_t* data = (uint8_t*)realloc(mData, desired);
2573 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002574 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2575 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002576 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002577 gParcelGlobalAllocSize += desired;
2578 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002579 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002580 mData = data;
2581 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002582 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002583 mError = NO_MEMORY;
2584 return NO_MEMORY;
2585 }
2586 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002587 if (mDataSize > desired) {
2588 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002589 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002590 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002591 if (mDataPos > desired) {
2592 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002593 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002594 }
2595 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002596
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002597 } else {
2598 // This is the first data. Easy!
2599 uint8_t* data = (uint8_t*)malloc(desired);
2600 if (!data) {
2601 mError = NO_MEMORY;
2602 return NO_MEMORY;
2603 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002604
Yi Kong91635562018-06-07 14:38:36 -07002605 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002606 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002607 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002608 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002609
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002610 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002611 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002612 gParcelGlobalAllocSize += desired;
2613 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002614 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002615
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002616 mData = data;
2617 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002618 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2619 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002620 mDataCapacity = desired;
2621 }
2622
2623 return NO_ERROR;
2624}
2625
2626void Parcel::initState()
2627{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002628 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002629 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002630 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002631 mDataSize = 0;
2632 mDataCapacity = 0;
2633 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002634 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2635 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002636 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002637 mObjectsSize = 0;
2638 mObjectsCapacity = 0;
2639 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002640 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002641 mHasFds = false;
2642 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002643 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002644 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002645 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002646 mWorkSourceRequestHeaderPosition = 0;
2647 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002648
2649 // racing multiple init leads only to multiple identical write
2650 if (gMaxFds == 0) {
2651 struct rlimit result;
2652 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2653 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002654 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002655 } else {
2656 ALOGW("Unable to getrlimit: %s", strerror(errno));
2657 gMaxFds = 1024;
2658 }
2659 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002660}
2661
2662void Parcel::scanForFds() const
2663{
2664 bool hasFds = false;
2665 for (size_t i=0; i<mObjectsSize; i++) {
2666 const flat_binder_object* flat
2667 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002668 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002669 hasFds = true;
2670 break;
2671 }
2672 }
2673 mHasFds = hasFds;
2674 mFdsKnown = true;
2675}
2676
Dan Sandleraa5c2342015-04-10 10:08:45 -04002677size_t Parcel::getBlobAshmemSize() const
2678{
Adrian Roos6bb31142015-10-22 16:46:12 -07002679 // This used to return the size of all blobs that were written to ashmem, now we're returning
2680 // the ashmem currently referenced by this Parcel, which should be equivalent.
2681 // TODO: Remove method once ABI can be changed.
2682 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002683}
2684
Adrian Rooscbf37262015-10-22 16:12:53 -07002685size_t Parcel::getOpenAshmemSize() const
2686{
2687 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002688}
2689
2690// --- Parcel::Blob ---
2691
2692Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002693 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002694}
2695
2696Parcel::Blob::~Blob() {
2697 release();
2698}
2699
2700void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002701 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002702 ::munmap(mData, mSize);
2703 }
2704 clear();
2705}
2706
Jeff Brown13b16042014-11-11 16:44:25 -08002707void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2708 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002709 mData = data;
2710 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002711 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002712}
2713
2714void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002715 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002716 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002717 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002718 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002719}
2720
Steven Moreland61ff8492019-09-26 16:05:45 -07002721} // namespace android