blob: 06f6249be02934413a2336f5698e570b0a36f830 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Steven Morelandbf1915b2020-07-16 22:43:02 +000023#include <linux/sched.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080024#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080029#include <sys/stat.h>
30#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070031#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080032#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070033
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/Binder.h>
35#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080036#include <binder/IPCThreadState.h>
37#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070038#include <binder/ProcessState.h>
Steven Moreland6e5a7752019-08-05 20:30:14 -070039#include <binder/Stability.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080040#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041#include <binder/TextOutput.h>
42
Mark Salyzynabed7f72016-01-27 08:02:48 -080043#include <cutils/ashmem.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080044#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080046#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String8.h>
48#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049
Mathias Agopian208059f2009-05-18 15:08:03 -070050#include <private/binder/binder_module.h>
Steven Morelanda4853cd2019-07-12 15:44:37 -070051#include "Static.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080054//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080055#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080056//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070057
58// ---------------------------------------------------------------------------
59
Nick Kralevichb6b14232015-04-02 09:36:02 -070060// This macro should never be used at runtime, as a too large value
61// of s could cause an integer overflow. Instead, you should always
62// use the wrapper function pad_size()
63#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
64
65static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070066 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070067 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070068 }
69 return PAD_SIZE_UNSAFE(s);
70}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070071
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070072// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060073#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070074
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075namespace android {
76
Steven Moreland7b102262019-08-01 15:48:43 -070077// many things compile this into prebuilts on the stack
78static_assert(sizeof(Parcel) == 60 || sizeof(Parcel) == 120);
79
Jeff Sharkey9de06a92020-09-11 12:07:10 -060080static std::atomic<size_t> gParcelGlobalAllocCount;
81static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -080082
Christopher Tatee4e0ae82016-03-24 16:03:44 -070083static size_t gMaxFds = 0;
84
Jeff Brown13b16042014-11-11 16:44:25 -080085// Maximum size of a blob to transfer in-place.
86static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
87
88enum {
89 BLOB_INPLACE = 0,
90 BLOB_ASHMEM_IMMUTABLE = 1,
91 BLOB_ASHMEM_MUTABLE = 2,
92};
93
Steven Morelandb1c81202019-04-05 18:49:55 -070094static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070095 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070096{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -070097 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098 case BINDER_TYPE_BINDER:
99 if (obj.binder) {
100 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800101 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700102 }
103 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104 case BINDER_TYPE_HANDLE: {
105 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700106 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700107 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
108 b->incStrong(who);
109 }
110 return;
111 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700112 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000113 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700114 // If we own an ashmem fd, keep track of how much memory it refers to.
115 int size = ashmem_get_size_region(obj.handle);
116 if (size > 0) {
117 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700118 }
119 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700120 return;
121 }
122 }
123
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700124 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700125}
126
Adrian Roos6bb31142015-10-22 16:46:12 -0700127static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700128 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700129{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700130 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700131 case BINDER_TYPE_BINDER:
132 if (obj.binder) {
133 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800134 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700135 }
136 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700137 case BINDER_TYPE_HANDLE: {
138 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700139 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700140 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
141 b->decStrong(who);
142 }
143 return;
144 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700145 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800146 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000147 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700148 int size = ashmem_get_size_region(obj.handle);
149 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800150 // ashmem size might have changed since last time it was accounted for, e.g.
151 // in acquire_object(). Value of *outAshmemSize is not critical since we are
152 // releasing the object anyway. Check for integer overflow condition.
153 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700154 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700155 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800156
157 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700158 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700159 return;
160 }
161 }
162
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700163 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700164}
165
Steven Morelanda86a3562019-08-01 23:28:34 +0000166status_t Parcel::finishFlattenBinder(
167 const sp<IBinder>& binder, const flat_binder_object& flat)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700168{
Steven Morelanda86a3562019-08-01 23:28:34 +0000169 status_t status = writeObject(flat, false);
170 if (status != OK) return status;
171
Steven Moreland6e5a7752019-08-05 20:30:14 -0700172 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Morelanda86a3562019-08-01 23:28:34 +0000173 return writeInt32(internal::Stability::get(binder.get()));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700174}
175
Steven Morelanda86a3562019-08-01 23:28:34 +0000176status_t Parcel::finishUnflattenBinder(
177 const sp<IBinder>& binder, sp<IBinder>* out) const
178{
179 int32_t stability;
180 status_t status = readInt32(&stability);
181 if (status != OK) return status;
182
Steven Moreland05929552019-07-31 17:51:25 -0700183 status = internal::Stability::set(binder.get(), stability, true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000184 if (status != OK) return status;
185
186 *out = binder;
187 return OK;
188}
189
Steven Morelandbf1915b2020-07-16 22:43:02 +0000190static constexpr inline int schedPolicyMask(int policy, int priority) {
191 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
192}
193
Steven Morelanda86a3562019-08-01 23:28:34 +0000194status_t Parcel::flattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700195{
196 flat_binder_object obj;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000197 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700198
Steven Morelandbf1915b2020-07-16 22:43:02 +0000199 int schedBits = 0;
200 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
201 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700202 }
203
Yi Kong91635562018-06-07 14:38:36 -0700204 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800205 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700206 if (!local) {
207 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700208 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000209 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700210 }
211 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700212 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800213 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700214 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800215 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700216 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000217 int policy = local->getMinSchedulerPolicy();
218 int priority = local->getMinSchedulerPriority();
219
220 if (policy != 0 || priority != 0) {
221 // override value, since it is set explicitly
222 schedBits = schedPolicyMask(policy, priority);
223 }
Steven Morelandf0212002018-12-26 13:59:23 -0800224 if (local->isRequestingSid()) {
225 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
226 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700227 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800228 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
229 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230 }
231 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700232 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800233 obj.binder = 0;
234 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700236
Steven Morelandbf1915b2020-07-16 22:43:02 +0000237 obj.flags |= schedBits;
238
Steven Morelanda86a3562019-08-01 23:28:34 +0000239 return finishFlattenBinder(binder, obj);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700240}
241
Steven Morelanda86a3562019-08-01 23:28:34 +0000242status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243{
Steven Morelanda86a3562019-08-01 23:28:34 +0000244 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700245
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700246 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700247 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000248 case BINDER_TYPE_BINDER: {
249 sp<IBinder> binder = reinterpret_cast<IBinder*>(flat->cookie);
250 return finishUnflattenBinder(binder, out);
251 }
252 case BINDER_TYPE_HANDLE: {
253 sp<IBinder> binder =
254 ProcessState::self()->getStrongProxyForHandle(flat->handle);
255 return finishUnflattenBinder(binder, out);
256 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700257 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258 }
259 return BAD_TYPE;
260}
261
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700262// ---------------------------------------------------------------------------
263
264Parcel::Parcel()
265{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800266 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700267 initState();
268}
269
270Parcel::~Parcel()
271{
272 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800273 LOG_ALLOC("Parcel %p: destroyed", this);
274}
275
276size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey9de06a92020-09-11 12:07:10 -0600277 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800278}
279
280size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey9de06a92020-09-11 12:07:10 -0600281 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700282}
283
284const uint8_t* Parcel::data() const
285{
286 return mData;
287}
288
289size_t Parcel::dataSize() const
290{
291 return (mDataSize > mDataPos ? mDataSize : mDataPos);
292}
293
294size_t Parcel::dataAvail() const
295{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700296 size_t result = dataSize() - dataPosition();
297 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700298 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700299 }
300 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700301}
302
303size_t Parcel::dataPosition() const
304{
305 return mDataPos;
306}
307
308size_t Parcel::dataCapacity() const
309{
310 return mDataCapacity;
311}
312
313status_t Parcel::setDataSize(size_t size)
314{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700315 if (size > INT32_MAX) {
316 // don't accept size_t values which may have come from an
317 // inadvertent conversion from a negative int.
318 return BAD_VALUE;
319 }
320
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700321 status_t err;
322 err = continueWrite(size);
323 if (err == NO_ERROR) {
324 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700325 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700326 }
327 return err;
328}
329
330void Parcel::setDataPosition(size_t pos) const
331{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700332 if (pos > INT32_MAX) {
333 // don't accept size_t values which may have come from an
334 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700335 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700336 }
337
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700338 mDataPos = pos;
339 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800340 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700341}
342
343status_t Parcel::setDataCapacity(size_t size)
344{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700345 if (size > INT32_MAX) {
346 // don't accept size_t values which may have come from an
347 // inadvertent conversion from a negative int.
348 return BAD_VALUE;
349 }
350
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700351 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700352 return NO_ERROR;
353}
354
355status_t Parcel::setData(const uint8_t* buffer, size_t len)
356{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700357 if (len > INT32_MAX) {
358 // don't accept size_t values which may have come from an
359 // inadvertent conversion from a negative int.
360 return BAD_VALUE;
361 }
362
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700363 status_t err = restartWrite(len);
364 if (err == NO_ERROR) {
365 memcpy(const_cast<uint8_t*>(data()), buffer, len);
366 mDataSize = len;
367 mFdsKnown = false;
368 }
369 return err;
370}
371
Andreas Huber51faf462011-04-13 10:21:56 -0700372status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700373{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700374 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700375 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800376 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700377 size_t size = parcel->mObjectsSize;
378 int startPos = mDataPos;
379 int firstIndex = -1, lastIndex = -2;
380
381 if (len == 0) {
382 return NO_ERROR;
383 }
384
Nick Kralevichb6b14232015-04-02 09:36:02 -0700385 if (len > INT32_MAX) {
386 // don't accept size_t values which may have come from an
387 // inadvertent conversion from a negative int.
388 return BAD_VALUE;
389 }
390
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700391 // range checks against the source parcel size
392 if ((offset > parcel->mDataSize)
393 || (len > parcel->mDataSize)
394 || (offset + len > parcel->mDataSize)) {
395 return BAD_VALUE;
396 }
397
398 // Count objects in range
399 for (int i = 0; i < (int) size; i++) {
400 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700401 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700402 if (firstIndex == -1) {
403 firstIndex = i;
404 }
405 lastIndex = i;
406 }
407 }
408 int numObjects = lastIndex - firstIndex + 1;
409
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700410 if ((mDataSize+len) > mDataCapacity) {
411 // grow data
412 err = growData(len);
413 if (err != NO_ERROR) {
414 return err;
415 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700416 }
417
418 // append data
419 memcpy(mData + mDataPos, data + offset, len);
420 mDataPos += len;
421 mDataSize += len;
422
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400423 err = NO_ERROR;
424
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700425 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200426 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700427 // grow objects
428 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenenda2f2fd2020-01-22 10:46:25 +0100429 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
430 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700431 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenenda2f2fd2020-01-22 10:46:25 +0100432 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800433 binder_size_t *objects =
434 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700435 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700436 return NO_MEMORY;
437 }
438 mObjects = objects;
439 mObjectsCapacity = newSize;
440 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700441
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700442 // append and acquire objects
443 int idx = mObjectsSize;
444 for (int i = firstIndex; i <= lastIndex; i++) {
445 size_t off = objects[i] - offset + startPos;
446 mObjects[idx++] = off;
447 mObjectsSize++;
448
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700449 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700450 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700451 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700452
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700453 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700454 // If this is a file descriptor, we need to dup it so the
455 // new Parcel now owns its own fd, and can declare that we
456 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800457 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800458 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400460 if (!mAllowFds) {
461 err = FDS_NOT_ALLOWED;
462 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700463 }
464 }
465 }
466
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400467 return err;
468}
469
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700470int Parcel::compareData(const Parcel& other) {
471 size_t size = dataSize();
472 if (size != other.dataSize()) {
473 return size < other.dataSize() ? -1 : 1;
474 }
475 return memcmp(data(), other.data(), size);
476}
477
Jeff Brown13b16042014-11-11 16:44:25 -0800478bool Parcel::allowFds() const
479{
480 return mAllowFds;
481}
482
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700483bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400484{
485 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700486 if (!allowFds) {
487 mAllowFds = false;
488 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400489 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700490}
491
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700492void Parcel::restoreAllowFds(bool lastValue)
493{
494 mAllowFds = lastValue;
495}
496
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700497bool Parcel::hasFileDescriptors() const
498{
499 if (!mFdsKnown) {
500 scanForFds();
501 }
502 return mHasFds;
503}
504
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000505void Parcel::updateWorkSourceRequestHeaderPosition() const {
506 // Only update the request headers once. We only want to point
507 // to the first headers read/written.
508 if (!mRequestHeaderPresent) {
509 mWorkSourceRequestHeaderPosition = dataPosition();
510 mRequestHeaderPresent = true;
511 }
512}
513
Steven Morelande70d8fb2020-08-20 00:15:14 +0000514#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Moreland0f452742019-07-31 15:50:51 +0000515constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
516#else
517constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
518#endif
519
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700520// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521status_t Parcel::writeInterfaceToken(const String16& interface)
522{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000523 const IPCThreadState* threadState = IPCThreadState::self();
524 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000525 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000526 writeInt32(threadState->shouldPropagateWorkSource() ?
527 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Moreland0f452742019-07-31 15:50:51 +0000528 writeInt32(kHeader);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700529 // currently the interface identification token is just its name as a string
530 return writeString16(interface);
531}
532
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000533bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
534{
535 if (!mRequestHeaderPresent) {
536 return false;
537 }
538
539 const size_t initialPosition = dataPosition();
540 setDataPosition(mWorkSourceRequestHeaderPosition);
541 status_t err = writeInt32(uid);
542 setDataPosition(initialPosition);
543 return err == NO_ERROR;
544}
545
Steven Moreland0891c9b2019-05-06 15:05:13 -0700546uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000547{
548 if (!mRequestHeaderPresent) {
549 return IPCThreadState::kUnsetWorkSource;
550 }
551
552 const size_t initialPosition = dataPosition();
553 setDataPosition(mWorkSourceRequestHeaderPosition);
554 uid_t uid = readInt32();
555 setDataPosition(initialPosition);
556 return uid;
557}
558
Mathias Agopian83c04462009-05-22 19:00:22 -0700559bool Parcel::checkInterface(IBinder* binder) const
560{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700561 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700562}
563
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700564bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700565 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700566{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700567 return enforceInterface(interface.string(), interface.size(), threadState);
568}
569
570bool Parcel::enforceInterface(const char16_t* interface,
571 size_t len,
572 IPCThreadState* threadState) const
573{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100574 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700575 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700576 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700577 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700578 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700579 if ((threadState->getLastTransactionBinderFlags() &
580 IBinder::FLAG_ONEWAY) != 0) {
581 // For one-way calls, the callee is running entirely
582 // disconnected from the caller, so disable StrictMode entirely.
583 // Not only does disk/network usage not impact the caller, but
584 // there's no way to commuicate back any violations anyway.
585 threadState->setStrictModePolicy(0);
586 } else {
587 threadState->setStrictModePolicy(strictPolicy);
588 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100589 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000590 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100591 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000592 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Moreland0f452742019-07-31 15:50:51 +0000593 // vendor header
594 int32_t header = readInt32();
595 if (header != kHeader) {
596 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
597 return false;
598 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100599 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700600 size_t parcel_interface_len;
601 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
602 if (len == parcel_interface_len &&
603 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700604 return true;
605 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700606 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700607 String8(interface, len).string(),
608 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700609 return false;
610 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700611}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700612
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700613size_t Parcel::objectsCount() const
614{
615 return mObjectsSize;
616}
617
618status_t Parcel::errorCheck() const
619{
620 return mError;
621}
622
623void Parcel::setError(status_t err)
624{
625 mError = err;
626}
627
628status_t Parcel::finishWrite(size_t len)
629{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700630 if (len > INT32_MAX) {
631 // don't accept size_t values which may have come from an
632 // inadvertent conversion from a negative int.
633 return BAD_VALUE;
634 }
635
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700636 //printf("Finish write of %d\n", len);
637 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700638 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700639 if (mDataPos > mDataSize) {
640 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700641 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700642 }
643 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
644 return NO_ERROR;
645}
646
647status_t Parcel::writeUnpadded(const void* data, size_t len)
648{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700649 if (len > INT32_MAX) {
650 // don't accept size_t values which may have come from an
651 // inadvertent conversion from a negative int.
652 return BAD_VALUE;
653 }
654
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700655 size_t end = mDataPos + len;
656 if (end < mDataPos) {
657 // integer overflow
658 return BAD_VALUE;
659 }
660
661 if (end <= mDataCapacity) {
662restart_write:
663 memcpy(mData+mDataPos, data, len);
664 return finishWrite(len);
665 }
666
667 status_t err = growData(len);
668 if (err == NO_ERROR) goto restart_write;
669 return err;
670}
671
672status_t Parcel::write(const void* data, size_t len)
673{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700674 if (len > INT32_MAX) {
675 // don't accept size_t values which may have come from an
676 // inadvertent conversion from a negative int.
677 return BAD_VALUE;
678 }
679
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700680 void* const d = writeInplace(len);
681 if (d) {
682 memcpy(d, data, len);
683 return NO_ERROR;
684 }
685 return mError;
686}
687
688void* Parcel::writeInplace(size_t len)
689{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700690 if (len > INT32_MAX) {
691 // don't accept size_t values which may have come from an
692 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700693 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700694 }
695
696 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700697
698 // sanity check for integer overflow
699 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700700 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700701 }
702
703 if ((mDataPos+padded) <= mDataCapacity) {
704restart_write:
705 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
706 uint8_t* const data = mData+mDataPos;
707
708 // Need to pad at end?
709 if (padded != len) {
710#if BYTE_ORDER == BIG_ENDIAN
711 static const uint32_t mask[4] = {
712 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
713 };
714#endif
715#if BYTE_ORDER == LITTLE_ENDIAN
716 static const uint32_t mask[4] = {
717 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
718 };
719#endif
720 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
721 // *reinterpret_cast<void**>(data+padded-4));
722 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
723 }
724
725 finishWrite(padded);
726 return data;
727 }
728
729 status_t err = growData(padded);
730 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700731 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700732}
733
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800734status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
735 const uint8_t* strData = (uint8_t*)str.data();
736 const size_t strLen= str.length();
737 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100738 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800739 return BAD_VALUE;
740 }
741
742 status_t err = writeInt32(utf16Len);
743 if (err) {
744 return err;
745 }
746
747 // Allocate enough bytes to hold our converted string and its terminating NULL.
748 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
749 if (!dst) {
750 return NO_MEMORY;
751 }
752
Sergio Girof4607432016-07-21 14:46:35 +0100753 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800754
755 return NO_ERROR;
756}
757
Jooyung Han2d5878e2020-01-23 12:45:10 +0900758status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) {
759 if (!str) {
760 return writeInt32(-1);
761 }
762 return writeUtf8AsUtf16(*str);
763}
764
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800765status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
766 if (!str) {
767 return writeInt32(-1);
768 }
769 return writeUtf8AsUtf16(*str);
770}
771
Daniel Normand0337ef2019-09-20 15:46:03 -0700772status_t Parcel::writeByteVectorInternal(const int8_t* data, size_t size) {
773 if (size > std::numeric_limits<int32_t>::max()) {
774 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -0700775 }
776
Daniel Normand0337ef2019-09-20 15:46:03 -0700777 status_t status = writeInt32(size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700778 if (status != OK) {
779 return status;
780 }
781
Daniel Normand0337ef2019-09-20 15:46:03 -0700782 return write(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700783}
784
Casey Dahlin185d3442016-02-09 11:08:35 -0800785status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700786 return writeByteVectorInternal(val.data(), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800787}
788
Jooyung Han2d5878e2020-01-23 12:45:10 +0900789status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val)
790{
791 if (!val) return writeInt32(-1);
792 return writeByteVectorInternal(val->data(), val->size());
793}
794
Casey Dahlin185d3442016-02-09 11:08:35 -0800795status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
796{
Daniel Normand0337ef2019-09-20 15:46:03 -0700797 if (!val) return writeInt32(-1);
798 return writeByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800799}
800
801status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700802 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val.data()), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800803}
804
Jooyung Han2d5878e2020-01-23 12:45:10 +0900805status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val)
806{
807 if (!val) return writeInt32(-1);
808 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
809}
810
Casey Dahlin185d3442016-02-09 11:08:35 -0800811status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
812{
Daniel Normand0337ef2019-09-20 15:46:03 -0700813 if (!val) return writeInt32(-1);
814 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800815}
816
Casey Dahlin451ff582015-10-19 18:12:18 -0700817status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
818{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800819 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700820}
821
Jooyung Han2d5878e2020-01-23 12:45:10 +0900822status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val)
823{
824 return writeNullableTypedVector(val, &Parcel::writeInt32);
825}
826
Casey Dahlinb9872622015-11-25 15:09:45 -0800827status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
828{
829 return writeNullableTypedVector(val, &Parcel::writeInt32);
830}
831
Casey Dahlin451ff582015-10-19 18:12:18 -0700832status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
833{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800834 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700835}
836
Jooyung Han2d5878e2020-01-23 12:45:10 +0900837status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val)
838{
839 return writeNullableTypedVector(val, &Parcel::writeInt64);
840}
841
Casey Dahlinb9872622015-11-25 15:09:45 -0800842status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
843{
844 return writeNullableTypedVector(val, &Parcel::writeInt64);
845}
846
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800847status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
848{
849 return writeTypedVector(val, &Parcel::writeUint64);
850}
851
Jooyung Han2d5878e2020-01-23 12:45:10 +0900852status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val)
853{
854 return writeNullableTypedVector(val, &Parcel::writeUint64);
855}
856
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800857status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
858{
859 return writeNullableTypedVector(val, &Parcel::writeUint64);
860}
861
Casey Dahlin451ff582015-10-19 18:12:18 -0700862status_t Parcel::writeFloatVector(const std::vector<float>& val)
863{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800864 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700865}
866
Jooyung Han2d5878e2020-01-23 12:45:10 +0900867status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val)
868{
869 return writeNullableTypedVector(val, &Parcel::writeFloat);
870}
871
Casey Dahlinb9872622015-11-25 15:09:45 -0800872status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
873{
874 return writeNullableTypedVector(val, &Parcel::writeFloat);
875}
876
Casey Dahlin451ff582015-10-19 18:12:18 -0700877status_t Parcel::writeDoubleVector(const std::vector<double>& val)
878{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800879 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700880}
881
Jooyung Han2d5878e2020-01-23 12:45:10 +0900882status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val)
883{
884 return writeNullableTypedVector(val, &Parcel::writeDouble);
885}
886
Casey Dahlinb9872622015-11-25 15:09:45 -0800887status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
888{
889 return writeNullableTypedVector(val, &Parcel::writeDouble);
890}
891
Casey Dahlin451ff582015-10-19 18:12:18 -0700892status_t Parcel::writeBoolVector(const std::vector<bool>& val)
893{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800894 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700895}
896
Jooyung Han2d5878e2020-01-23 12:45:10 +0900897status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val)
898{
899 return writeNullableTypedVector(val, &Parcel::writeBool);
900}
901
Casey Dahlinb9872622015-11-25 15:09:45 -0800902status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
903{
904 return writeNullableTypedVector(val, &Parcel::writeBool);
905}
906
Casey Dahlin451ff582015-10-19 18:12:18 -0700907status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
908{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800909 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700910}
911
Jooyung Han2d5878e2020-01-23 12:45:10 +0900912status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val)
913{
914 return writeNullableTypedVector(val, &Parcel::writeChar);
915}
916
Casey Dahlinb9872622015-11-25 15:09:45 -0800917status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
918{
919 return writeNullableTypedVector(val, &Parcel::writeChar);
920}
921
Casey Dahlin451ff582015-10-19 18:12:18 -0700922status_t Parcel::writeString16Vector(const std::vector<String16>& val)
923{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800924 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700925}
926
Casey Dahlinb9872622015-11-25 15:09:45 -0800927status_t Parcel::writeString16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +0900928 const std::optional<std::vector<std::optional<String16>>>& val)
929{
930 return writeNullableTypedVector(val, &Parcel::writeString16);
931}
932
933status_t Parcel::writeString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -0800934 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
935{
936 return writeNullableTypedVector(val, &Parcel::writeString16);
937}
938
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800939status_t Parcel::writeUtf8VectorAsUtf16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +0900940 const std::optional<std::vector<std::optional<std::string>>>& val) {
941 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
942}
943
944status_t Parcel::writeUtf8VectorAsUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800945 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
946 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
947}
948
949status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
950 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
951}
952
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700953status_t Parcel::writeInt32(int32_t val)
954{
Andreas Huber84a6d042009-08-17 13:33:27 -0700955 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700956}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800957
958status_t Parcel::writeUint32(uint32_t val)
959{
960 return writeAligned(val);
961}
962
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700963status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700964 if (len > INT32_MAX) {
965 // don't accept size_t values which may have come from an
966 // inadvertent conversion from a negative int.
967 return BAD_VALUE;
968 }
969
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700970 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700971 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700972 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700973 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700974 if (ret == NO_ERROR) {
975 ret = write(val, len * sizeof(*val));
976 }
977 return ret;
978}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700979status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700980 if (len > INT32_MAX) {
981 // don't accept size_t values which may have come from an
982 // inadvertent conversion from a negative int.
983 return BAD_VALUE;
984 }
985
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700986 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700987 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700988 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700989 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700990 if (ret == NO_ERROR) {
991 ret = write(val, len * sizeof(*val));
992 }
993 return ret;
994}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700995
Casey Dahlind6848f52015-10-15 15:44:59 -0700996status_t Parcel::writeBool(bool val)
997{
998 return writeInt32(int32_t(val));
999}
1000
1001status_t Parcel::writeChar(char16_t val)
1002{
1003 return writeInt32(int32_t(val));
1004}
1005
1006status_t Parcel::writeByte(int8_t val)
1007{
1008 return writeInt32(int32_t(val));
1009}
1010
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001011status_t Parcel::writeInt64(int64_t val)
1012{
Andreas Huber84a6d042009-08-17 13:33:27 -07001013 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001014}
1015
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001016status_t Parcel::writeUint64(uint64_t val)
1017{
1018 return writeAligned(val);
1019}
1020
Serban Constantinescuf683e012013-11-05 16:53:55 +00001021status_t Parcel::writePointer(uintptr_t val)
1022{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001023 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001024}
1025
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001026status_t Parcel::writeFloat(float val)
1027{
Andreas Huber84a6d042009-08-17 13:33:27 -07001028 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001029}
1030
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001031#if defined(__mips__) && defined(__mips_hard_float)
1032
1033status_t Parcel::writeDouble(double val)
1034{
1035 union {
1036 double d;
1037 unsigned long long ll;
1038 } u;
1039 u.d = val;
1040 return writeAligned(u.ll);
1041}
1042
1043#else
1044
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001045status_t Parcel::writeDouble(double val)
1046{
Andreas Huber84a6d042009-08-17 13:33:27 -07001047 return writeAligned(val);
1048}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001049
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001050#endif
1051
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001052status_t Parcel::writeCString(const char* str)
1053{
1054 return write(str, strlen(str)+1);
1055}
1056
1057status_t Parcel::writeString8(const String8& str)
1058{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001059 return writeString8(str.string(), str.size());
1060}
1061
1062status_t Parcel::writeString8(const char* str, size_t len)
1063{
1064 if (str == nullptr) return writeInt32(-1);
1065
1066 status_t err = writeInt32(len);
1067 if (err == NO_ERROR) {
1068 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1069 if (data) {
1070 memcpy(data, str, len);
1071 *reinterpret_cast<char*>(data+len) = 0;
1072 return NO_ERROR;
1073 }
1074 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001075 }
1076 return err;
1077}
1078
Jooyung Han2d5878e2020-01-23 12:45:10 +09001079status_t Parcel::writeString16(const std::optional<String16>& str)
1080{
1081 if (!str) {
1082 return writeInt32(-1);
1083 }
1084
1085 return writeString16(*str);
1086}
1087
Casey Dahlinb9872622015-11-25 15:09:45 -08001088status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1089{
1090 if (!str) {
1091 return writeInt32(-1);
1092 }
1093
1094 return writeString16(*str);
1095}
1096
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001097status_t Parcel::writeString16(const String16& str)
1098{
1099 return writeString16(str.string(), str.size());
1100}
1101
1102status_t Parcel::writeString16(const char16_t* str, size_t len)
1103{
Yi Kong91635562018-06-07 14:38:36 -07001104 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001105
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001106 status_t err = writeInt32(len);
1107 if (err == NO_ERROR) {
1108 len *= sizeof(char16_t);
1109 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1110 if (data) {
1111 memcpy(data, str, len);
1112 *reinterpret_cast<char16_t*>(data+len) = 0;
1113 return NO_ERROR;
1114 }
1115 err = mError;
1116 }
1117 return err;
1118}
1119
1120status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1121{
Steven Morelanda86a3562019-08-01 23:28:34 +00001122 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001123}
1124
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001125status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1126{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001127 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001128}
1129
Jooyung Han2d5878e2020-01-23 12:45:10 +09001130status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val)
1131{
1132 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1133}
1134
Casey Dahlinb9872622015-11-25 15:09:45 -08001135status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1136{
1137 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1138}
1139
Jooyung Han2d5878e2020-01-23 12:45:10 +09001140status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const {
1141 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
1142}
1143
Casey Dahlinb9872622015-11-25 15:09:45 -08001144status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001145 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001146}
1147
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001148status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001149 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001150}
1151
Casey Dahlinb9872622015-11-25 15:09:45 -08001152status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1153 if (!parcelable) {
1154 return writeInt32(0);
1155 }
1156
1157 return writeParcelable(*parcelable);
1158}
1159
Christopher Wiley97f048d2015-11-19 06:49:05 -08001160status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1161 status_t status = writeInt32(1); // parcelable is not null.
1162 if (status != OK) {
1163 return status;
1164 }
1165 return parcelable.writeToParcel(this);
1166}
1167
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001168status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001169{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001170 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001171 return BAD_TYPE;
1172
1173 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001174 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001175 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001177 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001178 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001180 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1181 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182
1183 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001184 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001185 return err;
1186 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001187 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001188 return err;
1189}
1190
Jeff Brown93ff1f92011-11-04 19:01:44 -07001191status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001192{
1193 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001194 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001195 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001196 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001197 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001198 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001199 return writeObject(obj, true);
1200}
1201
1202status_t Parcel::writeDupFileDescriptor(int fd)
1203{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001204 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001205 if (dupFd < 0) {
1206 return -errno;
1207 }
1208 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001209 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001210 close(dupFd);
1211 }
1212 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001213}
1214
Dianne Hackborn1941a402016-08-29 12:30:43 -07001215status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1216{
1217 writeInt32(0);
1218 return writeFileDescriptor(fd, takeOwnership);
1219}
1220
Ryo Hashimotobf551892018-05-31 16:58:35 +09001221status_t Parcel::writeDupParcelFileDescriptor(int fd)
1222{
1223 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1224 if (dupFd < 0) {
1225 return -errno;
1226 }
1227 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1228 if (err != OK) {
1229 close(dupFd);
1230 }
1231 return err;
1232}
1233
Christopher Wiley2cf19952016-04-11 11:09:37 -07001234status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001235 return writeDupFileDescriptor(fd.get());
1236}
1237
Christopher Wiley2cf19952016-04-11 11:09:37 -07001238status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001239 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1240}
1241
Jooyung Han2d5878e2020-01-23 12:45:10 +09001242status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) {
1243 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1244}
1245
Christopher Wiley2cf19952016-04-11 11:09:37 -07001246status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001247 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1248}
1249
Jeff Brown13b16042014-11-11 16:44:25 -08001250status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001251{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001252 if (len > INT32_MAX) {
1253 // don't accept size_t values which may have come from an
1254 // inadvertent conversion from a negative int.
1255 return BAD_VALUE;
1256 }
1257
Jeff Brown13b16042014-11-11 16:44:25 -08001258 status_t status;
1259 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001260 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001261 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001262 if (status) return status;
1263
1264 void* ptr = writeInplace(len);
1265 if (!ptr) return NO_MEMORY;
1266
Jeff Brown13b16042014-11-11 16:44:25 -08001267 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001268 return NO_ERROR;
1269 }
1270
Steve Block6807e592011-10-20 11:56:00 +01001271 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001272 int fd = ashmem_create_region("Parcel Blob", len);
1273 if (fd < 0) return NO_MEMORY;
1274
1275 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1276 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001277 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001278 } else {
Yi Kong91635562018-06-07 14:38:36 -07001279 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001280 if (ptr == MAP_FAILED) {
1281 status = -errno;
1282 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001283 if (!mutableCopy) {
1284 result = ashmem_set_prot_region(fd, PROT_READ);
1285 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001286 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001287 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001288 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001289 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001290 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001291 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001292 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001293 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001294 return NO_ERROR;
1295 }
1296 }
1297 }
1298 }
1299 ::munmap(ptr, len);
1300 }
1301 ::close(fd);
1302 return status;
1303}
1304
Jeff Brown13b16042014-11-11 16:44:25 -08001305status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1306{
1307 // Must match up with what's done in writeBlob.
1308 if (!mAllowFds) return FDS_NOT_ALLOWED;
1309 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1310 if (status) return status;
1311 return writeDupFileDescriptor(fd);
1312}
1313
Mathias Agopiane1424282013-07-29 21:24:40 -07001314status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001315{
1316 status_t err;
1317
1318 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001319 const size_t len = val.getFlattenedSize();
1320 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001321
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001322 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001323 // don't accept size_t values which may have come from an
1324 // inadvertent conversion from a negative int.
1325 return BAD_VALUE;
1326 }
1327
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001328 err = this->writeInt32(len);
1329 if (err) return err;
1330
1331 err = this->writeInt32(fd_count);
1332 if (err) return err;
1333
1334 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001335 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001336 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001337 return BAD_VALUE;
1338
Yi Kong91635562018-06-07 14:38:36 -07001339 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001340 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001341 fds = new (std::nothrow) int[fd_count];
1342 if (fds == nullptr) {
1343 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1344 return BAD_VALUE;
1345 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001346 }
1347
1348 err = val.flatten(buf, len, fds, fd_count);
1349 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1350 err = this->writeDupFileDescriptor( fds[i] );
1351 }
1352
1353 if (fd_count) {
1354 delete [] fds;
1355 }
1356
1357 return err;
1358}
1359
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001360status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1361{
1362 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1363 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1364 if (enoughData && enoughObjects) {
1365restart_write:
1366 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001367
Christopher Tate98e67d32015-06-03 18:44:15 -07001368 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001369 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001370 if (!mAllowFds) {
1371 // fail before modifying our object index
1372 return FDS_NOT_ALLOWED;
1373 }
1374 mHasFds = mFdsKnown = true;
1375 }
1376
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001377 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001378 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001379 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001380 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001381 mObjectsSize++;
1382 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001383
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001384 return finishWrite(sizeof(flat_binder_object));
1385 }
1386
1387 if (!enoughData) {
1388 const status_t err = growData(sizeof(val));
1389 if (err != NO_ERROR) return err;
1390 }
1391 if (!enoughObjects) {
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01001392 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1393 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001394 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01001395 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001396 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001397 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001398 mObjects = objects;
1399 mObjectsCapacity = newSize;
1400 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001401
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001402 goto restart_write;
1403}
1404
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001405status_t Parcel::writeNoException()
1406{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001407 binder::Status status;
1408 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001409}
1410
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001411status_t Parcel::validateReadData(size_t upperBound) const
1412{
1413 // Don't allow non-object reads on object data
1414 if (mObjectsSorted || mObjectsSize <= 1) {
1415data_sorted:
1416 // Expect to check only against the next object
1417 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1418 // For some reason the current read position is greater than the next object
1419 // hint. Iterate until we find the right object
1420 size_t nextObject = mNextObjectHint;
1421 do {
1422 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1423 // Requested info overlaps with an object
1424 ALOGE("Attempt to read from protected data in Parcel %p", this);
1425 return PERMISSION_DENIED;
1426 }
1427 nextObject++;
1428 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1429 mNextObjectHint = nextObject;
1430 }
1431 return NO_ERROR;
1432 }
1433 // Quickly determine if mObjects is sorted.
1434 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1435 binder_size_t* prevObj = currObj;
1436 while (currObj > mObjects) {
1437 prevObj--;
1438 if(*prevObj > *currObj) {
1439 goto data_unsorted;
1440 }
1441 currObj--;
1442 }
1443 mObjectsSorted = true;
1444 goto data_sorted;
1445
1446data_unsorted:
1447 // Insertion Sort mObjects
1448 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1449 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1450 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1451 binder_size_t temp = *iter0;
1452 binder_size_t* iter1 = iter0 - 1;
1453 while (iter1 >= mObjects && *iter1 > temp) {
1454 *(iter1 + 1) = *iter1;
1455 iter1--;
1456 }
1457 *(iter1 + 1) = temp;
1458 }
1459 mNextObjectHint = 0;
1460 mObjectsSorted = true;
1461 goto data_sorted;
1462}
1463
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001464status_t Parcel::read(void* outData, size_t len) const
1465{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001466 if (len > INT32_MAX) {
1467 // don't accept size_t values which may have come from an
1468 // inadvertent conversion from a negative int.
1469 return BAD_VALUE;
1470 }
1471
1472 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1473 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001474 if (mObjectsSize > 0) {
1475 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001476 if(err != NO_ERROR) {
1477 // Still increment the data position by the expected length
1478 mDataPos += pad_size(len);
1479 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1480 return err;
1481 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001482 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001483 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001484 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001485 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001486 return NO_ERROR;
1487 }
1488 return NOT_ENOUGH_DATA;
1489}
1490
1491const void* Parcel::readInplace(size_t len) const
1492{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001493 if (len > INT32_MAX) {
1494 // don't accept size_t values which may have come from an
1495 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001496 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001497 }
1498
1499 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1500 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001501 if (mObjectsSize > 0) {
1502 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001503 if(err != NO_ERROR) {
1504 // Still increment the data position by the expected length
1505 mDataPos += pad_size(len);
1506 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001507 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001508 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001509 }
1510
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001511 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001512 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001513 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001514 return data;
1515 }
Yi Kong91635562018-06-07 14:38:36 -07001516 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001517}
1518
Andreas Huber84a6d042009-08-17 13:33:27 -07001519template<class T>
1520status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001521 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001522
1523 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001524 if (mObjectsSize > 0) {
1525 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001526 if(err != NO_ERROR) {
1527 // Still increment the data position by the expected length
1528 mDataPos += sizeof(T);
1529 return err;
1530 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001531 }
1532
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001533 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001534 mDataPos += sizeof(T);
1535 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001536 return NO_ERROR;
1537 } else {
1538 return NOT_ENOUGH_DATA;
1539 }
1540}
1541
Andreas Huber84a6d042009-08-17 13:33:27 -07001542template<class T>
1543T Parcel::readAligned() const {
1544 T result;
1545 if (readAligned(&result) != NO_ERROR) {
1546 result = 0;
1547 }
1548
1549 return result;
1550}
1551
1552template<class T>
1553status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001554 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001555
1556 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1557restart_write:
1558 *reinterpret_cast<T*>(mData+mDataPos) = val;
1559 return finishWrite(sizeof(val));
1560 }
1561
1562 status_t err = growData(sizeof(val));
1563 if (err == NO_ERROR) goto restart_write;
1564 return err;
1565}
1566
Casey Dahlin185d3442016-02-09 11:08:35 -08001567status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001568 size_t size;
1569 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1570 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001571}
1572
1573status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001574 size_t size;
1575 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1576 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001577}
1578
Jooyung Han2d5878e2020-01-23 12:45:10 +09001579status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const {
1580 size_t size;
1581 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1582 if (!*val) {
1583 // reserveOutVector does not create the out vector if size is < 0.
1584 // This occurs when writing a null byte vector.
1585 return OK;
1586 }
1587 return readByteVectorInternal(&**val, size);
1588}
1589
Casey Dahlin185d3442016-02-09 11:08:35 -08001590status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001591 size_t size;
1592 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001593 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001594 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001595 // This occurs when writing a null byte vector.
1596 return OK;
1597 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001598 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001599}
1600
Jooyung Han2d5878e2020-01-23 12:45:10 +09001601status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const {
1602 size_t size;
1603 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1604 if (!*val) {
1605 // reserveOutVector does not create the out vector if size is < 0.
1606 // This occurs when writing a null byte vector.
1607 return OK;
1608 }
1609 return readByteVectorInternal(&**val, size);
1610}
1611
Casey Dahlin185d3442016-02-09 11:08:35 -08001612status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001613 size_t size;
1614 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001615 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001616 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001617 // This occurs when writing a null byte vector.
1618 return OK;
1619 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001620 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001621}
1622
Jooyung Han2d5878e2020-01-23 12:45:10 +09001623status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const {
1624 return readNullableTypedVector(val, &Parcel::readInt32);
1625}
1626
Casey Dahlinb9872622015-11-25 15:09:45 -08001627status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1628 return readNullableTypedVector(val, &Parcel::readInt32);
1629}
1630
Casey Dahlin451ff582015-10-19 18:12:18 -07001631status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001632 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001633}
1634
Jooyung Han2d5878e2020-01-23 12:45:10 +09001635status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const {
1636 return readNullableTypedVector(val, &Parcel::readInt64);
1637}
1638
Casey Dahlinb9872622015-11-25 15:09:45 -08001639status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1640 return readNullableTypedVector(val, &Parcel::readInt64);
1641}
1642
Casey Dahlin451ff582015-10-19 18:12:18 -07001643status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001644 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001645}
1646
Jooyung Han2d5878e2020-01-23 12:45:10 +09001647status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const {
1648 return readNullableTypedVector(val, &Parcel::readUint64);
1649}
1650
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001651status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1652 return readNullableTypedVector(val, &Parcel::readUint64);
1653}
1654
1655status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1656 return readTypedVector(val, &Parcel::readUint64);
1657}
1658
Jooyung Han2d5878e2020-01-23 12:45:10 +09001659status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const {
1660 return readNullableTypedVector(val, &Parcel::readFloat);
1661}
1662
Casey Dahlinb9872622015-11-25 15:09:45 -08001663status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1664 return readNullableTypedVector(val, &Parcel::readFloat);
1665}
1666
Casey Dahlin451ff582015-10-19 18:12:18 -07001667status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001668 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001669}
1670
Jooyung Han2d5878e2020-01-23 12:45:10 +09001671status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const {
1672 return readNullableTypedVector(val, &Parcel::readDouble);
1673}
1674
Casey Dahlinb9872622015-11-25 15:09:45 -08001675status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1676 return readNullableTypedVector(val, &Parcel::readDouble);
1677}
1678
Casey Dahlin451ff582015-10-19 18:12:18 -07001679status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001680 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001681}
1682
Jooyung Han2d5878e2020-01-23 12:45:10 +09001683status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const {
1684 const int32_t start = dataPosition();
1685 int32_t size;
1686 status_t status = readInt32(&size);
1687 val->reset();
1688
1689 if (status != OK || size < 0) {
1690 return status;
1691 }
1692
1693 setDataPosition(start);
1694 val->emplace();
1695
1696 status = readBoolVector(&**val);
1697
1698 if (status != OK) {
1699 val->reset();
1700 }
1701
1702 return status;
1703}
1704
Casey Dahlinb9872622015-11-25 15:09:45 -08001705status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1706 const int32_t start = dataPosition();
1707 int32_t size;
1708 status_t status = readInt32(&size);
1709 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001710
Casey Dahlinb9872622015-11-25 15:09:45 -08001711 if (status != OK || size < 0) {
1712 return status;
1713 }
1714
1715 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001716 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001717
1718 status = readBoolVector(val->get());
1719
1720 if (status != OK) {
1721 val->reset();
1722 }
1723
1724 return status;
1725}
1726
1727status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001728 int32_t size;
1729 status_t status = readInt32(&size);
1730
1731 if (status != OK) {
1732 return status;
1733 }
1734
1735 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001736 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001737 }
1738
1739 val->resize(size);
1740
1741 /* C++ bool handling means a vector of bools isn't necessarily addressable
1742 * (we might use individual bits)
1743 */
Christopher Wiley97887982015-10-27 16:33:47 -07001744 bool data;
1745 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001746 status = readBool(&data);
1747 (*val)[i] = data;
1748
1749 if (status != OK) {
1750 return status;
1751 }
1752 }
1753
1754 return OK;
1755}
1756
Jooyung Han2d5878e2020-01-23 12:45:10 +09001757status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const {
1758 return readNullableTypedVector(val, &Parcel::readChar);
1759}
1760
Casey Dahlinb9872622015-11-25 15:09:45 -08001761status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1762 return readNullableTypedVector(val, &Parcel::readChar);
1763}
1764
Casey Dahlin451ff582015-10-19 18:12:18 -07001765status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001766 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001767}
1768
Casey Dahlinb9872622015-11-25 15:09:45 -08001769status_t Parcel::readString16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +09001770 std::optional<std::vector<std::optional<String16>>>* val) const {
1771 return readNullableTypedVector(val, &Parcel::readString16);
1772}
1773
1774status_t Parcel::readString16Vector(
Casey Dahlinb9872622015-11-25 15:09:45 -08001775 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1776 return readNullableTypedVector(val, &Parcel::readString16);
1777}
1778
Casey Dahlin451ff582015-10-19 18:12:18 -07001779status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001780 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001781}
1782
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001783status_t Parcel::readUtf8VectorFromUtf16Vector(
Jooyung Han2d5878e2020-01-23 12:45:10 +09001784 std::optional<std::vector<std::optional<std::string>>>* val) const {
1785 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1786}
1787
1788status_t Parcel::readUtf8VectorFromUtf16Vector(
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001789 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1790 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1791}
1792
1793status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1794 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1795}
Casey Dahlin451ff582015-10-19 18:12:18 -07001796
Andreas Huber84a6d042009-08-17 13:33:27 -07001797status_t Parcel::readInt32(int32_t *pArg) const
1798{
1799 return readAligned(pArg);
1800}
1801
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001802int32_t Parcel::readInt32() const
1803{
Andreas Huber84a6d042009-08-17 13:33:27 -07001804 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001805}
1806
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001807status_t Parcel::readUint32(uint32_t *pArg) const
1808{
1809 return readAligned(pArg);
1810}
1811
1812uint32_t Parcel::readUint32() const
1813{
1814 return readAligned<uint32_t>();
1815}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001816
1817status_t Parcel::readInt64(int64_t *pArg) const
1818{
Andreas Huber84a6d042009-08-17 13:33:27 -07001819 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001820}
1821
1822
1823int64_t Parcel::readInt64() const
1824{
Andreas Huber84a6d042009-08-17 13:33:27 -07001825 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001826}
1827
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001828status_t Parcel::readUint64(uint64_t *pArg) const
1829{
1830 return readAligned(pArg);
1831}
1832
1833uint64_t Parcel::readUint64() const
1834{
1835 return readAligned<uint64_t>();
1836}
1837
Serban Constantinescuf683e012013-11-05 16:53:55 +00001838status_t Parcel::readPointer(uintptr_t *pArg) const
1839{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001840 status_t ret;
1841 binder_uintptr_t ptr;
1842 ret = readAligned(&ptr);
1843 if (!ret)
1844 *pArg = ptr;
1845 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001846}
1847
1848uintptr_t Parcel::readPointer() const
1849{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001850 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001851}
1852
1853
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001854status_t Parcel::readFloat(float *pArg) const
1855{
Andreas Huber84a6d042009-08-17 13:33:27 -07001856 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001857}
1858
1859
1860float Parcel::readFloat() const
1861{
Andreas Huber84a6d042009-08-17 13:33:27 -07001862 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001863}
1864
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001865#if defined(__mips__) && defined(__mips_hard_float)
1866
1867status_t Parcel::readDouble(double *pArg) const
1868{
1869 union {
1870 double d;
1871 unsigned long long ll;
1872 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001873 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001874 status_t status;
1875 status = readAligned(&u.ll);
1876 *pArg = u.d;
1877 return status;
1878}
1879
1880double Parcel::readDouble() const
1881{
1882 union {
1883 double d;
1884 unsigned long long ll;
1885 } u;
1886 u.ll = readAligned<unsigned long long>();
1887 return u.d;
1888}
1889
1890#else
1891
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001892status_t Parcel::readDouble(double *pArg) const
1893{
Andreas Huber84a6d042009-08-17 13:33:27 -07001894 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001895}
1896
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001897double Parcel::readDouble() const
1898{
Andreas Huber84a6d042009-08-17 13:33:27 -07001899 return readAligned<double>();
1900}
1901
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001902#endif
1903
Andreas Huber84a6d042009-08-17 13:33:27 -07001904status_t Parcel::readIntPtr(intptr_t *pArg) const
1905{
1906 return readAligned(pArg);
1907}
1908
1909
1910intptr_t Parcel::readIntPtr() const
1911{
1912 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001913}
1914
Casey Dahlind6848f52015-10-15 15:44:59 -07001915status_t Parcel::readBool(bool *pArg) const
1916{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001917 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001918 status_t ret = readInt32(&tmp);
1919 *pArg = (tmp != 0);
1920 return ret;
1921}
1922
1923bool Parcel::readBool() const
1924{
1925 return readInt32() != 0;
1926}
1927
1928status_t Parcel::readChar(char16_t *pArg) const
1929{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001930 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001931 status_t ret = readInt32(&tmp);
1932 *pArg = char16_t(tmp);
1933 return ret;
1934}
1935
1936char16_t Parcel::readChar() const
1937{
1938 return char16_t(readInt32());
1939}
1940
1941status_t Parcel::readByte(int8_t *pArg) const
1942{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001943 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001944 status_t ret = readInt32(&tmp);
1945 *pArg = int8_t(tmp);
1946 return ret;
1947}
1948
1949int8_t Parcel::readByte() const
1950{
1951 return int8_t(readInt32());
1952}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001953
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001954status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1955 size_t utf16Size = 0;
1956 const char16_t* src = readString16Inplace(&utf16Size);
1957 if (!src) {
1958 return UNEXPECTED_NULL;
1959 }
1960
1961 // Save ourselves the trouble, we're done.
1962 if (utf16Size == 0u) {
1963 str->clear();
1964 return NO_ERROR;
1965 }
1966
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001967 // Allow for closing '\0'
1968 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1969 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001970 return BAD_VALUE;
1971 }
1972 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001973 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001974 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001975 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1976 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001977 return NO_ERROR;
1978}
1979
Jooyung Han2d5878e2020-01-23 12:45:10 +09001980status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const {
1981 const int32_t start = dataPosition();
1982 int32_t size;
1983 status_t status = readInt32(&size);
1984 str->reset();
1985
1986 if (status != OK || size < 0) {
1987 return status;
1988 }
1989
1990 setDataPosition(start);
1991 str->emplace();
1992 return readUtf8FromUtf16(&**str);
1993}
1994
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001995status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1996 const int32_t start = dataPosition();
1997 int32_t size;
1998 status_t status = readInt32(&size);
1999 str->reset();
2000
2001 if (status != OK || size < 0) {
2002 return status;
2003 }
2004
2005 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002006 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002007 return readUtf8FromUtf16(str->get());
2008}
2009
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002010const char* Parcel::readCString() const
2011{
Steven Morelandf5e6c7e2019-05-17 13:14:06 -07002012 if (mDataPos < mDataSize) {
2013 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002014 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2015 // is the string's trailing NUL within the parcel's valid bounds?
2016 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2017 if (eos) {
2018 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002019 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002020 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002021 return str;
2022 }
2023 }
Yi Kong91635562018-06-07 14:38:36 -07002024 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002025}
2026
2027String8 Parcel::readString8() const
2028{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002029 size_t len;
2030 const char* str = readString8Inplace(&len);
2031 if (str) return String8(str, len);
2032 ALOGE("Reading a NULL string not supported here.");
2033 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002034}
2035
2036status_t Parcel::readString8(String8* pArg) const
2037{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002038 size_t len;
2039 const char* str = readString8Inplace(&len);
2040 if (str) {
2041 pArg->setTo(str, len);
2042 return 0;
2043 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002044 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002045 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002046 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002047}
2048
2049const char* Parcel::readString8Inplace(size_t* outLen) const
2050{
2051 int32_t size = readInt32();
2052 // watch for potential int overflow from size+1
2053 if (size >= 0 && size < INT32_MAX) {
2054 *outLen = size;
2055 const char* str = (const char*)readInplace(size+1);
2056 if (str != nullptr) {
2057 return str;
2058 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002059 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002060 *outLen = 0;
2061 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002062}
2063
2064String16 Parcel::readString16() const
2065{
2066 size_t len;
2067 const char16_t* str = readString16Inplace(&len);
2068 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002069 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002070 return String16();
2071}
2072
Jooyung Han2d5878e2020-01-23 12:45:10 +09002073status_t Parcel::readString16(std::optional<String16>* pArg) const
2074{
2075 const int32_t start = dataPosition();
2076 int32_t size;
2077 status_t status = readInt32(&size);
2078 pArg->reset();
2079
2080 if (status != OK || size < 0) {
2081 return status;
2082 }
2083
2084 setDataPosition(start);
2085 pArg->emplace();
2086
2087 status = readString16(&**pArg);
2088
2089 if (status != OK) {
2090 pArg->reset();
2091 }
2092
2093 return status;
2094}
2095
Casey Dahlinb9872622015-11-25 15:09:45 -08002096status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2097{
2098 const int32_t start = dataPosition();
2099 int32_t size;
2100 status_t status = readInt32(&size);
2101 pArg->reset();
2102
2103 if (status != OK || size < 0) {
2104 return status;
2105 }
2106
2107 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002108 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002109
2110 status = readString16(pArg->get());
2111
2112 if (status != OK) {
2113 pArg->reset();
2114 }
2115
2116 return status;
2117}
2118
Casey Dahlin451ff582015-10-19 18:12:18 -07002119status_t Parcel::readString16(String16* pArg) const
2120{
2121 size_t len;
2122 const char16_t* str = readString16Inplace(&len);
2123 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002124 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002125 return 0;
2126 } else {
2127 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002128 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002129 }
2130}
2131
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002132const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2133{
2134 int32_t size = readInt32();
2135 // watch for potential int overflow from size+1
2136 if (size >= 0 && size < INT32_MAX) {
2137 *outLen = size;
2138 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002139 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002140 return str;
2141 }
2142 }
2143 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002144 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002145}
2146
Casey Dahlinf0c13772015-10-27 18:33:56 -07002147status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2148{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002149 status_t status = readNullableStrongBinder(val);
2150 if (status == OK && !val->get()) {
2151 status = UNEXPECTED_NULL;
2152 }
2153 return status;
2154}
2155
2156status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2157{
Steven Morelanda86a3562019-08-01 23:28:34 +00002158 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002159}
2160
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002161sp<IBinder> Parcel::readStrongBinder() const
2162{
2163 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002164 // Note that a lot of code in Android reads binders by hand with this
2165 // method, and that code has historically been ok with getting nullptr
2166 // back (while ignoring error codes).
2167 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002168 return val;
2169}
2170
Christopher Wiley97f048d2015-11-19 06:49:05 -08002171status_t Parcel::readParcelable(Parcelable* parcelable) const {
2172 int32_t have_parcelable = 0;
2173 status_t status = readInt32(&have_parcelable);
2174 if (status != OK) {
2175 return status;
2176 }
2177 if (!have_parcelable) {
2178 return UNEXPECTED_NULL;
2179 }
2180 return parcelable->readFromParcel(this);
2181}
2182
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002183int32_t Parcel::readExceptionCode() const
2184{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002185 binder::Status status;
2186 status.readFromParcel(*this);
2187 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002188}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002189
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002190native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002191{
2192 int numFds, numInts;
2193 status_t err;
2194 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002195 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002196 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002197 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002198
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002199 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002200 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002201 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002202 }
2203
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002204 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002205 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002206 if (h->data[i] < 0) {
2207 for (int j = 0; j < i; j++) {
2208 close(h->data[j]);
2209 }
2210 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002211 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002212 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002213 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002214 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002215 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002216 native_handle_close(h);
2217 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002218 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002219 }
2220 return h;
2221}
2222
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002223int Parcel::readFileDescriptor() const
2224{
2225 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002226
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002227 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002228 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002229 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002230
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231 return BAD_TYPE;
2232}
2233
Dianne Hackborn1941a402016-08-29 12:30:43 -07002234int Parcel::readParcelFileDescriptor() const
2235{
2236 int32_t hasComm = readInt32();
2237 int fd = readFileDescriptor();
2238 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002239 // detach (owned by the binder driver)
2240 int comm = readFileDescriptor();
2241
2242 // warning: this must be kept in sync with:
2243 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2244 enum ParcelFileDescriptorStatus {
2245 DETACHED = 2,
2246 };
2247
2248#if BYTE_ORDER == BIG_ENDIAN
2249 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2250#endif
2251#if BYTE_ORDER == LITTLE_ENDIAN
2252 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2253#endif
2254
2255 ssize_t written = TEMP_FAILURE_RETRY(
2256 ::write(comm, &message, sizeof(message)));
2257
2258 if (written == -1 || written != sizeof(message)) {
2259 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2260 written, strerror(errno));
2261 return BAD_TYPE;
2262 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002263 }
2264 return fd;
2265}
2266
Christopher Wiley2cf19952016-04-11 11:09:37 -07002267status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002268{
2269 int got = readFileDescriptor();
2270
2271 if (got == BAD_TYPE) {
2272 return BAD_TYPE;
2273 }
2274
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002275 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002276
2277 if (val->get() < 0) {
2278 return BAD_VALUE;
2279 }
2280
2281 return OK;
2282}
2283
Ryo Hashimotobf551892018-05-31 16:58:35 +09002284status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2285{
2286 int got = readParcelFileDescriptor();
2287
2288 if (got == BAD_TYPE) {
2289 return BAD_TYPE;
2290 }
2291
2292 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2293
2294 if (val->get() < 0) {
2295 return BAD_VALUE;
2296 }
2297
2298 return OK;
2299}
Casey Dahlin06673e32015-11-23 13:24:23 -08002300
Jooyung Han2d5878e2020-01-23 12:45:10 +09002301status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const {
2302 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2303}
2304
Christopher Wiley2cf19952016-04-11 11:09:37 -07002305status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002306 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2307}
2308
Christopher Wiley2cf19952016-04-11 11:09:37 -07002309status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002310 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2311}
2312
Jeff Brown5707dbf2011-09-23 21:17:56 -07002313status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2314{
Jeff Brown13b16042014-11-11 16:44:25 -08002315 int32_t blobType;
2316 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002317 if (status) return status;
2318
Jeff Brown13b16042014-11-11 16:44:25 -08002319 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002320 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002321 const void* ptr = readInplace(len);
2322 if (!ptr) return BAD_VALUE;
2323
Jeff Brown13b16042014-11-11 16:44:25 -08002324 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002325 return NO_ERROR;
2326 }
2327
Steve Block6807e592011-10-20 11:56:00 +01002328 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002329 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002330 int fd = readFileDescriptor();
2331 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2332
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002333 if (!ashmem_valid(fd)) {
2334 ALOGE("invalid fd");
2335 return BAD_VALUE;
2336 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002337 int size = ashmem_get_size_region(fd);
2338 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002339 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002340 return BAD_VALUE;
2341 }
Yi Kong91635562018-06-07 14:38:36 -07002342 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002343 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002344 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002345
Jeff Brown13b16042014-11-11 16:44:25 -08002346 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002347 return NO_ERROR;
2348}
2349
Mathias Agopiane1424282013-07-29 21:24:40 -07002350status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002351{
2352 // size
2353 const size_t len = this->readInt32();
2354 const size_t fd_count = this->readInt32();
2355
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002356 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002357 // don't accept size_t values which may have come from an
2358 // inadvertent conversion from a negative int.
2359 return BAD_VALUE;
2360 }
2361
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002362 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002363 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002364 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002365 return BAD_VALUE;
2366
Yi Kong91635562018-06-07 14:38:36 -07002367 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002368 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002369 fds = new (std::nothrow) int[fd_count];
2370 if (fds == nullptr) {
2371 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2372 return BAD_VALUE;
2373 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002374 }
2375
2376 status_t err = NO_ERROR;
2377 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002378 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002379 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002380 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002381 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 -07002382 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2383 // Close all the file descriptors that were dup-ed.
2384 for (size_t j=0; j<i ;j++) {
2385 close(fds[j]);
2386 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002387 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002388 }
2389
2390 if (err == NO_ERROR) {
2391 err = val.unflatten(buf, len, fds, fd_count);
2392 }
2393
2394 if (fd_count) {
2395 delete [] fds;
2396 }
2397
2398 return err;
2399}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002400const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2401{
2402 const size_t DPOS = mDataPos;
2403 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2404 const flat_binder_object* obj
2405 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2406 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002407 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002408 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002409 // the object list, so we don't want to check for it when
2410 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002411 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002412 return obj;
2413 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002414
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002415 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002416 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002417 const size_t N = mObjectsSize;
2418 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002419
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002420 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002421 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002422 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002423
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002424 // Start at the current hint position, looking for an object at
2425 // the current data position.
2426 if (opos < N) {
2427 while (opos < (N-1) && OBJS[opos] < DPOS) {
2428 opos++;
2429 }
2430 } else {
2431 opos = N-1;
2432 }
2433 if (OBJS[opos] == DPOS) {
2434 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002435 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002436 this, DPOS, opos);
2437 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002438 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002439 return obj;
2440 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002441
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002442 // Look backwards for it...
2443 while (opos > 0 && OBJS[opos] > DPOS) {
2444 opos--;
2445 }
2446 if (OBJS[opos] == DPOS) {
2447 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002448 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002449 this, DPOS, opos);
2450 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002451 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002452 return obj;
2453 }
2454 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002455 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 -07002456 this, DPOS);
2457 }
Yi Kong91635562018-06-07 14:38:36 -07002458 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002459}
2460
2461void Parcel::closeFileDescriptors()
2462{
2463 size_t i = mObjectsSize;
2464 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002465 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002466 }
2467 while (i > 0) {
2468 i--;
2469 const flat_binder_object* flat
2470 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002471 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002472 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002473 close(flat->handle);
2474 }
2475 }
2476}
2477
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002478uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002480 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002481}
2482
2483size_t Parcel::ipcDataSize() const
2484{
2485 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2486}
2487
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002488uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002489{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002490 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002491}
2492
2493size_t Parcel::ipcObjectsCount() const
2494{
2495 return mObjectsSize;
2496}
2497
2498void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002499 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002500{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002501 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002502 freeDataNoInit();
2503 mError = NO_ERROR;
2504 mData = const_cast<uint8_t*>(data);
2505 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002506 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002507 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002508 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002509 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002510 mObjectsSize = mObjectsCapacity = objectsCount;
2511 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002512 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513 mOwner = relFunc;
2514 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002515 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002516 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002517 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002518 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002519 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002520 mObjectsSize = 0;
2521 break;
2522 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002523 const flat_binder_object* flat
2524 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2525 uint32_t type = flat->hdr.type;
2526 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2527 type == BINDER_TYPE_FD)) {
2528 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2529 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2530 // recover gracefully by clearing out the objects, and releasing the objects we do
2531 // know about.
2532 android_errorWriteLog(0x534e4554, "135930648");
2533 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2534 __func__, type, (uint64_t)offset);
2535 releaseObjects();
2536 mObjectsSize = 0;
2537 break;
2538 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002539 minOffset = offset + sizeof(flat_binder_object);
2540 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002541 scanForFds();
2542}
2543
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002544void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002545{
2546 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002547
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002548 if (errorCheck() != NO_ERROR) {
2549 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002550 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002551 } else if (dataSize() > 0) {
2552 const uint8_t* DATA = data();
2553 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002554 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002555 const size_t N = objectsCount();
2556 for (size_t i=0; i<N; i++) {
2557 const flat_binder_object* flat
2558 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2559 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002560 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002561 << " = " << flat->binder;
2562 }
2563 } else {
2564 to << "NULL";
2565 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002566
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002567 to << ")";
2568}
2569
2570void Parcel::releaseObjects()
2571{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002572 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002573 if (i == 0) {
2574 return;
2575 }
2576 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002577 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002578 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002579 while (i > 0) {
2580 i--;
2581 const flat_binder_object* flat
2582 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002583 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002584 }
2585}
2586
2587void Parcel::acquireObjects()
2588{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002589 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002590 if (i == 0) {
2591 return;
2592 }
2593 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002594 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002595 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002596 while (i > 0) {
2597 i--;
2598 const flat_binder_object* flat
2599 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002600 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002601 }
2602}
2603
2604void Parcel::freeData()
2605{
2606 freeDataNoInit();
2607 initState();
2608}
2609
2610void Parcel::freeDataNoInit()
2611{
2612 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002613 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002614 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002615 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2616 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002617 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002618 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002619 if (mData) {
2620 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey9de06a92020-09-11 12:07:10 -06002621 gParcelGlobalAllocSize -= mDataCapacity;
2622 gParcelGlobalAllocCount--;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002623 free(mData);
2624 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002625 if (mObjects) free(mObjects);
2626 }
2627}
2628
2629status_t Parcel::growData(size_t len)
2630{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002631 if (len > INT32_MAX) {
2632 // don't accept size_t values which may have come from an
2633 // inadvertent conversion from a negative int.
2634 return BAD_VALUE;
2635 }
2636
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01002637 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2638 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002639 size_t newSize = ((mDataSize+len)*3)/2;
2640 return (newSize <= mDataSize)
2641 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002642 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002643}
2644
2645status_t Parcel::restartWrite(size_t desired)
2646{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002647 if (desired > INT32_MAX) {
2648 // don't accept size_t values which may have come from an
2649 // inadvertent conversion from a negative int.
2650 return BAD_VALUE;
2651 }
2652
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002653 if (mOwner) {
2654 freeData();
2655 return continueWrite(desired);
2656 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002657
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002658 uint8_t* data = (uint8_t*)realloc(mData, desired);
2659 if (!data && desired > mDataCapacity) {
2660 mError = NO_MEMORY;
2661 return NO_MEMORY;
2662 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002663
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002664 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002665
Devin Moore4a0a55e2020-06-04 13:23:10 -07002666 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002667 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey9de06a92020-09-11 12:07:10 -06002668 if (mDataCapacity > desired) {
2669 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2670 } else {
2671 gParcelGlobalAllocSize += (desired - mDataCapacity);
2672 }
2673
Colin Cross83ec65e2015-12-08 17:15:50 -08002674 if (!mData) {
2675 gParcelGlobalAllocCount++;
2676 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002677 mData = data;
2678 mDataCapacity = desired;
2679 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002680
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002681 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002682 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2683 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2684
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002685 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002686 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002687 mObjectsSize = mObjectsCapacity = 0;
2688 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002689 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002690 mHasFds = false;
2691 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002692 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002693
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002694 return NO_ERROR;
2695}
2696
2697status_t Parcel::continueWrite(size_t desired)
2698{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002699 if (desired > INT32_MAX) {
2700 // don't accept size_t values which may have come from an
2701 // inadvertent conversion from a negative int.
2702 return BAD_VALUE;
2703 }
2704
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002705 // If shrinking, first adjust for any objects that appear
2706 // after the new data size.
2707 size_t objectsSize = mObjectsSize;
2708 if (desired < mDataSize) {
2709 if (desired == 0) {
2710 objectsSize = 0;
2711 } else {
2712 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002713 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002714 break;
2715 objectsSize--;
2716 }
2717 }
2718 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002719
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002720 if (mOwner) {
2721 // If the size is going to zero, just release the owner's data.
2722 if (desired == 0) {
2723 freeData();
2724 return NO_ERROR;
2725 }
2726
2727 // If there is a different owner, we need to take
2728 // posession.
2729 uint8_t* data = (uint8_t*)malloc(desired);
2730 if (!data) {
2731 mError = NO_MEMORY;
2732 return NO_MEMORY;
2733 }
Yi Kong91635562018-06-07 14:38:36 -07002734 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002735
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002736 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002737 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002738 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002739 free(data);
2740
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002741 mError = NO_MEMORY;
2742 return NO_MEMORY;
2743 }
2744
2745 // Little hack to only acquire references on objects
2746 // we will be keeping.
2747 size_t oldObjectsSize = mObjectsSize;
2748 mObjectsSize = objectsSize;
2749 acquireObjects();
2750 mObjectsSize = oldObjectsSize;
2751 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002752
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002753 if (mData) {
2754 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2755 }
2756 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002757 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002758 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002759 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002760 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002761 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002762
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002763 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002764 gParcelGlobalAllocSize += desired;
2765 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002766
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002767 mData = data;
2768 mObjects = objects;
2769 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002770 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002771 mDataCapacity = desired;
2772 mObjectsSize = mObjectsCapacity = objectsSize;
2773 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002774 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002775
2776 } else if (mData) {
2777 if (objectsSize < mObjectsSize) {
2778 // Need to release refs on any objects we are dropping.
2779 const sp<ProcessState> proc(ProcessState::self());
2780 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2781 const flat_binder_object* flat
2782 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002783 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002784 // will need to rescan because we may have lopped off the only FDs
2785 mFdsKnown = false;
2786 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002787 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002788 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002789
2790 if (objectsSize == 0) {
2791 free(mObjects);
2792 mObjects = nullptr;
Michael Wachenschwanzc67d9f32019-10-15 11:49:22 -07002793 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002794 } else {
2795 binder_size_t* objects =
2796 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2797 if (objects) {
2798 mObjects = objects;
Michael Wachenschwanzc67d9f32019-10-15 11:49:22 -07002799 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002800 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002801 }
2802 mObjectsSize = objectsSize;
2803 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002804 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002805 }
2806
2807 // We own the data, so we can just do a realloc().
2808 if (desired > mDataCapacity) {
2809 uint8_t* data = (uint8_t*)realloc(mData, desired);
2810 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002811 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2812 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002813 gParcelGlobalAllocSize += desired;
2814 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002815 mData = data;
2816 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002817 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002818 mError = NO_MEMORY;
2819 return NO_MEMORY;
2820 }
2821 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002822 if (mDataSize > desired) {
2823 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002824 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002825 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002826 if (mDataPos > desired) {
2827 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002828 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002829 }
2830 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002831
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002832 } else {
2833 // This is the first data. Easy!
2834 uint8_t* data = (uint8_t*)malloc(desired);
2835 if (!data) {
2836 mError = NO_MEMORY;
2837 return NO_MEMORY;
2838 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002839
Yi Kong91635562018-06-07 14:38:36 -07002840 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002841 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002842 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002843 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002844
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002845 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002846 gParcelGlobalAllocSize += desired;
2847 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002848
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002849 mData = data;
2850 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002851 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2852 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002853 mDataCapacity = desired;
2854 }
2855
2856 return NO_ERROR;
2857}
2858
2859void Parcel::initState()
2860{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002861 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002862 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002863 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002864 mDataSize = 0;
2865 mDataCapacity = 0;
2866 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002867 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2868 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002869 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002870 mObjectsSize = 0;
2871 mObjectsCapacity = 0;
2872 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002873 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002874 mHasFds = false;
2875 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002876 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002877 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002878 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002879 mWorkSourceRequestHeaderPosition = 0;
2880 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002881
2882 // racing multiple init leads only to multiple identical write
2883 if (gMaxFds == 0) {
2884 struct rlimit result;
2885 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2886 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002887 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002888 } else {
2889 ALOGW("Unable to getrlimit: %s", strerror(errno));
2890 gMaxFds = 1024;
2891 }
2892 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002893}
2894
2895void Parcel::scanForFds() const
2896{
2897 bool hasFds = false;
2898 for (size_t i=0; i<mObjectsSize; i++) {
2899 const flat_binder_object* flat
2900 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002901 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002902 hasFds = true;
2903 break;
2904 }
2905 }
2906 mHasFds = hasFds;
2907 mFdsKnown = true;
2908}
2909
Dan Sandleraa5c2342015-04-10 10:08:45 -04002910size_t Parcel::getBlobAshmemSize() const
2911{
Adrian Roos6bb31142015-10-22 16:46:12 -07002912 // This used to return the size of all blobs that were written to ashmem, now we're returning
2913 // the ashmem currently referenced by this Parcel, which should be equivalent.
2914 // TODO: Remove method once ABI can be changed.
2915 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002916}
2917
Adrian Rooscbf37262015-10-22 16:12:53 -07002918size_t Parcel::getOpenAshmemSize() const
2919{
2920 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002921}
2922
2923// --- Parcel::Blob ---
2924
2925Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002926 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002927}
2928
2929Parcel::Blob::~Blob() {
2930 release();
2931}
2932
2933void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002934 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002935 ::munmap(mData, mSize);
2936 }
2937 clear();
2938}
2939
Jeff Brown13b16042014-11-11 16:44:25 -08002940void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2941 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002942 mData = data;
2943 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002944 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002945}
2946
2947void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002948 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002949 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002950 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002951 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002952}
2953
Steven Moreland6511af52019-09-26 16:05:45 -07002954} // namespace android