blob: feeb819a8a8299e1fce49f398a7dac000bada635 [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>
Steven Morelandbf1915b2020-07-16 22:43:02 +000023#include <linux/sched.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080024#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080029#include <sys/stat.h>
30#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070031#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080032#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070033
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/Binder.h>
35#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080036#include <binder/IPCThreadState.h>
37#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070038#include <binder/ProcessState.h>
Steven Moreland6e5a7752019-08-05 20:30:14 -070039#include <binder/Stability.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080040#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041#include <binder/TextOutput.h>
42
Mark Salyzynabed7f72016-01-27 08:02:48 -080043#include <cutils/ashmem.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"
Steven Morelandf183fdd2020-10-27 00:12:12 +000052#include "Utils.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070054#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080055//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080056#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080057//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070058
59// ---------------------------------------------------------------------------
60
Nick Kralevichb6b14232015-04-02 09:36:02 -070061// This macro should never be used at runtime, as a too large value
62// of s could cause an integer overflow. Instead, you should always
63// use the wrapper function pad_size()
64#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
65
66static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070067 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070068 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070069 }
70 return PAD_SIZE_UNSAFE(s);
71}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070072
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070073// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060074#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070075
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070076namespace android {
77
Steven Moreland7b102262019-08-01 15:48:43 -070078// many things compile this into prebuilts on the stack
79static_assert(sizeof(Parcel) == 60 || sizeof(Parcel) == 120);
80
Jeff Sharkey8994c182020-09-11 12:07:10 -060081static std::atomic<size_t> gParcelGlobalAllocCount;
82static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -080083
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
Steven Morelandbf1915b2020-07-16 22:43:02 +0000191static constexpr inline int schedPolicyMask(int policy, int priority) {
192 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
193}
194
Steven Morelanda86a3562019-08-01 23:28:34 +0000195status_t Parcel::flattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700196{
197 flat_binder_object obj;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000198 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700199
Steven Morelandbf1915b2020-07-16 22:43:02 +0000200 int schedBits = 0;
201 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
202 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700203 }
204
Yi Kong91635562018-06-07 14:38:36 -0700205 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800206 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700207 if (!local) {
208 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700209 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000210 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700211 }
212 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700213 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800214 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700215 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800216 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700217 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000218 int policy = local->getMinSchedulerPolicy();
219 int priority = local->getMinSchedulerPriority();
220
221 if (policy != 0 || priority != 0) {
222 // override value, since it is set explicitly
223 schedBits = schedPolicyMask(policy, priority);
224 }
Steven Morelandf0212002018-12-26 13:59:23 -0800225 if (local->isRequestingSid()) {
226 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
227 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700228 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800229 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
230 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 }
232 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700233 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800234 obj.binder = 0;
235 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700236 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700237
Steven Morelandbf1915b2020-07-16 22:43:02 +0000238 obj.flags |= schedBits;
239
Steven Morelanda86a3562019-08-01 23:28:34 +0000240 return finishFlattenBinder(binder, obj);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700241}
242
Steven Morelanda86a3562019-08-01 23:28:34 +0000243status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700244{
Steven Morelanda86a3562019-08-01 23:28:34 +0000245 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700246
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700247 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700248 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000249 case BINDER_TYPE_BINDER: {
250 sp<IBinder> binder = reinterpret_cast<IBinder*>(flat->cookie);
251 return finishUnflattenBinder(binder, out);
252 }
253 case BINDER_TYPE_HANDLE: {
254 sp<IBinder> binder =
255 ProcessState::self()->getStrongProxyForHandle(flat->handle);
256 return finishUnflattenBinder(binder, out);
257 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700258 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700259 }
260 return BAD_TYPE;
261}
262
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700263// ---------------------------------------------------------------------------
264
265Parcel::Parcel()
266{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800267 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700268 initState();
269}
270
271Parcel::~Parcel()
272{
273 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800274 LOG_ALLOC("Parcel %p: destroyed", this);
275}
276
277size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600278 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800279}
280
281size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600282 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700283}
284
285const uint8_t* Parcel::data() const
286{
287 return mData;
288}
289
290size_t Parcel::dataSize() const
291{
292 return (mDataSize > mDataPos ? mDataSize : mDataPos);
293}
294
295size_t Parcel::dataAvail() const
296{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700297 size_t result = dataSize() - dataPosition();
298 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700299 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700300 }
301 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700302}
303
304size_t Parcel::dataPosition() const
305{
306 return mDataPos;
307}
308
309size_t Parcel::dataCapacity() const
310{
311 return mDataCapacity;
312}
313
314status_t Parcel::setDataSize(size_t size)
315{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700316 if (size > INT32_MAX) {
317 // don't accept size_t values which may have come from an
318 // inadvertent conversion from a negative int.
319 return BAD_VALUE;
320 }
321
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700322 status_t err;
323 err = continueWrite(size);
324 if (err == NO_ERROR) {
325 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700326 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700327 }
328 return err;
329}
330
331void Parcel::setDataPosition(size_t pos) const
332{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700333 if (pos > INT32_MAX) {
334 // don't accept size_t values which may have come from an
335 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700336 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700337 }
338
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700339 mDataPos = pos;
340 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800341 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700342}
343
344status_t Parcel::setDataCapacity(size_t size)
345{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700346 if (size > INT32_MAX) {
347 // don't accept size_t values which may have come from an
348 // inadvertent conversion from a negative int.
349 return BAD_VALUE;
350 }
351
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700352 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700353 return NO_ERROR;
354}
355
356status_t Parcel::setData(const uint8_t* buffer, size_t len)
357{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700358 if (len > INT32_MAX) {
359 // don't accept size_t values which may have come from an
360 // inadvertent conversion from a negative int.
361 return BAD_VALUE;
362 }
363
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700364 status_t err = restartWrite(len);
365 if (err == NO_ERROR) {
366 memcpy(const_cast<uint8_t*>(data()), buffer, len);
367 mDataSize = len;
368 mFdsKnown = false;
369 }
370 return err;
371}
372
Andreas Huber51faf462011-04-13 10:21:56 -0700373status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700374{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700375 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700376 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800377 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700378 size_t size = parcel->mObjectsSize;
379 int startPos = mDataPos;
380 int firstIndex = -1, lastIndex = -2;
381
382 if (len == 0) {
383 return NO_ERROR;
384 }
385
Nick Kralevichb6b14232015-04-02 09:36:02 -0700386 if (len > INT32_MAX) {
387 // don't accept size_t values which may have come from an
388 // inadvertent conversion from a negative int.
389 return BAD_VALUE;
390 }
391
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700392 // range checks against the source parcel size
393 if ((offset > parcel->mDataSize)
394 || (len > parcel->mDataSize)
395 || (offset + len > parcel->mDataSize)) {
396 return BAD_VALUE;
397 }
398
399 // Count objects in range
400 for (int i = 0; i < (int) size; i++) {
401 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700402 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700403 if (firstIndex == -1) {
404 firstIndex = i;
405 }
406 lastIndex = i;
407 }
408 }
409 int numObjects = lastIndex - firstIndex + 1;
410
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700411 if ((mDataSize+len) > mDataCapacity) {
412 // grow data
413 err = growData(len);
414 if (err != NO_ERROR) {
415 return err;
416 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700417 }
418
419 // append data
420 memcpy(mData + mDataPos, data + offset, len);
421 mDataPos += len;
422 mDataSize += len;
423
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400424 err = NO_ERROR;
425
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700426 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200427 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700428 // grow objects
429 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +0100430 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
431 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700432 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +0100433 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800434 binder_size_t *objects =
435 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700436 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437 return NO_MEMORY;
438 }
439 mObjects = objects;
440 mObjectsCapacity = newSize;
441 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700442
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700443 // append and acquire objects
444 int idx = mObjectsSize;
445 for (int i = firstIndex; i <= lastIndex; i++) {
446 size_t off = objects[i] - offset + startPos;
447 mObjects[idx++] = off;
448 mObjectsSize++;
449
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700450 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700451 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700452 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700453
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700454 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700455 // If this is a file descriptor, we need to dup it so the
456 // new Parcel now owns its own fd, and can declare that we
457 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800458 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800459 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700460 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400461 if (!mAllowFds) {
462 err = FDS_NOT_ALLOWED;
463 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700464 }
465 }
466 }
467
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400468 return err;
469}
470
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700471int Parcel::compareData(const Parcel& other) {
472 size_t size = dataSize();
473 if (size != other.dataSize()) {
474 return size < other.dataSize() ? -1 : 1;
475 }
476 return memcmp(data(), other.data(), size);
477}
478
Jeff Brown13b16042014-11-11 16:44:25 -0800479bool Parcel::allowFds() const
480{
481 return mAllowFds;
482}
483
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700484bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400485{
486 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700487 if (!allowFds) {
488 mAllowFds = false;
489 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400490 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700491}
492
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700493void Parcel::restoreAllowFds(bool lastValue)
494{
495 mAllowFds = lastValue;
496}
497
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700498bool Parcel::hasFileDescriptors() const
499{
500 if (!mFdsKnown) {
501 scanForFds();
502 }
503 return mHasFds;
504}
505
Steven Morelandf183fdd2020-10-27 00:12:12 +0000506void Parcel::markSensitive() const
507{
508 mDeallocZero = true;
509}
510
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000511void Parcel::updateWorkSourceRequestHeaderPosition() const {
512 // Only update the request headers once. We only want to point
513 // to the first headers read/written.
514 if (!mRequestHeaderPresent) {
515 mWorkSourceRequestHeaderPosition = dataPosition();
516 mRequestHeaderPresent = true;
517 }
518}
519
Jooyung Han1b228f42020-01-30 13:41:12 +0900520#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700521constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
522#else
523constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
524#endif
525
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700526// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700527status_t Parcel::writeInterfaceToken(const String16& interface)
528{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000529 return writeInterfaceToken(interface.string(), interface.size());
530}
531
532status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Olivier Gaillard91a04802018-11-14 17:32:41 +0000533 const IPCThreadState* threadState = IPCThreadState::self();
534 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000535 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000536 writeInt32(threadState->shouldPropagateWorkSource() ?
537 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700538 writeInt32(kHeader);
Steven Morelanddbc76c72020-10-01 18:02:48 +0000539
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700540 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000541 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700542}
543
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000544bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
545{
546 if (!mRequestHeaderPresent) {
547 return false;
548 }
549
550 const size_t initialPosition = dataPosition();
551 setDataPosition(mWorkSourceRequestHeaderPosition);
552 status_t err = writeInt32(uid);
553 setDataPosition(initialPosition);
554 return err == NO_ERROR;
555}
556
Steven Morelandf1b1e492019-05-06 15:05:13 -0700557uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000558{
559 if (!mRequestHeaderPresent) {
560 return IPCThreadState::kUnsetWorkSource;
561 }
562
563 const size_t initialPosition = dataPosition();
564 setDataPosition(mWorkSourceRequestHeaderPosition);
565 uid_t uid = readInt32();
566 setDataPosition(initialPosition);
567 return uid;
568}
569
Mathias Agopian83c04462009-05-22 19:00:22 -0700570bool Parcel::checkInterface(IBinder* binder) const
571{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700572 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700573}
574
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700575bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700576 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700577{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700578 return enforceInterface(interface.string(), interface.size(), threadState);
579}
580
581bool Parcel::enforceInterface(const char16_t* interface,
582 size_t len,
583 IPCThreadState* threadState) const
584{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100585 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700586 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700587 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700588 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700589 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700590 if ((threadState->getLastTransactionBinderFlags() &
591 IBinder::FLAG_ONEWAY) != 0) {
592 // For one-way calls, the callee is running entirely
593 // disconnected from the caller, so disable StrictMode entirely.
594 // Not only does disk/network usage not impact the caller, but
595 // there's no way to commuicate back any violations anyway.
596 threadState->setStrictModePolicy(0);
597 } else {
598 threadState->setStrictModePolicy(strictPolicy);
599 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100600 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000601 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100602 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000603 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700604 // vendor header
605 int32_t header = readInt32();
606 if (header != kHeader) {
607 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
608 return false;
609 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100610 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700611 size_t parcel_interface_len;
612 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
613 if (len == parcel_interface_len &&
614 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700615 return true;
616 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700617 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700618 String8(interface, len).string(),
619 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620 return false;
621 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700622}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700623
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700624size_t Parcel::objectsCount() const
625{
626 return mObjectsSize;
627}
628
629status_t Parcel::errorCheck() const
630{
631 return mError;
632}
633
634void Parcel::setError(status_t err)
635{
636 mError = err;
637}
638
639status_t Parcel::finishWrite(size_t len)
640{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700641 if (len > INT32_MAX) {
642 // don't accept size_t values which may have come from an
643 // inadvertent conversion from a negative int.
644 return BAD_VALUE;
645 }
646
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700647 //printf("Finish write of %d\n", len);
648 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700649 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700650 if (mDataPos > mDataSize) {
651 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700652 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700653 }
654 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
655 return NO_ERROR;
656}
657
658status_t Parcel::writeUnpadded(const void* data, size_t len)
659{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700660 if (len > INT32_MAX) {
661 // don't accept size_t values which may have come from an
662 // inadvertent conversion from a negative int.
663 return BAD_VALUE;
664 }
665
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700666 size_t end = mDataPos + len;
667 if (end < mDataPos) {
668 // integer overflow
669 return BAD_VALUE;
670 }
671
672 if (end <= mDataCapacity) {
673restart_write:
674 memcpy(mData+mDataPos, data, len);
675 return finishWrite(len);
676 }
677
678 status_t err = growData(len);
679 if (err == NO_ERROR) goto restart_write;
680 return err;
681}
682
683status_t Parcel::write(const void* data, size_t len)
684{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700685 if (len > INT32_MAX) {
686 // don't accept size_t values which may have come from an
687 // inadvertent conversion from a negative int.
688 return BAD_VALUE;
689 }
690
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700691 void* const d = writeInplace(len);
692 if (d) {
693 memcpy(d, data, len);
694 return NO_ERROR;
695 }
696 return mError;
697}
698
699void* Parcel::writeInplace(size_t len)
700{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700701 if (len > INT32_MAX) {
702 // don't accept size_t values which may have come from an
703 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700704 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700705 }
706
707 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700708
709 // sanity check for integer overflow
710 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700711 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700712 }
713
714 if ((mDataPos+padded) <= mDataCapacity) {
715restart_write:
716 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
717 uint8_t* const data = mData+mDataPos;
718
719 // Need to pad at end?
720 if (padded != len) {
721#if BYTE_ORDER == BIG_ENDIAN
722 static const uint32_t mask[4] = {
723 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
724 };
725#endif
726#if BYTE_ORDER == LITTLE_ENDIAN
727 static const uint32_t mask[4] = {
728 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
729 };
730#endif
731 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
732 // *reinterpret_cast<void**>(data+padded-4));
733 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
734 }
735
736 finishWrite(padded);
737 return data;
738 }
739
740 status_t err = growData(padded);
741 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700742 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700743}
744
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800745status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
746 const uint8_t* strData = (uint8_t*)str.data();
747 const size_t strLen= str.length();
748 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100749 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800750 return BAD_VALUE;
751 }
752
753 status_t err = writeInt32(utf16Len);
754 if (err) {
755 return err;
756 }
757
758 // Allocate enough bytes to hold our converted string and its terminating NULL.
759 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
760 if (!dst) {
761 return NO_MEMORY;
762 }
763
Sergio Girof4607432016-07-21 14:46:35 +0100764 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800765
766 return NO_ERROR;
767}
768
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900769status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) {
770 if (!str) {
771 return writeInt32(-1);
772 }
773 return writeUtf8AsUtf16(*str);
774}
775
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800776status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
777 if (!str) {
778 return writeInt32(-1);
779 }
780 return writeUtf8AsUtf16(*str);
781}
782
Daniel Normand0337ef2019-09-20 15:46:03 -0700783status_t Parcel::writeByteVectorInternal(const int8_t* data, size_t size) {
784 if (size > std::numeric_limits<int32_t>::max()) {
785 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -0700786 }
787
Daniel Normand0337ef2019-09-20 15:46:03 -0700788 status_t status = writeInt32(size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700789 if (status != OK) {
790 return status;
791 }
792
Daniel Normand0337ef2019-09-20 15:46:03 -0700793 return write(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700794}
795
Casey Dahlin185d3442016-02-09 11:08:35 -0800796status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700797 return writeByteVectorInternal(val.data(), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800798}
799
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900800status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val)
801{
802 if (!val) return writeInt32(-1);
803 return writeByteVectorInternal(val->data(), val->size());
804}
805
Casey Dahlin185d3442016-02-09 11:08:35 -0800806status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
807{
Daniel Normand0337ef2019-09-20 15:46:03 -0700808 if (!val) return writeInt32(-1);
809 return writeByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800810}
811
812status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700813 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val.data()), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800814}
815
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900816status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val)
817{
818 if (!val) return writeInt32(-1);
819 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
820}
821
Casey Dahlin185d3442016-02-09 11:08:35 -0800822status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
823{
Daniel Normand0337ef2019-09-20 15:46:03 -0700824 if (!val) return writeInt32(-1);
825 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800826}
827
Casey Dahlin451ff582015-10-19 18:12:18 -0700828status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
829{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800830 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700831}
832
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900833status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val)
834{
835 return writeNullableTypedVector(val, &Parcel::writeInt32);
836}
837
Casey Dahlinb9872622015-11-25 15:09:45 -0800838status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
839{
840 return writeNullableTypedVector(val, &Parcel::writeInt32);
841}
842
Casey Dahlin451ff582015-10-19 18:12:18 -0700843status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
844{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800845 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700846}
847
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900848status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val)
849{
850 return writeNullableTypedVector(val, &Parcel::writeInt64);
851}
852
Casey Dahlinb9872622015-11-25 15:09:45 -0800853status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
854{
855 return writeNullableTypedVector(val, &Parcel::writeInt64);
856}
857
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800858status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
859{
860 return writeTypedVector(val, &Parcel::writeUint64);
861}
862
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900863status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val)
864{
865 return writeNullableTypedVector(val, &Parcel::writeUint64);
866}
867
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800868status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
869{
870 return writeNullableTypedVector(val, &Parcel::writeUint64);
871}
872
Casey Dahlin451ff582015-10-19 18:12:18 -0700873status_t Parcel::writeFloatVector(const std::vector<float>& val)
874{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800875 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700876}
877
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900878status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val)
879{
880 return writeNullableTypedVector(val, &Parcel::writeFloat);
881}
882
Casey Dahlinb9872622015-11-25 15:09:45 -0800883status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
884{
885 return writeNullableTypedVector(val, &Parcel::writeFloat);
886}
887
Casey Dahlin451ff582015-10-19 18:12:18 -0700888status_t Parcel::writeDoubleVector(const std::vector<double>& val)
889{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800890 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700891}
892
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900893status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val)
894{
895 return writeNullableTypedVector(val, &Parcel::writeDouble);
896}
897
Casey Dahlinb9872622015-11-25 15:09:45 -0800898status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
899{
900 return writeNullableTypedVector(val, &Parcel::writeDouble);
901}
902
Casey Dahlin451ff582015-10-19 18:12:18 -0700903status_t Parcel::writeBoolVector(const std::vector<bool>& val)
904{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800905 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700906}
907
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900908status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val)
909{
910 return writeNullableTypedVector(val, &Parcel::writeBool);
911}
912
Casey Dahlinb9872622015-11-25 15:09:45 -0800913status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
914{
915 return writeNullableTypedVector(val, &Parcel::writeBool);
916}
917
Casey Dahlin451ff582015-10-19 18:12:18 -0700918status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
919{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800920 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700921}
922
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900923status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val)
924{
925 return writeNullableTypedVector(val, &Parcel::writeChar);
926}
927
Casey Dahlinb9872622015-11-25 15:09:45 -0800928status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
929{
930 return writeNullableTypedVector(val, &Parcel::writeChar);
931}
932
Casey Dahlin451ff582015-10-19 18:12:18 -0700933status_t Parcel::writeString16Vector(const std::vector<String16>& val)
934{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800935 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700936}
937
Casey Dahlinb9872622015-11-25 15:09:45 -0800938status_t Parcel::writeString16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900939 const std::optional<std::vector<std::optional<String16>>>& val)
940{
941 return writeNullableTypedVector(val, &Parcel::writeString16);
942}
943
944status_t Parcel::writeString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800945 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
946{
947 return writeNullableTypedVector(val, &Parcel::writeString16);
948}
949
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800950status_t Parcel::writeUtf8VectorAsUtf16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900951 const std::optional<std::vector<std::optional<std::string>>>& val) {
952 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
953}
954
955status_t Parcel::writeUtf8VectorAsUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800956 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
957 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
958}
959
960status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
961 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
962}
963
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700964status_t Parcel::writeInt32(int32_t val)
965{
Andreas Huber84a6d042009-08-17 13:33:27 -0700966 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700967}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800968
969status_t Parcel::writeUint32(uint32_t val)
970{
971 return writeAligned(val);
972}
973
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700974status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700975 if (len > INT32_MAX) {
976 // don't accept size_t values which may have come from an
977 // inadvertent conversion from a negative int.
978 return BAD_VALUE;
979 }
980
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700981 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700982 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700983 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700984 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700985 if (ret == NO_ERROR) {
986 ret = write(val, len * sizeof(*val));
987 }
988 return ret;
989}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700990status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700991 if (len > INT32_MAX) {
992 // don't accept size_t values which may have come from an
993 // inadvertent conversion from a negative int.
994 return BAD_VALUE;
995 }
996
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700997 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700998 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700999 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001000 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001001 if (ret == NO_ERROR) {
1002 ret = write(val, len * sizeof(*val));
1003 }
1004 return ret;
1005}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001006
Casey Dahlind6848f52015-10-15 15:44:59 -07001007status_t Parcel::writeBool(bool val)
1008{
1009 return writeInt32(int32_t(val));
1010}
1011
1012status_t Parcel::writeChar(char16_t val)
1013{
1014 return writeInt32(int32_t(val));
1015}
1016
1017status_t Parcel::writeByte(int8_t val)
1018{
1019 return writeInt32(int32_t(val));
1020}
1021
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001022status_t Parcel::writeInt64(int64_t val)
1023{
Andreas Huber84a6d042009-08-17 13:33:27 -07001024 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001025}
1026
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001027status_t Parcel::writeUint64(uint64_t val)
1028{
1029 return writeAligned(val);
1030}
1031
Serban Constantinescuf683e012013-11-05 16:53:55 +00001032status_t Parcel::writePointer(uintptr_t val)
1033{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001034 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001035}
1036
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001037status_t Parcel::writeFloat(float val)
1038{
Andreas Huber84a6d042009-08-17 13:33:27 -07001039 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001040}
1041
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001042#if defined(__mips__) && defined(__mips_hard_float)
1043
1044status_t Parcel::writeDouble(double val)
1045{
1046 union {
1047 double d;
1048 unsigned long long ll;
1049 } u;
1050 u.d = val;
1051 return writeAligned(u.ll);
1052}
1053
1054#else
1055
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001056status_t Parcel::writeDouble(double val)
1057{
Andreas Huber84a6d042009-08-17 13:33:27 -07001058 return writeAligned(val);
1059}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001060
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001061#endif
1062
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001063status_t Parcel::writeCString(const char* str)
1064{
1065 return write(str, strlen(str)+1);
1066}
1067
1068status_t Parcel::writeString8(const String8& str)
1069{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001070 return writeString8(str.string(), str.size());
1071}
1072
1073status_t Parcel::writeString8(const char* str, size_t len)
1074{
1075 if (str == nullptr) return writeInt32(-1);
1076
Jeff Sharkey18220902020-11-05 08:36:20 -07001077 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001078 status_t err = writeInt32(len);
1079 if (err == NO_ERROR) {
1080 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1081 if (data) {
1082 memcpy(data, str, len);
1083 *reinterpret_cast<char*>(data+len) = 0;
1084 return NO_ERROR;
1085 }
1086 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001087 }
1088 return err;
1089}
1090
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001091status_t Parcel::writeString16(const std::optional<String16>& str)
1092{
1093 if (!str) {
1094 return writeInt32(-1);
1095 }
1096
1097 return writeString16(*str);
1098}
1099
Casey Dahlinb9872622015-11-25 15:09:45 -08001100status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1101{
1102 if (!str) {
1103 return writeInt32(-1);
1104 }
1105
1106 return writeString16(*str);
1107}
1108
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001109status_t Parcel::writeString16(const String16& str)
1110{
1111 return writeString16(str.string(), str.size());
1112}
1113
1114status_t Parcel::writeString16(const char16_t* str, size_t len)
1115{
Yi Kong91635562018-06-07 14:38:36 -07001116 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001117
Jeff Sharkey18220902020-11-05 08:36:20 -07001118 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001119 status_t err = writeInt32(len);
1120 if (err == NO_ERROR) {
1121 len *= sizeof(char16_t);
1122 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1123 if (data) {
1124 memcpy(data, str, len);
1125 *reinterpret_cast<char16_t*>(data+len) = 0;
1126 return NO_ERROR;
1127 }
1128 err = mError;
1129 }
1130 return err;
1131}
1132
1133status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1134{
Steven Morelanda86a3562019-08-01 23:28:34 +00001135 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001136}
1137
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001138status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1139{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001140 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001141}
1142
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001143status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val)
1144{
1145 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1146}
1147
Casey Dahlinb9872622015-11-25 15:09:45 -08001148status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1149{
1150 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1151}
1152
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001153status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const {
1154 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
1155}
1156
Casey Dahlinb9872622015-11-25 15:09:45 -08001157status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001158 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001159}
1160
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001161status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001162 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001163}
1164
Casey Dahlinb9872622015-11-25 15:09:45 -08001165status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1166 if (!parcelable) {
1167 return writeInt32(0);
1168 }
1169
1170 return writeParcelable(*parcelable);
1171}
1172
Christopher Wiley97f048d2015-11-19 06:49:05 -08001173status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1174 status_t status = writeInt32(1); // parcelable is not null.
1175 if (status != OK) {
1176 return status;
1177 }
1178 return parcelable.writeToParcel(this);
1179}
1180
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001181status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001182{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001183 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001184 return BAD_TYPE;
1185
1186 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001187 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001188 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001190 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001191 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001193 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1194 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195
1196 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001197 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198 return err;
1199 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001200 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001201 return err;
1202}
1203
Jeff Brown93ff1f92011-11-04 19:01:44 -07001204status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001205{
1206 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001207 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001208 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001209 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001210 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001211 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001212 return writeObject(obj, true);
1213}
1214
1215status_t Parcel::writeDupFileDescriptor(int fd)
1216{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001217 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001218 if (dupFd < 0) {
1219 return -errno;
1220 }
1221 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001222 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001223 close(dupFd);
1224 }
1225 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001226}
1227
Dianne Hackborn1941a402016-08-29 12:30:43 -07001228status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1229{
1230 writeInt32(0);
1231 return writeFileDescriptor(fd, takeOwnership);
1232}
1233
Ryo Hashimotobf551892018-05-31 16:58:35 +09001234status_t Parcel::writeDupParcelFileDescriptor(int fd)
1235{
1236 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1237 if (dupFd < 0) {
1238 return -errno;
1239 }
1240 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1241 if (err != OK) {
1242 close(dupFd);
1243 }
1244 return err;
1245}
1246
Christopher Wiley2cf19952016-04-11 11:09:37 -07001247status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001248 return writeDupFileDescriptor(fd.get());
1249}
1250
Christopher Wiley2cf19952016-04-11 11:09:37 -07001251status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001252 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1253}
1254
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001255status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) {
1256 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1257}
1258
Christopher Wiley2cf19952016-04-11 11:09:37 -07001259status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001260 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1261}
1262
Jeff Brown13b16042014-11-11 16:44:25 -08001263status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001264{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001265 if (len > INT32_MAX) {
1266 // don't accept size_t values which may have come from an
1267 // inadvertent conversion from a negative int.
1268 return BAD_VALUE;
1269 }
1270
Jeff Brown13b16042014-11-11 16:44:25 -08001271 status_t status;
1272 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001273 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001274 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001275 if (status) return status;
1276
1277 void* ptr = writeInplace(len);
1278 if (!ptr) return NO_MEMORY;
1279
Jeff Brown13b16042014-11-11 16:44:25 -08001280 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001281 return NO_ERROR;
1282 }
1283
Steve Block6807e592011-10-20 11:56:00 +01001284 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001285 int fd = ashmem_create_region("Parcel Blob", len);
1286 if (fd < 0) return NO_MEMORY;
1287
1288 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1289 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001290 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001291 } else {
Yi Kong91635562018-06-07 14:38:36 -07001292 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001293 if (ptr == MAP_FAILED) {
1294 status = -errno;
1295 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001296 if (!mutableCopy) {
1297 result = ashmem_set_prot_region(fd, PROT_READ);
1298 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001299 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001300 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001301 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001302 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001303 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001304 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001305 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001306 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001307 return NO_ERROR;
1308 }
1309 }
1310 }
1311 }
1312 ::munmap(ptr, len);
1313 }
1314 ::close(fd);
1315 return status;
1316}
1317
Jeff Brown13b16042014-11-11 16:44:25 -08001318status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1319{
1320 // Must match up with what's done in writeBlob.
1321 if (!mAllowFds) return FDS_NOT_ALLOWED;
1322 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1323 if (status) return status;
1324 return writeDupFileDescriptor(fd);
1325}
1326
Mathias Agopiane1424282013-07-29 21:24:40 -07001327status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001328{
1329 status_t err;
1330
1331 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001332 const size_t len = val.getFlattenedSize();
1333 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001334
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001335 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001336 // don't accept size_t values which may have come from an
1337 // inadvertent conversion from a negative int.
1338 return BAD_VALUE;
1339 }
1340
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001341 err = this->writeInt32(len);
1342 if (err) return err;
1343
1344 err = this->writeInt32(fd_count);
1345 if (err) return err;
1346
1347 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001348 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001349 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001350 return BAD_VALUE;
1351
Yi Kong91635562018-06-07 14:38:36 -07001352 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001353 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001354 fds = new (std::nothrow) int[fd_count];
1355 if (fds == nullptr) {
1356 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1357 return BAD_VALUE;
1358 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001359 }
1360
1361 err = val.flatten(buf, len, fds, fd_count);
1362 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1363 err = this->writeDupFileDescriptor( fds[i] );
1364 }
1365
1366 if (fd_count) {
1367 delete [] fds;
1368 }
1369
1370 return err;
1371}
1372
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001373status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1374{
1375 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1376 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1377 if (enoughData && enoughObjects) {
1378restart_write:
1379 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001380
Christopher Tate98e67d32015-06-03 18:44:15 -07001381 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001382 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001383 if (!mAllowFds) {
1384 // fail before modifying our object index
1385 return FDS_NOT_ALLOWED;
1386 }
1387 mHasFds = mFdsKnown = true;
1388 }
1389
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001390 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001391 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001392 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001393 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001394 mObjectsSize++;
1395 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001396
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001397 return finishWrite(sizeof(flat_binder_object));
1398 }
1399
1400 if (!enoughData) {
1401 const status_t err = growData(sizeof(val));
1402 if (err != NO_ERROR) return err;
1403 }
1404 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001405 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1406 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001407 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001408 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001409 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001410 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001411 mObjects = objects;
1412 mObjectsCapacity = newSize;
1413 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001414
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001415 goto restart_write;
1416}
1417
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001418status_t Parcel::writeNoException()
1419{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001420 binder::Status status;
1421 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001422}
1423
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001424status_t Parcel::validateReadData(size_t upperBound) const
1425{
1426 // Don't allow non-object reads on object data
1427 if (mObjectsSorted || mObjectsSize <= 1) {
1428data_sorted:
1429 // Expect to check only against the next object
1430 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1431 // For some reason the current read position is greater than the next object
1432 // hint. Iterate until we find the right object
1433 size_t nextObject = mNextObjectHint;
1434 do {
1435 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1436 // Requested info overlaps with an object
1437 ALOGE("Attempt to read from protected data in Parcel %p", this);
1438 return PERMISSION_DENIED;
1439 }
1440 nextObject++;
1441 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1442 mNextObjectHint = nextObject;
1443 }
1444 return NO_ERROR;
1445 }
1446 // Quickly determine if mObjects is sorted.
1447 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1448 binder_size_t* prevObj = currObj;
1449 while (currObj > mObjects) {
1450 prevObj--;
1451 if(*prevObj > *currObj) {
1452 goto data_unsorted;
1453 }
1454 currObj--;
1455 }
1456 mObjectsSorted = true;
1457 goto data_sorted;
1458
1459data_unsorted:
1460 // Insertion Sort mObjects
1461 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1462 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1463 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1464 binder_size_t temp = *iter0;
1465 binder_size_t* iter1 = iter0 - 1;
1466 while (iter1 >= mObjects && *iter1 > temp) {
1467 *(iter1 + 1) = *iter1;
1468 iter1--;
1469 }
1470 *(iter1 + 1) = temp;
1471 }
1472 mNextObjectHint = 0;
1473 mObjectsSorted = true;
1474 goto data_sorted;
1475}
1476
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001477status_t Parcel::read(void* outData, size_t len) const
1478{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001479 if (len > INT32_MAX) {
1480 // don't accept size_t values which may have come from an
1481 // inadvertent conversion from a negative int.
1482 return BAD_VALUE;
1483 }
1484
1485 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1486 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001487 if (mObjectsSize > 0) {
1488 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001489 if(err != NO_ERROR) {
1490 // Still increment the data position by the expected length
1491 mDataPos += pad_size(len);
1492 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1493 return err;
1494 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001495 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001496 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001497 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001498 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001499 return NO_ERROR;
1500 }
1501 return NOT_ENOUGH_DATA;
1502}
1503
1504const void* Parcel::readInplace(size_t len) const
1505{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001506 if (len > INT32_MAX) {
1507 // don't accept size_t values which may have come from an
1508 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001509 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001510 }
1511
1512 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1513 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001514 if (mObjectsSize > 0) {
1515 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001516 if(err != NO_ERROR) {
1517 // Still increment the data position by the expected length
1518 mDataPos += pad_size(len);
1519 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001520 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001521 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001522 }
1523
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001524 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001525 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001526 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001527 return data;
1528 }
Yi Kong91635562018-06-07 14:38:36 -07001529 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001530}
1531
Andreas Huber84a6d042009-08-17 13:33:27 -07001532template<class T>
1533status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001534 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001535
1536 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001537 if (mObjectsSize > 0) {
1538 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001539 if(err != NO_ERROR) {
1540 // Still increment the data position by the expected length
1541 mDataPos += sizeof(T);
1542 return err;
1543 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001544 }
1545
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001546 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001547 mDataPos += sizeof(T);
1548 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001549 return NO_ERROR;
1550 } else {
1551 return NOT_ENOUGH_DATA;
1552 }
1553}
1554
Andreas Huber84a6d042009-08-17 13:33:27 -07001555template<class T>
1556T Parcel::readAligned() const {
1557 T result;
1558 if (readAligned(&result) != NO_ERROR) {
1559 result = 0;
1560 }
1561
1562 return result;
1563}
1564
1565template<class T>
1566status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001567 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001568
1569 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1570restart_write:
1571 *reinterpret_cast<T*>(mData+mDataPos) = val;
1572 return finishWrite(sizeof(val));
1573 }
1574
1575 status_t err = growData(sizeof(val));
1576 if (err == NO_ERROR) goto restart_write;
1577 return err;
1578}
1579
Casey Dahlin185d3442016-02-09 11:08:35 -08001580status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001581 size_t size;
1582 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1583 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001584}
1585
1586status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001587 size_t size;
1588 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1589 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001590}
1591
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001592status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const {
1593 size_t size;
1594 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1595 if (!*val) {
1596 // reserveOutVector does not create the out vector if size is < 0.
1597 // This occurs when writing a null byte vector.
1598 return OK;
1599 }
1600 return readByteVectorInternal(&**val, size);
1601}
1602
Casey Dahlin185d3442016-02-09 11:08:35 -08001603status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001604 size_t size;
1605 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001606 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001607 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001608 // This occurs when writing a null byte vector.
1609 return OK;
1610 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001611 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001612}
1613
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001614status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const {
1615 size_t size;
1616 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1617 if (!*val) {
1618 // reserveOutVector does not create the out vector if size is < 0.
1619 // This occurs when writing a null byte vector.
1620 return OK;
1621 }
1622 return readByteVectorInternal(&**val, size);
1623}
1624
Casey Dahlin185d3442016-02-09 11:08:35 -08001625status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001626 size_t size;
1627 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001628 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001629 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001630 // This occurs when writing a null byte vector.
1631 return OK;
1632 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001633 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001634}
1635
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001636status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const {
1637 return readNullableTypedVector(val, &Parcel::readInt32);
1638}
1639
Casey Dahlinb9872622015-11-25 15:09:45 -08001640status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1641 return readNullableTypedVector(val, &Parcel::readInt32);
1642}
1643
Casey Dahlin451ff582015-10-19 18:12:18 -07001644status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001645 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001646}
1647
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001648status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const {
1649 return readNullableTypedVector(val, &Parcel::readInt64);
1650}
1651
Casey Dahlinb9872622015-11-25 15:09:45 -08001652status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1653 return readNullableTypedVector(val, &Parcel::readInt64);
1654}
1655
Casey Dahlin451ff582015-10-19 18:12:18 -07001656status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001657 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001658}
1659
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001660status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const {
1661 return readNullableTypedVector(val, &Parcel::readUint64);
1662}
1663
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001664status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1665 return readNullableTypedVector(val, &Parcel::readUint64);
1666}
1667
1668status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1669 return readTypedVector(val, &Parcel::readUint64);
1670}
1671
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001672status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const {
1673 return readNullableTypedVector(val, &Parcel::readFloat);
1674}
1675
Casey Dahlinb9872622015-11-25 15:09:45 -08001676status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1677 return readNullableTypedVector(val, &Parcel::readFloat);
1678}
1679
Casey Dahlin451ff582015-10-19 18:12:18 -07001680status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001681 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001682}
1683
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001684status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const {
1685 return readNullableTypedVector(val, &Parcel::readDouble);
1686}
1687
Casey Dahlinb9872622015-11-25 15:09:45 -08001688status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1689 return readNullableTypedVector(val, &Parcel::readDouble);
1690}
1691
Casey Dahlin451ff582015-10-19 18:12:18 -07001692status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001693 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001694}
1695
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001696status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const {
1697 const int32_t start = dataPosition();
1698 int32_t size;
1699 status_t status = readInt32(&size);
1700 val->reset();
1701
1702 if (status != OK || size < 0) {
1703 return status;
1704 }
1705
1706 setDataPosition(start);
1707 val->emplace();
1708
1709 status = readBoolVector(&**val);
1710
1711 if (status != OK) {
1712 val->reset();
1713 }
1714
1715 return status;
1716}
1717
Casey Dahlinb9872622015-11-25 15:09:45 -08001718status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1719 const int32_t start = dataPosition();
1720 int32_t size;
1721 status_t status = readInt32(&size);
1722 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001723
Casey Dahlinb9872622015-11-25 15:09:45 -08001724 if (status != OK || size < 0) {
1725 return status;
1726 }
1727
1728 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001729 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001730
1731 status = readBoolVector(val->get());
1732
1733 if (status != OK) {
1734 val->reset();
1735 }
1736
1737 return status;
1738}
1739
1740status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001741 int32_t size;
1742 status_t status = readInt32(&size);
1743
1744 if (status != OK) {
1745 return status;
1746 }
1747
1748 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001749 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001750 }
1751
1752 val->resize(size);
1753
1754 /* C++ bool handling means a vector of bools isn't necessarily addressable
1755 * (we might use individual bits)
1756 */
Christopher Wiley97887982015-10-27 16:33:47 -07001757 bool data;
1758 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001759 status = readBool(&data);
1760 (*val)[i] = data;
1761
1762 if (status != OK) {
1763 return status;
1764 }
1765 }
1766
1767 return OK;
1768}
1769
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001770status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const {
1771 return readNullableTypedVector(val, &Parcel::readChar);
1772}
1773
Casey Dahlinb9872622015-11-25 15:09:45 -08001774status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1775 return readNullableTypedVector(val, &Parcel::readChar);
1776}
1777
Casey Dahlin451ff582015-10-19 18:12:18 -07001778status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001779 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001780}
1781
Casey Dahlinb9872622015-11-25 15:09:45 -08001782status_t Parcel::readString16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001783 std::optional<std::vector<std::optional<String16>>>* val) const {
1784 return readNullableTypedVector(val, &Parcel::readString16);
1785}
1786
1787status_t Parcel::readString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -08001788 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1789 return readNullableTypedVector(val, &Parcel::readString16);
1790}
1791
Casey Dahlin451ff582015-10-19 18:12:18 -07001792status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001793 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001794}
1795
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001796status_t Parcel::readUtf8VectorFromUtf16Vector(
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001797 std::optional<std::vector<std::optional<std::string>>>* val) const {
1798 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1799}
1800
1801status_t Parcel::readUtf8VectorFromUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001802 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1803 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1804}
1805
1806status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1807 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1808}
Casey Dahlin451ff582015-10-19 18:12:18 -07001809
Andreas Huber84a6d042009-08-17 13:33:27 -07001810status_t Parcel::readInt32(int32_t *pArg) const
1811{
1812 return readAligned(pArg);
1813}
1814
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001815int32_t Parcel::readInt32() const
1816{
Andreas Huber84a6d042009-08-17 13:33:27 -07001817 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001818}
1819
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001820status_t Parcel::readUint32(uint32_t *pArg) const
1821{
1822 return readAligned(pArg);
1823}
1824
1825uint32_t Parcel::readUint32() const
1826{
1827 return readAligned<uint32_t>();
1828}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001829
1830status_t Parcel::readInt64(int64_t *pArg) const
1831{
Andreas Huber84a6d042009-08-17 13:33:27 -07001832 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001833}
1834
1835
1836int64_t Parcel::readInt64() const
1837{
Andreas Huber84a6d042009-08-17 13:33:27 -07001838 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001839}
1840
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001841status_t Parcel::readUint64(uint64_t *pArg) const
1842{
1843 return readAligned(pArg);
1844}
1845
1846uint64_t Parcel::readUint64() const
1847{
1848 return readAligned<uint64_t>();
1849}
1850
Serban Constantinescuf683e012013-11-05 16:53:55 +00001851status_t Parcel::readPointer(uintptr_t *pArg) const
1852{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001853 status_t ret;
1854 binder_uintptr_t ptr;
1855 ret = readAligned(&ptr);
1856 if (!ret)
1857 *pArg = ptr;
1858 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001859}
1860
1861uintptr_t Parcel::readPointer() const
1862{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001863 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001864}
1865
1866
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001867status_t Parcel::readFloat(float *pArg) const
1868{
Andreas Huber84a6d042009-08-17 13:33:27 -07001869 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001870}
1871
1872
1873float Parcel::readFloat() const
1874{
Andreas Huber84a6d042009-08-17 13:33:27 -07001875 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001876}
1877
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001878#if defined(__mips__) && defined(__mips_hard_float)
1879
1880status_t Parcel::readDouble(double *pArg) const
1881{
1882 union {
1883 double d;
1884 unsigned long long ll;
1885 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001886 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001887 status_t status;
1888 status = readAligned(&u.ll);
1889 *pArg = u.d;
1890 return status;
1891}
1892
1893double Parcel::readDouble() const
1894{
1895 union {
1896 double d;
1897 unsigned long long ll;
1898 } u;
1899 u.ll = readAligned<unsigned long long>();
1900 return u.d;
1901}
1902
1903#else
1904
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001905status_t Parcel::readDouble(double *pArg) const
1906{
Andreas Huber84a6d042009-08-17 13:33:27 -07001907 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001908}
1909
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001910double Parcel::readDouble() const
1911{
Andreas Huber84a6d042009-08-17 13:33:27 -07001912 return readAligned<double>();
1913}
1914
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001915#endif
1916
Andreas Huber84a6d042009-08-17 13:33:27 -07001917status_t Parcel::readIntPtr(intptr_t *pArg) const
1918{
1919 return readAligned(pArg);
1920}
1921
1922
1923intptr_t Parcel::readIntPtr() const
1924{
1925 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001926}
1927
Casey Dahlind6848f52015-10-15 15:44:59 -07001928status_t Parcel::readBool(bool *pArg) const
1929{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001930 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001931 status_t ret = readInt32(&tmp);
1932 *pArg = (tmp != 0);
1933 return ret;
1934}
1935
1936bool Parcel::readBool() const
1937{
1938 return readInt32() != 0;
1939}
1940
1941status_t Parcel::readChar(char16_t *pArg) const
1942{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001943 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001944 status_t ret = readInt32(&tmp);
1945 *pArg = char16_t(tmp);
1946 return ret;
1947}
1948
1949char16_t Parcel::readChar() const
1950{
1951 return char16_t(readInt32());
1952}
1953
1954status_t Parcel::readByte(int8_t *pArg) const
1955{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001956 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001957 status_t ret = readInt32(&tmp);
1958 *pArg = int8_t(tmp);
1959 return ret;
1960}
1961
1962int8_t Parcel::readByte() const
1963{
1964 return int8_t(readInt32());
1965}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001966
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001967status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1968 size_t utf16Size = 0;
1969 const char16_t* src = readString16Inplace(&utf16Size);
1970 if (!src) {
1971 return UNEXPECTED_NULL;
1972 }
1973
1974 // Save ourselves the trouble, we're done.
1975 if (utf16Size == 0u) {
1976 str->clear();
1977 return NO_ERROR;
1978 }
1979
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001980 // Allow for closing '\0'
1981 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1982 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001983 return BAD_VALUE;
1984 }
1985 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001986 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001987 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001988 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1989 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001990 return NO_ERROR;
1991}
1992
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001993status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const {
1994 const int32_t start = dataPosition();
1995 int32_t size;
1996 status_t status = readInt32(&size);
1997 str->reset();
1998
1999 if (status != OK || size < 0) {
2000 return status;
2001 }
2002
2003 setDataPosition(start);
2004 str->emplace();
2005 return readUtf8FromUtf16(&**str);
2006}
2007
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002008status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2009 const int32_t start = dataPosition();
2010 int32_t size;
2011 status_t status = readInt32(&size);
2012 str->reset();
2013
2014 if (status != OK || size < 0) {
2015 return status;
2016 }
2017
2018 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002019 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002020 return readUtf8FromUtf16(str->get());
2021}
2022
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002023const char* Parcel::readCString() const
2024{
Steven Morelandd0d4b582019-05-17 13:14:06 -07002025 if (mDataPos < mDataSize) {
2026 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002027 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2028 // is the string's trailing NUL within the parcel's valid bounds?
2029 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2030 if (eos) {
2031 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002032 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002033 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002034 return str;
2035 }
2036 }
Yi Kong91635562018-06-07 14:38:36 -07002037 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002038}
2039
2040String8 Parcel::readString8() const
2041{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002042 size_t len;
2043 const char* str = readString8Inplace(&len);
2044 if (str) return String8(str, len);
2045 ALOGE("Reading a NULL string not supported here.");
2046 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002047}
2048
2049status_t Parcel::readString8(String8* pArg) const
2050{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002051 size_t len;
2052 const char* str = readString8Inplace(&len);
2053 if (str) {
2054 pArg->setTo(str, len);
2055 return 0;
2056 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002057 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002058 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002059 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002060}
2061
2062const char* Parcel::readString8Inplace(size_t* outLen) const
2063{
2064 int32_t size = readInt32();
2065 // watch for potential int overflow from size+1
2066 if (size >= 0 && size < INT32_MAX) {
2067 *outLen = size;
2068 const char* str = (const char*)readInplace(size+1);
2069 if (str != nullptr) {
2070 return str;
2071 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002072 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002073 *outLen = 0;
2074 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002075}
2076
2077String16 Parcel::readString16() const
2078{
2079 size_t len;
2080 const char16_t* str = readString16Inplace(&len);
2081 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002082 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002083 return String16();
2084}
2085
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002086status_t Parcel::readString16(std::optional<String16>* pArg) const
2087{
2088 const int32_t start = dataPosition();
2089 int32_t size;
2090 status_t status = readInt32(&size);
2091 pArg->reset();
2092
2093 if (status != OK || size < 0) {
2094 return status;
2095 }
2096
2097 setDataPosition(start);
2098 pArg->emplace();
2099
2100 status = readString16(&**pArg);
2101
2102 if (status != OK) {
2103 pArg->reset();
2104 }
2105
2106 return status;
2107}
2108
Casey Dahlinb9872622015-11-25 15:09:45 -08002109status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2110{
2111 const int32_t start = dataPosition();
2112 int32_t size;
2113 status_t status = readInt32(&size);
2114 pArg->reset();
2115
2116 if (status != OK || size < 0) {
2117 return status;
2118 }
2119
2120 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002121 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002122
2123 status = readString16(pArg->get());
2124
2125 if (status != OK) {
2126 pArg->reset();
2127 }
2128
2129 return status;
2130}
2131
Casey Dahlin451ff582015-10-19 18:12:18 -07002132status_t Parcel::readString16(String16* pArg) const
2133{
2134 size_t len;
2135 const char16_t* str = readString16Inplace(&len);
2136 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002137 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002138 return 0;
2139 } else {
2140 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002141 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002142 }
2143}
2144
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002145const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2146{
2147 int32_t size = readInt32();
2148 // watch for potential int overflow from size+1
2149 if (size >= 0 && size < INT32_MAX) {
2150 *outLen = size;
2151 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002152 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002153 return str;
2154 }
2155 }
2156 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002157 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002158}
2159
Casey Dahlinf0c13772015-10-27 18:33:56 -07002160status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2161{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002162 status_t status = readNullableStrongBinder(val);
2163 if (status == OK && !val->get()) {
2164 status = UNEXPECTED_NULL;
2165 }
2166 return status;
2167}
2168
2169status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2170{
Steven Morelanda86a3562019-08-01 23:28:34 +00002171 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002172}
2173
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002174sp<IBinder> Parcel::readStrongBinder() const
2175{
2176 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002177 // Note that a lot of code in Android reads binders by hand with this
2178 // method, and that code has historically been ok with getting nullptr
2179 // back (while ignoring error codes).
2180 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002181 return val;
2182}
2183
Christopher Wiley97f048d2015-11-19 06:49:05 -08002184status_t Parcel::readParcelable(Parcelable* parcelable) const {
2185 int32_t have_parcelable = 0;
2186 status_t status = readInt32(&have_parcelable);
2187 if (status != OK) {
2188 return status;
2189 }
2190 if (!have_parcelable) {
2191 return UNEXPECTED_NULL;
2192 }
2193 return parcelable->readFromParcel(this);
2194}
2195
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002196int32_t Parcel::readExceptionCode() const
2197{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002198 binder::Status status;
2199 status.readFromParcel(*this);
2200 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002201}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002202
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002203native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002204{
2205 int numFds, numInts;
2206 status_t err;
2207 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002208 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002209 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002210 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002211
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002212 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002213 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002214 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002215 }
2216
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002217 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002218 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002219 if (h->data[i] < 0) {
2220 for (int j = 0; j < i; j++) {
2221 close(h->data[j]);
2222 }
2223 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002224 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002225 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002226 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002227 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002228 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002229 native_handle_close(h);
2230 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002231 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002232 }
2233 return h;
2234}
2235
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002236int Parcel::readFileDescriptor() const
2237{
2238 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002239
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002240 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002241 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002242 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002243
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002244 return BAD_TYPE;
2245}
2246
Dianne Hackborn1941a402016-08-29 12:30:43 -07002247int Parcel::readParcelFileDescriptor() const
2248{
2249 int32_t hasComm = readInt32();
2250 int fd = readFileDescriptor();
2251 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002252 // detach (owned by the binder driver)
2253 int comm = readFileDescriptor();
2254
2255 // warning: this must be kept in sync with:
2256 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2257 enum ParcelFileDescriptorStatus {
2258 DETACHED = 2,
2259 };
2260
2261#if BYTE_ORDER == BIG_ENDIAN
2262 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2263#endif
2264#if BYTE_ORDER == LITTLE_ENDIAN
2265 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2266#endif
2267
2268 ssize_t written = TEMP_FAILURE_RETRY(
2269 ::write(comm, &message, sizeof(message)));
2270
2271 if (written == -1 || written != sizeof(message)) {
2272 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2273 written, strerror(errno));
2274 return BAD_TYPE;
2275 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002276 }
2277 return fd;
2278}
2279
Christopher Wiley2cf19952016-04-11 11:09:37 -07002280status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002281{
2282 int got = readFileDescriptor();
2283
2284 if (got == BAD_TYPE) {
2285 return BAD_TYPE;
2286 }
2287
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002288 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002289
2290 if (val->get() < 0) {
2291 return BAD_VALUE;
2292 }
2293
2294 return OK;
2295}
2296
Ryo Hashimotobf551892018-05-31 16:58:35 +09002297status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2298{
2299 int got = readParcelFileDescriptor();
2300
2301 if (got == BAD_TYPE) {
2302 return BAD_TYPE;
2303 }
2304
2305 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2306
2307 if (val->get() < 0) {
2308 return BAD_VALUE;
2309 }
2310
2311 return OK;
2312}
Casey Dahlin06673e32015-11-23 13:24:23 -08002313
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002314status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const {
2315 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2316}
2317
Christopher Wiley2cf19952016-04-11 11:09:37 -07002318status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002319 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2320}
2321
Christopher Wiley2cf19952016-04-11 11:09:37 -07002322status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002323 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2324}
2325
Jeff Brown5707dbf2011-09-23 21:17:56 -07002326status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2327{
Jeff Brown13b16042014-11-11 16:44:25 -08002328 int32_t blobType;
2329 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002330 if (status) return status;
2331
Jeff Brown13b16042014-11-11 16:44:25 -08002332 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002333 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002334 const void* ptr = readInplace(len);
2335 if (!ptr) return BAD_VALUE;
2336
Jeff Brown13b16042014-11-11 16:44:25 -08002337 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002338 return NO_ERROR;
2339 }
2340
Steve Block6807e592011-10-20 11:56:00 +01002341 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002342 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002343 int fd = readFileDescriptor();
2344 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2345
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002346 if (!ashmem_valid(fd)) {
2347 ALOGE("invalid fd");
2348 return BAD_VALUE;
2349 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002350 int size = ashmem_get_size_region(fd);
2351 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002352 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002353 return BAD_VALUE;
2354 }
Yi Kong91635562018-06-07 14:38:36 -07002355 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002356 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002357 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002358
Jeff Brown13b16042014-11-11 16:44:25 -08002359 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002360 return NO_ERROR;
2361}
2362
Mathias Agopiane1424282013-07-29 21:24:40 -07002363status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002364{
2365 // size
2366 const size_t len = this->readInt32();
2367 const size_t fd_count = this->readInt32();
2368
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002369 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002370 // don't accept size_t values which may have come from an
2371 // inadvertent conversion from a negative int.
2372 return BAD_VALUE;
2373 }
2374
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002375 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002376 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002377 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002378 return BAD_VALUE;
2379
Yi Kong91635562018-06-07 14:38:36 -07002380 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002381 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002382 fds = new (std::nothrow) int[fd_count];
2383 if (fds == nullptr) {
2384 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2385 return BAD_VALUE;
2386 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002387 }
2388
2389 status_t err = NO_ERROR;
2390 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002391 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002392 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002393 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002394 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 -07002395 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2396 // Close all the file descriptors that were dup-ed.
2397 for (size_t j=0; j<i ;j++) {
2398 close(fds[j]);
2399 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002400 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002401 }
2402
2403 if (err == NO_ERROR) {
2404 err = val.unflatten(buf, len, fds, fd_count);
2405 }
2406
2407 if (fd_count) {
2408 delete [] fds;
2409 }
2410
2411 return err;
2412}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002413const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2414{
2415 const size_t DPOS = mDataPos;
2416 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2417 const flat_binder_object* obj
2418 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2419 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002420 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002421 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002422 // the object list, so we don't want to check for it when
2423 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002424 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002425 return obj;
2426 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002427
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002428 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002429 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002430 const size_t N = mObjectsSize;
2431 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002432
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002433 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002434 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002436
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002437 // Start at the current hint position, looking for an object at
2438 // the current data position.
2439 if (opos < N) {
2440 while (opos < (N-1) && OBJS[opos] < DPOS) {
2441 opos++;
2442 }
2443 } else {
2444 opos = N-1;
2445 }
2446 if (OBJS[opos] == DPOS) {
2447 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002448 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002449 this, DPOS, opos);
2450 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002451 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002452 return obj;
2453 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002454
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002455 // Look backwards for it...
2456 while (opos > 0 && OBJS[opos] > DPOS) {
2457 opos--;
2458 }
2459 if (OBJS[opos] == DPOS) {
2460 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002461 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002462 this, DPOS, opos);
2463 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002464 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002465 return obj;
2466 }
2467 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002468 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 -07002469 this, DPOS);
2470 }
Yi Kong91635562018-06-07 14:38:36 -07002471 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002472}
2473
2474void Parcel::closeFileDescriptors()
2475{
2476 size_t i = mObjectsSize;
2477 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002478 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479 }
2480 while (i > 0) {
2481 i--;
2482 const flat_binder_object* flat
2483 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002484 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002485 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002486 close(flat->handle);
2487 }
2488 }
2489}
2490
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002491uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002492{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002493 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002494}
2495
2496size_t Parcel::ipcDataSize() const
2497{
2498 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2499}
2500
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002501uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002502{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002503 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002504}
2505
2506size_t Parcel::ipcObjectsCount() const
2507{
2508 return mObjectsSize;
2509}
2510
2511void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002512 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002514 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002515 freeDataNoInit();
2516 mError = NO_ERROR;
2517 mData = const_cast<uint8_t*>(data);
2518 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002519 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002520 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002521 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002522 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002523 mObjectsSize = mObjectsCapacity = objectsCount;
2524 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002525 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002526 mOwner = relFunc;
2527 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002528 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002529 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002530 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002531 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002532 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002533 mObjectsSize = 0;
2534 break;
2535 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002536 const flat_binder_object* flat
2537 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2538 uint32_t type = flat->hdr.type;
2539 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2540 type == BINDER_TYPE_FD)) {
2541 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2542 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2543 // recover gracefully by clearing out the objects, and releasing the objects we do
2544 // know about.
2545 android_errorWriteLog(0x534e4554, "135930648");
2546 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2547 __func__, type, (uint64_t)offset);
2548 releaseObjects();
2549 mObjectsSize = 0;
2550 break;
2551 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002552 minOffset = offset + sizeof(flat_binder_object);
2553 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002554 scanForFds();
2555}
2556
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002557void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002558{
2559 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002560
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002561 if (errorCheck() != NO_ERROR) {
2562 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002563 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002564 } else if (dataSize() > 0) {
2565 const uint8_t* DATA = data();
2566 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002567 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002568 const size_t N = objectsCount();
2569 for (size_t i=0; i<N; i++) {
2570 const flat_binder_object* flat
2571 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2572 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002573 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002574 << " = " << flat->binder;
2575 }
2576 } else {
2577 to << "NULL";
2578 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002579
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002580 to << ")";
2581}
2582
2583void Parcel::releaseObjects()
2584{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002585 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002586 if (i == 0) {
2587 return;
2588 }
2589 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002590 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002591 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002592 while (i > 0) {
2593 i--;
2594 const flat_binder_object* flat
2595 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002596 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002597 }
2598}
2599
2600void Parcel::acquireObjects()
2601{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002602 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002603 if (i == 0) {
2604 return;
2605 }
2606 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002607 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002608 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002609 while (i > 0) {
2610 i--;
2611 const flat_binder_object* flat
2612 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002613 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002614 }
2615}
2616
2617void Parcel::freeData()
2618{
2619 freeDataNoInit();
2620 initState();
2621}
2622
2623void Parcel::freeDataNoInit()
2624{
2625 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002626 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002627 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002628 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2629 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002630 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002631 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002632 if (mData) {
2633 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002634 gParcelGlobalAllocSize -= mDataCapacity;
2635 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002636 if (mDeallocZero) {
2637 zeroMemory(mData, mDataSize);
2638 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002639 free(mData);
2640 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002641 if (mObjects) free(mObjects);
2642 }
2643}
2644
2645status_t Parcel::growData(size_t len)
2646{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002647 if (len > INT32_MAX) {
2648 // don't accept size_t values which may have come from an
2649 // inadvertent conversion from a negative int.
2650 return BAD_VALUE;
2651 }
2652
Martijn Coenen93fe5182020-01-22 10:46:25 +01002653 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2654 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002655 size_t newSize = ((mDataSize+len)*3)/2;
2656 return (newSize <= mDataSize)
2657 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002658 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002659}
2660
Steven Morelandf183fdd2020-10-27 00:12:12 +00002661static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2662 if (!zero) {
2663 return (uint8_t*)realloc(data, newCapacity);
2664 }
2665 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2666 if (!newData) {
2667 return nullptr;
2668 }
2669
2670 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2671 zeroMemory(data, oldCapacity);
2672 free(data);
2673 return newData;
2674}
2675
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002676status_t Parcel::restartWrite(size_t desired)
2677{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002678 if (desired > INT32_MAX) {
2679 // don't accept size_t values which may have come from an
2680 // inadvertent conversion from a negative int.
2681 return BAD_VALUE;
2682 }
2683
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002684 if (mOwner) {
2685 freeData();
2686 return continueWrite(desired);
2687 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002688
Steven Morelandf183fdd2020-10-27 00:12:12 +00002689 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002690 if (!data && desired > mDataCapacity) {
2691 mError = NO_MEMORY;
2692 return NO_MEMORY;
2693 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002694
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002695 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002696
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002697 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002698 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002699 if (mDataCapacity > desired) {
2700 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2701 } else {
2702 gParcelGlobalAllocSize += (desired - mDataCapacity);
2703 }
2704
Colin Cross83ec65e2015-12-08 17:15:50 -08002705 if (!mData) {
2706 gParcelGlobalAllocCount++;
2707 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002708 mData = data;
2709 mDataCapacity = desired;
2710 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002711
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002712 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002713 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2714 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2715
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002716 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002717 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002718 mObjectsSize = mObjectsCapacity = 0;
2719 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002720 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002721 mHasFds = false;
2722 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002723 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002724
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002725 return NO_ERROR;
2726}
2727
2728status_t Parcel::continueWrite(size_t desired)
2729{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002730 if (desired > INT32_MAX) {
2731 // don't accept size_t values which may have come from an
2732 // inadvertent conversion from a negative int.
2733 return BAD_VALUE;
2734 }
2735
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002736 // If shrinking, first adjust for any objects that appear
2737 // after the new data size.
2738 size_t objectsSize = mObjectsSize;
2739 if (desired < mDataSize) {
2740 if (desired == 0) {
2741 objectsSize = 0;
2742 } else {
2743 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002744 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002745 break;
2746 objectsSize--;
2747 }
2748 }
2749 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002750
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002751 if (mOwner) {
2752 // If the size is going to zero, just release the owner's data.
2753 if (desired == 0) {
2754 freeData();
2755 return NO_ERROR;
2756 }
2757
2758 // If there is a different owner, we need to take
2759 // posession.
2760 uint8_t* data = (uint8_t*)malloc(desired);
2761 if (!data) {
2762 mError = NO_MEMORY;
2763 return NO_MEMORY;
2764 }
Yi Kong91635562018-06-07 14:38:36 -07002765 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002766
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002767 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002768 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002769 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002770 free(data);
2771
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002772 mError = NO_MEMORY;
2773 return NO_MEMORY;
2774 }
2775
2776 // Little hack to only acquire references on objects
2777 // we will be keeping.
2778 size_t oldObjectsSize = mObjectsSize;
2779 mObjectsSize = objectsSize;
2780 acquireObjects();
2781 mObjectsSize = oldObjectsSize;
2782 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002783
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002784 if (mData) {
2785 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2786 }
2787 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002788 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002789 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002790 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002791 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002792 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002793
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002794 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002795 gParcelGlobalAllocSize += desired;
2796 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002797
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002798 mData = data;
2799 mObjects = objects;
2800 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002801 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002802 mDataCapacity = desired;
2803 mObjectsSize = mObjectsCapacity = objectsSize;
2804 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002805 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002806
2807 } else if (mData) {
2808 if (objectsSize < mObjectsSize) {
2809 // Need to release refs on any objects we are dropping.
2810 const sp<ProcessState> proc(ProcessState::self());
2811 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2812 const flat_binder_object* flat
2813 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002814 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002815 // will need to rescan because we may have lopped off the only FDs
2816 mFdsKnown = false;
2817 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002818 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002819 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002820
2821 if (objectsSize == 0) {
2822 free(mObjects);
2823 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002824 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002825 } else {
2826 binder_size_t* objects =
2827 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2828 if (objects) {
2829 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002830 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002831 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002832 }
2833 mObjectsSize = objectsSize;
2834 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002835 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002836 }
2837
2838 // We own the data, so we can just do a realloc().
2839 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002840 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002841 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002842 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2843 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002844 gParcelGlobalAllocSize += desired;
2845 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002846 mData = data;
2847 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002848 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002849 mError = NO_MEMORY;
2850 return NO_MEMORY;
2851 }
2852 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002853 if (mDataSize > desired) {
2854 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002855 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002856 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002857 if (mDataPos > desired) {
2858 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002859 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002860 }
2861 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002862
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002863 } else {
2864 // This is the first data. Easy!
2865 uint8_t* data = (uint8_t*)malloc(desired);
2866 if (!data) {
2867 mError = NO_MEMORY;
2868 return NO_MEMORY;
2869 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002870
Yi Kong91635562018-06-07 14:38:36 -07002871 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002872 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002873 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002874 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002875
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002876 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002877 gParcelGlobalAllocSize += desired;
2878 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002879
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002880 mData = data;
2881 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002882 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2883 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002884 mDataCapacity = desired;
2885 }
2886
2887 return NO_ERROR;
2888}
2889
2890void Parcel::initState()
2891{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002892 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002893 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002894 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002895 mDataSize = 0;
2896 mDataCapacity = 0;
2897 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002898 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2899 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002900 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002901 mObjectsSize = 0;
2902 mObjectsCapacity = 0;
2903 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002904 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002905 mHasFds = false;
2906 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002907 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002908 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07002909 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002910 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002911 mWorkSourceRequestHeaderPosition = 0;
2912 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002913
2914 // racing multiple init leads only to multiple identical write
2915 if (gMaxFds == 0) {
2916 struct rlimit result;
2917 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2918 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002919 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002920 } else {
2921 ALOGW("Unable to getrlimit: %s", strerror(errno));
2922 gMaxFds = 1024;
2923 }
2924 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002925}
2926
2927void Parcel::scanForFds() const
2928{
2929 bool hasFds = false;
2930 for (size_t i=0; i<mObjectsSize; i++) {
2931 const flat_binder_object* flat
2932 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002933 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002934 hasFds = true;
2935 break;
2936 }
2937 }
2938 mHasFds = hasFds;
2939 mFdsKnown = true;
2940}
2941
Dan Sandleraa5c2342015-04-10 10:08:45 -04002942size_t Parcel::getBlobAshmemSize() const
2943{
Adrian Roos6bb31142015-10-22 16:46:12 -07002944 // This used to return the size of all blobs that were written to ashmem, now we're returning
2945 // the ashmem currently referenced by this Parcel, which should be equivalent.
2946 // TODO: Remove method once ABI can be changed.
2947 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002948}
2949
Adrian Rooscbf37262015-10-22 16:12:53 -07002950size_t Parcel::getOpenAshmemSize() const
2951{
2952 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002953}
2954
2955// --- Parcel::Blob ---
2956
2957Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002958 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002959}
2960
2961Parcel::Blob::~Blob() {
2962 release();
2963}
2964
2965void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002966 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002967 ::munmap(mData, mSize);
2968 }
2969 clear();
2970}
2971
Jeff Brown13b16042014-11-11 16:44:25 -08002972void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2973 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002974 mData = data;
2975 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002976 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002977}
2978
2979void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002980 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002981 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002982 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002983 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002984}
2985
Steven Moreland61ff8492019-09-26 16:05:45 -07002986} // namespace android