blob: 822247f561d606906e7086adfec585c26b59a506 [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{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100561 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700562 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700563 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700564 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700565 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700566 if ((threadState->getLastTransactionBinderFlags() &
567 IBinder::FLAG_ONEWAY) != 0) {
568 // For one-way calls, the callee is running entirely
569 // disconnected from the caller, so disable StrictMode entirely.
570 // Not only does disk/network usage not impact the caller, but
571 // there's no way to commuicate back any violations anyway.
572 threadState->setStrictModePolicy(0);
573 } else {
574 threadState->setStrictModePolicy(strictPolicy);
575 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100576 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000577 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100578 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000579 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700580 // vendor header
581 int32_t header = readInt32();
582 if (header != kHeader) {
583 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
584 return false;
585 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100586 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700587 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700588 if (str == interface) {
589 return true;
590 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700591 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700592 String8(interface).string(), String8(str).string());
593 return false;
594 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700595}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700596
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700597size_t Parcel::objectsCount() const
598{
599 return mObjectsSize;
600}
601
602status_t Parcel::errorCheck() const
603{
604 return mError;
605}
606
607void Parcel::setError(status_t err)
608{
609 mError = err;
610}
611
612status_t Parcel::finishWrite(size_t len)
613{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700614 if (len > INT32_MAX) {
615 // don't accept size_t values which may have come from an
616 // inadvertent conversion from a negative int.
617 return BAD_VALUE;
618 }
619
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620 //printf("Finish write of %d\n", len);
621 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700622 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700623 if (mDataPos > mDataSize) {
624 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700625 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700626 }
627 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
628 return NO_ERROR;
629}
630
631status_t Parcel::writeUnpadded(const void* data, size_t len)
632{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700633 if (len > INT32_MAX) {
634 // don't accept size_t values which may have come from an
635 // inadvertent conversion from a negative int.
636 return BAD_VALUE;
637 }
638
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700639 size_t end = mDataPos + len;
640 if (end < mDataPos) {
641 // integer overflow
642 return BAD_VALUE;
643 }
644
645 if (end <= mDataCapacity) {
646restart_write:
647 memcpy(mData+mDataPos, data, len);
648 return finishWrite(len);
649 }
650
651 status_t err = growData(len);
652 if (err == NO_ERROR) goto restart_write;
653 return err;
654}
655
656status_t Parcel::write(const void* data, size_t len)
657{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700658 if (len > INT32_MAX) {
659 // don't accept size_t values which may have come from an
660 // inadvertent conversion from a negative int.
661 return BAD_VALUE;
662 }
663
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700664 void* const d = writeInplace(len);
665 if (d) {
666 memcpy(d, data, len);
667 return NO_ERROR;
668 }
669 return mError;
670}
671
672void* Parcel::writeInplace(size_t len)
673{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700674 if (len > INT32_MAX) {
675 // don't accept size_t values which may have come from an
676 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700677 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700678 }
679
680 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700681
682 // sanity check for integer overflow
683 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700684 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700685 }
686
687 if ((mDataPos+padded) <= mDataCapacity) {
688restart_write:
689 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
690 uint8_t* const data = mData+mDataPos;
691
692 // Need to pad at end?
693 if (padded != len) {
694#if BYTE_ORDER == BIG_ENDIAN
695 static const uint32_t mask[4] = {
696 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
697 };
698#endif
699#if BYTE_ORDER == LITTLE_ENDIAN
700 static const uint32_t mask[4] = {
701 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
702 };
703#endif
704 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
705 // *reinterpret_cast<void**>(data+padded-4));
706 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
707 }
708
709 finishWrite(padded);
710 return data;
711 }
712
713 status_t err = growData(padded);
714 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700715 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700716}
717
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800718status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
719 const uint8_t* strData = (uint8_t*)str.data();
720 const size_t strLen= str.length();
721 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100722 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800723 return BAD_VALUE;
724 }
725
726 status_t err = writeInt32(utf16Len);
727 if (err) {
728 return err;
729 }
730
731 // Allocate enough bytes to hold our converted string and its terminating NULL.
732 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
733 if (!dst) {
734 return NO_MEMORY;
735 }
736
Sergio Girof4607432016-07-21 14:46:35 +0100737 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800738
739 return NO_ERROR;
740}
741
742status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
743 if (!str) {
744 return writeInt32(-1);
745 }
746 return writeUtf8AsUtf16(*str);
747}
748
Daniel Normand0337ef2019-09-20 15:46:03 -0700749status_t Parcel::writeByteVectorInternal(const int8_t* data, size_t size) {
750 if (size > std::numeric_limits<int32_t>::max()) {
751 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -0700752 }
753
Daniel Normand0337ef2019-09-20 15:46:03 -0700754 status_t status = writeInt32(size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700755 if (status != OK) {
756 return status;
757 }
758
Daniel Normand0337ef2019-09-20 15:46:03 -0700759 return write(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700760}
761
Casey Dahlin185d3442016-02-09 11:08:35 -0800762status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700763 return writeByteVectorInternal(val.data(), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800764}
765
766status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
767{
Daniel Normand0337ef2019-09-20 15:46:03 -0700768 if (!val) return writeInt32(-1);
769 return writeByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800770}
771
772status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700773 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val.data()), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800774}
775
776status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
777{
Daniel Normand0337ef2019-09-20 15:46:03 -0700778 if (!val) return writeInt32(-1);
779 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800780}
781
Casey Dahlin451ff582015-10-19 18:12:18 -0700782status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
783{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800784 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700785}
786
Casey Dahlinb9872622015-11-25 15:09:45 -0800787status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
788{
789 return writeNullableTypedVector(val, &Parcel::writeInt32);
790}
791
Casey Dahlin451ff582015-10-19 18:12:18 -0700792status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
793{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800794 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700795}
796
Casey Dahlinb9872622015-11-25 15:09:45 -0800797status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
798{
799 return writeNullableTypedVector(val, &Parcel::writeInt64);
800}
801
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800802status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
803{
804 return writeTypedVector(val, &Parcel::writeUint64);
805}
806
807status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
808{
809 return writeNullableTypedVector(val, &Parcel::writeUint64);
810}
811
Casey Dahlin451ff582015-10-19 18:12:18 -0700812status_t Parcel::writeFloatVector(const std::vector<float>& val)
813{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800814 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700815}
816
Casey Dahlinb9872622015-11-25 15:09:45 -0800817status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
818{
819 return writeNullableTypedVector(val, &Parcel::writeFloat);
820}
821
Casey Dahlin451ff582015-10-19 18:12:18 -0700822status_t Parcel::writeDoubleVector(const std::vector<double>& val)
823{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800824 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700825}
826
Casey Dahlinb9872622015-11-25 15:09:45 -0800827status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
828{
829 return writeNullableTypedVector(val, &Parcel::writeDouble);
830}
831
Casey Dahlin451ff582015-10-19 18:12:18 -0700832status_t Parcel::writeBoolVector(const std::vector<bool>& val)
833{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800834 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700835}
836
Casey Dahlinb9872622015-11-25 15:09:45 -0800837status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
838{
839 return writeNullableTypedVector(val, &Parcel::writeBool);
840}
841
Casey Dahlin451ff582015-10-19 18:12:18 -0700842status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
843{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800844 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700845}
846
Casey Dahlinb9872622015-11-25 15:09:45 -0800847status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
848{
849 return writeNullableTypedVector(val, &Parcel::writeChar);
850}
851
Casey Dahlin451ff582015-10-19 18:12:18 -0700852status_t Parcel::writeString16Vector(const std::vector<String16>& val)
853{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800854 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700855}
856
Casey Dahlinb9872622015-11-25 15:09:45 -0800857status_t Parcel::writeString16Vector(
858 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
859{
860 return writeNullableTypedVector(val, &Parcel::writeString16);
861}
862
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800863status_t Parcel::writeUtf8VectorAsUtf16Vector(
864 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
865 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
866}
867
868status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
869 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
870}
871
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700872status_t Parcel::writeInt32(int32_t val)
873{
Andreas Huber84a6d042009-08-17 13:33:27 -0700874 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700875}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800876
877status_t Parcel::writeUint32(uint32_t val)
878{
879 return writeAligned(val);
880}
881
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700882status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700883 if (len > INT32_MAX) {
884 // don't accept size_t values which may have come from an
885 // inadvertent conversion from a negative int.
886 return BAD_VALUE;
887 }
888
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700889 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700890 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700891 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700892 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700893 if (ret == NO_ERROR) {
894 ret = write(val, len * sizeof(*val));
895 }
896 return ret;
897}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700898status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700899 if (len > INT32_MAX) {
900 // don't accept size_t values which may have come from an
901 // inadvertent conversion from a negative int.
902 return BAD_VALUE;
903 }
904
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700905 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700906 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700907 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700908 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700909 if (ret == NO_ERROR) {
910 ret = write(val, len * sizeof(*val));
911 }
912 return ret;
913}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700914
Casey Dahlind6848f52015-10-15 15:44:59 -0700915status_t Parcel::writeBool(bool val)
916{
917 return writeInt32(int32_t(val));
918}
919
920status_t Parcel::writeChar(char16_t val)
921{
922 return writeInt32(int32_t(val));
923}
924
925status_t Parcel::writeByte(int8_t val)
926{
927 return writeInt32(int32_t(val));
928}
929
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700930status_t Parcel::writeInt64(int64_t val)
931{
Andreas Huber84a6d042009-08-17 13:33:27 -0700932 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700933}
934
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700935status_t Parcel::writeUint64(uint64_t val)
936{
937 return writeAligned(val);
938}
939
Serban Constantinescuf683e012013-11-05 16:53:55 +0000940status_t Parcel::writePointer(uintptr_t val)
941{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800942 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000943}
944
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700945status_t Parcel::writeFloat(float val)
946{
Andreas Huber84a6d042009-08-17 13:33:27 -0700947 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700948}
949
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800950#if defined(__mips__) && defined(__mips_hard_float)
951
952status_t Parcel::writeDouble(double val)
953{
954 union {
955 double d;
956 unsigned long long ll;
957 } u;
958 u.d = val;
959 return writeAligned(u.ll);
960}
961
962#else
963
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700964status_t Parcel::writeDouble(double val)
965{
Andreas Huber84a6d042009-08-17 13:33:27 -0700966 return writeAligned(val);
967}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700968
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800969#endif
970
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700971status_t Parcel::writeCString(const char* str)
972{
973 return write(str, strlen(str)+1);
974}
975
976status_t Parcel::writeString8(const String8& str)
977{
978 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100979 // only write string if its length is more than zero characters,
980 // as readString8 will only read if the length field is non-zero.
981 // this is slightly different from how writeString16 works.
982 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700983 err = write(str.string(), str.bytes()+1);
984 }
985 return err;
986}
987
Casey Dahlinb9872622015-11-25 15:09:45 -0800988status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
989{
990 if (!str) {
991 return writeInt32(-1);
992 }
993
994 return writeString16(*str);
995}
996
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700997status_t Parcel::writeString16(const String16& str)
998{
999 return writeString16(str.string(), str.size());
1000}
1001
1002status_t Parcel::writeString16(const char16_t* str, size_t len)
1003{
Yi Kong91635562018-06-07 14:38:36 -07001004 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001005
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001006 status_t err = writeInt32(len);
1007 if (err == NO_ERROR) {
1008 len *= sizeof(char16_t);
1009 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1010 if (data) {
1011 memcpy(data, str, len);
1012 *reinterpret_cast<char16_t*>(data+len) = 0;
1013 return NO_ERROR;
1014 }
1015 err = mError;
1016 }
1017 return err;
1018}
1019
1020status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1021{
Steven Morelanda86a3562019-08-01 23:28:34 +00001022 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001023}
1024
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001025status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1026{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001027 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001028}
1029
Casey Dahlinb9872622015-11-25 15:09:45 -08001030status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1031{
1032 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1033}
1034
1035status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001036 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001037}
1038
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001039status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001040 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001041}
1042
Casey Dahlinb9872622015-11-25 15:09:45 -08001043status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1044 if (!parcelable) {
1045 return writeInt32(0);
1046 }
1047
1048 return writeParcelable(*parcelable);
1049}
1050
Christopher Wiley97f048d2015-11-19 06:49:05 -08001051status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1052 status_t status = writeInt32(1); // parcelable is not null.
1053 if (status != OK) {
1054 return status;
1055 }
1056 return parcelable.writeToParcel(this);
1057}
1058
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001059status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001060{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001061 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001062 return BAD_TYPE;
1063
1064 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001065 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001066 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001068 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001069 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001071 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1072 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073
1074 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001075 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076 return err;
1077 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001078 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001079 return err;
1080}
1081
Jeff Brown93ff1f92011-11-04 19:01:44 -07001082status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001083{
1084 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001085 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001086 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001087 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001088 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001089 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001090 return writeObject(obj, true);
1091}
1092
1093status_t Parcel::writeDupFileDescriptor(int fd)
1094{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001095 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001096 if (dupFd < 0) {
1097 return -errno;
1098 }
1099 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001100 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001101 close(dupFd);
1102 }
1103 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001104}
1105
Dianne Hackborn1941a402016-08-29 12:30:43 -07001106status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1107{
1108 writeInt32(0);
1109 return writeFileDescriptor(fd, takeOwnership);
1110}
1111
Ryo Hashimotobf551892018-05-31 16:58:35 +09001112status_t Parcel::writeDupParcelFileDescriptor(int fd)
1113{
1114 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1115 if (dupFd < 0) {
1116 return -errno;
1117 }
1118 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1119 if (err != OK) {
1120 close(dupFd);
1121 }
1122 return err;
1123}
1124
Christopher Wiley2cf19952016-04-11 11:09:37 -07001125status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001126 return writeDupFileDescriptor(fd.get());
1127}
1128
Christopher Wiley2cf19952016-04-11 11:09:37 -07001129status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001130 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1131}
1132
Christopher Wiley2cf19952016-04-11 11:09:37 -07001133status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001134 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1135}
1136
Jeff Brown13b16042014-11-11 16:44:25 -08001137status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001138{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001139 if (len > INT32_MAX) {
1140 // don't accept size_t values which may have come from an
1141 // inadvertent conversion from a negative int.
1142 return BAD_VALUE;
1143 }
1144
Jeff Brown13b16042014-11-11 16:44:25 -08001145 status_t status;
1146 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001147 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001148 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001149 if (status) return status;
1150
1151 void* ptr = writeInplace(len);
1152 if (!ptr) return NO_MEMORY;
1153
Jeff Brown13b16042014-11-11 16:44:25 -08001154 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001155 return NO_ERROR;
1156 }
1157
Steve Block6807e592011-10-20 11:56:00 +01001158 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001159 int fd = ashmem_create_region("Parcel Blob", len);
1160 if (fd < 0) return NO_MEMORY;
1161
1162 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1163 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001164 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001165 } else {
Yi Kong91635562018-06-07 14:38:36 -07001166 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001167 if (ptr == MAP_FAILED) {
1168 status = -errno;
1169 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001170 if (!mutableCopy) {
1171 result = ashmem_set_prot_region(fd, PROT_READ);
1172 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001173 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001174 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001175 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001176 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001177 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001178 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001179 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001180 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001181 return NO_ERROR;
1182 }
1183 }
1184 }
1185 }
1186 ::munmap(ptr, len);
1187 }
1188 ::close(fd);
1189 return status;
1190}
1191
Jeff Brown13b16042014-11-11 16:44:25 -08001192status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1193{
1194 // Must match up with what's done in writeBlob.
1195 if (!mAllowFds) return FDS_NOT_ALLOWED;
1196 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1197 if (status) return status;
1198 return writeDupFileDescriptor(fd);
1199}
1200
Mathias Agopiane1424282013-07-29 21:24:40 -07001201status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001202{
1203 status_t err;
1204
1205 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001206 const size_t len = val.getFlattenedSize();
1207 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001208
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001209 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001210 // don't accept size_t values which may have come from an
1211 // inadvertent conversion from a negative int.
1212 return BAD_VALUE;
1213 }
1214
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001215 err = this->writeInt32(len);
1216 if (err) return err;
1217
1218 err = this->writeInt32(fd_count);
1219 if (err) return err;
1220
1221 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001222 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001223 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001224 return BAD_VALUE;
1225
Yi Kong91635562018-06-07 14:38:36 -07001226 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001227 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001228 fds = new (std::nothrow) int[fd_count];
1229 if (fds == nullptr) {
1230 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1231 return BAD_VALUE;
1232 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001233 }
1234
1235 err = val.flatten(buf, len, fds, fd_count);
1236 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1237 err = this->writeDupFileDescriptor( fds[i] );
1238 }
1239
1240 if (fd_count) {
1241 delete [] fds;
1242 }
1243
1244 return err;
1245}
1246
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001247status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1248{
1249 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1250 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1251 if (enoughData && enoughObjects) {
1252restart_write:
1253 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001254
Christopher Tate98e67d32015-06-03 18:44:15 -07001255 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001256 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001257 if (!mAllowFds) {
1258 // fail before modifying our object index
1259 return FDS_NOT_ALLOWED;
1260 }
1261 mHasFds = mFdsKnown = true;
1262 }
1263
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001264 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001265 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001266 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001267 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001268 mObjectsSize++;
1269 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001270
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001271 return finishWrite(sizeof(flat_binder_object));
1272 }
1273
1274 if (!enoughData) {
1275 const status_t err = growData(sizeof(val));
1276 if (err != NO_ERROR) return err;
1277 }
1278 if (!enoughObjects) {
1279 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001280 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001281 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001282 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001283 mObjects = objects;
1284 mObjectsCapacity = newSize;
1285 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001286
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001287 goto restart_write;
1288}
1289
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001290status_t Parcel::writeNoException()
1291{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001292 binder::Status status;
1293 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001294}
1295
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001296status_t Parcel::validateReadData(size_t upperBound) const
1297{
1298 // Don't allow non-object reads on object data
1299 if (mObjectsSorted || mObjectsSize <= 1) {
1300data_sorted:
1301 // Expect to check only against the next object
1302 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1303 // For some reason the current read position is greater than the next object
1304 // hint. Iterate until we find the right object
1305 size_t nextObject = mNextObjectHint;
1306 do {
1307 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1308 // Requested info overlaps with an object
1309 ALOGE("Attempt to read from protected data in Parcel %p", this);
1310 return PERMISSION_DENIED;
1311 }
1312 nextObject++;
1313 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1314 mNextObjectHint = nextObject;
1315 }
1316 return NO_ERROR;
1317 }
1318 // Quickly determine if mObjects is sorted.
1319 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1320 binder_size_t* prevObj = currObj;
1321 while (currObj > mObjects) {
1322 prevObj--;
1323 if(*prevObj > *currObj) {
1324 goto data_unsorted;
1325 }
1326 currObj--;
1327 }
1328 mObjectsSorted = true;
1329 goto data_sorted;
1330
1331data_unsorted:
1332 // Insertion Sort mObjects
1333 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1334 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1335 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1336 binder_size_t temp = *iter0;
1337 binder_size_t* iter1 = iter0 - 1;
1338 while (iter1 >= mObjects && *iter1 > temp) {
1339 *(iter1 + 1) = *iter1;
1340 iter1--;
1341 }
1342 *(iter1 + 1) = temp;
1343 }
1344 mNextObjectHint = 0;
1345 mObjectsSorted = true;
1346 goto data_sorted;
1347}
1348
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001349status_t Parcel::read(void* outData, size_t len) const
1350{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001351 if (len > INT32_MAX) {
1352 // don't accept size_t values which may have come from an
1353 // inadvertent conversion from a negative int.
1354 return BAD_VALUE;
1355 }
1356
1357 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1358 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001359 if (mObjectsSize > 0) {
1360 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001361 if(err != NO_ERROR) {
1362 // Still increment the data position by the expected length
1363 mDataPos += pad_size(len);
1364 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1365 return err;
1366 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001367 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001368 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001369 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001370 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001371 return NO_ERROR;
1372 }
1373 return NOT_ENOUGH_DATA;
1374}
1375
1376const void* Parcel::readInplace(size_t len) const
1377{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001378 if (len > INT32_MAX) {
1379 // don't accept size_t values which may have come from an
1380 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001381 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001382 }
1383
1384 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1385 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001386 if (mObjectsSize > 0) {
1387 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001388 if(err != NO_ERROR) {
1389 // Still increment the data position by the expected length
1390 mDataPos += pad_size(len);
1391 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001392 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001393 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001394 }
1395
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001396 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001397 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001398 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001399 return data;
1400 }
Yi Kong91635562018-06-07 14:38:36 -07001401 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001402}
1403
Andreas Huber84a6d042009-08-17 13:33:27 -07001404template<class T>
1405status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001406 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001407
1408 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001409 if (mObjectsSize > 0) {
1410 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001411 if(err != NO_ERROR) {
1412 // Still increment the data position by the expected length
1413 mDataPos += sizeof(T);
1414 return err;
1415 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001416 }
1417
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001418 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001419 mDataPos += sizeof(T);
1420 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001421 return NO_ERROR;
1422 } else {
1423 return NOT_ENOUGH_DATA;
1424 }
1425}
1426
Andreas Huber84a6d042009-08-17 13:33:27 -07001427template<class T>
1428T Parcel::readAligned() const {
1429 T result;
1430 if (readAligned(&result) != NO_ERROR) {
1431 result = 0;
1432 }
1433
1434 return result;
1435}
1436
1437template<class T>
1438status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001439 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001440
1441 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1442restart_write:
1443 *reinterpret_cast<T*>(mData+mDataPos) = val;
1444 return finishWrite(sizeof(val));
1445 }
1446
1447 status_t err = growData(sizeof(val));
1448 if (err == NO_ERROR) goto restart_write;
1449 return err;
1450}
1451
Casey Dahlin185d3442016-02-09 11:08:35 -08001452status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001453 size_t size;
1454 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1455 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001456}
1457
1458status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001459 size_t size;
1460 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1461 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001462}
1463
1464status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001465 size_t size;
1466 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001467 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001468 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001469 // This occurs when writing a null byte vector.
1470 return OK;
1471 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001472 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001473}
1474
1475status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001476 size_t size;
1477 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001478 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001479 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001480 // This occurs when writing a null byte vector.
1481 return OK;
1482 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001483 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001484}
1485
Casey Dahlinb9872622015-11-25 15:09:45 -08001486status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1487 return readNullableTypedVector(val, &Parcel::readInt32);
1488}
1489
Casey Dahlin451ff582015-10-19 18:12:18 -07001490status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001491 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001492}
1493
Casey Dahlinb9872622015-11-25 15:09:45 -08001494status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1495 return readNullableTypedVector(val, &Parcel::readInt64);
1496}
1497
Casey Dahlin451ff582015-10-19 18:12:18 -07001498status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001499 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001500}
1501
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001502status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1503 return readNullableTypedVector(val, &Parcel::readUint64);
1504}
1505
1506status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1507 return readTypedVector(val, &Parcel::readUint64);
1508}
1509
Casey Dahlinb9872622015-11-25 15:09:45 -08001510status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1511 return readNullableTypedVector(val, &Parcel::readFloat);
1512}
1513
Casey Dahlin451ff582015-10-19 18:12:18 -07001514status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001515 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001516}
1517
Casey Dahlinb9872622015-11-25 15:09:45 -08001518status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1519 return readNullableTypedVector(val, &Parcel::readDouble);
1520}
1521
Casey Dahlin451ff582015-10-19 18:12:18 -07001522status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001523 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001524}
1525
Casey Dahlinb9872622015-11-25 15:09:45 -08001526status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1527 const int32_t start = dataPosition();
1528 int32_t size;
1529 status_t status = readInt32(&size);
1530 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001531
Casey Dahlinb9872622015-11-25 15:09:45 -08001532 if (status != OK || size < 0) {
1533 return status;
1534 }
1535
1536 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001537 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001538
1539 status = readBoolVector(val->get());
1540
1541 if (status != OK) {
1542 val->reset();
1543 }
1544
1545 return status;
1546}
1547
1548status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001549 int32_t size;
1550 status_t status = readInt32(&size);
1551
1552 if (status != OK) {
1553 return status;
1554 }
1555
1556 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001557 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001558 }
1559
1560 val->resize(size);
1561
1562 /* C++ bool handling means a vector of bools isn't necessarily addressable
1563 * (we might use individual bits)
1564 */
Christopher Wiley97887982015-10-27 16:33:47 -07001565 bool data;
1566 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001567 status = readBool(&data);
1568 (*val)[i] = data;
1569
1570 if (status != OK) {
1571 return status;
1572 }
1573 }
1574
1575 return OK;
1576}
1577
Casey Dahlinb9872622015-11-25 15:09:45 -08001578status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1579 return readNullableTypedVector(val, &Parcel::readChar);
1580}
1581
Casey Dahlin451ff582015-10-19 18:12:18 -07001582status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001583 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001584}
1585
Casey Dahlinb9872622015-11-25 15:09:45 -08001586status_t Parcel::readString16Vector(
1587 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1588 return readNullableTypedVector(val, &Parcel::readString16);
1589}
1590
Casey Dahlin451ff582015-10-19 18:12:18 -07001591status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001592 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001593}
1594
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001595status_t Parcel::readUtf8VectorFromUtf16Vector(
1596 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1597 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1598}
1599
1600status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1601 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1602}
Casey Dahlin451ff582015-10-19 18:12:18 -07001603
Andreas Huber84a6d042009-08-17 13:33:27 -07001604status_t Parcel::readInt32(int32_t *pArg) const
1605{
1606 return readAligned(pArg);
1607}
1608
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001609int32_t Parcel::readInt32() const
1610{
Andreas Huber84a6d042009-08-17 13:33:27 -07001611 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001612}
1613
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001614status_t Parcel::readUint32(uint32_t *pArg) const
1615{
1616 return readAligned(pArg);
1617}
1618
1619uint32_t Parcel::readUint32() const
1620{
1621 return readAligned<uint32_t>();
1622}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001623
1624status_t Parcel::readInt64(int64_t *pArg) const
1625{
Andreas Huber84a6d042009-08-17 13:33:27 -07001626 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001627}
1628
1629
1630int64_t Parcel::readInt64() const
1631{
Andreas Huber84a6d042009-08-17 13:33:27 -07001632 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001633}
1634
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001635status_t Parcel::readUint64(uint64_t *pArg) const
1636{
1637 return readAligned(pArg);
1638}
1639
1640uint64_t Parcel::readUint64() const
1641{
1642 return readAligned<uint64_t>();
1643}
1644
Serban Constantinescuf683e012013-11-05 16:53:55 +00001645status_t Parcel::readPointer(uintptr_t *pArg) const
1646{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001647 status_t ret;
1648 binder_uintptr_t ptr;
1649 ret = readAligned(&ptr);
1650 if (!ret)
1651 *pArg = ptr;
1652 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001653}
1654
1655uintptr_t Parcel::readPointer() const
1656{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001657 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001658}
1659
1660
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001661status_t Parcel::readFloat(float *pArg) const
1662{
Andreas Huber84a6d042009-08-17 13:33:27 -07001663 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001664}
1665
1666
1667float Parcel::readFloat() const
1668{
Andreas Huber84a6d042009-08-17 13:33:27 -07001669 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001670}
1671
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001672#if defined(__mips__) && defined(__mips_hard_float)
1673
1674status_t Parcel::readDouble(double *pArg) const
1675{
1676 union {
1677 double d;
1678 unsigned long long ll;
1679 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001680 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001681 status_t status;
1682 status = readAligned(&u.ll);
1683 *pArg = u.d;
1684 return status;
1685}
1686
1687double Parcel::readDouble() const
1688{
1689 union {
1690 double d;
1691 unsigned long long ll;
1692 } u;
1693 u.ll = readAligned<unsigned long long>();
1694 return u.d;
1695}
1696
1697#else
1698
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001699status_t Parcel::readDouble(double *pArg) const
1700{
Andreas Huber84a6d042009-08-17 13:33:27 -07001701 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001702}
1703
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001704double Parcel::readDouble() const
1705{
Andreas Huber84a6d042009-08-17 13:33:27 -07001706 return readAligned<double>();
1707}
1708
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001709#endif
1710
Andreas Huber84a6d042009-08-17 13:33:27 -07001711status_t Parcel::readIntPtr(intptr_t *pArg) const
1712{
1713 return readAligned(pArg);
1714}
1715
1716
1717intptr_t Parcel::readIntPtr() const
1718{
1719 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001720}
1721
Casey Dahlind6848f52015-10-15 15:44:59 -07001722status_t Parcel::readBool(bool *pArg) const
1723{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001724 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001725 status_t ret = readInt32(&tmp);
1726 *pArg = (tmp != 0);
1727 return ret;
1728}
1729
1730bool Parcel::readBool() const
1731{
1732 return readInt32() != 0;
1733}
1734
1735status_t Parcel::readChar(char16_t *pArg) const
1736{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001737 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001738 status_t ret = readInt32(&tmp);
1739 *pArg = char16_t(tmp);
1740 return ret;
1741}
1742
1743char16_t Parcel::readChar() const
1744{
1745 return char16_t(readInt32());
1746}
1747
1748status_t Parcel::readByte(int8_t *pArg) const
1749{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001750 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001751 status_t ret = readInt32(&tmp);
1752 *pArg = int8_t(tmp);
1753 return ret;
1754}
1755
1756int8_t Parcel::readByte() const
1757{
1758 return int8_t(readInt32());
1759}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001760
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001761status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1762 size_t utf16Size = 0;
1763 const char16_t* src = readString16Inplace(&utf16Size);
1764 if (!src) {
1765 return UNEXPECTED_NULL;
1766 }
1767
1768 // Save ourselves the trouble, we're done.
1769 if (utf16Size == 0u) {
1770 str->clear();
1771 return NO_ERROR;
1772 }
1773
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001774 // Allow for closing '\0'
1775 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1776 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001777 return BAD_VALUE;
1778 }
1779 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001780 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001781 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001782 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1783 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001784 return NO_ERROR;
1785}
1786
1787status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1788 const int32_t start = dataPosition();
1789 int32_t size;
1790 status_t status = readInt32(&size);
1791 str->reset();
1792
1793 if (status != OK || size < 0) {
1794 return status;
1795 }
1796
1797 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001798 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001799 return readUtf8FromUtf16(str->get());
1800}
1801
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001802const char* Parcel::readCString() const
1803{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001804 if (mDataPos < mDataSize) {
1805 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001806 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1807 // is the string's trailing NUL within the parcel's valid bounds?
1808 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1809 if (eos) {
1810 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001811 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001812 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001813 return str;
1814 }
1815 }
Yi Kong91635562018-06-07 14:38:36 -07001816 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001817}
1818
1819String8 Parcel::readString8() const
1820{
Roshan Pius87b64d22016-07-18 12:51:02 -07001821 String8 retString;
1822 status_t status = readString8(&retString);
1823 if (status != OK) {
1824 // We don't care about errors here, so just return an empty string.
1825 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001826 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001827 return retString;
1828}
1829
1830status_t Parcel::readString8(String8* pArg) const
1831{
1832 int32_t size;
1833 status_t status = readInt32(&size);
1834 if (status != OK) {
1835 return status;
1836 }
1837 // watch for potential int overflow from size+1
1838 if (size < 0 || size >= INT32_MAX) {
1839 return BAD_VALUE;
1840 }
1841 // |writeString8| writes nothing for empty string.
1842 if (size == 0) {
1843 *pArg = String8();
1844 return OK;
1845 }
1846 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07001847 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07001848 return BAD_VALUE;
1849 }
1850 pArg->setTo(str, size);
1851 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001852}
1853
1854String16 Parcel::readString16() const
1855{
1856 size_t len;
1857 const char16_t* str = readString16Inplace(&len);
1858 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001859 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001860 return String16();
1861}
1862
Casey Dahlinb9872622015-11-25 15:09:45 -08001863status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1864{
1865 const int32_t start = dataPosition();
1866 int32_t size;
1867 status_t status = readInt32(&size);
1868 pArg->reset();
1869
1870 if (status != OK || size < 0) {
1871 return status;
1872 }
1873
1874 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001875 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001876
1877 status = readString16(pArg->get());
1878
1879 if (status != OK) {
1880 pArg->reset();
1881 }
1882
1883 return status;
1884}
1885
Casey Dahlin451ff582015-10-19 18:12:18 -07001886status_t Parcel::readString16(String16* pArg) const
1887{
1888 size_t len;
1889 const char16_t* str = readString16Inplace(&len);
1890 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001891 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001892 return 0;
1893 } else {
1894 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001895 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001896 }
1897}
1898
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001899const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1900{
1901 int32_t size = readInt32();
1902 // watch for potential int overflow from size+1
1903 if (size >= 0 && size < INT32_MAX) {
1904 *outLen = size;
1905 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001906 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001907 return str;
1908 }
1909 }
1910 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001911 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001912}
1913
Casey Dahlinf0c13772015-10-27 18:33:56 -07001914status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1915{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001916 status_t status = readNullableStrongBinder(val);
1917 if (status == OK && !val->get()) {
1918 status = UNEXPECTED_NULL;
1919 }
1920 return status;
1921}
1922
1923status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1924{
Steven Morelanda86a3562019-08-01 23:28:34 +00001925 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001926}
1927
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001928sp<IBinder> Parcel::readStrongBinder() const
1929{
1930 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001931 // Note that a lot of code in Android reads binders by hand with this
1932 // method, and that code has historically been ok with getting nullptr
1933 // back (while ignoring error codes).
1934 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001935 return val;
1936}
1937
Christopher Wiley97f048d2015-11-19 06:49:05 -08001938status_t Parcel::readParcelable(Parcelable* parcelable) const {
1939 int32_t have_parcelable = 0;
1940 status_t status = readInt32(&have_parcelable);
1941 if (status != OK) {
1942 return status;
1943 }
1944 if (!have_parcelable) {
1945 return UNEXPECTED_NULL;
1946 }
1947 return parcelable->readFromParcel(this);
1948}
1949
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001950int32_t Parcel::readExceptionCode() const
1951{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001952 binder::Status status;
1953 status.readFromParcel(*this);
1954 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001955}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001956
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001957native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001958{
1959 int numFds, numInts;
1960 status_t err;
1961 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001962 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001963 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001964 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001965
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001966 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001967 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001968 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001969 }
1970
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001971 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001972 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001973 if (h->data[i] < 0) {
1974 for (int j = 0; j < i; j++) {
1975 close(h->data[j]);
1976 }
1977 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001978 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001979 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001980 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001981 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001982 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001983 native_handle_close(h);
1984 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001985 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001986 }
1987 return h;
1988}
1989
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001990int Parcel::readFileDescriptor() const
1991{
1992 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001993
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001994 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001995 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001996 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001997
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001998 return BAD_TYPE;
1999}
2000
Dianne Hackborn1941a402016-08-29 12:30:43 -07002001int Parcel::readParcelFileDescriptor() const
2002{
2003 int32_t hasComm = readInt32();
2004 int fd = readFileDescriptor();
2005 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002006 // detach (owned by the binder driver)
2007 int comm = readFileDescriptor();
2008
2009 // warning: this must be kept in sync with:
2010 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2011 enum ParcelFileDescriptorStatus {
2012 DETACHED = 2,
2013 };
2014
2015#if BYTE_ORDER == BIG_ENDIAN
2016 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2017#endif
2018#if BYTE_ORDER == LITTLE_ENDIAN
2019 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2020#endif
2021
2022 ssize_t written = TEMP_FAILURE_RETRY(
2023 ::write(comm, &message, sizeof(message)));
2024
2025 if (written == -1 || written != sizeof(message)) {
2026 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2027 written, strerror(errno));
2028 return BAD_TYPE;
2029 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002030 }
2031 return fd;
2032}
2033
Christopher Wiley2cf19952016-04-11 11:09:37 -07002034status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002035{
2036 int got = readFileDescriptor();
2037
2038 if (got == BAD_TYPE) {
2039 return BAD_TYPE;
2040 }
2041
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002042 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002043
2044 if (val->get() < 0) {
2045 return BAD_VALUE;
2046 }
2047
2048 return OK;
2049}
2050
Ryo Hashimotobf551892018-05-31 16:58:35 +09002051status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2052{
2053 int got = readParcelFileDescriptor();
2054
2055 if (got == BAD_TYPE) {
2056 return BAD_TYPE;
2057 }
2058
2059 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2060
2061 if (val->get() < 0) {
2062 return BAD_VALUE;
2063 }
2064
2065 return OK;
2066}
Casey Dahlin06673e32015-11-23 13:24:23 -08002067
Christopher Wiley2cf19952016-04-11 11:09:37 -07002068status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002069 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2070}
2071
Christopher Wiley2cf19952016-04-11 11:09:37 -07002072status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002073 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2074}
2075
Jeff Brown5707dbf2011-09-23 21:17:56 -07002076status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2077{
Jeff Brown13b16042014-11-11 16:44:25 -08002078 int32_t blobType;
2079 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002080 if (status) return status;
2081
Jeff Brown13b16042014-11-11 16:44:25 -08002082 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002083 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002084 const void* ptr = readInplace(len);
2085 if (!ptr) return BAD_VALUE;
2086
Jeff Brown13b16042014-11-11 16:44:25 -08002087 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002088 return NO_ERROR;
2089 }
2090
Steve Block6807e592011-10-20 11:56:00 +01002091 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002092 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002093 int fd = readFileDescriptor();
2094 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2095
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002096 if (!ashmem_valid(fd)) {
2097 ALOGE("invalid fd");
2098 return BAD_VALUE;
2099 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002100 int size = ashmem_get_size_region(fd);
2101 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002102 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002103 return BAD_VALUE;
2104 }
Yi Kong91635562018-06-07 14:38:36 -07002105 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002106 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002107 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002108
Jeff Brown13b16042014-11-11 16:44:25 -08002109 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002110 return NO_ERROR;
2111}
2112
Mathias Agopiane1424282013-07-29 21:24:40 -07002113status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002114{
2115 // size
2116 const size_t len = this->readInt32();
2117 const size_t fd_count = this->readInt32();
2118
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002119 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002120 // don't accept size_t values which may have come from an
2121 // inadvertent conversion from a negative int.
2122 return BAD_VALUE;
2123 }
2124
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002125 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002126 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002127 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002128 return BAD_VALUE;
2129
Yi Kong91635562018-06-07 14:38:36 -07002130 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002131 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002132 fds = new (std::nothrow) int[fd_count];
2133 if (fds == nullptr) {
2134 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2135 return BAD_VALUE;
2136 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002137 }
2138
2139 status_t err = NO_ERROR;
2140 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002141 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002142 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002143 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002144 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 -07002145 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2146 // Close all the file descriptors that were dup-ed.
2147 for (size_t j=0; j<i ;j++) {
2148 close(fds[j]);
2149 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002150 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002151 }
2152
2153 if (err == NO_ERROR) {
2154 err = val.unflatten(buf, len, fds, fd_count);
2155 }
2156
2157 if (fd_count) {
2158 delete [] fds;
2159 }
2160
2161 return err;
2162}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002163const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2164{
2165 const size_t DPOS = mDataPos;
2166 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2167 const flat_binder_object* obj
2168 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2169 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002170 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002171 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002172 // the object list, so we don't want to check for it when
2173 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002174 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002175 return obj;
2176 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002177
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002178 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002179 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002180 const size_t N = mObjectsSize;
2181 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002182
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002183 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002184 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002185 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002186
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002187 // Start at the current hint position, looking for an object at
2188 // the current data position.
2189 if (opos < N) {
2190 while (opos < (N-1) && OBJS[opos] < DPOS) {
2191 opos++;
2192 }
2193 } else {
2194 opos = N-1;
2195 }
2196 if (OBJS[opos] == DPOS) {
2197 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002198 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002199 this, DPOS, opos);
2200 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002201 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002202 return obj;
2203 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002204
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002205 // Look backwards for it...
2206 while (opos > 0 && OBJS[opos] > DPOS) {
2207 opos--;
2208 }
2209 if (OBJS[opos] == DPOS) {
2210 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002211 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002212 this, DPOS, opos);
2213 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002214 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002215 return obj;
2216 }
2217 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002218 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 -07002219 this, DPOS);
2220 }
Yi Kong91635562018-06-07 14:38:36 -07002221 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002222}
2223
2224void Parcel::closeFileDescriptors()
2225{
2226 size_t i = mObjectsSize;
2227 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002228 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002229 }
2230 while (i > 0) {
2231 i--;
2232 const flat_binder_object* flat
2233 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002234 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002235 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002236 close(flat->handle);
2237 }
2238 }
2239}
2240
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002241uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002242{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002243 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002244}
2245
2246size_t Parcel::ipcDataSize() const
2247{
2248 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2249}
2250
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002251uintptr_t Parcel::ipcObjects() 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>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002254}
2255
2256size_t Parcel::ipcObjectsCount() const
2257{
2258 return mObjectsSize;
2259}
2260
2261void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002262 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002263{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002264 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002265 freeDataNoInit();
2266 mError = NO_ERROR;
2267 mData = const_cast<uint8_t*>(data);
2268 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002269 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002270 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002271 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002272 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002273 mObjectsSize = mObjectsCapacity = objectsCount;
2274 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002275 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002276 mOwner = relFunc;
2277 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002278 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002279 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002280 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002281 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002282 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002283 mObjectsSize = 0;
2284 break;
2285 }
2286 minOffset = offset + sizeof(flat_binder_object);
2287 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002288 scanForFds();
2289}
2290
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002291void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002292{
2293 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002294
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002295 if (errorCheck() != NO_ERROR) {
2296 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002297 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002298 } else if (dataSize() > 0) {
2299 const uint8_t* DATA = data();
2300 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002301 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002302 const size_t N = objectsCount();
2303 for (size_t i=0; i<N; i++) {
2304 const flat_binder_object* flat
2305 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2306 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002307 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002308 << " = " << flat->binder;
2309 }
2310 } else {
2311 to << "NULL";
2312 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002313
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002314 to << ")";
2315}
2316
2317void Parcel::releaseObjects()
2318{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002319 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002320 if (i == 0) {
2321 return;
2322 }
2323 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002324 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002325 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002326 while (i > 0) {
2327 i--;
2328 const flat_binder_object* flat
2329 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002330 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002331 }
2332}
2333
2334void Parcel::acquireObjects()
2335{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002336 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002337 if (i == 0) {
2338 return;
2339 }
2340 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002341 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002342 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002343 while (i > 0) {
2344 i--;
2345 const flat_binder_object* flat
2346 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002347 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002348 }
2349}
2350
2351void Parcel::freeData()
2352{
2353 freeDataNoInit();
2354 initState();
2355}
2356
2357void Parcel::freeDataNoInit()
2358{
2359 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002360 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002361 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002362 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2363 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002364 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002365 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002366 if (mData) {
2367 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002368 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002369 if (mDataCapacity <= gParcelGlobalAllocSize) {
2370 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2371 } else {
2372 gParcelGlobalAllocSize = 0;
2373 }
2374 if (gParcelGlobalAllocCount > 0) {
2375 gParcelGlobalAllocCount--;
2376 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002377 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002378 free(mData);
2379 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002380 if (mObjects) free(mObjects);
2381 }
2382}
2383
2384status_t Parcel::growData(size_t len)
2385{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002386 if (len > INT32_MAX) {
2387 // don't accept size_t values which may have come from an
2388 // inadvertent conversion from a negative int.
2389 return BAD_VALUE;
2390 }
2391
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002392 size_t newSize = ((mDataSize+len)*3)/2;
2393 return (newSize <= mDataSize)
2394 ? (status_t) NO_MEMORY
2395 : continueWrite(newSize);
2396}
2397
2398status_t Parcel::restartWrite(size_t desired)
2399{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002400 if (desired > INT32_MAX) {
2401 // don't accept size_t values which may have come from an
2402 // inadvertent conversion from a negative int.
2403 return BAD_VALUE;
2404 }
2405
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002406 if (mOwner) {
2407 freeData();
2408 return continueWrite(desired);
2409 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002410
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002411 uint8_t* data = (uint8_t*)realloc(mData, desired);
2412 if (!data && desired > mDataCapacity) {
2413 mError = NO_MEMORY;
2414 return NO_MEMORY;
2415 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002416
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002417 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002418
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002419 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002420 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002421 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002422 gParcelGlobalAllocSize += desired;
2423 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002424 if (!mData) {
2425 gParcelGlobalAllocCount++;
2426 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002427 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002428 mData = data;
2429 mDataCapacity = desired;
2430 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002431
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002432 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002433 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2434 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2435
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002436 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002437 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002438 mObjectsSize = mObjectsCapacity = 0;
2439 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002440 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002441 mHasFds = false;
2442 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002443 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002444
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002445 return NO_ERROR;
2446}
2447
2448status_t Parcel::continueWrite(size_t desired)
2449{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002450 if (desired > INT32_MAX) {
2451 // don't accept size_t values which may have come from an
2452 // inadvertent conversion from a negative int.
2453 return BAD_VALUE;
2454 }
2455
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002456 // If shrinking, first adjust for any objects that appear
2457 // after the new data size.
2458 size_t objectsSize = mObjectsSize;
2459 if (desired < mDataSize) {
2460 if (desired == 0) {
2461 objectsSize = 0;
2462 } else {
2463 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002464 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002465 break;
2466 objectsSize--;
2467 }
2468 }
2469 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002470
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002471 if (mOwner) {
2472 // If the size is going to zero, just release the owner's data.
2473 if (desired == 0) {
2474 freeData();
2475 return NO_ERROR;
2476 }
2477
2478 // If there is a different owner, we need to take
2479 // posession.
2480 uint8_t* data = (uint8_t*)malloc(desired);
2481 if (!data) {
2482 mError = NO_MEMORY;
2483 return NO_MEMORY;
2484 }
Yi Kong91635562018-06-07 14:38:36 -07002485 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002486
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002487 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002488 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002489 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002490 free(data);
2491
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002492 mError = NO_MEMORY;
2493 return NO_MEMORY;
2494 }
2495
2496 // Little hack to only acquire references on objects
2497 // we will be keeping.
2498 size_t oldObjectsSize = mObjectsSize;
2499 mObjectsSize = objectsSize;
2500 acquireObjects();
2501 mObjectsSize = oldObjectsSize;
2502 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002503
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002504 if (mData) {
2505 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2506 }
2507 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002508 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002509 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002510 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002511 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002512 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002514 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002515 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002516 gParcelGlobalAllocSize += desired;
2517 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002518 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002519
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002520 mData = data;
2521 mObjects = objects;
2522 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002523 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002524 mDataCapacity = desired;
2525 mObjectsSize = mObjectsCapacity = objectsSize;
2526 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002527 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002528
2529 } else if (mData) {
2530 if (objectsSize < mObjectsSize) {
2531 // Need to release refs on any objects we are dropping.
2532 const sp<ProcessState> proc(ProcessState::self());
2533 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2534 const flat_binder_object* flat
2535 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002536 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002537 // will need to rescan because we may have lopped off the only FDs
2538 mFdsKnown = false;
2539 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002540 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002541 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002542
2543 if (objectsSize == 0) {
2544 free(mObjects);
2545 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002546 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002547 } else {
2548 binder_size_t* objects =
2549 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2550 if (objects) {
2551 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002552 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002553 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002554 }
2555 mObjectsSize = objectsSize;
2556 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002557 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002558 }
2559
2560 // We own the data, so we can just do a realloc().
2561 if (desired > mDataCapacity) {
2562 uint8_t* data = (uint8_t*)realloc(mData, desired);
2563 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002564 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2565 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002566 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002567 gParcelGlobalAllocSize += desired;
2568 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002569 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002570 mData = data;
2571 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002572 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002573 mError = NO_MEMORY;
2574 return NO_MEMORY;
2575 }
2576 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002577 if (mDataSize > desired) {
2578 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002579 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002580 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002581 if (mDataPos > desired) {
2582 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002583 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002584 }
2585 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002586
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002587 } else {
2588 // This is the first data. Easy!
2589 uint8_t* data = (uint8_t*)malloc(desired);
2590 if (!data) {
2591 mError = NO_MEMORY;
2592 return NO_MEMORY;
2593 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002594
Yi Kong91635562018-06-07 14:38:36 -07002595 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002596 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002597 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002598 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002599
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002600 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002601 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002602 gParcelGlobalAllocSize += desired;
2603 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002604 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002605
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002606 mData = data;
2607 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002608 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2609 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002610 mDataCapacity = desired;
2611 }
2612
2613 return NO_ERROR;
2614}
2615
2616void Parcel::initState()
2617{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002618 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002619 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002620 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002621 mDataSize = 0;
2622 mDataCapacity = 0;
2623 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002624 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2625 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002626 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002627 mObjectsSize = 0;
2628 mObjectsCapacity = 0;
2629 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002630 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002631 mHasFds = false;
2632 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002633 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002634 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002635 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002636 mWorkSourceRequestHeaderPosition = 0;
2637 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002638
2639 // racing multiple init leads only to multiple identical write
2640 if (gMaxFds == 0) {
2641 struct rlimit result;
2642 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2643 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002644 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002645 } else {
2646 ALOGW("Unable to getrlimit: %s", strerror(errno));
2647 gMaxFds = 1024;
2648 }
2649 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002650}
2651
2652void Parcel::scanForFds() const
2653{
2654 bool hasFds = false;
2655 for (size_t i=0; i<mObjectsSize; i++) {
2656 const flat_binder_object* flat
2657 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002658 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002659 hasFds = true;
2660 break;
2661 }
2662 }
2663 mHasFds = hasFds;
2664 mFdsKnown = true;
2665}
2666
Dan Sandleraa5c2342015-04-10 10:08:45 -04002667size_t Parcel::getBlobAshmemSize() const
2668{
Adrian Roos6bb31142015-10-22 16:46:12 -07002669 // This used to return the size of all blobs that were written to ashmem, now we're returning
2670 // the ashmem currently referenced by this Parcel, which should be equivalent.
2671 // TODO: Remove method once ABI can be changed.
2672 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002673}
2674
Adrian Rooscbf37262015-10-22 16:12:53 -07002675size_t Parcel::getOpenAshmemSize() const
2676{
2677 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002678}
2679
2680// --- Parcel::Blob ---
2681
2682Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002683 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002684}
2685
2686Parcel::Blob::~Blob() {
2687 release();
2688}
2689
2690void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002691 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002692 ::munmap(mData, mSize);
2693 }
2694 clear();
2695}
2696
Jeff Brown13b16042014-11-11 16:44:25 -08002697void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2698 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002699 mData = data;
2700 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002701 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002702}
2703
2704void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002705 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002706 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002707 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002708 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002709}
2710
Steven Moreland61ff8492019-09-26 16:05:45 -07002711} // namespace android