blob: 198c275a0976d0753aac1eeb2bd4639f8153a9d6 [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>
Mathias Agopian002e1e52013-05-06 20:20:50 -070044#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080045#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080047#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070048#include <utils/String8.h>
49#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070050
Mathias Agopian208059f2009-05-18 15:08:03 -070051#include <private/binder/binder_module.h>
Steven Morelanda4853cd2019-07-12 15:44:37 -070052#include "Static.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 Sharkey9de06a92020-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 Sharkey9de06a92020-09-11 12:07:10 -0600278 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800279}
280
281size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey9de06a92020-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 Coenenda2f2fd2020-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 Coenenda2f2fd2020-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
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000506void Parcel::updateWorkSourceRequestHeaderPosition() const {
507 // Only update the request headers once. We only want to point
508 // to the first headers read/written.
509 if (!mRequestHeaderPresent) {
510 mWorkSourceRequestHeaderPosition = dataPosition();
511 mRequestHeaderPresent = true;
512 }
513}
514
Jooyung Han1b228f42020-01-30 13:41:12 +0900515#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Moreland0f452742019-07-31 15:50:51 +0000516constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
517#else
518constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
519#endif
520
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700521// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700522status_t Parcel::writeInterfaceToken(const String16& interface)
523{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000524 const IPCThreadState* threadState = IPCThreadState::self();
525 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000526 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000527 writeInt32(threadState->shouldPropagateWorkSource() ?
528 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Moreland0f452742019-07-31 15:50:51 +0000529 writeInt32(kHeader);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700530 // currently the interface identification token is just its name as a string
531 return writeString16(interface);
532}
533
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000534bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
535{
536 if (!mRequestHeaderPresent) {
537 return false;
538 }
539
540 const size_t initialPosition = dataPosition();
541 setDataPosition(mWorkSourceRequestHeaderPosition);
542 status_t err = writeInt32(uid);
543 setDataPosition(initialPosition);
544 return err == NO_ERROR;
545}
546
Steven Moreland0891c9b2019-05-06 15:05:13 -0700547uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000548{
549 if (!mRequestHeaderPresent) {
550 return IPCThreadState::kUnsetWorkSource;
551 }
552
553 const size_t initialPosition = dataPosition();
554 setDataPosition(mWorkSourceRequestHeaderPosition);
555 uid_t uid = readInt32();
556 setDataPosition(initialPosition);
557 return uid;
558}
559
Mathias Agopian83c04462009-05-22 19:00:22 -0700560bool Parcel::checkInterface(IBinder* binder) const
561{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700562 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700563}
564
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700565bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700566 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700567{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700568 return enforceInterface(interface.string(), interface.size(), threadState);
569}
570
571bool Parcel::enforceInterface(const char16_t* interface,
572 size_t len,
573 IPCThreadState* threadState) const
574{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100575 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700576 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700577 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700578 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700579 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700580 if ((threadState->getLastTransactionBinderFlags() &
581 IBinder::FLAG_ONEWAY) != 0) {
582 // For one-way calls, the callee is running entirely
583 // disconnected from the caller, so disable StrictMode entirely.
584 // Not only does disk/network usage not impact the caller, but
585 // there's no way to commuicate back any violations anyway.
586 threadState->setStrictModePolicy(0);
587 } else {
588 threadState->setStrictModePolicy(strictPolicy);
589 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100590 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000591 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100592 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000593 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Moreland0f452742019-07-31 15:50:51 +0000594 // vendor header
595 int32_t header = readInt32();
596 if (header != kHeader) {
597 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
598 return false;
599 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100600 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700601 size_t parcel_interface_len;
602 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
603 if (len == parcel_interface_len &&
604 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700605 return true;
606 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700607 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700608 String8(interface, len).string(),
609 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700610 return false;
611 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700612}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700613
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700614size_t Parcel::objectsCount() const
615{
616 return mObjectsSize;
617}
618
619status_t Parcel::errorCheck() const
620{
621 return mError;
622}
623
624void Parcel::setError(status_t err)
625{
626 mError = err;
627}
628
629status_t Parcel::finishWrite(size_t len)
630{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700631 if (len > INT32_MAX) {
632 // don't accept size_t values which may have come from an
633 // inadvertent conversion from a negative int.
634 return BAD_VALUE;
635 }
636
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700637 //printf("Finish write of %d\n", len);
638 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700639 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700640 if (mDataPos > mDataSize) {
641 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700642 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700643 }
644 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
645 return NO_ERROR;
646}
647
648status_t Parcel::writeUnpadded(const void* data, size_t len)
649{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700650 if (len > INT32_MAX) {
651 // don't accept size_t values which may have come from an
652 // inadvertent conversion from a negative int.
653 return BAD_VALUE;
654 }
655
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700656 size_t end = mDataPos + len;
657 if (end < mDataPos) {
658 // integer overflow
659 return BAD_VALUE;
660 }
661
662 if (end <= mDataCapacity) {
663restart_write:
664 memcpy(mData+mDataPos, data, len);
665 return finishWrite(len);
666 }
667
668 status_t err = growData(len);
669 if (err == NO_ERROR) goto restart_write;
670 return err;
671}
672
673status_t Parcel::write(const void* data, size_t len)
674{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700675 if (len > INT32_MAX) {
676 // don't accept size_t values which may have come from an
677 // inadvertent conversion from a negative int.
678 return BAD_VALUE;
679 }
680
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700681 void* const d = writeInplace(len);
682 if (d) {
683 memcpy(d, data, len);
684 return NO_ERROR;
685 }
686 return mError;
687}
688
689void* Parcel::writeInplace(size_t len)
690{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700691 if (len > INT32_MAX) {
692 // don't accept size_t values which may have come from an
693 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700694 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700695 }
696
697 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700698
699 // sanity check for integer overflow
700 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700701 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700702 }
703
704 if ((mDataPos+padded) <= mDataCapacity) {
705restart_write:
706 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
707 uint8_t* const data = mData+mDataPos;
708
709 // Need to pad at end?
710 if (padded != len) {
711#if BYTE_ORDER == BIG_ENDIAN
712 static const uint32_t mask[4] = {
713 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
714 };
715#endif
716#if BYTE_ORDER == LITTLE_ENDIAN
717 static const uint32_t mask[4] = {
718 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
719 };
720#endif
721 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
722 // *reinterpret_cast<void**>(data+padded-4));
723 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
724 }
725
726 finishWrite(padded);
727 return data;
728 }
729
730 status_t err = growData(padded);
731 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700732 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700733}
734
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800735status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
736 const uint8_t* strData = (uint8_t*)str.data();
737 const size_t strLen= str.length();
738 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100739 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800740 return BAD_VALUE;
741 }
742
743 status_t err = writeInt32(utf16Len);
744 if (err) {
745 return err;
746 }
747
748 // Allocate enough bytes to hold our converted string and its terminating NULL.
749 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
750 if (!dst) {
751 return NO_MEMORY;
752 }
753
Sergio Girof4607432016-07-21 14:46:35 +0100754 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800755
756 return NO_ERROR;
757}
758
Jooyung Han2d5878e2020-01-23 12:45:10 +0900759status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) {
760 if (!str) {
761 return writeInt32(-1);
762 }
763 return writeUtf8AsUtf16(*str);
764}
765
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800766status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
767 if (!str) {
768 return writeInt32(-1);
769 }
770 return writeUtf8AsUtf16(*str);
771}
772
Daniel Normand0337ef2019-09-20 15:46:03 -0700773status_t Parcel::writeByteVectorInternal(const int8_t* data, size_t size) {
774 if (size > std::numeric_limits<int32_t>::max()) {
775 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -0700776 }
777
Daniel Normand0337ef2019-09-20 15:46:03 -0700778 status_t status = writeInt32(size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700779 if (status != OK) {
780 return status;
781 }
782
Daniel Normand0337ef2019-09-20 15:46:03 -0700783 return write(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700784}
785
Casey Dahlin185d3442016-02-09 11:08:35 -0800786status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700787 return writeByteVectorInternal(val.data(), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800788}
789
Jooyung Han2d5878e2020-01-23 12:45:10 +0900790status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val)
791{
792 if (!val) return writeInt32(-1);
793 return writeByteVectorInternal(val->data(), val->size());
794}
795
Casey Dahlin185d3442016-02-09 11:08:35 -0800796status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
797{
Daniel Normand0337ef2019-09-20 15:46:03 -0700798 if (!val) return writeInt32(-1);
799 return writeByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800800}
801
802status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700803 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val.data()), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800804}
805
Jooyung Han2d5878e2020-01-23 12:45:10 +0900806status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val)
807{
808 if (!val) return writeInt32(-1);
809 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
810}
811
Casey Dahlin185d3442016-02-09 11:08:35 -0800812status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
813{
Daniel Normand0337ef2019-09-20 15:46:03 -0700814 if (!val) return writeInt32(-1);
815 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800816}
817
Casey Dahlin451ff582015-10-19 18:12:18 -0700818status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
819{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800820 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700821}
822
Jooyung Han2d5878e2020-01-23 12:45:10 +0900823status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val)
824{
825 return writeNullableTypedVector(val, &Parcel::writeInt32);
826}
827
Casey Dahlinb9872622015-11-25 15:09:45 -0800828status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
829{
830 return writeNullableTypedVector(val, &Parcel::writeInt32);
831}
832
Casey Dahlin451ff582015-10-19 18:12:18 -0700833status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
834{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800835 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700836}
837
Jooyung Han2d5878e2020-01-23 12:45:10 +0900838status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val)
839{
840 return writeNullableTypedVector(val, &Parcel::writeInt64);
841}
842
Casey Dahlinb9872622015-11-25 15:09:45 -0800843status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
844{
845 return writeNullableTypedVector(val, &Parcel::writeInt64);
846}
847
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800848status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
849{
850 return writeTypedVector(val, &Parcel::writeUint64);
851}
852
Jooyung Han2d5878e2020-01-23 12:45:10 +0900853status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val)
854{
855 return writeNullableTypedVector(val, &Parcel::writeUint64);
856}
857
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800858status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
859{
860 return writeNullableTypedVector(val, &Parcel::writeUint64);
861}
862
Casey Dahlin451ff582015-10-19 18:12:18 -0700863status_t Parcel::writeFloatVector(const std::vector<float>& val)
864{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800865 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700866}
867
Jooyung Han2d5878e2020-01-23 12:45:10 +0900868status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val)
869{
870 return writeNullableTypedVector(val, &Parcel::writeFloat);
871}
872
Casey Dahlinb9872622015-11-25 15:09:45 -0800873status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
874{
875 return writeNullableTypedVector(val, &Parcel::writeFloat);
876}
877
Casey Dahlin451ff582015-10-19 18:12:18 -0700878status_t Parcel::writeDoubleVector(const std::vector<double>& val)
879{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800880 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700881}
882
Jooyung Han2d5878e2020-01-23 12:45:10 +0900883status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val)
884{
885 return writeNullableTypedVector(val, &Parcel::writeDouble);
886}
887
Casey Dahlinb9872622015-11-25 15:09:45 -0800888status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
889{
890 return writeNullableTypedVector(val, &Parcel::writeDouble);
891}
892
Casey Dahlin451ff582015-10-19 18:12:18 -0700893status_t Parcel::writeBoolVector(const std::vector<bool>& val)
894{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800895 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700896}
897
Jooyung Han2d5878e2020-01-23 12:45:10 +0900898status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val)
899{
900 return writeNullableTypedVector(val, &Parcel::writeBool);
901}
902
Casey Dahlinb9872622015-11-25 15:09:45 -0800903status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
904{
905 return writeNullableTypedVector(val, &Parcel::writeBool);
906}
907
Casey Dahlin451ff582015-10-19 18:12:18 -0700908status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
909{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800910 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700911}
912
Jooyung Han2d5878e2020-01-23 12:45:10 +0900913status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val)
914{
915 return writeNullableTypedVector(val, &Parcel::writeChar);
916}
917
Casey Dahlinb9872622015-11-25 15:09:45 -0800918status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
919{
920 return writeNullableTypedVector(val, &Parcel::writeChar);
921}
922
Casey Dahlin451ff582015-10-19 18:12:18 -0700923status_t Parcel::writeString16Vector(const std::vector<String16>& val)
924{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800925 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700926}
927
Casey Dahlinb9872622015-11-25 15:09:45 -0800928status_t Parcel::writeString16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +0900929 const std::optional<std::vector<std::optional<String16>>>& val)
930{
931 return writeNullableTypedVector(val, &Parcel::writeString16);
932}
933
934status_t Parcel::writeString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800935 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
936{
937 return writeNullableTypedVector(val, &Parcel::writeString16);
938}
939
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800940status_t Parcel::writeUtf8VectorAsUtf16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +0900941 const std::optional<std::vector<std::optional<std::string>>>& val) {
942 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
943}
944
945status_t Parcel::writeUtf8VectorAsUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800946 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
947 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
948}
949
950status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
951 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
952}
953
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700954status_t Parcel::writeInt32(int32_t val)
955{
Andreas Huber84a6d042009-08-17 13:33:27 -0700956 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700957}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800958
959status_t Parcel::writeUint32(uint32_t val)
960{
961 return writeAligned(val);
962}
963
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700964status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700965 if (len > INT32_MAX) {
966 // don't accept size_t values which may have come from an
967 // inadvertent conversion from a negative int.
968 return BAD_VALUE;
969 }
970
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700971 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700972 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700973 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700974 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700975 if (ret == NO_ERROR) {
976 ret = write(val, len * sizeof(*val));
977 }
978 return ret;
979}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700980status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700981 if (len > INT32_MAX) {
982 // don't accept size_t values which may have come from an
983 // inadvertent conversion from a negative int.
984 return BAD_VALUE;
985 }
986
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700987 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700988 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700989 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700990 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700991 if (ret == NO_ERROR) {
992 ret = write(val, len * sizeof(*val));
993 }
994 return ret;
995}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700996
Casey Dahlind6848f52015-10-15 15:44:59 -0700997status_t Parcel::writeBool(bool val)
998{
999 return writeInt32(int32_t(val));
1000}
1001
1002status_t Parcel::writeChar(char16_t val)
1003{
1004 return writeInt32(int32_t(val));
1005}
1006
1007status_t Parcel::writeByte(int8_t val)
1008{
1009 return writeInt32(int32_t(val));
1010}
1011
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001012status_t Parcel::writeInt64(int64_t val)
1013{
Andreas Huber84a6d042009-08-17 13:33:27 -07001014 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001015}
1016
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001017status_t Parcel::writeUint64(uint64_t val)
1018{
1019 return writeAligned(val);
1020}
1021
Serban Constantinescuf683e012013-11-05 16:53:55 +00001022status_t Parcel::writePointer(uintptr_t val)
1023{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001024 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001025}
1026
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001027status_t Parcel::writeFloat(float val)
1028{
Andreas Huber84a6d042009-08-17 13:33:27 -07001029 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001030}
1031
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001032#if defined(__mips__) && defined(__mips_hard_float)
1033
1034status_t Parcel::writeDouble(double val)
1035{
1036 union {
1037 double d;
1038 unsigned long long ll;
1039 } u;
1040 u.d = val;
1041 return writeAligned(u.ll);
1042}
1043
1044#else
1045
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001046status_t Parcel::writeDouble(double val)
1047{
Andreas Huber84a6d042009-08-17 13:33:27 -07001048 return writeAligned(val);
1049}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001050
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001051#endif
1052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001053status_t Parcel::writeCString(const char* str)
1054{
1055 return write(str, strlen(str)+1);
1056}
1057
1058status_t Parcel::writeString8(const String8& str)
1059{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001060 return writeString8(str.string(), str.size());
1061}
1062
1063status_t Parcel::writeString8(const char* str, size_t len)
1064{
1065 if (str == nullptr) return writeInt32(-1);
1066
1067 status_t err = writeInt32(len);
1068 if (err == NO_ERROR) {
1069 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1070 if (data) {
1071 memcpy(data, str, len);
1072 *reinterpret_cast<char*>(data+len) = 0;
1073 return NO_ERROR;
1074 }
1075 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001076 }
1077 return err;
1078}
1079
Jooyung Han2d5878e2020-01-23 12:45:10 +09001080status_t Parcel::writeString16(const std::optional<String16>& str)
1081{
1082 if (!str) {
1083 return writeInt32(-1);
1084 }
1085
1086 return writeString16(*str);
1087}
1088
Casey Dahlinb9872622015-11-25 15:09:45 -08001089status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1090{
1091 if (!str) {
1092 return writeInt32(-1);
1093 }
1094
1095 return writeString16(*str);
1096}
1097
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001098status_t Parcel::writeString16(const String16& str)
1099{
1100 return writeString16(str.string(), str.size());
1101}
1102
1103status_t Parcel::writeString16(const char16_t* str, size_t len)
1104{
Yi Kong91635562018-06-07 14:38:36 -07001105 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001106
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001107 status_t err = writeInt32(len);
1108 if (err == NO_ERROR) {
1109 len *= sizeof(char16_t);
1110 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1111 if (data) {
1112 memcpy(data, str, len);
1113 *reinterpret_cast<char16_t*>(data+len) = 0;
1114 return NO_ERROR;
1115 }
1116 err = mError;
1117 }
1118 return err;
1119}
1120
1121status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1122{
Steven Morelanda86a3562019-08-01 23:28:34 +00001123 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001124}
1125
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001126status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1127{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001128 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001129}
1130
Jooyung Han2d5878e2020-01-23 12:45:10 +09001131status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val)
1132{
1133 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1134}
1135
Casey Dahlinb9872622015-11-25 15:09:45 -08001136status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1137{
1138 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1139}
1140
Jooyung Han2d5878e2020-01-23 12:45:10 +09001141status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const {
1142 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
1143}
1144
Casey Dahlinb9872622015-11-25 15:09:45 -08001145status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001146 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001147}
1148
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001149status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001150 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001151}
1152
Casey Dahlinb9872622015-11-25 15:09:45 -08001153status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1154 if (!parcelable) {
1155 return writeInt32(0);
1156 }
1157
1158 return writeParcelable(*parcelable);
1159}
1160
Christopher Wiley97f048d2015-11-19 06:49:05 -08001161status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1162 status_t status = writeInt32(1); // parcelable is not null.
1163 if (status != OK) {
1164 return status;
1165 }
1166 return parcelable.writeToParcel(this);
1167}
1168
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001169status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001170{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001171 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001172 return BAD_TYPE;
1173
1174 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001175 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001176 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001178 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001179 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001181 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1182 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183
1184 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001185 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186 return err;
1187 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001188 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001189 return err;
1190}
1191
Jeff Brown93ff1f92011-11-04 19:01:44 -07001192status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001193{
1194 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001195 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001196 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001197 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001198 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001199 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001200 return writeObject(obj, true);
1201}
1202
1203status_t Parcel::writeDupFileDescriptor(int fd)
1204{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001205 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001206 if (dupFd < 0) {
1207 return -errno;
1208 }
1209 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001210 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001211 close(dupFd);
1212 }
1213 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001214}
1215
Dianne Hackborn1941a402016-08-29 12:30:43 -07001216status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1217{
1218 writeInt32(0);
1219 return writeFileDescriptor(fd, takeOwnership);
1220}
1221
Ryo Hashimotobf551892018-05-31 16:58:35 +09001222status_t Parcel::writeDupParcelFileDescriptor(int fd)
1223{
1224 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1225 if (dupFd < 0) {
1226 return -errno;
1227 }
1228 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1229 if (err != OK) {
1230 close(dupFd);
1231 }
1232 return err;
1233}
1234
Christopher Wiley2cf19952016-04-11 11:09:37 -07001235status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001236 return writeDupFileDescriptor(fd.get());
1237}
1238
Christopher Wiley2cf19952016-04-11 11:09:37 -07001239status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001240 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1241}
1242
Jooyung Han2d5878e2020-01-23 12:45:10 +09001243status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) {
1244 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1245}
1246
Christopher Wiley2cf19952016-04-11 11:09:37 -07001247status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001248 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1249}
1250
Jeff Brown13b16042014-11-11 16:44:25 -08001251status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001252{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001253 if (len > INT32_MAX) {
1254 // don't accept size_t values which may have come from an
1255 // inadvertent conversion from a negative int.
1256 return BAD_VALUE;
1257 }
1258
Jeff Brown13b16042014-11-11 16:44:25 -08001259 status_t status;
1260 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001261 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001262 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001263 if (status) return status;
1264
1265 void* ptr = writeInplace(len);
1266 if (!ptr) return NO_MEMORY;
1267
Jeff Brown13b16042014-11-11 16:44:25 -08001268 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001269 return NO_ERROR;
1270 }
1271
Steve Block6807e592011-10-20 11:56:00 +01001272 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001273 int fd = ashmem_create_region("Parcel Blob", len);
1274 if (fd < 0) return NO_MEMORY;
1275
1276 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1277 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001278 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001279 } else {
Yi Kong91635562018-06-07 14:38:36 -07001280 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001281 if (ptr == MAP_FAILED) {
1282 status = -errno;
1283 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001284 if (!mutableCopy) {
1285 result = ashmem_set_prot_region(fd, PROT_READ);
1286 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001287 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001288 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001289 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001290 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001291 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001292 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001293 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001294 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001295 return NO_ERROR;
1296 }
1297 }
1298 }
1299 }
1300 ::munmap(ptr, len);
1301 }
1302 ::close(fd);
1303 return status;
1304}
1305
Jeff Brown13b16042014-11-11 16:44:25 -08001306status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1307{
1308 // Must match up with what's done in writeBlob.
1309 if (!mAllowFds) return FDS_NOT_ALLOWED;
1310 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1311 if (status) return status;
1312 return writeDupFileDescriptor(fd);
1313}
1314
Mathias Agopiane1424282013-07-29 21:24:40 -07001315status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001316{
1317 status_t err;
1318
1319 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001320 const size_t len = val.getFlattenedSize();
1321 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001322
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001323 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001324 // don't accept size_t values which may have come from an
1325 // inadvertent conversion from a negative int.
1326 return BAD_VALUE;
1327 }
1328
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001329 err = this->writeInt32(len);
1330 if (err) return err;
1331
1332 err = this->writeInt32(fd_count);
1333 if (err) return err;
1334
1335 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001336 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001337 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001338 return BAD_VALUE;
1339
Yi Kong91635562018-06-07 14:38:36 -07001340 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001341 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001342 fds = new (std::nothrow) int[fd_count];
1343 if (fds == nullptr) {
1344 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1345 return BAD_VALUE;
1346 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001347 }
1348
1349 err = val.flatten(buf, len, fds, fd_count);
1350 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1351 err = this->writeDupFileDescriptor( fds[i] );
1352 }
1353
1354 if (fd_count) {
1355 delete [] fds;
1356 }
1357
1358 return err;
1359}
1360
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001361status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1362{
1363 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1364 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1365 if (enoughData && enoughObjects) {
1366restart_write:
1367 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001368
Christopher Tate98e67d32015-06-03 18:44:15 -07001369 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001370 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001371 if (!mAllowFds) {
1372 // fail before modifying our object index
1373 return FDS_NOT_ALLOWED;
1374 }
1375 mHasFds = mFdsKnown = true;
1376 }
1377
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001378 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001379 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001380 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001381 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001382 mObjectsSize++;
1383 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001384
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001385 return finishWrite(sizeof(flat_binder_object));
1386 }
1387
1388 if (!enoughData) {
1389 const status_t err = growData(sizeof(val));
1390 if (err != NO_ERROR) return err;
1391 }
1392 if (!enoughObjects) {
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01001393 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1394 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001395 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01001396 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001397 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001398 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001399 mObjects = objects;
1400 mObjectsCapacity = newSize;
1401 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001402
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001403 goto restart_write;
1404}
1405
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001406status_t Parcel::writeNoException()
1407{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001408 binder::Status status;
1409 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001410}
1411
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001412status_t Parcel::validateReadData(size_t upperBound) const
1413{
1414 // Don't allow non-object reads on object data
1415 if (mObjectsSorted || mObjectsSize <= 1) {
1416data_sorted:
1417 // Expect to check only against the next object
1418 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1419 // For some reason the current read position is greater than the next object
1420 // hint. Iterate until we find the right object
1421 size_t nextObject = mNextObjectHint;
1422 do {
1423 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1424 // Requested info overlaps with an object
1425 ALOGE("Attempt to read from protected data in Parcel %p", this);
1426 return PERMISSION_DENIED;
1427 }
1428 nextObject++;
1429 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1430 mNextObjectHint = nextObject;
1431 }
1432 return NO_ERROR;
1433 }
1434 // Quickly determine if mObjects is sorted.
1435 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1436 binder_size_t* prevObj = currObj;
1437 while (currObj > mObjects) {
1438 prevObj--;
1439 if(*prevObj > *currObj) {
1440 goto data_unsorted;
1441 }
1442 currObj--;
1443 }
1444 mObjectsSorted = true;
1445 goto data_sorted;
1446
1447data_unsorted:
1448 // Insertion Sort mObjects
1449 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1450 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1451 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1452 binder_size_t temp = *iter0;
1453 binder_size_t* iter1 = iter0 - 1;
1454 while (iter1 >= mObjects && *iter1 > temp) {
1455 *(iter1 + 1) = *iter1;
1456 iter1--;
1457 }
1458 *(iter1 + 1) = temp;
1459 }
1460 mNextObjectHint = 0;
1461 mObjectsSorted = true;
1462 goto data_sorted;
1463}
1464
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001465status_t Parcel::read(void* outData, size_t len) const
1466{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001467 if (len > INT32_MAX) {
1468 // don't accept size_t values which may have come from an
1469 // inadvertent conversion from a negative int.
1470 return BAD_VALUE;
1471 }
1472
1473 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1474 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001475 if (mObjectsSize > 0) {
1476 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001477 if(err != NO_ERROR) {
1478 // Still increment the data position by the expected length
1479 mDataPos += pad_size(len);
1480 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1481 return err;
1482 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001483 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001484 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001485 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001486 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001487 return NO_ERROR;
1488 }
1489 return NOT_ENOUGH_DATA;
1490}
1491
1492const void* Parcel::readInplace(size_t len) const
1493{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001494 if (len > INT32_MAX) {
1495 // don't accept size_t values which may have come from an
1496 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001497 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001498 }
1499
1500 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1501 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001502 if (mObjectsSize > 0) {
1503 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001504 if(err != NO_ERROR) {
1505 // Still increment the data position by the expected length
1506 mDataPos += pad_size(len);
1507 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001508 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001509 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001510 }
1511
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001512 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001513 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001514 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001515 return data;
1516 }
Yi Kong91635562018-06-07 14:38:36 -07001517 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001518}
1519
Andreas Huber84a6d042009-08-17 13:33:27 -07001520template<class T>
1521status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001522 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001523
1524 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001525 if (mObjectsSize > 0) {
1526 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001527 if(err != NO_ERROR) {
1528 // Still increment the data position by the expected length
1529 mDataPos += sizeof(T);
1530 return err;
1531 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001532 }
1533
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001534 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001535 mDataPos += sizeof(T);
1536 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001537 return NO_ERROR;
1538 } else {
1539 return NOT_ENOUGH_DATA;
1540 }
1541}
1542
Andreas Huber84a6d042009-08-17 13:33:27 -07001543template<class T>
1544T Parcel::readAligned() const {
1545 T result;
1546 if (readAligned(&result) != NO_ERROR) {
1547 result = 0;
1548 }
1549
1550 return result;
1551}
1552
1553template<class T>
1554status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001555 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001556
1557 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1558restart_write:
1559 *reinterpret_cast<T*>(mData+mDataPos) = val;
1560 return finishWrite(sizeof(val));
1561 }
1562
1563 status_t err = growData(sizeof(val));
1564 if (err == NO_ERROR) goto restart_write;
1565 return err;
1566}
1567
Casey Dahlin185d3442016-02-09 11:08:35 -08001568status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001569 size_t size;
1570 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1571 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001572}
1573
1574status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001575 size_t size;
1576 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1577 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001578}
1579
Jooyung Han2d5878e2020-01-23 12:45:10 +09001580status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const {
1581 size_t size;
1582 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1583 if (!*val) {
1584 // reserveOutVector does not create the out vector if size is < 0.
1585 // This occurs when writing a null byte vector.
1586 return OK;
1587 }
1588 return readByteVectorInternal(&**val, size);
1589}
1590
Casey Dahlin185d3442016-02-09 11:08:35 -08001591status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001592 size_t size;
1593 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001594 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001595 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001596 // This occurs when writing a null byte vector.
1597 return OK;
1598 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001599 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001600}
1601
Jooyung Han2d5878e2020-01-23 12:45:10 +09001602status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const {
1603 size_t size;
1604 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1605 if (!*val) {
1606 // reserveOutVector does not create the out vector if size is < 0.
1607 // This occurs when writing a null byte vector.
1608 return OK;
1609 }
1610 return readByteVectorInternal(&**val, size);
1611}
1612
Casey Dahlin185d3442016-02-09 11:08:35 -08001613status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001614 size_t size;
1615 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001616 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001617 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001618 // This occurs when writing a null byte vector.
1619 return OK;
1620 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001621 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001622}
1623
Jooyung Han2d5878e2020-01-23 12:45:10 +09001624status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const {
1625 return readNullableTypedVector(val, &Parcel::readInt32);
1626}
1627
Casey Dahlinb9872622015-11-25 15:09:45 -08001628status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1629 return readNullableTypedVector(val, &Parcel::readInt32);
1630}
1631
Casey Dahlin451ff582015-10-19 18:12:18 -07001632status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001633 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001634}
1635
Jooyung Han2d5878e2020-01-23 12:45:10 +09001636status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const {
1637 return readNullableTypedVector(val, &Parcel::readInt64);
1638}
1639
Casey Dahlinb9872622015-11-25 15:09:45 -08001640status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1641 return readNullableTypedVector(val, &Parcel::readInt64);
1642}
1643
Casey Dahlin451ff582015-10-19 18:12:18 -07001644status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001645 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001646}
1647
Jooyung Han2d5878e2020-01-23 12:45:10 +09001648status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const {
1649 return readNullableTypedVector(val, &Parcel::readUint64);
1650}
1651
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001652status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1653 return readNullableTypedVector(val, &Parcel::readUint64);
1654}
1655
1656status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1657 return readTypedVector(val, &Parcel::readUint64);
1658}
1659
Jooyung Han2d5878e2020-01-23 12:45:10 +09001660status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const {
1661 return readNullableTypedVector(val, &Parcel::readFloat);
1662}
1663
Casey Dahlinb9872622015-11-25 15:09:45 -08001664status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1665 return readNullableTypedVector(val, &Parcel::readFloat);
1666}
1667
Casey Dahlin451ff582015-10-19 18:12:18 -07001668status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001669 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001670}
1671
Jooyung Han2d5878e2020-01-23 12:45:10 +09001672status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const {
1673 return readNullableTypedVector(val, &Parcel::readDouble);
1674}
1675
Casey Dahlinb9872622015-11-25 15:09:45 -08001676status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1677 return readNullableTypedVector(val, &Parcel::readDouble);
1678}
1679
Casey Dahlin451ff582015-10-19 18:12:18 -07001680status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001681 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001682}
1683
Jooyung Han2d5878e2020-01-23 12:45:10 +09001684status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const {
1685 const int32_t start = dataPosition();
1686 int32_t size;
1687 status_t status = readInt32(&size);
1688 val->reset();
1689
1690 if (status != OK || size < 0) {
1691 return status;
1692 }
1693
1694 setDataPosition(start);
1695 val->emplace();
1696
1697 status = readBoolVector(&**val);
1698
1699 if (status != OK) {
1700 val->reset();
1701 }
1702
1703 return status;
1704}
1705
Casey Dahlinb9872622015-11-25 15:09:45 -08001706status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1707 const int32_t start = dataPosition();
1708 int32_t size;
1709 status_t status = readInt32(&size);
1710 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001711
Casey Dahlinb9872622015-11-25 15:09:45 -08001712 if (status != OK || size < 0) {
1713 return status;
1714 }
1715
1716 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001717 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001718
1719 status = readBoolVector(val->get());
1720
1721 if (status != OK) {
1722 val->reset();
1723 }
1724
1725 return status;
1726}
1727
1728status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001729 int32_t size;
1730 status_t status = readInt32(&size);
1731
1732 if (status != OK) {
1733 return status;
1734 }
1735
1736 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001737 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001738 }
1739
1740 val->resize(size);
1741
1742 /* C++ bool handling means a vector of bools isn't necessarily addressable
1743 * (we might use individual bits)
1744 */
Christopher Wiley97887982015-10-27 16:33:47 -07001745 bool data;
1746 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001747 status = readBool(&data);
1748 (*val)[i] = data;
1749
1750 if (status != OK) {
1751 return status;
1752 }
1753 }
1754
1755 return OK;
1756}
1757
Jooyung Han2d5878e2020-01-23 12:45:10 +09001758status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const {
1759 return readNullableTypedVector(val, &Parcel::readChar);
1760}
1761
Casey Dahlinb9872622015-11-25 15:09:45 -08001762status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1763 return readNullableTypedVector(val, &Parcel::readChar);
1764}
1765
Casey Dahlin451ff582015-10-19 18:12:18 -07001766status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001767 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001768}
1769
Casey Dahlinb9872622015-11-25 15:09:45 -08001770status_t Parcel::readString16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +09001771 std::optional<std::vector<std::optional<String16>>>* val) const {
1772 return readNullableTypedVector(val, &Parcel::readString16);
1773}
1774
1775status_t Parcel::readString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -08001776 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1777 return readNullableTypedVector(val, &Parcel::readString16);
1778}
1779
Casey Dahlin451ff582015-10-19 18:12:18 -07001780status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001781 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001782}
1783
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001784status_t Parcel::readUtf8VectorFromUtf16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +09001785 std::optional<std::vector<std::optional<std::string>>>* val) const {
1786 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1787}
1788
1789status_t Parcel::readUtf8VectorFromUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001790 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1791 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1792}
1793
1794status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1795 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1796}
Casey Dahlin451ff582015-10-19 18:12:18 -07001797
Andreas Huber84a6d042009-08-17 13:33:27 -07001798status_t Parcel::readInt32(int32_t *pArg) const
1799{
1800 return readAligned(pArg);
1801}
1802
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001803int32_t Parcel::readInt32() const
1804{
Andreas Huber84a6d042009-08-17 13:33:27 -07001805 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001806}
1807
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001808status_t Parcel::readUint32(uint32_t *pArg) const
1809{
1810 return readAligned(pArg);
1811}
1812
1813uint32_t Parcel::readUint32() const
1814{
1815 return readAligned<uint32_t>();
1816}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001817
1818status_t Parcel::readInt64(int64_t *pArg) const
1819{
Andreas Huber84a6d042009-08-17 13:33:27 -07001820 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001821}
1822
1823
1824int64_t Parcel::readInt64() const
1825{
Andreas Huber84a6d042009-08-17 13:33:27 -07001826 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001827}
1828
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001829status_t Parcel::readUint64(uint64_t *pArg) const
1830{
1831 return readAligned(pArg);
1832}
1833
1834uint64_t Parcel::readUint64() const
1835{
1836 return readAligned<uint64_t>();
1837}
1838
Serban Constantinescuf683e012013-11-05 16:53:55 +00001839status_t Parcel::readPointer(uintptr_t *pArg) const
1840{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001841 status_t ret;
1842 binder_uintptr_t ptr;
1843 ret = readAligned(&ptr);
1844 if (!ret)
1845 *pArg = ptr;
1846 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001847}
1848
1849uintptr_t Parcel::readPointer() const
1850{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001851 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001852}
1853
1854
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001855status_t Parcel::readFloat(float *pArg) const
1856{
Andreas Huber84a6d042009-08-17 13:33:27 -07001857 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001858}
1859
1860
1861float Parcel::readFloat() const
1862{
Andreas Huber84a6d042009-08-17 13:33:27 -07001863 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001864}
1865
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001866#if defined(__mips__) && defined(__mips_hard_float)
1867
1868status_t Parcel::readDouble(double *pArg) const
1869{
1870 union {
1871 double d;
1872 unsigned long long ll;
1873 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001874 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001875 status_t status;
1876 status = readAligned(&u.ll);
1877 *pArg = u.d;
1878 return status;
1879}
1880
1881double Parcel::readDouble() const
1882{
1883 union {
1884 double d;
1885 unsigned long long ll;
1886 } u;
1887 u.ll = readAligned<unsigned long long>();
1888 return u.d;
1889}
1890
1891#else
1892
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001893status_t Parcel::readDouble(double *pArg) const
1894{
Andreas Huber84a6d042009-08-17 13:33:27 -07001895 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001896}
1897
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001898double Parcel::readDouble() const
1899{
Andreas Huber84a6d042009-08-17 13:33:27 -07001900 return readAligned<double>();
1901}
1902
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001903#endif
1904
Andreas Huber84a6d042009-08-17 13:33:27 -07001905status_t Parcel::readIntPtr(intptr_t *pArg) const
1906{
1907 return readAligned(pArg);
1908}
1909
1910
1911intptr_t Parcel::readIntPtr() const
1912{
1913 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001914}
1915
Casey Dahlind6848f52015-10-15 15:44:59 -07001916status_t Parcel::readBool(bool *pArg) const
1917{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001918 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001919 status_t ret = readInt32(&tmp);
1920 *pArg = (tmp != 0);
1921 return ret;
1922}
1923
1924bool Parcel::readBool() const
1925{
1926 return readInt32() != 0;
1927}
1928
1929status_t Parcel::readChar(char16_t *pArg) const
1930{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001931 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001932 status_t ret = readInt32(&tmp);
1933 *pArg = char16_t(tmp);
1934 return ret;
1935}
1936
1937char16_t Parcel::readChar() const
1938{
1939 return char16_t(readInt32());
1940}
1941
1942status_t Parcel::readByte(int8_t *pArg) const
1943{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001944 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001945 status_t ret = readInt32(&tmp);
1946 *pArg = int8_t(tmp);
1947 return ret;
1948}
1949
1950int8_t Parcel::readByte() const
1951{
1952 return int8_t(readInt32());
1953}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001954
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001955status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1956 size_t utf16Size = 0;
1957 const char16_t* src = readString16Inplace(&utf16Size);
1958 if (!src) {
1959 return UNEXPECTED_NULL;
1960 }
1961
1962 // Save ourselves the trouble, we're done.
1963 if (utf16Size == 0u) {
1964 str->clear();
1965 return NO_ERROR;
1966 }
1967
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001968 // Allow for closing '\0'
1969 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1970 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001971 return BAD_VALUE;
1972 }
1973 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001974 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001975 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001976 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1977 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001978 return NO_ERROR;
1979}
1980
Jooyung Han2d5878e2020-01-23 12:45:10 +09001981status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const {
1982 const int32_t start = dataPosition();
1983 int32_t size;
1984 status_t status = readInt32(&size);
1985 str->reset();
1986
1987 if (status != OK || size < 0) {
1988 return status;
1989 }
1990
1991 setDataPosition(start);
1992 str->emplace();
1993 return readUtf8FromUtf16(&**str);
1994}
1995
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001996status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1997 const int32_t start = dataPosition();
1998 int32_t size;
1999 status_t status = readInt32(&size);
2000 str->reset();
2001
2002 if (status != OK || size < 0) {
2003 return status;
2004 }
2005
2006 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002007 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002008 return readUtf8FromUtf16(str->get());
2009}
2010
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002011const char* Parcel::readCString() const
2012{
Steven Morelandf5e6c7e2019-05-17 13:14:06 -07002013 if (mDataPos < mDataSize) {
2014 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002015 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2016 // is the string's trailing NUL within the parcel's valid bounds?
2017 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2018 if (eos) {
2019 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002020 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002021 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002022 return str;
2023 }
2024 }
Yi Kong91635562018-06-07 14:38:36 -07002025 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002026}
2027
2028String8 Parcel::readString8() const
2029{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002030 size_t len;
2031 const char* str = readString8Inplace(&len);
2032 if (str) return String8(str, len);
2033 ALOGE("Reading a NULL string not supported here.");
2034 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002035}
2036
2037status_t Parcel::readString8(String8* pArg) const
2038{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002039 size_t len;
2040 const char* str = readString8Inplace(&len);
2041 if (str) {
2042 pArg->setTo(str, len);
2043 return 0;
2044 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002045 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002046 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002047 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002048}
2049
2050const char* Parcel::readString8Inplace(size_t* outLen) const
2051{
2052 int32_t size = readInt32();
2053 // watch for potential int overflow from size+1
2054 if (size >= 0 && size < INT32_MAX) {
2055 *outLen = size;
2056 const char* str = (const char*)readInplace(size+1);
2057 if (str != nullptr) {
2058 return str;
2059 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002060 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002061 *outLen = 0;
2062 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002063}
2064
2065String16 Parcel::readString16() const
2066{
2067 size_t len;
2068 const char16_t* str = readString16Inplace(&len);
2069 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002070 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002071 return String16();
2072}
2073
Jooyung Han2d5878e2020-01-23 12:45:10 +09002074status_t Parcel::readString16(std::optional<String16>* pArg) const
2075{
2076 const int32_t start = dataPosition();
2077 int32_t size;
2078 status_t status = readInt32(&size);
2079 pArg->reset();
2080
2081 if (status != OK || size < 0) {
2082 return status;
2083 }
2084
2085 setDataPosition(start);
2086 pArg->emplace();
2087
2088 status = readString16(&**pArg);
2089
2090 if (status != OK) {
2091 pArg->reset();
2092 }
2093
2094 return status;
2095}
2096
Casey Dahlinb9872622015-11-25 15:09:45 -08002097status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2098{
2099 const int32_t start = dataPosition();
2100 int32_t size;
2101 status_t status = readInt32(&size);
2102 pArg->reset();
2103
2104 if (status != OK || size < 0) {
2105 return status;
2106 }
2107
2108 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002109 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002110
2111 status = readString16(pArg->get());
2112
2113 if (status != OK) {
2114 pArg->reset();
2115 }
2116
2117 return status;
2118}
2119
Casey Dahlin451ff582015-10-19 18:12:18 -07002120status_t Parcel::readString16(String16* pArg) const
2121{
2122 size_t len;
2123 const char16_t* str = readString16Inplace(&len);
2124 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002125 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002126 return 0;
2127 } else {
2128 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002129 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002130 }
2131}
2132
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002133const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2134{
2135 int32_t size = readInt32();
2136 // watch for potential int overflow from size+1
2137 if (size >= 0 && size < INT32_MAX) {
2138 *outLen = size;
2139 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002140 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141 return str;
2142 }
2143 }
2144 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002145 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002146}
2147
Casey Dahlinf0c13772015-10-27 18:33:56 -07002148status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2149{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002150 status_t status = readNullableStrongBinder(val);
2151 if (status == OK && !val->get()) {
2152 status = UNEXPECTED_NULL;
2153 }
2154 return status;
2155}
2156
2157status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2158{
Steven Morelanda86a3562019-08-01 23:28:34 +00002159 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002160}
2161
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002162sp<IBinder> Parcel::readStrongBinder() const
2163{
2164 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002165 // Note that a lot of code in Android reads binders by hand with this
2166 // method, and that code has historically been ok with getting nullptr
2167 // back (while ignoring error codes).
2168 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002169 return val;
2170}
2171
Christopher Wiley97f048d2015-11-19 06:49:05 -08002172status_t Parcel::readParcelable(Parcelable* parcelable) const {
2173 int32_t have_parcelable = 0;
2174 status_t status = readInt32(&have_parcelable);
2175 if (status != OK) {
2176 return status;
2177 }
2178 if (!have_parcelable) {
2179 return UNEXPECTED_NULL;
2180 }
2181 return parcelable->readFromParcel(this);
2182}
2183
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002184int32_t Parcel::readExceptionCode() const
2185{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002186 binder::Status status;
2187 status.readFromParcel(*this);
2188 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002189}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002190
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002191native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002192{
2193 int numFds, numInts;
2194 status_t err;
2195 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002196 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002197 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002198 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002199
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002200 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002201 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002202 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002203 }
2204
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002205 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002206 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002207 if (h->data[i] < 0) {
2208 for (int j = 0; j < i; j++) {
2209 close(h->data[j]);
2210 }
2211 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002212 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002213 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002214 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002215 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002216 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002217 native_handle_close(h);
2218 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002219 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002220 }
2221 return h;
2222}
2223
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002224int Parcel::readFileDescriptor() const
2225{
2226 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002227
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002228 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002229 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002230 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002231
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002232 return BAD_TYPE;
2233}
2234
Dianne Hackborn1941a402016-08-29 12:30:43 -07002235int Parcel::readParcelFileDescriptor() const
2236{
2237 int32_t hasComm = readInt32();
2238 int fd = readFileDescriptor();
2239 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002240 // detach (owned by the binder driver)
2241 int comm = readFileDescriptor();
2242
2243 // warning: this must be kept in sync with:
2244 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2245 enum ParcelFileDescriptorStatus {
2246 DETACHED = 2,
2247 };
2248
2249#if BYTE_ORDER == BIG_ENDIAN
2250 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2251#endif
2252#if BYTE_ORDER == LITTLE_ENDIAN
2253 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2254#endif
2255
2256 ssize_t written = TEMP_FAILURE_RETRY(
2257 ::write(comm, &message, sizeof(message)));
2258
2259 if (written == -1 || written != sizeof(message)) {
2260 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2261 written, strerror(errno));
2262 return BAD_TYPE;
2263 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002264 }
2265 return fd;
2266}
2267
Christopher Wiley2cf19952016-04-11 11:09:37 -07002268status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002269{
2270 int got = readFileDescriptor();
2271
2272 if (got == BAD_TYPE) {
2273 return BAD_TYPE;
2274 }
2275
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002276 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002277
2278 if (val->get() < 0) {
2279 return BAD_VALUE;
2280 }
2281
2282 return OK;
2283}
2284
Ryo Hashimotobf551892018-05-31 16:58:35 +09002285status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2286{
2287 int got = readParcelFileDescriptor();
2288
2289 if (got == BAD_TYPE) {
2290 return BAD_TYPE;
2291 }
2292
2293 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2294
2295 if (val->get() < 0) {
2296 return BAD_VALUE;
2297 }
2298
2299 return OK;
2300}
Casey Dahlin06673e32015-11-23 13:24:23 -08002301
Jooyung Han2d5878e2020-01-23 12:45:10 +09002302status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const {
2303 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2304}
2305
Christopher Wiley2cf19952016-04-11 11:09:37 -07002306status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002307 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2308}
2309
Christopher Wiley2cf19952016-04-11 11:09:37 -07002310status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002311 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2312}
2313
Jeff Brown5707dbf2011-09-23 21:17:56 -07002314status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2315{
Jeff Brown13b16042014-11-11 16:44:25 -08002316 int32_t blobType;
2317 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002318 if (status) return status;
2319
Jeff Brown13b16042014-11-11 16:44:25 -08002320 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002321 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002322 const void* ptr = readInplace(len);
2323 if (!ptr) return BAD_VALUE;
2324
Jeff Brown13b16042014-11-11 16:44:25 -08002325 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002326 return NO_ERROR;
2327 }
2328
Steve Block6807e592011-10-20 11:56:00 +01002329 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002330 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002331 int fd = readFileDescriptor();
2332 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2333
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002334 if (!ashmem_valid(fd)) {
2335 ALOGE("invalid fd");
2336 return BAD_VALUE;
2337 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002338 int size = ashmem_get_size_region(fd);
2339 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002340 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002341 return BAD_VALUE;
2342 }
Yi Kong91635562018-06-07 14:38:36 -07002343 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002344 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002345 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002346
Jeff Brown13b16042014-11-11 16:44:25 -08002347 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002348 return NO_ERROR;
2349}
2350
Mathias Agopiane1424282013-07-29 21:24:40 -07002351status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002352{
2353 // size
2354 const size_t len = this->readInt32();
2355 const size_t fd_count = this->readInt32();
2356
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002357 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002358 // don't accept size_t values which may have come from an
2359 // inadvertent conversion from a negative int.
2360 return BAD_VALUE;
2361 }
2362
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002363 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002364 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002365 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002366 return BAD_VALUE;
2367
Yi Kong91635562018-06-07 14:38:36 -07002368 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002369 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002370 fds = new (std::nothrow) int[fd_count];
2371 if (fds == nullptr) {
2372 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2373 return BAD_VALUE;
2374 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002375 }
2376
2377 status_t err = NO_ERROR;
2378 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002379 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002380 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002381 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002382 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 -07002383 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2384 // Close all the file descriptors that were dup-ed.
2385 for (size_t j=0; j<i ;j++) {
2386 close(fds[j]);
2387 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002388 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002389 }
2390
2391 if (err == NO_ERROR) {
2392 err = val.unflatten(buf, len, fds, fd_count);
2393 }
2394
2395 if (fd_count) {
2396 delete [] fds;
2397 }
2398
2399 return err;
2400}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002401const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2402{
2403 const size_t DPOS = mDataPos;
2404 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2405 const flat_binder_object* obj
2406 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2407 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002408 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002409 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002410 // the object list, so we don't want to check for it when
2411 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002412 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002413 return obj;
2414 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002415
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002416 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002417 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002418 const size_t N = mObjectsSize;
2419 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002420
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002422 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002423 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002424
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002425 // Start at the current hint position, looking for an object at
2426 // the current data position.
2427 if (opos < N) {
2428 while (opos < (N-1) && OBJS[opos] < DPOS) {
2429 opos++;
2430 }
2431 } else {
2432 opos = N-1;
2433 }
2434 if (OBJS[opos] == DPOS) {
2435 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002436 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002437 this, DPOS, opos);
2438 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002439 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002440 return obj;
2441 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002442
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002443 // Look backwards for it...
2444 while (opos > 0 && OBJS[opos] > DPOS) {
2445 opos--;
2446 }
2447 if (OBJS[opos] == DPOS) {
2448 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002449 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002450 this, DPOS, opos);
2451 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002452 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002453 return obj;
2454 }
2455 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002456 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 -07002457 this, DPOS);
2458 }
Yi Kong91635562018-06-07 14:38:36 -07002459 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002460}
2461
2462void Parcel::closeFileDescriptors()
2463{
2464 size_t i = mObjectsSize;
2465 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002466 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002467 }
2468 while (i > 0) {
2469 i--;
2470 const flat_binder_object* flat
2471 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002472 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002473 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002474 close(flat->handle);
2475 }
2476 }
2477}
2478
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002479uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002480{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002481 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002482}
2483
2484size_t Parcel::ipcDataSize() const
2485{
2486 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2487}
2488
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002489uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002490{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002491 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002492}
2493
2494size_t Parcel::ipcObjectsCount() const
2495{
2496 return mObjectsSize;
2497}
2498
2499void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002500 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002501{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002502 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002503 freeDataNoInit();
2504 mError = NO_ERROR;
2505 mData = const_cast<uint8_t*>(data);
2506 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002507 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002509 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002510 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002511 mObjectsSize = mObjectsCapacity = objectsCount;
2512 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002513 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002514 mOwner = relFunc;
2515 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002516 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002517 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002518 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002519 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002520 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002521 mObjectsSize = 0;
2522 break;
2523 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002524 const flat_binder_object* flat
2525 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2526 uint32_t type = flat->hdr.type;
2527 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2528 type == BINDER_TYPE_FD)) {
2529 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2530 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2531 // recover gracefully by clearing out the objects, and releasing the objects we do
2532 // know about.
2533 android_errorWriteLog(0x534e4554, "135930648");
2534 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2535 __func__, type, (uint64_t)offset);
2536 releaseObjects();
2537 mObjectsSize = 0;
2538 break;
2539 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002540 minOffset = offset + sizeof(flat_binder_object);
2541 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002542 scanForFds();
2543}
2544
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002545void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002546{
2547 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002548
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002549 if (errorCheck() != NO_ERROR) {
2550 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002551 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002552 } else if (dataSize() > 0) {
2553 const uint8_t* DATA = data();
2554 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002555 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002556 const size_t N = objectsCount();
2557 for (size_t i=0; i<N; i++) {
2558 const flat_binder_object* flat
2559 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2560 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002561 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002562 << " = " << flat->binder;
2563 }
2564 } else {
2565 to << "NULL";
2566 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002567
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002568 to << ")";
2569}
2570
2571void Parcel::releaseObjects()
2572{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002573 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002574 if (i == 0) {
2575 return;
2576 }
2577 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002578 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002579 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002580 while (i > 0) {
2581 i--;
2582 const flat_binder_object* flat
2583 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002584 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002585 }
2586}
2587
2588void Parcel::acquireObjects()
2589{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002590 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002591 if (i == 0) {
2592 return;
2593 }
2594 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002595 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002596 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002597 while (i > 0) {
2598 i--;
2599 const flat_binder_object* flat
2600 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002601 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002602 }
2603}
2604
2605void Parcel::freeData()
2606{
2607 freeDataNoInit();
2608 initState();
2609}
2610
2611void Parcel::freeDataNoInit()
2612{
2613 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002614 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002615 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002616 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2617 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002618 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002619 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002620 if (mData) {
2621 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey9de06a92020-09-11 12:07:10 -06002622 gParcelGlobalAllocSize -= mDataCapacity;
2623 gParcelGlobalAllocCount--;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002624 free(mData);
2625 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002626 if (mObjects) free(mObjects);
2627 }
2628}
2629
2630status_t Parcel::growData(size_t len)
2631{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002632 if (len > INT32_MAX) {
2633 // don't accept size_t values which may have come from an
2634 // inadvertent conversion from a negative int.
2635 return BAD_VALUE;
2636 }
2637
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01002638 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2639 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002640 size_t newSize = ((mDataSize+len)*3)/2;
2641 return (newSize <= mDataSize)
2642 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002643 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002644}
2645
2646status_t Parcel::restartWrite(size_t desired)
2647{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002648 if (desired > INT32_MAX) {
2649 // don't accept size_t values which may have come from an
2650 // inadvertent conversion from a negative int.
2651 return BAD_VALUE;
2652 }
2653
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002654 if (mOwner) {
2655 freeData();
2656 return continueWrite(desired);
2657 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002658
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002659 uint8_t* data = (uint8_t*)realloc(mData, desired);
2660 if (!data && desired > mDataCapacity) {
2661 mError = NO_MEMORY;
2662 return NO_MEMORY;
2663 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002664
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002665 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002666
Devin Moore4a0a55e2020-06-04 13:23:10 -07002667 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002668 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey9de06a92020-09-11 12:07:10 -06002669 if (mDataCapacity > desired) {
2670 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2671 } else {
2672 gParcelGlobalAllocSize += (desired - mDataCapacity);
2673 }
2674
Colin Cross83ec65e2015-12-08 17:15:50 -08002675 if (!mData) {
2676 gParcelGlobalAllocCount++;
2677 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002678 mData = data;
2679 mDataCapacity = desired;
2680 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002681
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002682 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002683 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2684 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2685
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002686 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002687 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002688 mObjectsSize = mObjectsCapacity = 0;
2689 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002690 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002691 mHasFds = false;
2692 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002693 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002694
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002695 return NO_ERROR;
2696}
2697
2698status_t Parcel::continueWrite(size_t desired)
2699{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002700 if (desired > INT32_MAX) {
2701 // don't accept size_t values which may have come from an
2702 // inadvertent conversion from a negative int.
2703 return BAD_VALUE;
2704 }
2705
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002706 // If shrinking, first adjust for any objects that appear
2707 // after the new data size.
2708 size_t objectsSize = mObjectsSize;
2709 if (desired < mDataSize) {
2710 if (desired == 0) {
2711 objectsSize = 0;
2712 } else {
2713 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002714 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002715 break;
2716 objectsSize--;
2717 }
2718 }
2719 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002720
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002721 if (mOwner) {
2722 // If the size is going to zero, just release the owner's data.
2723 if (desired == 0) {
2724 freeData();
2725 return NO_ERROR;
2726 }
2727
2728 // If there is a different owner, we need to take
2729 // posession.
2730 uint8_t* data = (uint8_t*)malloc(desired);
2731 if (!data) {
2732 mError = NO_MEMORY;
2733 return NO_MEMORY;
2734 }
Yi Kong91635562018-06-07 14:38:36 -07002735 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002736
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002737 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002738 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002739 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002740 free(data);
2741
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002742 mError = NO_MEMORY;
2743 return NO_MEMORY;
2744 }
2745
2746 // Little hack to only acquire references on objects
2747 // we will be keeping.
2748 size_t oldObjectsSize = mObjectsSize;
2749 mObjectsSize = objectsSize;
2750 acquireObjects();
2751 mObjectsSize = oldObjectsSize;
2752 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002753
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002754 if (mData) {
2755 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2756 }
2757 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002758 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002759 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002760 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002761 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002762 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002763
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002764 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002765 gParcelGlobalAllocSize += desired;
2766 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002767
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002768 mData = data;
2769 mObjects = objects;
2770 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002771 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002772 mDataCapacity = desired;
2773 mObjectsSize = mObjectsCapacity = objectsSize;
2774 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002775 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002776
2777 } else if (mData) {
2778 if (objectsSize < mObjectsSize) {
2779 // Need to release refs on any objects we are dropping.
2780 const sp<ProcessState> proc(ProcessState::self());
2781 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2782 const flat_binder_object* flat
2783 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002784 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002785 // will need to rescan because we may have lopped off the only FDs
2786 mFdsKnown = false;
2787 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002788 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002789 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002790
2791 if (objectsSize == 0) {
2792 free(mObjects);
2793 mObjects = nullptr;
Michael Wachenschwanzc67d9f32019-10-15 11:49:22 -07002794 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002795 } else {
2796 binder_size_t* objects =
2797 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2798 if (objects) {
2799 mObjects = objects;
Michael Wachenschwanzc67d9f32019-10-15 11:49:22 -07002800 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002801 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002802 }
2803 mObjectsSize = 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
2808 // We own the data, so we can just do a realloc().
2809 if (desired > mDataCapacity) {
2810 uint8_t* data = (uint8_t*)realloc(mData, desired);
2811 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002812 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2813 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002814 gParcelGlobalAllocSize += desired;
2815 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002816 mData = data;
2817 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002818 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002819 mError = NO_MEMORY;
2820 return NO_MEMORY;
2821 }
2822 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002823 if (mDataSize > desired) {
2824 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002825 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002826 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002827 if (mDataPos > desired) {
2828 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002829 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002830 }
2831 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002832
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002833 } else {
2834 // This is the first data. Easy!
2835 uint8_t* data = (uint8_t*)malloc(desired);
2836 if (!data) {
2837 mError = NO_MEMORY;
2838 return NO_MEMORY;
2839 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002840
Yi Kong91635562018-06-07 14:38:36 -07002841 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002842 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002843 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002844 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002845
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002846 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002847 gParcelGlobalAllocSize += desired;
2848 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002849
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002850 mData = data;
2851 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002852 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2853 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002854 mDataCapacity = desired;
2855 }
2856
2857 return NO_ERROR;
2858}
2859
2860void Parcel::initState()
2861{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002862 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002863 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002864 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002865 mDataSize = 0;
2866 mDataCapacity = 0;
2867 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002868 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2869 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002870 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002871 mObjectsSize = 0;
2872 mObjectsCapacity = 0;
2873 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002874 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002875 mHasFds = false;
2876 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002877 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002878 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002879 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002880 mWorkSourceRequestHeaderPosition = 0;
2881 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002882
2883 // racing multiple init leads only to multiple identical write
2884 if (gMaxFds == 0) {
2885 struct rlimit result;
2886 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2887 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002888 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002889 } else {
2890 ALOGW("Unable to getrlimit: %s", strerror(errno));
2891 gMaxFds = 1024;
2892 }
2893 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002894}
2895
2896void Parcel::scanForFds() const
2897{
2898 bool hasFds = false;
2899 for (size_t i=0; i<mObjectsSize; i++) {
2900 const flat_binder_object* flat
2901 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002902 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002903 hasFds = true;
2904 break;
2905 }
2906 }
2907 mHasFds = hasFds;
2908 mFdsKnown = true;
2909}
2910
Dan Sandleraa5c2342015-04-10 10:08:45 -04002911size_t Parcel::getBlobAshmemSize() const
2912{
Adrian Roos6bb31142015-10-22 16:46:12 -07002913 // This used to return the size of all blobs that were written to ashmem, now we're returning
2914 // the ashmem currently referenced by this Parcel, which should be equivalent.
2915 // TODO: Remove method once ABI can be changed.
2916 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002917}
2918
Adrian Rooscbf37262015-10-22 16:12:53 -07002919size_t Parcel::getOpenAshmemSize() const
2920{
2921 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002922}
2923
2924// --- Parcel::Blob ---
2925
2926Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002927 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002928}
2929
2930Parcel::Blob::~Blob() {
2931 release();
2932}
2933
2934void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002935 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002936 ::munmap(mData, mSize);
2937 }
2938 clear();
2939}
2940
Jeff Brown13b16042014-11-11 16:44:25 -08002941void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2942 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002943 mData = data;
2944 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002945 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002946}
2947
2948void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002949 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002950 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002951 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002952 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002953}
2954
Steven Moreland6511af52019-09-26 16:05:45 -07002955} // namespace android