blob: 64a4f9bf6d249012ffcdb1a5124dfa31c6ee013e [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 Morelandb96ea772020-07-16 22:43:02 +000023#include <linux/sched.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080024#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080029#include <sys/stat.h>
30#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070031#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080032#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070033
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/Binder.h>
35#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080036#include <binder/IPCThreadState.h>
37#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070038#include <binder/ProcessState.h>
Steven Moreland6e5a7752019-08-05 20:30:14 -070039#include <binder/Stability.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080040#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041#include <binder/TextOutput.h>
42
Mark Salyzynabed7f72016-01-27 08:02:48 -080043#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070044#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080045#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080047#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070048#include <utils/String8.h>
49#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070050
Mathias Agopian208059f2009-05-18 15:08:03 -070051#include <private/binder/binder_module.h>
Steven Morelanda4853cd2019-07-12 15:44:37 -070052#include "Static.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070054#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080055//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080056#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080057//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070058
59// ---------------------------------------------------------------------------
60
Nick Kralevichb6b14232015-04-02 09:36:02 -070061// This macro should never be used at runtime, as a too large value
62// of s could cause an integer overflow. Instead, you should always
63// use the wrapper function pad_size()
64#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
65
66static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070067 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070068 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070069 }
70 return PAD_SIZE_UNSAFE(s);
71}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070072
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070073// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060074#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070075
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070076namespace android {
77
Steven Moreland7b102262019-08-01 15:48:43 -070078// many things compile this into prebuilts on the stack
79static_assert(sizeof(Parcel) == 60 || sizeof(Parcel) == 120);
80
Dianne Hackborna4cff882014-11-13 17:07:40 -080081static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
82static size_t gParcelGlobalAllocSize = 0;
83static size_t gParcelGlobalAllocCount = 0;
84
Christopher Tatee4e0ae82016-03-24 16:03:44 -070085static size_t gMaxFds = 0;
86
Jeff Brown13b16042014-11-11 16:44:25 -080087// Maximum size of a blob to transfer in-place.
88static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
89
90enum {
91 BLOB_INPLACE = 0,
92 BLOB_ASHMEM_IMMUTABLE = 1,
93 BLOB_ASHMEM_MUTABLE = 2,
94};
95
Steven Morelandb1c81202019-04-05 18:49:55 -070096static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070097 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -070099 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700100 case BINDER_TYPE_BINDER:
101 if (obj.binder) {
102 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800103 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104 }
105 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106 case BINDER_TYPE_HANDLE: {
107 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700108 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
110 b->incStrong(who);
111 }
112 return;
113 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700114 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000115 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700116 // If we own an ashmem fd, keep track of how much memory it refers to.
117 int size = ashmem_get_size_region(obj.handle);
118 if (size > 0) {
119 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700120 }
121 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700122 return;
123 }
124 }
125
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700126 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700127}
128
Adrian Roos6bb31142015-10-22 16:46:12 -0700129static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700130 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700131{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700132 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700133 case BINDER_TYPE_BINDER:
134 if (obj.binder) {
135 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800136 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700137 }
138 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 case BINDER_TYPE_HANDLE: {
140 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700141 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
143 b->decStrong(who);
144 }
145 return;
146 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800148 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000149 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700150 int size = ashmem_get_size_region(obj.handle);
151 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800152 // ashmem size might have changed since last time it was accounted for, e.g.
153 // in acquire_object(). Value of *outAshmemSize is not critical since we are
154 // releasing the object anyway. Check for integer overflow condition.
155 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700156 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700157 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800158
159 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700160 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700161 return;
162 }
163 }
164
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700165 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700166}
167
Steven Morelanda86a3562019-08-01 23:28:34 +0000168status_t Parcel::finishFlattenBinder(
169 const sp<IBinder>& binder, const flat_binder_object& flat)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700170{
Steven Morelanda86a3562019-08-01 23:28:34 +0000171 status_t status = writeObject(flat, false);
172 if (status != OK) return status;
173
Steven Moreland6e5a7752019-08-05 20:30:14 -0700174 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Morelanda86a3562019-08-01 23:28:34 +0000175 return writeInt32(internal::Stability::get(binder.get()));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700176}
177
Steven Morelanda86a3562019-08-01 23:28:34 +0000178status_t Parcel::finishUnflattenBinder(
179 const sp<IBinder>& binder, sp<IBinder>* out) const
180{
181 int32_t stability;
182 status_t status = readInt32(&stability);
183 if (status != OK) return status;
184
Steven Moreland05929552019-07-31 17:51:25 -0700185 status = internal::Stability::set(binder.get(), stability, true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000186 if (status != OK) return status;
187
188 *out = binder;
189 return OK;
190}
191
Steven Morelandb96ea772020-07-16 22:43:02 +0000192static constexpr inline int schedPolicyMask(int policy, int priority) {
193 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
194}
195
Steven Morelanda86a3562019-08-01 23:28:34 +0000196status_t Parcel::flattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700197{
198 flat_binder_object obj;
Steven Morelandb96ea772020-07-16 22:43:02 +0000199 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700200
Steven Morelandb96ea772020-07-16 22:43:02 +0000201 int schedBits = 0;
202 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
203 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700204 }
205
Yi Kong91635562018-06-07 14:38:36 -0700206 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800207 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700208 if (!local) {
209 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700210 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000211 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700212 }
213 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700214 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800215 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700216 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800217 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700218 } else {
Steven Morelandb96ea772020-07-16 22:43:02 +0000219 int policy = local->getMinSchedulerPolicy();
220 int priority = local->getMinSchedulerPriority();
221
222 if (policy != 0 || priority != 0) {
223 // override value, since it is set explicitly
224 schedBits = schedPolicyMask(policy, priority);
225 }
Steven Morelandf0212002018-12-26 13:59:23 -0800226 if (local->isRequestingSid()) {
227 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
228 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700229 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800230 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
231 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700232 }
233 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700234 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800235 obj.binder = 0;
236 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700237 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700238
Steven Morelandb96ea772020-07-16 22:43:02 +0000239 obj.flags |= schedBits;
240
Steven Morelanda86a3562019-08-01 23:28:34 +0000241 return finishFlattenBinder(binder, obj);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700242}
243
Steven Morelanda86a3562019-08-01 23:28:34 +0000244status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700245{
Steven Morelanda86a3562019-08-01 23:28:34 +0000246 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700247
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700249 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000250 case BINDER_TYPE_BINDER: {
251 sp<IBinder> binder = reinterpret_cast<IBinder*>(flat->cookie);
252 return finishUnflattenBinder(binder, out);
253 }
254 case BINDER_TYPE_HANDLE: {
255 sp<IBinder> binder =
256 ProcessState::self()->getStrongProxyForHandle(flat->handle);
257 return finishUnflattenBinder(binder, out);
258 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700259 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700260 }
261 return BAD_TYPE;
262}
263
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700264// ---------------------------------------------------------------------------
265
266Parcel::Parcel()
267{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800268 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700269 initState();
270}
271
272Parcel::~Parcel()
273{
274 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800275 LOG_ALLOC("Parcel %p: destroyed", this);
276}
277
278size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800279 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
280 size_t size = gParcelGlobalAllocSize;
281 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
282 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800283}
284
285size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800286 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
287 size_t count = gParcelGlobalAllocCount;
288 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
289 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700290}
291
292const uint8_t* Parcel::data() const
293{
294 return mData;
295}
296
297size_t Parcel::dataSize() const
298{
299 return (mDataSize > mDataPos ? mDataSize : mDataPos);
300}
301
302size_t Parcel::dataAvail() const
303{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700304 size_t result = dataSize() - dataPosition();
305 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700306 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700307 }
308 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700309}
310
311size_t Parcel::dataPosition() const
312{
313 return mDataPos;
314}
315
316size_t Parcel::dataCapacity() const
317{
318 return mDataCapacity;
319}
320
321status_t Parcel::setDataSize(size_t size)
322{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700323 if (size > INT32_MAX) {
324 // don't accept size_t values which may have come from an
325 // inadvertent conversion from a negative int.
326 return BAD_VALUE;
327 }
328
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700329 status_t err;
330 err = continueWrite(size);
331 if (err == NO_ERROR) {
332 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700333 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700334 }
335 return err;
336}
337
338void Parcel::setDataPosition(size_t pos) const
339{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700340 if (pos > INT32_MAX) {
341 // don't accept size_t values which may have come from an
342 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700343 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700344 }
345
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700346 mDataPos = pos;
347 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800348 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700349}
350
351status_t Parcel::setDataCapacity(size_t size)
352{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700353 if (size > INT32_MAX) {
354 // don't accept size_t values which may have come from an
355 // inadvertent conversion from a negative int.
356 return BAD_VALUE;
357 }
358
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700359 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700360 return NO_ERROR;
361}
362
363status_t Parcel::setData(const uint8_t* buffer, size_t len)
364{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700365 if (len > INT32_MAX) {
366 // don't accept size_t values which may have come from an
367 // inadvertent conversion from a negative int.
368 return BAD_VALUE;
369 }
370
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700371 status_t err = restartWrite(len);
372 if (err == NO_ERROR) {
373 memcpy(const_cast<uint8_t*>(data()), buffer, len);
374 mDataSize = len;
375 mFdsKnown = false;
376 }
377 return err;
378}
379
Andreas Huber51faf462011-04-13 10:21:56 -0700380status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700381{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700382 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700383 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800384 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700385 size_t size = parcel->mObjectsSize;
386 int startPos = mDataPos;
387 int firstIndex = -1, lastIndex = -2;
388
389 if (len == 0) {
390 return NO_ERROR;
391 }
392
Nick Kralevichb6b14232015-04-02 09:36:02 -0700393 if (len > INT32_MAX) {
394 // don't accept size_t values which may have come from an
395 // inadvertent conversion from a negative int.
396 return BAD_VALUE;
397 }
398
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700399 // range checks against the source parcel size
400 if ((offset > parcel->mDataSize)
401 || (len > parcel->mDataSize)
402 || (offset + len > parcel->mDataSize)) {
403 return BAD_VALUE;
404 }
405
406 // Count objects in range
407 for (int i = 0; i < (int) size; i++) {
408 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700409 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700410 if (firstIndex == -1) {
411 firstIndex = i;
412 }
413 lastIndex = i;
414 }
415 }
416 int numObjects = lastIndex - firstIndex + 1;
417
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700418 if ((mDataSize+len) > mDataCapacity) {
419 // grow data
420 err = growData(len);
421 if (err != NO_ERROR) {
422 return err;
423 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700424 }
425
426 // append data
427 memcpy(mData + mDataPos, data + offset, len);
428 mDataPos += len;
429 mDataSize += len;
430
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400431 err = NO_ERROR;
432
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700433 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200434 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700435 // grow objects
436 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenenda2f2fd2020-01-22 10:46:25 +0100437 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
438 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700439 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenenda2f2fd2020-01-22 10:46:25 +0100440 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800441 binder_size_t *objects =
442 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700443 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700444 return NO_MEMORY;
445 }
446 mObjects = objects;
447 mObjectsCapacity = newSize;
448 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700449
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700450 // append and acquire objects
451 int idx = mObjectsSize;
452 for (int i = firstIndex; i <= lastIndex; i++) {
453 size_t off = objects[i] - offset + startPos;
454 mObjects[idx++] = off;
455 mObjectsSize++;
456
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700457 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700458 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700459 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700460
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700461 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700462 // If this is a file descriptor, we need to dup it so the
463 // new Parcel now owns its own fd, and can declare that we
464 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800465 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800466 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700467 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400468 if (!mAllowFds) {
469 err = FDS_NOT_ALLOWED;
470 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700471 }
472 }
473 }
474
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400475 return err;
476}
477
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700478int Parcel::compareData(const Parcel& other) {
479 size_t size = dataSize();
480 if (size != other.dataSize()) {
481 return size < other.dataSize() ? -1 : 1;
482 }
483 return memcmp(data(), other.data(), size);
484}
485
Jeff Brown13b16042014-11-11 16:44:25 -0800486bool Parcel::allowFds() const
487{
488 return mAllowFds;
489}
490
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700491bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400492{
493 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700494 if (!allowFds) {
495 mAllowFds = false;
496 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400497 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700498}
499
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700500void Parcel::restoreAllowFds(bool lastValue)
501{
502 mAllowFds = lastValue;
503}
504
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700505bool Parcel::hasFileDescriptors() const
506{
507 if (!mFdsKnown) {
508 scanForFds();
509 }
510 return mHasFds;
511}
512
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000513void Parcel::updateWorkSourceRequestHeaderPosition() const {
514 // Only update the request headers once. We only want to point
515 // to the first headers read/written.
516 if (!mRequestHeaderPresent) {
517 mWorkSourceRequestHeaderPosition = dataPosition();
518 mRequestHeaderPresent = true;
519 }
520}
521
Jooyung Han1b228f42020-01-30 13:41:12 +0900522#if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
Steven Moreland0f452742019-07-31 15:50:51 +0000523constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
524#else
525constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
526#endif
527
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700528// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700529status_t Parcel::writeInterfaceToken(const String16& interface)
530{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000531 const IPCThreadState* threadState = IPCThreadState::self();
532 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000533 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000534 writeInt32(threadState->shouldPropagateWorkSource() ?
535 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Moreland0f452742019-07-31 15:50:51 +0000536 writeInt32(kHeader);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700537 // currently the interface identification token is just its name as a string
538 return writeString16(interface);
539}
540
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000541bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
542{
543 if (!mRequestHeaderPresent) {
544 return false;
545 }
546
547 const size_t initialPosition = dataPosition();
548 setDataPosition(mWorkSourceRequestHeaderPosition);
549 status_t err = writeInt32(uid);
550 setDataPosition(initialPosition);
551 return err == NO_ERROR;
552}
553
Steven Moreland0891c9b2019-05-06 15:05:13 -0700554uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000555{
556 if (!mRequestHeaderPresent) {
557 return IPCThreadState::kUnsetWorkSource;
558 }
559
560 const size_t initialPosition = dataPosition();
561 setDataPosition(mWorkSourceRequestHeaderPosition);
562 uid_t uid = readInt32();
563 setDataPosition(initialPosition);
564 return uid;
565}
566
Mathias Agopian83c04462009-05-22 19:00:22 -0700567bool Parcel::checkInterface(IBinder* binder) const
568{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700569 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700570}
571
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700572bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700573 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700574{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700575 return enforceInterface(interface.string(), interface.size(), threadState);
576}
577
578bool Parcel::enforceInterface(const char16_t* interface,
579 size_t len,
580 IPCThreadState* threadState) const
581{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100582 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700583 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700584 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700585 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700586 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700587 if ((threadState->getLastTransactionBinderFlags() &
588 IBinder::FLAG_ONEWAY) != 0) {
589 // For one-way calls, the callee is running entirely
590 // disconnected from the caller, so disable StrictMode entirely.
591 // Not only does disk/network usage not impact the caller, but
592 // there's no way to commuicate back any violations anyway.
593 threadState->setStrictModePolicy(0);
594 } else {
595 threadState->setStrictModePolicy(strictPolicy);
596 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100597 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000598 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100599 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000600 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Moreland0f452742019-07-31 15:50:51 +0000601 // vendor header
602 int32_t header = readInt32();
603 if (header != kHeader) {
604 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
605 return false;
606 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100607 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700608 size_t parcel_interface_len;
609 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
610 if (len == parcel_interface_len &&
611 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700612 return true;
613 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700614 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700615 String8(interface, len).string(),
616 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700617 return false;
618 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700619}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700621size_t Parcel::objectsCount() const
622{
623 return mObjectsSize;
624}
625
626status_t Parcel::errorCheck() const
627{
628 return mError;
629}
630
631void Parcel::setError(status_t err)
632{
633 mError = err;
634}
635
636status_t Parcel::finishWrite(size_t len)
637{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700638 if (len > INT32_MAX) {
639 // don't accept size_t values which may have come from an
640 // inadvertent conversion from a negative int.
641 return BAD_VALUE;
642 }
643
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700644 //printf("Finish write of %d\n", len);
645 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700646 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700647 if (mDataPos > mDataSize) {
648 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700649 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700650 }
651 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
652 return NO_ERROR;
653}
654
655status_t Parcel::writeUnpadded(const void* data, size_t len)
656{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700657 if (len > INT32_MAX) {
658 // don't accept size_t values which may have come from an
659 // inadvertent conversion from a negative int.
660 return BAD_VALUE;
661 }
662
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700663 size_t end = mDataPos + len;
664 if (end < mDataPos) {
665 // integer overflow
666 return BAD_VALUE;
667 }
668
669 if (end <= mDataCapacity) {
670restart_write:
671 memcpy(mData+mDataPos, data, len);
672 return finishWrite(len);
673 }
674
675 status_t err = growData(len);
676 if (err == NO_ERROR) goto restart_write;
677 return err;
678}
679
680status_t Parcel::write(const void* data, size_t len)
681{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700682 if (len > INT32_MAX) {
683 // don't accept size_t values which may have come from an
684 // inadvertent conversion from a negative int.
685 return BAD_VALUE;
686 }
687
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700688 void* const d = writeInplace(len);
689 if (d) {
690 memcpy(d, data, len);
691 return NO_ERROR;
692 }
693 return mError;
694}
695
696void* Parcel::writeInplace(size_t len)
697{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700698 if (len > INT32_MAX) {
699 // don't accept size_t values which may have come from an
700 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700701 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700702 }
703
704 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700705
706 // sanity check for integer overflow
707 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700708 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700709 }
710
711 if ((mDataPos+padded) <= mDataCapacity) {
712restart_write:
713 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
714 uint8_t* const data = mData+mDataPos;
715
716 // Need to pad at end?
717 if (padded != len) {
718#if BYTE_ORDER == BIG_ENDIAN
719 static const uint32_t mask[4] = {
720 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
721 };
722#endif
723#if BYTE_ORDER == LITTLE_ENDIAN
724 static const uint32_t mask[4] = {
725 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
726 };
727#endif
728 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
729 // *reinterpret_cast<void**>(data+padded-4));
730 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
731 }
732
733 finishWrite(padded);
734 return data;
735 }
736
737 status_t err = growData(padded);
738 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700739 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700740}
741
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800742status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
743 const uint8_t* strData = (uint8_t*)str.data();
744 const size_t strLen= str.length();
745 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100746 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800747 return BAD_VALUE;
748 }
749
750 status_t err = writeInt32(utf16Len);
751 if (err) {
752 return err;
753 }
754
755 // Allocate enough bytes to hold our converted string and its terminating NULL.
756 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
757 if (!dst) {
758 return NO_MEMORY;
759 }
760
Sergio Girof4607432016-07-21 14:46:35 +0100761 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800762
763 return NO_ERROR;
764}
765
766status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
767 if (!str) {
768 return writeInt32(-1);
769 }
770 return writeUtf8AsUtf16(*str);
771}
772
Daniel Normand0337ef2019-09-20 15:46:03 -0700773status_t Parcel::writeByteVectorInternal(const int8_t* data, size_t size) {
774 if (size > std::numeric_limits<int32_t>::max()) {
775 return BAD_VALUE;
Casey Dahlin451ff582015-10-19 18:12:18 -0700776 }
777
Daniel Normand0337ef2019-09-20 15:46:03 -0700778 status_t status = writeInt32(size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700779 if (status != OK) {
780 return status;
781 }
782
Daniel Normand0337ef2019-09-20 15:46:03 -0700783 return write(data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -0700784}
785
Casey Dahlin185d3442016-02-09 11:08:35 -0800786status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700787 return writeByteVectorInternal(val.data(), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800788}
789
790status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
791{
Daniel Normand0337ef2019-09-20 15:46:03 -0700792 if (!val) return writeInt32(-1);
793 return writeByteVectorInternal(val->data(), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800794}
795
796status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
Daniel Normand0337ef2019-09-20 15:46:03 -0700797 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val.data()), val.size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800798}
799
800status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
801{
Daniel Normand0337ef2019-09-20 15:46:03 -0700802 if (!val) return writeInt32(-1);
803 return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
Casey Dahlin185d3442016-02-09 11:08:35 -0800804}
805
Casey Dahlin451ff582015-10-19 18:12:18 -0700806status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
807{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800808 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700809}
810
Casey Dahlinb9872622015-11-25 15:09:45 -0800811status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
812{
813 return writeNullableTypedVector(val, &Parcel::writeInt32);
814}
815
Casey Dahlin451ff582015-10-19 18:12:18 -0700816status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
817{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800818 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700819}
820
Casey Dahlinb9872622015-11-25 15:09:45 -0800821status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
822{
823 return writeNullableTypedVector(val, &Parcel::writeInt64);
824}
825
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800826status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
827{
828 return writeTypedVector(val, &Parcel::writeUint64);
829}
830
831status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
832{
833 return writeNullableTypedVector(val, &Parcel::writeUint64);
834}
835
Casey Dahlin451ff582015-10-19 18:12:18 -0700836status_t Parcel::writeFloatVector(const std::vector<float>& val)
837{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800838 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700839}
840
Casey Dahlinb9872622015-11-25 15:09:45 -0800841status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
842{
843 return writeNullableTypedVector(val, &Parcel::writeFloat);
844}
845
Casey Dahlin451ff582015-10-19 18:12:18 -0700846status_t Parcel::writeDoubleVector(const std::vector<double>& val)
847{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800848 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700849}
850
Casey Dahlinb9872622015-11-25 15:09:45 -0800851status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
852{
853 return writeNullableTypedVector(val, &Parcel::writeDouble);
854}
855
Casey Dahlin451ff582015-10-19 18:12:18 -0700856status_t Parcel::writeBoolVector(const std::vector<bool>& val)
857{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800858 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700859}
860
Casey Dahlinb9872622015-11-25 15:09:45 -0800861status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
862{
863 return writeNullableTypedVector(val, &Parcel::writeBool);
864}
865
Casey Dahlin451ff582015-10-19 18:12:18 -0700866status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
867{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800868 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700869}
870
Casey Dahlinb9872622015-11-25 15:09:45 -0800871status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
872{
873 return writeNullableTypedVector(val, &Parcel::writeChar);
874}
875
Casey Dahlin451ff582015-10-19 18:12:18 -0700876status_t Parcel::writeString16Vector(const std::vector<String16>& val)
877{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800878 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700879}
880
Casey Dahlinb9872622015-11-25 15:09:45 -0800881status_t Parcel::writeString16Vector(
882 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
883{
884 return writeNullableTypedVector(val, &Parcel::writeString16);
885}
886
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800887status_t Parcel::writeUtf8VectorAsUtf16Vector(
888 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
889 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
890}
891
892status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
893 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
894}
895
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700896status_t Parcel::writeInt32(int32_t val)
897{
Andreas Huber84a6d042009-08-17 13:33:27 -0700898 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700899}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800900
901status_t Parcel::writeUint32(uint32_t val)
902{
903 return writeAligned(val);
904}
905
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700906status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700907 if (len > INT32_MAX) {
908 // don't accept size_t values which may have come from an
909 // inadvertent conversion from a negative int.
910 return BAD_VALUE;
911 }
912
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700913 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700914 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700915 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700916 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700917 if (ret == NO_ERROR) {
918 ret = write(val, len * sizeof(*val));
919 }
920 return ret;
921}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700922status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700923 if (len > INT32_MAX) {
924 // don't accept size_t values which may have come from an
925 // inadvertent conversion from a negative int.
926 return BAD_VALUE;
927 }
928
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700929 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700930 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700931 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700932 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700933 if (ret == NO_ERROR) {
934 ret = write(val, len * sizeof(*val));
935 }
936 return ret;
937}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700938
Casey Dahlind6848f52015-10-15 15:44:59 -0700939status_t Parcel::writeBool(bool val)
940{
941 return writeInt32(int32_t(val));
942}
943
944status_t Parcel::writeChar(char16_t val)
945{
946 return writeInt32(int32_t(val));
947}
948
949status_t Parcel::writeByte(int8_t val)
950{
951 return writeInt32(int32_t(val));
952}
953
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700954status_t Parcel::writeInt64(int64_t val)
955{
Andreas Huber84a6d042009-08-17 13:33:27 -0700956 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700957}
958
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700959status_t Parcel::writeUint64(uint64_t val)
960{
961 return writeAligned(val);
962}
963
Serban Constantinescuf683e012013-11-05 16:53:55 +0000964status_t Parcel::writePointer(uintptr_t val)
965{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800966 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000967}
968
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700969status_t Parcel::writeFloat(float val)
970{
Andreas Huber84a6d042009-08-17 13:33:27 -0700971 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700972}
973
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800974#if defined(__mips__) && defined(__mips_hard_float)
975
976status_t Parcel::writeDouble(double val)
977{
978 union {
979 double d;
980 unsigned long long ll;
981 } u;
982 u.d = val;
983 return writeAligned(u.ll);
984}
985
986#else
987
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700988status_t Parcel::writeDouble(double val)
989{
Andreas Huber84a6d042009-08-17 13:33:27 -0700990 return writeAligned(val);
991}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700992
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800993#endif
994
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700995status_t Parcel::writeCString(const char* str)
996{
997 return write(str, strlen(str)+1);
998}
999
1000status_t Parcel::writeString8(const String8& str)
1001{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001002 return writeString8(str.string(), str.size());
1003}
1004
1005status_t Parcel::writeString8(const char* str, size_t len)
1006{
1007 if (str == nullptr) return writeInt32(-1);
1008
1009 status_t err = writeInt32(len);
1010 if (err == NO_ERROR) {
1011 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1012 if (data) {
1013 memcpy(data, str, len);
1014 *reinterpret_cast<char*>(data+len) = 0;
1015 return NO_ERROR;
1016 }
1017 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001018 }
1019 return err;
1020}
1021
Casey Dahlinb9872622015-11-25 15:09:45 -08001022status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1023{
1024 if (!str) {
1025 return writeInt32(-1);
1026 }
1027
1028 return writeString16(*str);
1029}
1030
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001031status_t Parcel::writeString16(const String16& str)
1032{
1033 return writeString16(str.string(), str.size());
1034}
1035
1036status_t Parcel::writeString16(const char16_t* str, size_t len)
1037{
Yi Kong91635562018-06-07 14:38:36 -07001038 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001039
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001040 status_t err = writeInt32(len);
1041 if (err == NO_ERROR) {
1042 len *= sizeof(char16_t);
1043 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1044 if (data) {
1045 memcpy(data, str, len);
1046 *reinterpret_cast<char16_t*>(data+len) = 0;
1047 return NO_ERROR;
1048 }
1049 err = mError;
1050 }
1051 return err;
1052}
1053
1054status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1055{
Steven Morelanda86a3562019-08-01 23:28:34 +00001056 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001057}
1058
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001059status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1060{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001061 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001062}
1063
Casey Dahlinb9872622015-11-25 15:09:45 -08001064status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1065{
1066 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1067}
1068
1069status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001070 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001071}
1072
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001073status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001074 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001075}
1076
Casey Dahlinb9872622015-11-25 15:09:45 -08001077status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1078 if (!parcelable) {
1079 return writeInt32(0);
1080 }
1081
1082 return writeParcelable(*parcelable);
1083}
1084
Christopher Wiley97f048d2015-11-19 06:49:05 -08001085status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1086 status_t status = writeInt32(1); // parcelable is not null.
1087 if (status != OK) {
1088 return status;
1089 }
1090 return parcelable.writeToParcel(this);
1091}
1092
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001093status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001094{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001095 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001096 return BAD_TYPE;
1097
1098 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001099 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001100 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001102 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001103 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001105 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1106 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001107
1108 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001109 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110 return err;
1111 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001112 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001113 return err;
1114}
1115
Jeff Brown93ff1f92011-11-04 19:01:44 -07001116status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001117{
1118 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001119 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001120 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001121 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001122 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001123 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001124 return writeObject(obj, true);
1125}
1126
1127status_t Parcel::writeDupFileDescriptor(int fd)
1128{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001129 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001130 if (dupFd < 0) {
1131 return -errno;
1132 }
1133 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001134 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001135 close(dupFd);
1136 }
1137 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001138}
1139
Dianne Hackborn1941a402016-08-29 12:30:43 -07001140status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1141{
1142 writeInt32(0);
1143 return writeFileDescriptor(fd, takeOwnership);
1144}
1145
Ryo Hashimotobf551892018-05-31 16:58:35 +09001146status_t Parcel::writeDupParcelFileDescriptor(int fd)
1147{
1148 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1149 if (dupFd < 0) {
1150 return -errno;
1151 }
1152 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1153 if (err != OK) {
1154 close(dupFd);
1155 }
1156 return err;
1157}
1158
Christopher Wiley2cf19952016-04-11 11:09:37 -07001159status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001160 return writeDupFileDescriptor(fd.get());
1161}
1162
Christopher Wiley2cf19952016-04-11 11:09:37 -07001163status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001164 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1165}
1166
Christopher Wiley2cf19952016-04-11 11:09:37 -07001167status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001168 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1169}
1170
Jeff Brown13b16042014-11-11 16:44:25 -08001171status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001172{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001173 if (len > INT32_MAX) {
1174 // don't accept size_t values which may have come from an
1175 // inadvertent conversion from a negative int.
1176 return BAD_VALUE;
1177 }
1178
Jeff Brown13b16042014-11-11 16:44:25 -08001179 status_t status;
1180 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001181 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001182 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001183 if (status) return status;
1184
1185 void* ptr = writeInplace(len);
1186 if (!ptr) return NO_MEMORY;
1187
Jeff Brown13b16042014-11-11 16:44:25 -08001188 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001189 return NO_ERROR;
1190 }
1191
Steve Block6807e592011-10-20 11:56:00 +01001192 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001193 int fd = ashmem_create_region("Parcel Blob", len);
1194 if (fd < 0) return NO_MEMORY;
1195
1196 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1197 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001198 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001199 } else {
Yi Kong91635562018-06-07 14:38:36 -07001200 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001201 if (ptr == MAP_FAILED) {
1202 status = -errno;
1203 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001204 if (!mutableCopy) {
1205 result = ashmem_set_prot_region(fd, PROT_READ);
1206 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001207 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001208 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001209 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001210 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001211 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001212 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001213 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001214 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001215 return NO_ERROR;
1216 }
1217 }
1218 }
1219 }
1220 ::munmap(ptr, len);
1221 }
1222 ::close(fd);
1223 return status;
1224}
1225
Jeff Brown13b16042014-11-11 16:44:25 -08001226status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1227{
1228 // Must match up with what's done in writeBlob.
1229 if (!mAllowFds) return FDS_NOT_ALLOWED;
1230 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1231 if (status) return status;
1232 return writeDupFileDescriptor(fd);
1233}
1234
Mathias Agopiane1424282013-07-29 21:24:40 -07001235status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001236{
1237 status_t err;
1238
1239 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001240 const size_t len = val.getFlattenedSize();
1241 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001242
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001243 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001244 // don't accept size_t values which may have come from an
1245 // inadvertent conversion from a negative int.
1246 return BAD_VALUE;
1247 }
1248
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001249 err = this->writeInt32(len);
1250 if (err) return err;
1251
1252 err = this->writeInt32(fd_count);
1253 if (err) return err;
1254
1255 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001256 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001257 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001258 return BAD_VALUE;
1259
Yi Kong91635562018-06-07 14:38:36 -07001260 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001261 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001262 fds = new (std::nothrow) int[fd_count];
1263 if (fds == nullptr) {
1264 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1265 return BAD_VALUE;
1266 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001267 }
1268
1269 err = val.flatten(buf, len, fds, fd_count);
1270 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1271 err = this->writeDupFileDescriptor( fds[i] );
1272 }
1273
1274 if (fd_count) {
1275 delete [] fds;
1276 }
1277
1278 return err;
1279}
1280
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001281status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1282{
1283 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1284 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1285 if (enoughData && enoughObjects) {
1286restart_write:
1287 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001288
Christopher Tate98e67d32015-06-03 18:44:15 -07001289 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001290 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001291 if (!mAllowFds) {
1292 // fail before modifying our object index
1293 return FDS_NOT_ALLOWED;
1294 }
1295 mHasFds = mFdsKnown = true;
1296 }
1297
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001298 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001299 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001300 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001301 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001302 mObjectsSize++;
1303 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001304
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001305 return finishWrite(sizeof(flat_binder_object));
1306 }
1307
1308 if (!enoughData) {
1309 const status_t err = growData(sizeof(val));
1310 if (err != NO_ERROR) return err;
1311 }
1312 if (!enoughObjects) {
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01001313 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1314 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001315 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01001316 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001317 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001318 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001319 mObjects = objects;
1320 mObjectsCapacity = newSize;
1321 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001322
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001323 goto restart_write;
1324}
1325
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001326status_t Parcel::writeNoException()
1327{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001328 binder::Status status;
1329 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001330}
1331
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001332status_t Parcel::validateReadData(size_t upperBound) const
1333{
1334 // Don't allow non-object reads on object data
1335 if (mObjectsSorted || mObjectsSize <= 1) {
1336data_sorted:
1337 // Expect to check only against the next object
1338 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1339 // For some reason the current read position is greater than the next object
1340 // hint. Iterate until we find the right object
1341 size_t nextObject = mNextObjectHint;
1342 do {
1343 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1344 // Requested info overlaps with an object
1345 ALOGE("Attempt to read from protected data in Parcel %p", this);
1346 return PERMISSION_DENIED;
1347 }
1348 nextObject++;
1349 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1350 mNextObjectHint = nextObject;
1351 }
1352 return NO_ERROR;
1353 }
1354 // Quickly determine if mObjects is sorted.
1355 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1356 binder_size_t* prevObj = currObj;
1357 while (currObj > mObjects) {
1358 prevObj--;
1359 if(*prevObj > *currObj) {
1360 goto data_unsorted;
1361 }
1362 currObj--;
1363 }
1364 mObjectsSorted = true;
1365 goto data_sorted;
1366
1367data_unsorted:
1368 // Insertion Sort mObjects
1369 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1370 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1371 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1372 binder_size_t temp = *iter0;
1373 binder_size_t* iter1 = iter0 - 1;
1374 while (iter1 >= mObjects && *iter1 > temp) {
1375 *(iter1 + 1) = *iter1;
1376 iter1--;
1377 }
1378 *(iter1 + 1) = temp;
1379 }
1380 mNextObjectHint = 0;
1381 mObjectsSorted = true;
1382 goto data_sorted;
1383}
1384
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001385status_t Parcel::read(void* outData, size_t len) const
1386{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001387 if (len > INT32_MAX) {
1388 // don't accept size_t values which may have come from an
1389 // inadvertent conversion from a negative int.
1390 return BAD_VALUE;
1391 }
1392
1393 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1394 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001395 if (mObjectsSize > 0) {
1396 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001397 if(err != NO_ERROR) {
1398 // Still increment the data position by the expected length
1399 mDataPos += pad_size(len);
1400 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1401 return err;
1402 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001403 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001404 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001405 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001406 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001407 return NO_ERROR;
1408 }
1409 return NOT_ENOUGH_DATA;
1410}
1411
1412const void* Parcel::readInplace(size_t len) const
1413{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001414 if (len > INT32_MAX) {
1415 // don't accept size_t values which may have come from an
1416 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001417 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001418 }
1419
1420 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1421 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001422 if (mObjectsSize > 0) {
1423 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001424 if(err != NO_ERROR) {
1425 // Still increment the data position by the expected length
1426 mDataPos += pad_size(len);
1427 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001428 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001429 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001430 }
1431
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001432 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001433 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001434 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001435 return data;
1436 }
Yi Kong91635562018-06-07 14:38:36 -07001437 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001438}
1439
Andreas Huber84a6d042009-08-17 13:33:27 -07001440template<class T>
1441status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001442 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001443
1444 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001445 if (mObjectsSize > 0) {
1446 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001447 if(err != NO_ERROR) {
1448 // Still increment the data position by the expected length
1449 mDataPos += sizeof(T);
1450 return err;
1451 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001452 }
1453
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001454 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001455 mDataPos += sizeof(T);
1456 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001457 return NO_ERROR;
1458 } else {
1459 return NOT_ENOUGH_DATA;
1460 }
1461}
1462
Andreas Huber84a6d042009-08-17 13:33:27 -07001463template<class T>
1464T Parcel::readAligned() const {
1465 T result;
1466 if (readAligned(&result) != NO_ERROR) {
1467 result = 0;
1468 }
1469
1470 return result;
1471}
1472
1473template<class T>
1474status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001475 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001476
1477 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1478restart_write:
1479 *reinterpret_cast<T*>(mData+mDataPos) = val;
1480 return finishWrite(sizeof(val));
1481 }
1482
1483 status_t err = growData(sizeof(val));
1484 if (err == NO_ERROR) goto restart_write;
1485 return err;
1486}
1487
Casey Dahlin185d3442016-02-09 11:08:35 -08001488status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001489 size_t size;
1490 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1491 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001492}
1493
1494status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001495 size_t size;
1496 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
1497 return readByteVectorInternal(val, size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001498}
1499
1500status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001501 size_t size;
1502 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001503 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001504 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001505 // This occurs when writing a null byte vector.
1506 return OK;
1507 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001508 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001509}
1510
1511status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
Daniel Normanc8646c32019-10-30 10:33:22 -07001512 size_t size;
1513 if (status_t status = reserveOutVector(val, &size); status != OK) return status;
Daniel Normand0337ef2019-09-20 15:46:03 -07001514 if (val->get() == nullptr) {
Daniel Normanc8646c32019-10-30 10:33:22 -07001515 // reserveOutVector does not create the out vector if size is < 0.
Daniel Normand0337ef2019-09-20 15:46:03 -07001516 // This occurs when writing a null byte vector.
1517 return OK;
1518 }
Daniel Normanc8646c32019-10-30 10:33:22 -07001519 return readByteVectorInternal(val->get(), size);
Casey Dahlin185d3442016-02-09 11:08:35 -08001520}
1521
Casey Dahlinb9872622015-11-25 15:09:45 -08001522status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1523 return readNullableTypedVector(val, &Parcel::readInt32);
1524}
1525
Casey Dahlin451ff582015-10-19 18:12:18 -07001526status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001527 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001528}
1529
Casey Dahlinb9872622015-11-25 15:09:45 -08001530status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1531 return readNullableTypedVector(val, &Parcel::readInt64);
1532}
1533
Casey Dahlin451ff582015-10-19 18:12:18 -07001534status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001535 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001536}
1537
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001538status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1539 return readNullableTypedVector(val, &Parcel::readUint64);
1540}
1541
1542status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1543 return readTypedVector(val, &Parcel::readUint64);
1544}
1545
Casey Dahlinb9872622015-11-25 15:09:45 -08001546status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1547 return readNullableTypedVector(val, &Parcel::readFloat);
1548}
1549
Casey Dahlin451ff582015-10-19 18:12:18 -07001550status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001551 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001552}
1553
Casey Dahlinb9872622015-11-25 15:09:45 -08001554status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1555 return readNullableTypedVector(val, &Parcel::readDouble);
1556}
1557
Casey Dahlin451ff582015-10-19 18:12:18 -07001558status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001559 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001560}
1561
Casey Dahlinb9872622015-11-25 15:09:45 -08001562status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1563 const int32_t start = dataPosition();
1564 int32_t size;
1565 status_t status = readInt32(&size);
1566 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001567
Casey Dahlinb9872622015-11-25 15:09:45 -08001568 if (status != OK || size < 0) {
1569 return status;
1570 }
1571
1572 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001573 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001574
1575 status = readBoolVector(val->get());
1576
1577 if (status != OK) {
1578 val->reset();
1579 }
1580
1581 return status;
1582}
1583
1584status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001585 int32_t size;
1586 status_t status = readInt32(&size);
1587
1588 if (status != OK) {
1589 return status;
1590 }
1591
1592 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001593 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001594 }
1595
1596 val->resize(size);
1597
1598 /* C++ bool handling means a vector of bools isn't necessarily addressable
1599 * (we might use individual bits)
1600 */
Christopher Wiley97887982015-10-27 16:33:47 -07001601 bool data;
1602 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001603 status = readBool(&data);
1604 (*val)[i] = data;
1605
1606 if (status != OK) {
1607 return status;
1608 }
1609 }
1610
1611 return OK;
1612}
1613
Casey Dahlinb9872622015-11-25 15:09:45 -08001614status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1615 return readNullableTypedVector(val, &Parcel::readChar);
1616}
1617
Casey Dahlin451ff582015-10-19 18:12:18 -07001618status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001619 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001620}
1621
Casey Dahlinb9872622015-11-25 15:09:45 -08001622status_t Parcel::readString16Vector(
1623 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1624 return readNullableTypedVector(val, &Parcel::readString16);
1625}
1626
Casey Dahlin451ff582015-10-19 18:12:18 -07001627status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001628 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001629}
1630
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001631status_t Parcel::readUtf8VectorFromUtf16Vector(
1632 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1633 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1634}
1635
1636status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1637 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1638}
Casey Dahlin451ff582015-10-19 18:12:18 -07001639
Andreas Huber84a6d042009-08-17 13:33:27 -07001640status_t Parcel::readInt32(int32_t *pArg) const
1641{
1642 return readAligned(pArg);
1643}
1644
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001645int32_t Parcel::readInt32() const
1646{
Andreas Huber84a6d042009-08-17 13:33:27 -07001647 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001648}
1649
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001650status_t Parcel::readUint32(uint32_t *pArg) const
1651{
1652 return readAligned(pArg);
1653}
1654
1655uint32_t Parcel::readUint32() const
1656{
1657 return readAligned<uint32_t>();
1658}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001659
1660status_t Parcel::readInt64(int64_t *pArg) const
1661{
Andreas Huber84a6d042009-08-17 13:33:27 -07001662 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001663}
1664
1665
1666int64_t Parcel::readInt64() const
1667{
Andreas Huber84a6d042009-08-17 13:33:27 -07001668 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001669}
1670
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001671status_t Parcel::readUint64(uint64_t *pArg) const
1672{
1673 return readAligned(pArg);
1674}
1675
1676uint64_t Parcel::readUint64() const
1677{
1678 return readAligned<uint64_t>();
1679}
1680
Serban Constantinescuf683e012013-11-05 16:53:55 +00001681status_t Parcel::readPointer(uintptr_t *pArg) const
1682{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001683 status_t ret;
1684 binder_uintptr_t ptr;
1685 ret = readAligned(&ptr);
1686 if (!ret)
1687 *pArg = ptr;
1688 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001689}
1690
1691uintptr_t Parcel::readPointer() const
1692{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001693 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001694}
1695
1696
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001697status_t Parcel::readFloat(float *pArg) const
1698{
Andreas Huber84a6d042009-08-17 13:33:27 -07001699 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001700}
1701
1702
1703float Parcel::readFloat() const
1704{
Andreas Huber84a6d042009-08-17 13:33:27 -07001705 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001706}
1707
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001708#if defined(__mips__) && defined(__mips_hard_float)
1709
1710status_t Parcel::readDouble(double *pArg) const
1711{
1712 union {
1713 double d;
1714 unsigned long long ll;
1715 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001716 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001717 status_t status;
1718 status = readAligned(&u.ll);
1719 *pArg = u.d;
1720 return status;
1721}
1722
1723double Parcel::readDouble() const
1724{
1725 union {
1726 double d;
1727 unsigned long long ll;
1728 } u;
1729 u.ll = readAligned<unsigned long long>();
1730 return u.d;
1731}
1732
1733#else
1734
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001735status_t Parcel::readDouble(double *pArg) const
1736{
Andreas Huber84a6d042009-08-17 13:33:27 -07001737 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001738}
1739
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001740double Parcel::readDouble() const
1741{
Andreas Huber84a6d042009-08-17 13:33:27 -07001742 return readAligned<double>();
1743}
1744
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001745#endif
1746
Andreas Huber84a6d042009-08-17 13:33:27 -07001747status_t Parcel::readIntPtr(intptr_t *pArg) const
1748{
1749 return readAligned(pArg);
1750}
1751
1752
1753intptr_t Parcel::readIntPtr() const
1754{
1755 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001756}
1757
Casey Dahlind6848f52015-10-15 15:44:59 -07001758status_t Parcel::readBool(bool *pArg) const
1759{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001760 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001761 status_t ret = readInt32(&tmp);
1762 *pArg = (tmp != 0);
1763 return ret;
1764}
1765
1766bool Parcel::readBool() const
1767{
1768 return readInt32() != 0;
1769}
1770
1771status_t Parcel::readChar(char16_t *pArg) const
1772{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001773 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001774 status_t ret = readInt32(&tmp);
1775 *pArg = char16_t(tmp);
1776 return ret;
1777}
1778
1779char16_t Parcel::readChar() const
1780{
1781 return char16_t(readInt32());
1782}
1783
1784status_t Parcel::readByte(int8_t *pArg) const
1785{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001786 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001787 status_t ret = readInt32(&tmp);
1788 *pArg = int8_t(tmp);
1789 return ret;
1790}
1791
1792int8_t Parcel::readByte() const
1793{
1794 return int8_t(readInt32());
1795}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001796
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001797status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1798 size_t utf16Size = 0;
1799 const char16_t* src = readString16Inplace(&utf16Size);
1800 if (!src) {
1801 return UNEXPECTED_NULL;
1802 }
1803
1804 // Save ourselves the trouble, we're done.
1805 if (utf16Size == 0u) {
1806 str->clear();
1807 return NO_ERROR;
1808 }
1809
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001810 // Allow for closing '\0'
1811 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1812 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001813 return BAD_VALUE;
1814 }
1815 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001816 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001817 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001818 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1819 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001820 return NO_ERROR;
1821}
1822
1823status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1824 const int32_t start = dataPosition();
1825 int32_t size;
1826 status_t status = readInt32(&size);
1827 str->reset();
1828
1829 if (status != OK || size < 0) {
1830 return status;
1831 }
1832
1833 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001834 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001835 return readUtf8FromUtf16(str->get());
1836}
1837
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001838const char* Parcel::readCString() const
1839{
Steven Morelandf5e6c7e2019-05-17 13:14:06 -07001840 if (mDataPos < mDataSize) {
1841 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001842 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1843 // is the string's trailing NUL within the parcel's valid bounds?
1844 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1845 if (eos) {
1846 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001847 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001848 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001849 return str;
1850 }
1851 }
Yi Kong91635562018-06-07 14:38:36 -07001852 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001853}
1854
1855String8 Parcel::readString8() const
1856{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001857 size_t len;
1858 const char* str = readString8Inplace(&len);
1859 if (str) return String8(str, len);
1860 ALOGE("Reading a NULL string not supported here.");
1861 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07001862}
1863
1864status_t Parcel::readString8(String8* pArg) const
1865{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001866 size_t len;
1867 const char* str = readString8Inplace(&len);
1868 if (str) {
1869 pArg->setTo(str, len);
1870 return 0;
1871 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07001872 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001873 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07001874 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001875}
1876
1877const char* Parcel::readString8Inplace(size_t* outLen) const
1878{
1879 int32_t size = readInt32();
1880 // watch for potential int overflow from size+1
1881 if (size >= 0 && size < INT32_MAX) {
1882 *outLen = size;
1883 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00001884 if (str != nullptr) {
1885 if (str[size] == '\0') {
1886 return str;
1887 }
1888 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001889 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001890 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001891 *outLen = 0;
1892 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001893}
1894
1895String16 Parcel::readString16() const
1896{
1897 size_t len;
1898 const char16_t* str = readString16Inplace(&len);
1899 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001900 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001901 return String16();
1902}
1903
Casey Dahlinb9872622015-11-25 15:09:45 -08001904status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1905{
1906 const int32_t start = dataPosition();
1907 int32_t size;
1908 status_t status = readInt32(&size);
1909 pArg->reset();
1910
1911 if (status != OK || size < 0) {
1912 return status;
1913 }
1914
1915 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001916 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001917
1918 status = readString16(pArg->get());
1919
1920 if (status != OK) {
1921 pArg->reset();
1922 }
1923
1924 return status;
1925}
1926
Casey Dahlin451ff582015-10-19 18:12:18 -07001927status_t Parcel::readString16(String16* pArg) const
1928{
1929 size_t len;
1930 const char16_t* str = readString16Inplace(&len);
1931 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001932 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001933 return 0;
1934 } else {
1935 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001936 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001937 }
1938}
1939
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001940const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1941{
1942 int32_t size = readInt32();
1943 // watch for potential int overflow from size+1
1944 if (size >= 0 && size < INT32_MAX) {
1945 *outLen = size;
1946 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00001947 if (str != nullptr) {
1948 if (str[size] == u'\0') {
1949 return str;
1950 }
1951 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001952 }
1953 }
1954 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001955 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001956}
1957
Casey Dahlinf0c13772015-10-27 18:33:56 -07001958status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1959{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001960 status_t status = readNullableStrongBinder(val);
1961 if (status == OK && !val->get()) {
1962 status = UNEXPECTED_NULL;
1963 }
1964 return status;
1965}
1966
1967status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1968{
Steven Morelanda86a3562019-08-01 23:28:34 +00001969 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001970}
1971
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001972sp<IBinder> Parcel::readStrongBinder() const
1973{
1974 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001975 // Note that a lot of code in Android reads binders by hand with this
1976 // method, and that code has historically been ok with getting nullptr
1977 // back (while ignoring error codes).
1978 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001979 return val;
1980}
1981
Christopher Wiley97f048d2015-11-19 06:49:05 -08001982status_t Parcel::readParcelable(Parcelable* parcelable) const {
1983 int32_t have_parcelable = 0;
1984 status_t status = readInt32(&have_parcelable);
1985 if (status != OK) {
1986 return status;
1987 }
1988 if (!have_parcelable) {
1989 return UNEXPECTED_NULL;
1990 }
1991 return parcelable->readFromParcel(this);
1992}
1993
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001994int32_t Parcel::readExceptionCode() const
1995{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001996 binder::Status status;
1997 status.readFromParcel(*this);
1998 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001999}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002000
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002001native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002002{
2003 int numFds, numInts;
2004 status_t err;
2005 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002006 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002007 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002008 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002009
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002010 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002011 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002012 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002013 }
2014
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002015 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002016 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002017 if (h->data[i] < 0) {
2018 for (int j = 0; j < i; j++) {
2019 close(h->data[j]);
2020 }
2021 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002022 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002023 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002024 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002025 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002026 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002027 native_handle_close(h);
2028 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002029 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002030 }
2031 return h;
2032}
2033
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002034int Parcel::readFileDescriptor() const
2035{
2036 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002037
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002038 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002039 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002040 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002041
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002042 return BAD_TYPE;
2043}
2044
Dianne Hackborn1941a402016-08-29 12:30:43 -07002045int Parcel::readParcelFileDescriptor() const
2046{
2047 int32_t hasComm = readInt32();
2048 int fd = readFileDescriptor();
2049 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002050 // detach (owned by the binder driver)
2051 int comm = readFileDescriptor();
2052
2053 // warning: this must be kept in sync with:
2054 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2055 enum ParcelFileDescriptorStatus {
2056 DETACHED = 2,
2057 };
2058
2059#if BYTE_ORDER == BIG_ENDIAN
2060 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2061#endif
2062#if BYTE_ORDER == LITTLE_ENDIAN
2063 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2064#endif
2065
2066 ssize_t written = TEMP_FAILURE_RETRY(
2067 ::write(comm, &message, sizeof(message)));
2068
2069 if (written == -1 || written != sizeof(message)) {
2070 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2071 written, strerror(errno));
2072 return BAD_TYPE;
2073 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002074 }
2075 return fd;
2076}
2077
Christopher Wiley2cf19952016-04-11 11:09:37 -07002078status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002079{
2080 int got = readFileDescriptor();
2081
2082 if (got == BAD_TYPE) {
2083 return BAD_TYPE;
2084 }
2085
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002086 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002087
2088 if (val->get() < 0) {
2089 return BAD_VALUE;
2090 }
2091
2092 return OK;
2093}
2094
Ryo Hashimotobf551892018-05-31 16:58:35 +09002095status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2096{
2097 int got = readParcelFileDescriptor();
2098
2099 if (got == BAD_TYPE) {
2100 return BAD_TYPE;
2101 }
2102
2103 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2104
2105 if (val->get() < 0) {
2106 return BAD_VALUE;
2107 }
2108
2109 return OK;
2110}
Casey Dahlin06673e32015-11-23 13:24:23 -08002111
Christopher Wiley2cf19952016-04-11 11:09:37 -07002112status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002113 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2114}
2115
Christopher Wiley2cf19952016-04-11 11:09:37 -07002116status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002117 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2118}
2119
Jeff Brown5707dbf2011-09-23 21:17:56 -07002120status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2121{
Jeff Brown13b16042014-11-11 16:44:25 -08002122 int32_t blobType;
2123 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002124 if (status) return status;
2125
Jeff Brown13b16042014-11-11 16:44:25 -08002126 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002127 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002128 const void* ptr = readInplace(len);
2129 if (!ptr) return BAD_VALUE;
2130
Jeff Brown13b16042014-11-11 16:44:25 -08002131 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002132 return NO_ERROR;
2133 }
2134
Steve Block6807e592011-10-20 11:56:00 +01002135 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002136 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002137 int fd = readFileDescriptor();
2138 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2139
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002140 if (!ashmem_valid(fd)) {
2141 ALOGE("invalid fd");
2142 return BAD_VALUE;
2143 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002144 int size = ashmem_get_size_region(fd);
2145 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002146 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002147 return BAD_VALUE;
2148 }
Yi Kong91635562018-06-07 14:38:36 -07002149 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002150 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002151 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002152
Jeff Brown13b16042014-11-11 16:44:25 -08002153 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002154 return NO_ERROR;
2155}
2156
Mathias Agopiane1424282013-07-29 21:24:40 -07002157status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002158{
2159 // size
2160 const size_t len = this->readInt32();
2161 const size_t fd_count = this->readInt32();
2162
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002163 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002164 // don't accept size_t values which may have come from an
2165 // inadvertent conversion from a negative int.
2166 return BAD_VALUE;
2167 }
2168
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002169 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002170 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002171 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002172 return BAD_VALUE;
2173
Yi Kong91635562018-06-07 14:38:36 -07002174 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002175 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002176 fds = new (std::nothrow) int[fd_count];
2177 if (fds == nullptr) {
2178 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2179 return BAD_VALUE;
2180 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002181 }
2182
2183 status_t err = NO_ERROR;
2184 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002185 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002186 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002187 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002188 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 -07002189 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2190 // Close all the file descriptors that were dup-ed.
2191 for (size_t j=0; j<i ;j++) {
2192 close(fds[j]);
2193 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002194 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002195 }
2196
2197 if (err == NO_ERROR) {
2198 err = val.unflatten(buf, len, fds, fd_count);
2199 }
2200
2201 if (fd_count) {
2202 delete [] fds;
2203 }
2204
2205 return err;
2206}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002207const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2208{
2209 const size_t DPOS = mDataPos;
2210 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2211 const flat_binder_object* obj
2212 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2213 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002214 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002215 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002216 // the object list, so we don't want to check for it when
2217 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002218 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002219 return obj;
2220 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002221
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002222 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002223 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002224 const size_t N = mObjectsSize;
2225 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002226
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002227 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002228 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002229 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002230
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231 // Start at the current hint position, looking for an object at
2232 // the current data position.
2233 if (opos < N) {
2234 while (opos < (N-1) && OBJS[opos] < DPOS) {
2235 opos++;
2236 }
2237 } else {
2238 opos = N-1;
2239 }
2240 if (OBJS[opos] == DPOS) {
2241 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002242 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002243 this, DPOS, opos);
2244 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002245 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002246 return obj;
2247 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002248
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002249 // Look backwards for it...
2250 while (opos > 0 && OBJS[opos] > DPOS) {
2251 opos--;
2252 }
2253 if (OBJS[opos] == DPOS) {
2254 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002255 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002256 this, DPOS, opos);
2257 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002258 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002259 return obj;
2260 }
2261 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002262 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 -07002263 this, DPOS);
2264 }
Yi Kong91635562018-06-07 14:38:36 -07002265 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002266}
2267
2268void Parcel::closeFileDescriptors()
2269{
2270 size_t i = mObjectsSize;
2271 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002272 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002273 }
2274 while (i > 0) {
2275 i--;
2276 const flat_binder_object* flat
2277 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002278 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002279 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002280 close(flat->handle);
2281 }
2282 }
2283}
2284
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002285uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002286{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002287 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002288}
2289
2290size_t Parcel::ipcDataSize() const
2291{
2292 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2293}
2294
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002295uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002296{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002297 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002298}
2299
2300size_t Parcel::ipcObjectsCount() const
2301{
2302 return mObjectsSize;
2303}
2304
2305void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002306 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002307{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002308 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002309 freeDataNoInit();
2310 mError = NO_ERROR;
2311 mData = const_cast<uint8_t*>(data);
2312 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002313 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002314 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002315 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002316 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002317 mObjectsSize = mObjectsCapacity = objectsCount;
2318 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002319 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002320 mOwner = relFunc;
2321 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002322 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002323 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002324 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002325 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002326 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002327 mObjectsSize = 0;
2328 break;
2329 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002330 const flat_binder_object* flat
2331 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2332 uint32_t type = flat->hdr.type;
2333 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2334 type == BINDER_TYPE_FD)) {
2335 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2336 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2337 // recover gracefully by clearing out the objects, and releasing the objects we do
2338 // know about.
2339 android_errorWriteLog(0x534e4554, "135930648");
2340 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2341 __func__, type, (uint64_t)offset);
2342 releaseObjects();
2343 mObjectsSize = 0;
2344 break;
2345 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002346 minOffset = offset + sizeof(flat_binder_object);
2347 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002348 scanForFds();
2349}
2350
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002351void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002352{
2353 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002354
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002355 if (errorCheck() != NO_ERROR) {
2356 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002357 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002358 } else if (dataSize() > 0) {
2359 const uint8_t* DATA = data();
2360 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002361 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002362 const size_t N = objectsCount();
2363 for (size_t i=0; i<N; i++) {
2364 const flat_binder_object* flat
2365 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2366 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002367 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002368 << " = " << flat->binder;
2369 }
2370 } else {
2371 to << "NULL";
2372 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002373
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002374 to << ")";
2375}
2376
2377void Parcel::releaseObjects()
2378{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002379 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002380 if (i == 0) {
2381 return;
2382 }
2383 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002384 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002385 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002386 while (i > 0) {
2387 i--;
2388 const flat_binder_object* flat
2389 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002390 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002391 }
2392}
2393
2394void Parcel::acquireObjects()
2395{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002396 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002397 if (i == 0) {
2398 return;
2399 }
2400 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002401 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002402 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002403 while (i > 0) {
2404 i--;
2405 const flat_binder_object* flat
2406 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002407 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002408 }
2409}
2410
2411void Parcel::freeData()
2412{
2413 freeDataNoInit();
2414 initState();
2415}
2416
2417void Parcel::freeDataNoInit()
2418{
2419 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002420 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002421 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002422 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2423 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002424 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002425 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002426 if (mData) {
2427 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002428 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002429 if (mDataCapacity <= gParcelGlobalAllocSize) {
2430 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2431 } else {
2432 gParcelGlobalAllocSize = 0;
2433 }
2434 if (gParcelGlobalAllocCount > 0) {
2435 gParcelGlobalAllocCount--;
2436 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002437 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002438 free(mData);
2439 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002440 if (mObjects) free(mObjects);
2441 }
2442}
2443
2444status_t Parcel::growData(size_t len)
2445{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002446 if (len > INT32_MAX) {
2447 // don't accept size_t values which may have come from an
2448 // inadvertent conversion from a negative int.
2449 return BAD_VALUE;
2450 }
2451
Martijn Coenenda2f2fd2020-01-22 10:46:25 +01002452 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2453 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002454 size_t newSize = ((mDataSize+len)*3)/2;
2455 return (newSize <= mDataSize)
2456 ? (status_t) NO_MEMORY
2457 : continueWrite(newSize);
2458}
2459
2460status_t Parcel::restartWrite(size_t desired)
2461{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002462 if (desired > INT32_MAX) {
2463 // don't accept size_t values which may have come from an
2464 // inadvertent conversion from a negative int.
2465 return BAD_VALUE;
2466 }
2467
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002468 if (mOwner) {
2469 freeData();
2470 return continueWrite(desired);
2471 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002472
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002473 uint8_t* data = (uint8_t*)realloc(mData, desired);
2474 if (!data && desired > mDataCapacity) {
2475 mError = NO_MEMORY;
2476 return NO_MEMORY;
2477 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002478
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002480
Devin Moore4a0a55e2020-06-04 13:23:10 -07002481 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002482 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002483 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002484 gParcelGlobalAllocSize += desired;
2485 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002486 if (!mData) {
2487 gParcelGlobalAllocCount++;
2488 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002489 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002490 mData = data;
2491 mDataCapacity = desired;
2492 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002493
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002494 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002495 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2496 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2497
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002498 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002499 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002500 mObjectsSize = mObjectsCapacity = 0;
2501 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002502 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002503 mHasFds = false;
2504 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002505 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002506
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002507 return NO_ERROR;
2508}
2509
2510status_t Parcel::continueWrite(size_t desired)
2511{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002512 if (desired > INT32_MAX) {
2513 // don't accept size_t values which may have come from an
2514 // inadvertent conversion from a negative int.
2515 return BAD_VALUE;
2516 }
2517
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002518 // If shrinking, first adjust for any objects that appear
2519 // after the new data size.
2520 size_t objectsSize = mObjectsSize;
2521 if (desired < mDataSize) {
2522 if (desired == 0) {
2523 objectsSize = 0;
2524 } else {
2525 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002526 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002527 break;
2528 objectsSize--;
2529 }
2530 }
2531 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002532
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002533 if (mOwner) {
2534 // If the size is going to zero, just release the owner's data.
2535 if (desired == 0) {
2536 freeData();
2537 return NO_ERROR;
2538 }
2539
2540 // If there is a different owner, we need to take
2541 // posession.
2542 uint8_t* data = (uint8_t*)malloc(desired);
2543 if (!data) {
2544 mError = NO_MEMORY;
2545 return NO_MEMORY;
2546 }
Yi Kong91635562018-06-07 14:38:36 -07002547 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002548
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002549 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002550 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002551 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002552 free(data);
2553
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002554 mError = NO_MEMORY;
2555 return NO_MEMORY;
2556 }
2557
2558 // Little hack to only acquire references on objects
2559 // we will be keeping.
2560 size_t oldObjectsSize = mObjectsSize;
2561 mObjectsSize = objectsSize;
2562 acquireObjects();
2563 mObjectsSize = oldObjectsSize;
2564 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002565
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002566 if (mData) {
2567 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2568 }
2569 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002570 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002571 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002572 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002573 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002574 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002575
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002576 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002577 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002578 gParcelGlobalAllocSize += desired;
2579 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002580 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002581
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002582 mData = data;
2583 mObjects = objects;
2584 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002585 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002586 mDataCapacity = desired;
2587 mObjectsSize = mObjectsCapacity = objectsSize;
2588 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002589 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002590
2591 } else if (mData) {
2592 if (objectsSize < mObjectsSize) {
2593 // Need to release refs on any objects we are dropping.
2594 const sp<ProcessState> proc(ProcessState::self());
2595 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2596 const flat_binder_object* flat
2597 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002598 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002599 // will need to rescan because we may have lopped off the only FDs
2600 mFdsKnown = false;
2601 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002602 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002603 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002604
2605 if (objectsSize == 0) {
2606 free(mObjects);
2607 mObjects = nullptr;
Michael Wachenschwanzc67d9f32019-10-15 11:49:22 -07002608 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002609 } else {
2610 binder_size_t* objects =
2611 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2612 if (objects) {
2613 mObjects = objects;
Michael Wachenschwanzc67d9f32019-10-15 11:49:22 -07002614 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002615 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002616 }
2617 mObjectsSize = objectsSize;
2618 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002619 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002620 }
2621
2622 // We own the data, so we can just do a realloc().
2623 if (desired > mDataCapacity) {
2624 uint8_t* data = (uint8_t*)realloc(mData, desired);
2625 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002626 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2627 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002628 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002629 gParcelGlobalAllocSize += desired;
2630 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002631 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002632 mData = data;
2633 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002634 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002635 mError = NO_MEMORY;
2636 return NO_MEMORY;
2637 }
2638 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002639 if (mDataSize > desired) {
2640 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002641 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002642 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002643 if (mDataPos > desired) {
2644 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002645 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002646 }
2647 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002648
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002649 } else {
2650 // This is the first data. Easy!
2651 uint8_t* data = (uint8_t*)malloc(desired);
2652 if (!data) {
2653 mError = NO_MEMORY;
2654 return NO_MEMORY;
2655 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002656
Yi Kong91635562018-06-07 14:38:36 -07002657 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002658 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002659 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002660 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002661
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002662 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002663 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002664 gParcelGlobalAllocSize += desired;
2665 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002666 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002667
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002668 mData = data;
2669 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002670 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2671 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002672 mDataCapacity = desired;
2673 }
2674
2675 return NO_ERROR;
2676}
2677
2678void Parcel::initState()
2679{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002680 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002681 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002682 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002683 mDataSize = 0;
2684 mDataCapacity = 0;
2685 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002686 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2687 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002688 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002689 mObjectsSize = 0;
2690 mObjectsCapacity = 0;
2691 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002692 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002693 mHasFds = false;
2694 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002695 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002696 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002697 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002698 mWorkSourceRequestHeaderPosition = 0;
2699 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002700
2701 // racing multiple init leads only to multiple identical write
2702 if (gMaxFds == 0) {
2703 struct rlimit result;
2704 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2705 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002706 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002707 } else {
2708 ALOGW("Unable to getrlimit: %s", strerror(errno));
2709 gMaxFds = 1024;
2710 }
2711 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002712}
2713
2714void Parcel::scanForFds() const
2715{
2716 bool hasFds = false;
2717 for (size_t i=0; i<mObjectsSize; i++) {
2718 const flat_binder_object* flat
2719 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002720 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002721 hasFds = true;
2722 break;
2723 }
2724 }
2725 mHasFds = hasFds;
2726 mFdsKnown = true;
2727}
2728
Dan Sandleraa5c2342015-04-10 10:08:45 -04002729size_t Parcel::getBlobAshmemSize() const
2730{
Adrian Roos6bb31142015-10-22 16:46:12 -07002731 // This used to return the size of all blobs that were written to ashmem, now we're returning
2732 // the ashmem currently referenced by this Parcel, which should be equivalent.
2733 // TODO: Remove method once ABI can be changed.
2734 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002735}
2736
Adrian Rooscbf37262015-10-22 16:12:53 -07002737size_t Parcel::getOpenAshmemSize() const
2738{
2739 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002740}
2741
2742// --- Parcel::Blob ---
2743
2744Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002745 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002746}
2747
2748Parcel::Blob::~Blob() {
2749 release();
2750}
2751
2752void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002753 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002754 ::munmap(mData, mSize);
2755 }
2756 clear();
2757}
2758
Jeff Brown13b16042014-11-11 16:44:25 -08002759void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2760 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002761 mData = data;
2762 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002763 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002764}
2765
2766void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002767 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002768 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002769 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002770 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002771}
2772
Steven Moreland6511af52019-09-26 16:05:45 -07002773} // namespace android