blob: 580573e2794859c0c1a0c68371f950d10f8016db [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080023#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080028#include <sys/stat.h>
29#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070030#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080031#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070032
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/Binder.h>
34#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080035#include <binder/IPCThreadState.h>
36#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070037#include <binder/ProcessState.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080038#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070039#include <binder/TextOutput.h>
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -080040#include <binder/Value.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041
Mark Salyzynabed7f72016-01-27 08:02:48 -080042#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070043#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080044#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080046#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String8.h>
48#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049
Mathias Agopian208059f2009-05-18 15:08:03 -070050#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080051#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053#ifndef INT32_MAX
54#define INT32_MAX ((int32_t)(2147483647))
55#endif
56
57#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080058//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080059#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080060//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070061
62// ---------------------------------------------------------------------------
63
Nick Kralevichb6b14232015-04-02 09:36:02 -070064// This macro should never be used at runtime, as a too large value
65// of s could cause an integer overflow. Instead, you should always
66// use the wrapper function pad_size()
67#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
68
69static size_t pad_size(size_t s) {
70 if (s > (SIZE_T_MAX - 3)) {
71 abort();
72 }
73 return PAD_SIZE_UNSAFE(s);
74}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070076// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060077#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070078
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070079namespace android {
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;
106 case BINDER_TYPE_WEAK_BINDER:
107 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800108 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 return;
110 case BINDER_TYPE_HANDLE: {
111 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700112 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700113 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
114 b->incStrong(who);
115 }
116 return;
117 }
118 case BINDER_TYPE_WEAK_HANDLE: {
119 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700120 if (b != nullptr) b.get_refs()->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121 return;
122 }
123 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000124 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700125 // If we own an ashmem fd, keep track of how much memory it refers to.
126 int size = ashmem_get_size_region(obj.handle);
127 if (size > 0) {
128 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700129 }
130 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700131 return;
132 }
133 }
134
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700135 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136}
137
Adrian Roos6bb31142015-10-22 16:46:12 -0700138static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700139 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700140{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700141 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142 case BINDER_TYPE_BINDER:
143 if (obj.binder) {
144 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800145 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700146 }
147 return;
148 case BINDER_TYPE_WEAK_BINDER:
149 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800150 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700151 return;
152 case BINDER_TYPE_HANDLE: {
153 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700154 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700155 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
156 b->decStrong(who);
157 }
158 return;
159 }
160 case BINDER_TYPE_WEAK_HANDLE: {
161 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700162 if (b != nullptr) b.get_refs()->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700163 return;
164 }
165 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800166 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000167 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700168 int size = ashmem_get_size_region(obj.handle);
169 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800170 // ashmem size might have changed since last time it was accounted for, e.g.
171 // in acquire_object(). Value of *outAshmemSize is not critical since we are
172 // releasing the object anyway. Check for integer overflow condition.
173 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700174 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700175 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800176
177 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700178 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700179 return;
180 }
181 }
182
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700183 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700184}
185
186inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800187 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700188{
189 return out->writeObject(flat, false);
190}
191
Steven Morelandb1c81202019-04-05 18:49:55 -0700192static status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700193 const sp<IBinder>& binder, Parcel* out)
194{
195 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700196
Martijn Coenen2b631742017-05-05 11:16:59 -0700197 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
198 /* minimum priority for all nodes is nice 0 */
199 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
200 } else {
201 /* minimum priority for all nodes is MAX_NICE(19) */
202 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
203 }
204
Yi Kong91635562018-06-07 14:38:36 -0700205 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800206 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700207 if (!local) {
208 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700209 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000210 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700211 }
212 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700213 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800214 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700215 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800216 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700217 } else {
Steven Morelandf0212002018-12-26 13:59:23 -0800218 if (local->isRequestingSid()) {
219 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
220 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700221 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800222 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
223 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700224 }
225 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700226 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800227 obj.binder = 0;
228 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700230
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 return finish_flatten_binder(binder, obj, out);
232}
233
Steven Morelandb1c81202019-04-05 18:49:55 -0700234static status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235 const wp<IBinder>& binder, Parcel* out)
236{
237 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700238
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700239 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Yi Kong91635562018-06-07 14:38:36 -0700240 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700241 sp<IBinder> real = binder.promote();
Yi Kong91635562018-06-07 14:38:36 -0700242 if (real != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243 IBinder *local = real->localBinder();
244 if (!local) {
245 BpBinder *proxy = real->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700246 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000247 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 }
249 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700250 obj.hdr.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800251 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700252 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800253 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700254 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700255 obj.hdr.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800256 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
257 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258 }
259 return finish_flatten_binder(real, obj, out);
260 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700261
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700262 // XXX How to deal? In order to flatten the given binder,
263 // we need to probe it for information, which requires a primary
264 // reference... but we don't have one.
265 //
266 // The OpenBinder implementation uses a dynamic_cast<> here,
267 // but we can't do that with the different reference counting
268 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000269 ALOGE("Unable to unflatten Binder weak reference!");
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700270 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800271 obj.binder = 0;
272 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700273 return finish_flatten_binder(nullptr, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700274
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700275 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700276 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800277 obj.binder = 0;
278 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700279 return finish_flatten_binder(nullptr, obj, out);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700280 }
281}
282
283inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800284 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
285 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700286{
287 return NO_ERROR;
288}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700289
Steven Morelandb1c81202019-04-05 18:49:55 -0700290static status_t unflatten_binder(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700291 const Parcel& in, sp<IBinder>* out)
292{
293 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700294
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700295 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700296 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700297 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800298 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700299 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700300 case BINDER_TYPE_HANDLE:
301 *out = proc->getStrongProxyForHandle(flat->handle);
302 return finish_unflatten_binder(
303 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700304 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700305 }
306 return BAD_TYPE;
307}
308
Steven Morelandb1c81202019-04-05 18:49:55 -0700309static status_t unflatten_binder(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700310 const Parcel& in, wp<IBinder>* out)
311{
312 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700313
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700314 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700315 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700316 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800317 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700318 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700319 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800320 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700321 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800322 reinterpret_cast<IBinder*>(flat->cookie),
323 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700324 } else {
Yi Kong91635562018-06-07 14:38:36 -0700325 *out = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700326 }
Yi Kong91635562018-06-07 14:38:36 -0700327 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700328 case BINDER_TYPE_HANDLE:
329 case BINDER_TYPE_WEAK_HANDLE:
330 *out = proc->getWeakProxyForHandle(flat->handle);
331 return finish_unflatten_binder(
332 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
333 }
334 }
335 return BAD_TYPE;
336}
337
338// ---------------------------------------------------------------------------
339
340Parcel::Parcel()
341{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800342 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700343 initState();
344}
345
346Parcel::~Parcel()
347{
348 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800349 LOG_ALLOC("Parcel %p: destroyed", this);
350}
351
352size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800353 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
354 size_t size = gParcelGlobalAllocSize;
355 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
356 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800357}
358
359size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800360 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
361 size_t count = gParcelGlobalAllocCount;
362 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
363 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700364}
365
366const uint8_t* Parcel::data() const
367{
368 return mData;
369}
370
371size_t Parcel::dataSize() const
372{
373 return (mDataSize > mDataPos ? mDataSize : mDataPos);
374}
375
376size_t Parcel::dataAvail() const
377{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700378 size_t result = dataSize() - dataPosition();
379 if (result > INT32_MAX) {
380 abort();
381 }
382 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700383}
384
385size_t Parcel::dataPosition() const
386{
387 return mDataPos;
388}
389
390size_t Parcel::dataCapacity() const
391{
392 return mDataCapacity;
393}
394
395status_t Parcel::setDataSize(size_t size)
396{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700397 if (size > INT32_MAX) {
398 // don't accept size_t values which may have come from an
399 // inadvertent conversion from a negative int.
400 return BAD_VALUE;
401 }
402
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700403 status_t err;
404 err = continueWrite(size);
405 if (err == NO_ERROR) {
406 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700407 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700408 }
409 return err;
410}
411
412void Parcel::setDataPosition(size_t pos) const
413{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700414 if (pos > INT32_MAX) {
415 // don't accept size_t values which may have come from an
416 // inadvertent conversion from a negative int.
417 abort();
418 }
419
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700420 mDataPos = pos;
421 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800422 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700423}
424
425status_t Parcel::setDataCapacity(size_t size)
426{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700427 if (size > INT32_MAX) {
428 // don't accept size_t values which may have come from an
429 // inadvertent conversion from a negative int.
430 return BAD_VALUE;
431 }
432
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700433 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 return NO_ERROR;
435}
436
437status_t Parcel::setData(const uint8_t* buffer, size_t len)
438{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700439 if (len > INT32_MAX) {
440 // don't accept size_t values which may have come from an
441 // inadvertent conversion from a negative int.
442 return BAD_VALUE;
443 }
444
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700445 status_t err = restartWrite(len);
446 if (err == NO_ERROR) {
447 memcpy(const_cast<uint8_t*>(data()), buffer, len);
448 mDataSize = len;
449 mFdsKnown = false;
450 }
451 return err;
452}
453
Andreas Huber51faf462011-04-13 10:21:56 -0700454status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700455{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700456 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700457 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800458 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 size_t size = parcel->mObjectsSize;
460 int startPos = mDataPos;
461 int firstIndex = -1, lastIndex = -2;
462
463 if (len == 0) {
464 return NO_ERROR;
465 }
466
Nick Kralevichb6b14232015-04-02 09:36:02 -0700467 if (len > INT32_MAX) {
468 // don't accept size_t values which may have come from an
469 // inadvertent conversion from a negative int.
470 return BAD_VALUE;
471 }
472
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473 // range checks against the source parcel size
474 if ((offset > parcel->mDataSize)
475 || (len > parcel->mDataSize)
476 || (offset + len > parcel->mDataSize)) {
477 return BAD_VALUE;
478 }
479
480 // Count objects in range
481 for (int i = 0; i < (int) size; i++) {
482 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700483 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700484 if (firstIndex == -1) {
485 firstIndex = i;
486 }
487 lastIndex = i;
488 }
489 }
490 int numObjects = lastIndex - firstIndex + 1;
491
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700492 if ((mDataSize+len) > mDataCapacity) {
493 // grow data
494 err = growData(len);
495 if (err != NO_ERROR) {
496 return err;
497 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700498 }
499
500 // append data
501 memcpy(mData + mDataPos, data + offset, len);
502 mDataPos += len;
503 mDataSize += len;
504
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400505 err = NO_ERROR;
506
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700507 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200508 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700509 // grow objects
510 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700511 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700512 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800513 binder_size_t *objects =
514 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700515 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700516 return NO_MEMORY;
517 }
518 mObjects = objects;
519 mObjectsCapacity = newSize;
520 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700521
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700522 // append and acquire objects
523 int idx = mObjectsSize;
524 for (int i = firstIndex; i <= lastIndex; i++) {
525 size_t off = objects[i] - offset + startPos;
526 mObjects[idx++] = off;
527 mObjectsSize++;
528
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700529 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700530 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700531 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700532
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700533 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700534 // If this is a file descriptor, we need to dup it so the
535 // new Parcel now owns its own fd, and can declare that we
536 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800537 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800538 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700539 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400540 if (!mAllowFds) {
541 err = FDS_NOT_ALLOWED;
542 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543 }
544 }
545 }
546
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400547 return err;
548}
549
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700550int Parcel::compareData(const Parcel& other) {
551 size_t size = dataSize();
552 if (size != other.dataSize()) {
553 return size < other.dataSize() ? -1 : 1;
554 }
555 return memcmp(data(), other.data(), size);
556}
557
Jeff Brown13b16042014-11-11 16:44:25 -0800558bool Parcel::allowFds() const
559{
560 return mAllowFds;
561}
562
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700563bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400564{
565 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700566 if (!allowFds) {
567 mAllowFds = false;
568 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400569 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700570}
571
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700572void Parcel::restoreAllowFds(bool lastValue)
573{
574 mAllowFds = lastValue;
575}
576
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700577bool Parcel::hasFileDescriptors() const
578{
579 if (!mFdsKnown) {
580 scanForFds();
581 }
582 return mHasFds;
583}
584
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000585void Parcel::updateWorkSourceRequestHeaderPosition() const {
586 // Only update the request headers once. We only want to point
587 // to the first headers read/written.
588 if (!mRequestHeaderPresent) {
589 mWorkSourceRequestHeaderPosition = dataPosition();
590 mRequestHeaderPresent = true;
591 }
592}
593
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700594// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700595status_t Parcel::writeInterfaceToken(const String16& interface)
596{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000597 const IPCThreadState* threadState = IPCThreadState::self();
598 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000599 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000600 writeInt32(threadState->shouldPropagateWorkSource() ?
601 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700602 // currently the interface identification token is just its name as a string
603 return writeString16(interface);
604}
605
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000606bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
607{
608 if (!mRequestHeaderPresent) {
609 return false;
610 }
611
612 const size_t initialPosition = dataPosition();
613 setDataPosition(mWorkSourceRequestHeaderPosition);
614 status_t err = writeInt32(uid);
615 setDataPosition(initialPosition);
616 return err == NO_ERROR;
617}
618
Steven Moreland0891c9b2019-05-06 15:05:13 -0700619uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000620{
621 if (!mRequestHeaderPresent) {
622 return IPCThreadState::kUnsetWorkSource;
623 }
624
625 const size_t initialPosition = dataPosition();
626 setDataPosition(mWorkSourceRequestHeaderPosition);
627 uid_t uid = readInt32();
628 setDataPosition(initialPosition);
629 return uid;
630}
631
Mathias Agopian83c04462009-05-22 19:00:22 -0700632bool Parcel::checkInterface(IBinder* binder) const
633{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700634 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700635}
636
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700637bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700638 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700639{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100640 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700641 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700642 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700643 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700644 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700645 if ((threadState->getLastTransactionBinderFlags() &
646 IBinder::FLAG_ONEWAY) != 0) {
647 // For one-way calls, the callee is running entirely
648 // disconnected from the caller, so disable StrictMode entirely.
649 // Not only does disk/network usage not impact the caller, but
650 // there's no way to commuicate back any violations anyway.
651 threadState->setStrictModePolicy(0);
652 } else {
653 threadState->setStrictModePolicy(strictPolicy);
654 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100655 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000656 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100657 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000658 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100659 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700660 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700661 if (str == interface) {
662 return true;
663 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700664 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700665 String8(interface).string(), String8(str).string());
666 return false;
667 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700668}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700669
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800670const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700671{
672 return mObjects;
673}
674
675size_t Parcel::objectsCount() const
676{
677 return mObjectsSize;
678}
679
680status_t Parcel::errorCheck() const
681{
682 return mError;
683}
684
685void Parcel::setError(status_t err)
686{
687 mError = err;
688}
689
690status_t Parcel::finishWrite(size_t len)
691{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700692 if (len > INT32_MAX) {
693 // don't accept size_t values which may have come from an
694 // inadvertent conversion from a negative int.
695 return BAD_VALUE;
696 }
697
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700698 //printf("Finish write of %d\n", len);
699 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700700 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700701 if (mDataPos > mDataSize) {
702 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700703 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700704 }
705 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
706 return NO_ERROR;
707}
708
709status_t Parcel::writeUnpadded(const void* data, size_t len)
710{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700711 if (len > INT32_MAX) {
712 // don't accept size_t values which may have come from an
713 // inadvertent conversion from a negative int.
714 return BAD_VALUE;
715 }
716
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700717 size_t end = mDataPos + len;
718 if (end < mDataPos) {
719 // integer overflow
720 return BAD_VALUE;
721 }
722
723 if (end <= mDataCapacity) {
724restart_write:
725 memcpy(mData+mDataPos, data, len);
726 return finishWrite(len);
727 }
728
729 status_t err = growData(len);
730 if (err == NO_ERROR) goto restart_write;
731 return err;
732}
733
734status_t Parcel::write(const void* data, size_t len)
735{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700736 if (len > INT32_MAX) {
737 // don't accept size_t values which may have come from an
738 // inadvertent conversion from a negative int.
739 return BAD_VALUE;
740 }
741
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700742 void* const d = writeInplace(len);
743 if (d) {
744 memcpy(d, data, len);
745 return NO_ERROR;
746 }
747 return mError;
748}
749
750void* Parcel::writeInplace(size_t len)
751{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700752 if (len > INT32_MAX) {
753 // don't accept size_t values which may have come from an
754 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700755 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700756 }
757
758 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700759
760 // sanity check for integer overflow
761 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700762 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700763 }
764
765 if ((mDataPos+padded) <= mDataCapacity) {
766restart_write:
767 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
768 uint8_t* const data = mData+mDataPos;
769
770 // Need to pad at end?
771 if (padded != len) {
772#if BYTE_ORDER == BIG_ENDIAN
773 static const uint32_t mask[4] = {
774 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
775 };
776#endif
777#if BYTE_ORDER == LITTLE_ENDIAN
778 static const uint32_t mask[4] = {
779 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
780 };
781#endif
782 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
783 // *reinterpret_cast<void**>(data+padded-4));
784 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
785 }
786
787 finishWrite(padded);
788 return data;
789 }
790
791 status_t err = growData(padded);
792 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700793 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700794}
795
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800796status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
797 const uint8_t* strData = (uint8_t*)str.data();
798 const size_t strLen= str.length();
799 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100800 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800801 return BAD_VALUE;
802 }
803
804 status_t err = writeInt32(utf16Len);
805 if (err) {
806 return err;
807 }
808
809 // Allocate enough bytes to hold our converted string and its terminating NULL.
810 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
811 if (!dst) {
812 return NO_MEMORY;
813 }
814
Sergio Girof4607432016-07-21 14:46:35 +0100815 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800816
817 return NO_ERROR;
818}
819
820status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
821 if (!str) {
822 return writeInt32(-1);
823 }
824 return writeUtf8AsUtf16(*str);
825}
826
Casey Dahlin185d3442016-02-09 11:08:35 -0800827namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800828
Casey Dahlin185d3442016-02-09 11:08:35 -0800829template<typename T>
830status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700831{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700832 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700833 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700834 status = BAD_VALUE;
835 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700836 }
837
Casey Dahlin185d3442016-02-09 11:08:35 -0800838 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700839 if (status != OK) {
840 return status;
841 }
842
Casey Dahlin185d3442016-02-09 11:08:35 -0800843 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700844 if (!data) {
845 status = BAD_VALUE;
846 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700847 }
848
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700849 memcpy(data, val.data(), val.size());
850 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700851}
852
Casey Dahlin185d3442016-02-09 11:08:35 -0800853template<typename T>
854status_t writeByteVectorInternalPtr(Parcel* parcel,
855 const std::unique_ptr<std::vector<T>>& val)
856{
857 if (!val) {
858 return parcel->writeInt32(-1);
859 }
860
861 return writeByteVectorInternal(parcel, *val);
862}
863
864} // namespace
865
866status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
867 return writeByteVectorInternal(this, val);
868}
869
870status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
871{
872 return writeByteVectorInternalPtr(this, val);
873}
874
875status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
876 return writeByteVectorInternal(this, val);
877}
878
879status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
880{
881 return writeByteVectorInternalPtr(this, val);
882}
883
Casey Dahlin451ff582015-10-19 18:12:18 -0700884status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
885{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800886 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700887}
888
Casey Dahlinb9872622015-11-25 15:09:45 -0800889status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
890{
891 return writeNullableTypedVector(val, &Parcel::writeInt32);
892}
893
Casey Dahlin451ff582015-10-19 18:12:18 -0700894status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
895{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800896 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700897}
898
Casey Dahlinb9872622015-11-25 15:09:45 -0800899status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
900{
901 return writeNullableTypedVector(val, &Parcel::writeInt64);
902}
903
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800904status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
905{
906 return writeTypedVector(val, &Parcel::writeUint64);
907}
908
909status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
910{
911 return writeNullableTypedVector(val, &Parcel::writeUint64);
912}
913
Casey Dahlin451ff582015-10-19 18:12:18 -0700914status_t Parcel::writeFloatVector(const std::vector<float>& val)
915{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800916 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700917}
918
Casey Dahlinb9872622015-11-25 15:09:45 -0800919status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
920{
921 return writeNullableTypedVector(val, &Parcel::writeFloat);
922}
923
Casey Dahlin451ff582015-10-19 18:12:18 -0700924status_t Parcel::writeDoubleVector(const std::vector<double>& val)
925{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800926 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700927}
928
Casey Dahlinb9872622015-11-25 15:09:45 -0800929status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
930{
931 return writeNullableTypedVector(val, &Parcel::writeDouble);
932}
933
Casey Dahlin451ff582015-10-19 18:12:18 -0700934status_t Parcel::writeBoolVector(const std::vector<bool>& val)
935{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800936 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700937}
938
Casey Dahlinb9872622015-11-25 15:09:45 -0800939status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
940{
941 return writeNullableTypedVector(val, &Parcel::writeBool);
942}
943
Casey Dahlin451ff582015-10-19 18:12:18 -0700944status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
945{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800946 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700947}
948
Casey Dahlinb9872622015-11-25 15:09:45 -0800949status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
950{
951 return writeNullableTypedVector(val, &Parcel::writeChar);
952}
953
Casey Dahlin451ff582015-10-19 18:12:18 -0700954status_t Parcel::writeString16Vector(const std::vector<String16>& val)
955{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800956 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700957}
958
Casey Dahlinb9872622015-11-25 15:09:45 -0800959status_t Parcel::writeString16Vector(
960 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
961{
962 return writeNullableTypedVector(val, &Parcel::writeString16);
963}
964
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800965status_t Parcel::writeUtf8VectorAsUtf16Vector(
966 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
967 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
968}
969
970status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
971 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
972}
973
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700974status_t Parcel::writeInt32(int32_t val)
975{
Andreas Huber84a6d042009-08-17 13:33:27 -0700976 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700977}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800978
979status_t Parcel::writeUint32(uint32_t val)
980{
981 return writeAligned(val);
982}
983
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700984status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700985 if (len > INT32_MAX) {
986 // don't accept size_t values which may have come from an
987 // inadvertent conversion from a negative int.
988 return BAD_VALUE;
989 }
990
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700991 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700992 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700993 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700994 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700995 if (ret == NO_ERROR) {
996 ret = write(val, len * sizeof(*val));
997 }
998 return ret;
999}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001000status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001001 if (len > INT32_MAX) {
1002 // don't accept size_t values which may have come from an
1003 // inadvertent conversion from a negative int.
1004 return BAD_VALUE;
1005 }
1006
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001007 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001008 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001009 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001010 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001011 if (ret == NO_ERROR) {
1012 ret = write(val, len * sizeof(*val));
1013 }
1014 return ret;
1015}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001016
Casey Dahlind6848f52015-10-15 15:44:59 -07001017status_t Parcel::writeBool(bool val)
1018{
1019 return writeInt32(int32_t(val));
1020}
1021
1022status_t Parcel::writeChar(char16_t val)
1023{
1024 return writeInt32(int32_t(val));
1025}
1026
1027status_t Parcel::writeByte(int8_t val)
1028{
1029 return writeInt32(int32_t(val));
1030}
1031
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001032status_t Parcel::writeInt64(int64_t val)
1033{
Andreas Huber84a6d042009-08-17 13:33:27 -07001034 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001035}
1036
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001037status_t Parcel::writeUint64(uint64_t val)
1038{
1039 return writeAligned(val);
1040}
1041
Serban Constantinescuf683e012013-11-05 16:53:55 +00001042status_t Parcel::writePointer(uintptr_t val)
1043{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001044 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001045}
1046
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001047status_t Parcel::writeFloat(float val)
1048{
Andreas Huber84a6d042009-08-17 13:33:27 -07001049 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001050}
1051
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001052#if defined(__mips__) && defined(__mips_hard_float)
1053
1054status_t Parcel::writeDouble(double val)
1055{
1056 union {
1057 double d;
1058 unsigned long long ll;
1059 } u;
1060 u.d = val;
1061 return writeAligned(u.ll);
1062}
1063
1064#else
1065
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001066status_t Parcel::writeDouble(double val)
1067{
Andreas Huber84a6d042009-08-17 13:33:27 -07001068 return writeAligned(val);
1069}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001070
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001071#endif
1072
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001073status_t Parcel::writeCString(const char* str)
1074{
1075 return write(str, strlen(str)+1);
1076}
1077
1078status_t Parcel::writeString8(const String8& str)
1079{
1080 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001081 // only write string if its length is more than zero characters,
1082 // as readString8 will only read if the length field is non-zero.
1083 // this is slightly different from how writeString16 works.
1084 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001085 err = write(str.string(), str.bytes()+1);
1086 }
1087 return err;
1088}
1089
Casey Dahlinb9872622015-11-25 15:09:45 -08001090status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1091{
1092 if (!str) {
1093 return writeInt32(-1);
1094 }
1095
1096 return writeString16(*str);
1097}
1098
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001099status_t Parcel::writeString16(const String16& str)
1100{
1101 return writeString16(str.string(), str.size());
1102}
1103
1104status_t Parcel::writeString16(const char16_t* str, size_t len)
1105{
Yi Kong91635562018-06-07 14:38:36 -07001106 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001107
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001108 status_t err = writeInt32(len);
1109 if (err == NO_ERROR) {
1110 len *= sizeof(char16_t);
1111 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1112 if (data) {
1113 memcpy(data, str, len);
1114 *reinterpret_cast<char16_t*>(data+len) = 0;
1115 return NO_ERROR;
1116 }
1117 err = mError;
1118 }
1119 return err;
1120}
1121
1122status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1123{
1124 return flatten_binder(ProcessState::self(), val, this);
1125}
1126
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001127status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1128{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001129 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001130}
1131
Casey Dahlinb9872622015-11-25 15:09:45 -08001132status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1133{
1134 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1135}
1136
1137status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001138 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001139}
1140
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001141status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001142 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001143}
1144
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001145status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1146{
1147 return flatten_binder(ProcessState::self(), val, this);
1148}
1149
Casey Dahlinb9872622015-11-25 15:09:45 -08001150status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1151 if (!parcelable) {
1152 return writeInt32(0);
1153 }
1154
1155 return writeParcelable(*parcelable);
1156}
1157
Christopher Wiley97f048d2015-11-19 06:49:05 -08001158status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1159 status_t status = writeInt32(1); // parcelable is not null.
1160 if (status != OK) {
1161 return status;
1162 }
1163 return parcelable.writeToParcel(this);
1164}
1165
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001166status_t Parcel::writeValue(const binder::Value& value) {
1167 return value.writeToParcel(this);
1168}
1169
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001170status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001171{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001172 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001173 return BAD_TYPE;
1174
1175 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001176 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001177 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001179 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001180 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001182 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1183 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184
1185 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001186 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 return err;
1188 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001189 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001190 return err;
1191}
1192
Jeff Brown93ff1f92011-11-04 19:01:44 -07001193status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001194{
1195 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001196 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001197 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001198 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001199 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001200 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001201 return writeObject(obj, true);
1202}
1203
1204status_t Parcel::writeDupFileDescriptor(int fd)
1205{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001206 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001207 if (dupFd < 0) {
1208 return -errno;
1209 }
1210 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001211 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001212 close(dupFd);
1213 }
1214 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001215}
1216
Dianne Hackborn1941a402016-08-29 12:30:43 -07001217status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1218{
1219 writeInt32(0);
1220 return writeFileDescriptor(fd, takeOwnership);
1221}
1222
Ryo Hashimotobf551892018-05-31 16:58:35 +09001223status_t Parcel::writeDupParcelFileDescriptor(int fd)
1224{
1225 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1226 if (dupFd < 0) {
1227 return -errno;
1228 }
1229 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1230 if (err != OK) {
1231 close(dupFd);
1232 }
1233 return err;
1234}
1235
Christopher Wiley2cf19952016-04-11 11:09:37 -07001236status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001237 return writeDupFileDescriptor(fd.get());
1238}
1239
Christopher Wiley2cf19952016-04-11 11:09:37 -07001240status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001241 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1242}
1243
Christopher Wiley2cf19952016-04-11 11:09:37 -07001244status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001245 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1246}
1247
Jeff Brown13b16042014-11-11 16:44:25 -08001248status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001249{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001250 if (len > INT32_MAX) {
1251 // don't accept size_t values which may have come from an
1252 // inadvertent conversion from a negative int.
1253 return BAD_VALUE;
1254 }
1255
Jeff Brown13b16042014-11-11 16:44:25 -08001256 status_t status;
1257 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001258 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001259 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001260 if (status) return status;
1261
1262 void* ptr = writeInplace(len);
1263 if (!ptr) return NO_MEMORY;
1264
Jeff Brown13b16042014-11-11 16:44:25 -08001265 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001266 return NO_ERROR;
1267 }
1268
Steve Block6807e592011-10-20 11:56:00 +01001269 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001270 int fd = ashmem_create_region("Parcel Blob", len);
1271 if (fd < 0) return NO_MEMORY;
1272
1273 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1274 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001275 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001276 } else {
Yi Kong91635562018-06-07 14:38:36 -07001277 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001278 if (ptr == MAP_FAILED) {
1279 status = -errno;
1280 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001281 if (!mutableCopy) {
1282 result = ashmem_set_prot_region(fd, PROT_READ);
1283 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001284 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001285 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001286 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001287 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001288 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001289 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001290 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001291 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001292 return NO_ERROR;
1293 }
1294 }
1295 }
1296 }
1297 ::munmap(ptr, len);
1298 }
1299 ::close(fd);
1300 return status;
1301}
1302
Jeff Brown13b16042014-11-11 16:44:25 -08001303status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1304{
1305 // Must match up with what's done in writeBlob.
1306 if (!mAllowFds) return FDS_NOT_ALLOWED;
1307 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1308 if (status) return status;
1309 return writeDupFileDescriptor(fd);
1310}
1311
Mathias Agopiane1424282013-07-29 21:24:40 -07001312status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001313{
1314 status_t err;
1315
1316 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001317 const size_t len = val.getFlattenedSize();
1318 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001319
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001320 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001321 // don't accept size_t values which may have come from an
1322 // inadvertent conversion from a negative int.
1323 return BAD_VALUE;
1324 }
1325
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001326 err = this->writeInt32(len);
1327 if (err) return err;
1328
1329 err = this->writeInt32(fd_count);
1330 if (err) return err;
1331
1332 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001333 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001334 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001335 return BAD_VALUE;
1336
Yi Kong91635562018-06-07 14:38:36 -07001337 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001338 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001339 fds = new (std::nothrow) int[fd_count];
1340 if (fds == nullptr) {
1341 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1342 return BAD_VALUE;
1343 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001344 }
1345
1346 err = val.flatten(buf, len, fds, fd_count);
1347 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1348 err = this->writeDupFileDescriptor( fds[i] );
1349 }
1350
1351 if (fd_count) {
1352 delete [] fds;
1353 }
1354
1355 return err;
1356}
1357
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001358status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1359{
1360 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1361 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1362 if (enoughData && enoughObjects) {
1363restart_write:
1364 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001365
Christopher Tate98e67d32015-06-03 18:44:15 -07001366 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001367 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001368 if (!mAllowFds) {
1369 // fail before modifying our object index
1370 return FDS_NOT_ALLOWED;
1371 }
1372 mHasFds = mFdsKnown = true;
1373 }
1374
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001375 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001376 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001377 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001378 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001379 mObjectsSize++;
1380 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001381
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001382 return finishWrite(sizeof(flat_binder_object));
1383 }
1384
1385 if (!enoughData) {
1386 const status_t err = growData(sizeof(val));
1387 if (err != NO_ERROR) return err;
1388 }
1389 if (!enoughObjects) {
1390 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001391 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001392 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001393 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001394 mObjects = objects;
1395 mObjectsCapacity = newSize;
1396 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001397
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001398 goto restart_write;
1399}
1400
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001401status_t Parcel::writeNoException()
1402{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001403 binder::Status status;
1404 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001405}
1406
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001407status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1408{
1409 using ::std::map;
1410 using ::android::binder::Value;
1411 using ::android::binder::Map;
1412
1413 Map::const_iterator iter;
1414 status_t ret;
1415
1416 ret = writeInt32(map_in.size());
1417
1418 if (ret != NO_ERROR) {
1419 return ret;
1420 }
1421
1422 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1423 ret = writeValue(Value(iter->first));
1424 if (ret != NO_ERROR) {
1425 return ret;
1426 }
1427
1428 ret = writeValue(iter->second);
1429 if (ret != NO_ERROR) {
1430 return ret;
1431 }
1432 }
1433
1434 return ret;
1435}
1436
1437status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1438{
Yi Kong91635562018-06-07 14:38:36 -07001439 if (map == nullptr) {
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001440 return writeInt32(-1);
1441 }
1442
1443 return writeMap(*map.get());
1444}
1445
1446status_t Parcel::readMap(::android::binder::Map* map_out)const
1447{
1448 using ::std::map;
1449 using ::android::String16;
1450 using ::android::String8;
1451 using ::android::binder::Value;
1452 using ::android::binder::Map;
1453
1454 status_t ret = NO_ERROR;
1455 int32_t count;
1456
1457 ret = readInt32(&count);
1458 if (ret != NO_ERROR) {
1459 return ret;
1460 }
1461
1462 if (count < 0) {
1463 ALOGE("readMap: Unexpected count: %d", count);
1464 return (count == -1)
1465 ? UNEXPECTED_NULL
1466 : BAD_VALUE;
1467 }
1468
1469 map_out->clear();
1470
1471 while (count--) {
1472 Map::key_type key;
1473 Value value;
1474
1475 ret = readValue(&value);
1476 if (ret != NO_ERROR) {
1477 return ret;
1478 }
1479
1480 if (!value.getString(&key)) {
1481 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1482 return BAD_VALUE;
1483 }
1484
1485 ret = readValue(&value);
1486 if (ret != NO_ERROR) {
1487 return ret;
1488 }
1489
1490 (*map_out)[key] = value;
1491 }
1492
1493 return ret;
1494}
1495
1496status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1497{
1498 const size_t start = dataPosition();
1499 int32_t count;
1500 status_t status = readInt32(&count);
1501 map->reset();
1502
1503 if (status != OK || count == -1) {
1504 return status;
1505 }
1506
1507 setDataPosition(start);
1508 map->reset(new binder::Map());
1509
1510 status = readMap(map->get());
1511
1512 if (status != OK) {
1513 map->reset();
1514 }
1515
1516 return status;
1517}
1518
1519
1520
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001521void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001522{
1523 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1524}
1525
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001526status_t Parcel::validateReadData(size_t upperBound) const
1527{
1528 // Don't allow non-object reads on object data
1529 if (mObjectsSorted || mObjectsSize <= 1) {
1530data_sorted:
1531 // Expect to check only against the next object
1532 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1533 // For some reason the current read position is greater than the next object
1534 // hint. Iterate until we find the right object
1535 size_t nextObject = mNextObjectHint;
1536 do {
1537 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1538 // Requested info overlaps with an object
1539 ALOGE("Attempt to read from protected data in Parcel %p", this);
1540 return PERMISSION_DENIED;
1541 }
1542 nextObject++;
1543 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1544 mNextObjectHint = nextObject;
1545 }
1546 return NO_ERROR;
1547 }
1548 // Quickly determine if mObjects is sorted.
1549 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1550 binder_size_t* prevObj = currObj;
1551 while (currObj > mObjects) {
1552 prevObj--;
1553 if(*prevObj > *currObj) {
1554 goto data_unsorted;
1555 }
1556 currObj--;
1557 }
1558 mObjectsSorted = true;
1559 goto data_sorted;
1560
1561data_unsorted:
1562 // Insertion Sort mObjects
1563 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1564 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1565 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1566 binder_size_t temp = *iter0;
1567 binder_size_t* iter1 = iter0 - 1;
1568 while (iter1 >= mObjects && *iter1 > temp) {
1569 *(iter1 + 1) = *iter1;
1570 iter1--;
1571 }
1572 *(iter1 + 1) = temp;
1573 }
1574 mNextObjectHint = 0;
1575 mObjectsSorted = true;
1576 goto data_sorted;
1577}
1578
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001579status_t Parcel::read(void* outData, size_t len) const
1580{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001581 if (len > INT32_MAX) {
1582 // don't accept size_t values which may have come from an
1583 // inadvertent conversion from a negative int.
1584 return BAD_VALUE;
1585 }
1586
1587 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1588 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001589 if (mObjectsSize > 0) {
1590 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001591 if(err != NO_ERROR) {
1592 // Still increment the data position by the expected length
1593 mDataPos += pad_size(len);
1594 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1595 return err;
1596 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001597 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001598 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001599 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001600 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001601 return NO_ERROR;
1602 }
1603 return NOT_ENOUGH_DATA;
1604}
1605
1606const void* Parcel::readInplace(size_t len) const
1607{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001608 if (len > INT32_MAX) {
1609 // don't accept size_t values which may have come from an
1610 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001611 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001612 }
1613
1614 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1615 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001616 if (mObjectsSize > 0) {
1617 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001618 if(err != NO_ERROR) {
1619 // Still increment the data position by the expected length
1620 mDataPos += pad_size(len);
1621 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001622 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001623 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001624 }
1625
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001626 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001627 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001628 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001629 return data;
1630 }
Yi Kong91635562018-06-07 14:38:36 -07001631 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001632}
1633
Andreas Huber84a6d042009-08-17 13:33:27 -07001634template<class T>
1635status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001636 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001637
1638 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001639 if (mObjectsSize > 0) {
1640 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001641 if(err != NO_ERROR) {
1642 // Still increment the data position by the expected length
1643 mDataPos += sizeof(T);
1644 return err;
1645 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001646 }
1647
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001648 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001649 mDataPos += sizeof(T);
1650 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001651 return NO_ERROR;
1652 } else {
1653 return NOT_ENOUGH_DATA;
1654 }
1655}
1656
Andreas Huber84a6d042009-08-17 13:33:27 -07001657template<class T>
1658T Parcel::readAligned() const {
1659 T result;
1660 if (readAligned(&result) != NO_ERROR) {
1661 result = 0;
1662 }
1663
1664 return result;
1665}
1666
1667template<class T>
1668status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001669 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001670
1671 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1672restart_write:
1673 *reinterpret_cast<T*>(mData+mDataPos) = val;
1674 return finishWrite(sizeof(val));
1675 }
1676
1677 status_t err = growData(sizeof(val));
1678 if (err == NO_ERROR) goto restart_write;
1679 return err;
1680}
1681
Casey Dahlin185d3442016-02-09 11:08:35 -08001682namespace {
1683
1684template<typename T>
1685status_t readByteVectorInternal(const Parcel* parcel,
1686 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001687 val->clear();
1688
1689 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001690 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001691
1692 if (status != OK) {
1693 return status;
1694 }
1695
Christopher Wiley4db672d2015-11-10 09:44:30 -08001696 if (size < 0) {
1697 status = UNEXPECTED_NULL;
1698 return status;
1699 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001700 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001701 status = BAD_VALUE;
1702 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001703 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001704
Paul Lietar433e87b2016-09-16 10:39:32 -07001705 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001706 if (!data) {
1707 status = BAD_VALUE;
1708 return status;
1709 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001710 val->reserve(size);
1711 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001712
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001713 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001714}
1715
Casey Dahlin185d3442016-02-09 11:08:35 -08001716template<typename T>
1717status_t readByteVectorInternalPtr(
1718 const Parcel* parcel,
1719 std::unique_ptr<std::vector<T>>* val) {
1720 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001721 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001722 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001723 val->reset();
1724
1725 if (status != OK || size < 0) {
1726 return status;
1727 }
1728
Casey Dahlin185d3442016-02-09 11:08:35 -08001729 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001730 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001731
Casey Dahlin185d3442016-02-09 11:08:35 -08001732 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001733
1734 if (status != OK) {
1735 val->reset();
1736 }
1737
1738 return status;
1739}
1740
Casey Dahlin185d3442016-02-09 11:08:35 -08001741} // namespace
1742
1743status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1744 return readByteVectorInternal(this, val);
1745}
1746
1747status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1748 return readByteVectorInternal(this, val);
1749}
1750
1751status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1752 return readByteVectorInternalPtr(this, val);
1753}
1754
1755status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1756 return readByteVectorInternalPtr(this, val);
1757}
1758
Casey Dahlinb9872622015-11-25 15:09:45 -08001759status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1760 return readNullableTypedVector(val, &Parcel::readInt32);
1761}
1762
Casey Dahlin451ff582015-10-19 18:12:18 -07001763status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001764 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001765}
1766
Casey Dahlinb9872622015-11-25 15:09:45 -08001767status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1768 return readNullableTypedVector(val, &Parcel::readInt64);
1769}
1770
Casey Dahlin451ff582015-10-19 18:12:18 -07001771status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001772 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001773}
1774
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001775status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1776 return readNullableTypedVector(val, &Parcel::readUint64);
1777}
1778
1779status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1780 return readTypedVector(val, &Parcel::readUint64);
1781}
1782
Casey Dahlinb9872622015-11-25 15:09:45 -08001783status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1784 return readNullableTypedVector(val, &Parcel::readFloat);
1785}
1786
Casey Dahlin451ff582015-10-19 18:12:18 -07001787status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001788 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001789}
1790
Casey Dahlinb9872622015-11-25 15:09:45 -08001791status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1792 return readNullableTypedVector(val, &Parcel::readDouble);
1793}
1794
Casey Dahlin451ff582015-10-19 18:12:18 -07001795status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001796 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001797}
1798
Casey Dahlinb9872622015-11-25 15:09:45 -08001799status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1800 const int32_t start = dataPosition();
1801 int32_t size;
1802 status_t status = readInt32(&size);
1803 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001804
Casey Dahlinb9872622015-11-25 15:09:45 -08001805 if (status != OK || size < 0) {
1806 return status;
1807 }
1808
1809 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001810 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001811
1812 status = readBoolVector(val->get());
1813
1814 if (status != OK) {
1815 val->reset();
1816 }
1817
1818 return status;
1819}
1820
1821status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001822 int32_t size;
1823 status_t status = readInt32(&size);
1824
1825 if (status != OK) {
1826 return status;
1827 }
1828
1829 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001830 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001831 }
1832
1833 val->resize(size);
1834
1835 /* C++ bool handling means a vector of bools isn't necessarily addressable
1836 * (we might use individual bits)
1837 */
Christopher Wiley97887982015-10-27 16:33:47 -07001838 bool data;
1839 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001840 status = readBool(&data);
1841 (*val)[i] = data;
1842
1843 if (status != OK) {
1844 return status;
1845 }
1846 }
1847
1848 return OK;
1849}
1850
Casey Dahlinb9872622015-11-25 15:09:45 -08001851status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1852 return readNullableTypedVector(val, &Parcel::readChar);
1853}
1854
Casey Dahlin451ff582015-10-19 18:12:18 -07001855status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001856 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001857}
1858
Casey Dahlinb9872622015-11-25 15:09:45 -08001859status_t Parcel::readString16Vector(
1860 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1861 return readNullableTypedVector(val, &Parcel::readString16);
1862}
1863
Casey Dahlin451ff582015-10-19 18:12:18 -07001864status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001865 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001866}
1867
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001868status_t Parcel::readUtf8VectorFromUtf16Vector(
1869 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1870 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1871}
1872
1873status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1874 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1875}
Casey Dahlin451ff582015-10-19 18:12:18 -07001876
Andreas Huber84a6d042009-08-17 13:33:27 -07001877status_t Parcel::readInt32(int32_t *pArg) const
1878{
1879 return readAligned(pArg);
1880}
1881
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001882int32_t Parcel::readInt32() const
1883{
Andreas Huber84a6d042009-08-17 13:33:27 -07001884 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001885}
1886
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001887status_t Parcel::readUint32(uint32_t *pArg) const
1888{
1889 return readAligned(pArg);
1890}
1891
1892uint32_t Parcel::readUint32() const
1893{
1894 return readAligned<uint32_t>();
1895}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001896
1897status_t Parcel::readInt64(int64_t *pArg) const
1898{
Andreas Huber84a6d042009-08-17 13:33:27 -07001899 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001900}
1901
1902
1903int64_t Parcel::readInt64() const
1904{
Andreas Huber84a6d042009-08-17 13:33:27 -07001905 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001906}
1907
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001908status_t Parcel::readUint64(uint64_t *pArg) const
1909{
1910 return readAligned(pArg);
1911}
1912
1913uint64_t Parcel::readUint64() const
1914{
1915 return readAligned<uint64_t>();
1916}
1917
Serban Constantinescuf683e012013-11-05 16:53:55 +00001918status_t Parcel::readPointer(uintptr_t *pArg) const
1919{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001920 status_t ret;
1921 binder_uintptr_t ptr;
1922 ret = readAligned(&ptr);
1923 if (!ret)
1924 *pArg = ptr;
1925 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001926}
1927
1928uintptr_t Parcel::readPointer() const
1929{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001930 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001931}
1932
1933
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001934status_t Parcel::readFloat(float *pArg) const
1935{
Andreas Huber84a6d042009-08-17 13:33:27 -07001936 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001937}
1938
1939
1940float Parcel::readFloat() const
1941{
Andreas Huber84a6d042009-08-17 13:33:27 -07001942 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001943}
1944
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001945#if defined(__mips__) && defined(__mips_hard_float)
1946
1947status_t Parcel::readDouble(double *pArg) const
1948{
1949 union {
1950 double d;
1951 unsigned long long ll;
1952 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001953 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001954 status_t status;
1955 status = readAligned(&u.ll);
1956 *pArg = u.d;
1957 return status;
1958}
1959
1960double Parcel::readDouble() const
1961{
1962 union {
1963 double d;
1964 unsigned long long ll;
1965 } u;
1966 u.ll = readAligned<unsigned long long>();
1967 return u.d;
1968}
1969
1970#else
1971
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001972status_t Parcel::readDouble(double *pArg) const
1973{
Andreas Huber84a6d042009-08-17 13:33:27 -07001974 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001975}
1976
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001977double Parcel::readDouble() const
1978{
Andreas Huber84a6d042009-08-17 13:33:27 -07001979 return readAligned<double>();
1980}
1981
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001982#endif
1983
Andreas Huber84a6d042009-08-17 13:33:27 -07001984status_t Parcel::readIntPtr(intptr_t *pArg) const
1985{
1986 return readAligned(pArg);
1987}
1988
1989
1990intptr_t Parcel::readIntPtr() const
1991{
1992 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001993}
1994
Casey Dahlind6848f52015-10-15 15:44:59 -07001995status_t Parcel::readBool(bool *pArg) const
1996{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001997 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001998 status_t ret = readInt32(&tmp);
1999 *pArg = (tmp != 0);
2000 return ret;
2001}
2002
2003bool Parcel::readBool() const
2004{
2005 return readInt32() != 0;
2006}
2007
2008status_t Parcel::readChar(char16_t *pArg) const
2009{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002010 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002011 status_t ret = readInt32(&tmp);
2012 *pArg = char16_t(tmp);
2013 return ret;
2014}
2015
2016char16_t Parcel::readChar() const
2017{
2018 return char16_t(readInt32());
2019}
2020
2021status_t Parcel::readByte(int8_t *pArg) const
2022{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002023 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002024 status_t ret = readInt32(&tmp);
2025 *pArg = int8_t(tmp);
2026 return ret;
2027}
2028
2029int8_t Parcel::readByte() const
2030{
2031 return int8_t(readInt32());
2032}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002033
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002034status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2035 size_t utf16Size = 0;
2036 const char16_t* src = readString16Inplace(&utf16Size);
2037 if (!src) {
2038 return UNEXPECTED_NULL;
2039 }
2040
2041 // Save ourselves the trouble, we're done.
2042 if (utf16Size == 0u) {
2043 str->clear();
2044 return NO_ERROR;
2045 }
2046
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002047 // Allow for closing '\0'
2048 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2049 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002050 return BAD_VALUE;
2051 }
2052 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002053 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002054 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002055 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2056 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002057 return NO_ERROR;
2058}
2059
2060status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2061 const int32_t start = dataPosition();
2062 int32_t size;
2063 status_t status = readInt32(&size);
2064 str->reset();
2065
2066 if (status != OK || size < 0) {
2067 return status;
2068 }
2069
2070 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002071 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002072 return readUtf8FromUtf16(str->get());
2073}
2074
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002075const char* Parcel::readCString() const
2076{
Steven Morelandf5e6c7e2019-05-17 13:14:06 -07002077 if (mDataPos < mDataSize) {
2078 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002079 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2080 // is the string's trailing NUL within the parcel's valid bounds?
2081 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2082 if (eos) {
2083 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002084 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002085 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002086 return str;
2087 }
2088 }
Yi Kong91635562018-06-07 14:38:36 -07002089 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002090}
2091
2092String8 Parcel::readString8() const
2093{
Roshan Pius87b64d22016-07-18 12:51:02 -07002094 String8 retString;
2095 status_t status = readString8(&retString);
2096 if (status != OK) {
2097 // We don't care about errors here, so just return an empty string.
2098 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002099 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002100 return retString;
2101}
2102
2103status_t Parcel::readString8(String8* pArg) const
2104{
2105 int32_t size;
2106 status_t status = readInt32(&size);
2107 if (status != OK) {
2108 return status;
2109 }
2110 // watch for potential int overflow from size+1
2111 if (size < 0 || size >= INT32_MAX) {
2112 return BAD_VALUE;
2113 }
2114 // |writeString8| writes nothing for empty string.
2115 if (size == 0) {
2116 *pArg = String8();
2117 return OK;
2118 }
2119 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07002120 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002121 return BAD_VALUE;
2122 }
2123 pArg->setTo(str, size);
2124 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002125}
2126
2127String16 Parcel::readString16() const
2128{
2129 size_t len;
2130 const char16_t* str = readString16Inplace(&len);
2131 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002132 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002133 return String16();
2134}
2135
Casey Dahlinb9872622015-11-25 15:09:45 -08002136status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2137{
2138 const int32_t start = dataPosition();
2139 int32_t size;
2140 status_t status = readInt32(&size);
2141 pArg->reset();
2142
2143 if (status != OK || size < 0) {
2144 return status;
2145 }
2146
2147 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002148 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002149
2150 status = readString16(pArg->get());
2151
2152 if (status != OK) {
2153 pArg->reset();
2154 }
2155
2156 return status;
2157}
2158
Casey Dahlin451ff582015-10-19 18:12:18 -07002159status_t Parcel::readString16(String16* pArg) const
2160{
2161 size_t len;
2162 const char16_t* str = readString16Inplace(&len);
2163 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002164 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002165 return 0;
2166 } else {
2167 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002168 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002169 }
2170}
2171
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002172const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2173{
2174 int32_t size = readInt32();
2175 // watch for potential int overflow from size+1
2176 if (size >= 0 && size < INT32_MAX) {
2177 *outLen = size;
2178 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002179 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002180 return str;
2181 }
2182 }
2183 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002184 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002185}
2186
Casey Dahlinf0c13772015-10-27 18:33:56 -07002187status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2188{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002189 status_t status = readNullableStrongBinder(val);
2190 if (status == OK && !val->get()) {
2191 status = UNEXPECTED_NULL;
2192 }
2193 return status;
2194}
2195
2196status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2197{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002198 return unflatten_binder(ProcessState::self(), *this, val);
2199}
2200
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002201sp<IBinder> Parcel::readStrongBinder() const
2202{
2203 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002204 // Note that a lot of code in Android reads binders by hand with this
2205 // method, and that code has historically been ok with getting nullptr
2206 // back (while ignoring error codes).
2207 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002208 return val;
2209}
2210
2211wp<IBinder> Parcel::readWeakBinder() const
2212{
2213 wp<IBinder> val;
2214 unflatten_binder(ProcessState::self(), *this, &val);
2215 return val;
2216}
2217
Christopher Wiley97f048d2015-11-19 06:49:05 -08002218status_t Parcel::readParcelable(Parcelable* parcelable) const {
2219 int32_t have_parcelable = 0;
2220 status_t status = readInt32(&have_parcelable);
2221 if (status != OK) {
2222 return status;
2223 }
2224 if (!have_parcelable) {
2225 return UNEXPECTED_NULL;
2226 }
2227 return parcelable->readFromParcel(this);
2228}
2229
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002230status_t Parcel::readValue(binder::Value* value) const {
2231 return value->readFromParcel(this);
2232}
2233
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002234int32_t Parcel::readExceptionCode() const
2235{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002236 binder::Status status;
2237 status.readFromParcel(*this);
2238 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002239}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002240
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002241native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002242{
2243 int numFds, numInts;
2244 status_t err;
2245 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002246 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002247 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002248 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002249
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002250 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002251 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002252 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002253 }
2254
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002255 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002256 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002257 if (h->data[i] < 0) {
2258 for (int j = 0; j < i; j++) {
2259 close(h->data[j]);
2260 }
2261 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002262 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002263 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002264 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002265 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002266 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002267 native_handle_close(h);
2268 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002269 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002270 }
2271 return h;
2272}
2273
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002274int Parcel::readFileDescriptor() const
2275{
2276 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002277
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002278 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002279 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002280 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002281
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002282 return BAD_TYPE;
2283}
2284
Dianne Hackborn1941a402016-08-29 12:30:43 -07002285int Parcel::readParcelFileDescriptor() const
2286{
2287 int32_t hasComm = readInt32();
2288 int fd = readFileDescriptor();
2289 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002290 // detach (owned by the binder driver)
2291 int comm = readFileDescriptor();
2292
2293 // warning: this must be kept in sync with:
2294 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2295 enum ParcelFileDescriptorStatus {
2296 DETACHED = 2,
2297 };
2298
2299#if BYTE_ORDER == BIG_ENDIAN
2300 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2301#endif
2302#if BYTE_ORDER == LITTLE_ENDIAN
2303 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2304#endif
2305
2306 ssize_t written = TEMP_FAILURE_RETRY(
2307 ::write(comm, &message, sizeof(message)));
2308
2309 if (written == -1 || written != sizeof(message)) {
2310 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2311 written, strerror(errno));
2312 return BAD_TYPE;
2313 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002314 }
2315 return fd;
2316}
2317
Christopher Wiley2cf19952016-04-11 11:09:37 -07002318status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002319{
2320 int got = readFileDescriptor();
2321
2322 if (got == BAD_TYPE) {
2323 return BAD_TYPE;
2324 }
2325
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002326 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002327
2328 if (val->get() < 0) {
2329 return BAD_VALUE;
2330 }
2331
2332 return OK;
2333}
2334
Ryo Hashimotobf551892018-05-31 16:58:35 +09002335status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2336{
2337 int got = readParcelFileDescriptor();
2338
2339 if (got == BAD_TYPE) {
2340 return BAD_TYPE;
2341 }
2342
2343 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2344
2345 if (val->get() < 0) {
2346 return BAD_VALUE;
2347 }
2348
2349 return OK;
2350}
Casey Dahlin06673e32015-11-23 13:24:23 -08002351
Christopher Wiley2cf19952016-04-11 11:09:37 -07002352status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002353 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2354}
2355
Christopher Wiley2cf19952016-04-11 11:09:37 -07002356status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002357 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2358}
2359
Jeff Brown5707dbf2011-09-23 21:17:56 -07002360status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2361{
Jeff Brown13b16042014-11-11 16:44:25 -08002362 int32_t blobType;
2363 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002364 if (status) return status;
2365
Jeff Brown13b16042014-11-11 16:44:25 -08002366 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002367 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002368 const void* ptr = readInplace(len);
2369 if (!ptr) return BAD_VALUE;
2370
Jeff Brown13b16042014-11-11 16:44:25 -08002371 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002372 return NO_ERROR;
2373 }
2374
Steve Block6807e592011-10-20 11:56:00 +01002375 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002376 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002377 int fd = readFileDescriptor();
2378 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2379
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002380 if (!ashmem_valid(fd)) {
2381 ALOGE("invalid fd");
2382 return BAD_VALUE;
2383 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002384 int size = ashmem_get_size_region(fd);
2385 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002386 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002387 return BAD_VALUE;
2388 }
Yi Kong91635562018-06-07 14:38:36 -07002389 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002390 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002391 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002392
Jeff Brown13b16042014-11-11 16:44:25 -08002393 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002394 return NO_ERROR;
2395}
2396
Mathias Agopiane1424282013-07-29 21:24:40 -07002397status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002398{
2399 // size
2400 const size_t len = this->readInt32();
2401 const size_t fd_count = this->readInt32();
2402
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002403 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002404 // don't accept size_t values which may have come from an
2405 // inadvertent conversion from a negative int.
2406 return BAD_VALUE;
2407 }
2408
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002409 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002410 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002411 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002412 return BAD_VALUE;
2413
Yi Kong91635562018-06-07 14:38:36 -07002414 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002415 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002416 fds = new (std::nothrow) int[fd_count];
2417 if (fds == nullptr) {
2418 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2419 return BAD_VALUE;
2420 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002421 }
2422
2423 status_t err = NO_ERROR;
2424 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002425 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002426 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002427 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002428 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 -07002429 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2430 // Close all the file descriptors that were dup-ed.
2431 for (size_t j=0; j<i ;j++) {
2432 close(fds[j]);
2433 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002434 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002435 }
2436
2437 if (err == NO_ERROR) {
2438 err = val.unflatten(buf, len, fds, fd_count);
2439 }
2440
2441 if (fd_count) {
2442 delete [] fds;
2443 }
2444
2445 return err;
2446}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002447const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2448{
2449 const size_t DPOS = mDataPos;
2450 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2451 const flat_binder_object* obj
2452 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2453 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002454 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002455 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002456 // the object list, so we don't want to check for it when
2457 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002458 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002459 return obj;
2460 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002461
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002462 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002463 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002464 const size_t N = mObjectsSize;
2465 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002466
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002467 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002468 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002469 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002470
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002471 // Start at the current hint position, looking for an object at
2472 // the current data position.
2473 if (opos < N) {
2474 while (opos < (N-1) && OBJS[opos] < DPOS) {
2475 opos++;
2476 }
2477 } else {
2478 opos = N-1;
2479 }
2480 if (OBJS[opos] == DPOS) {
2481 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002482 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002483 this, DPOS, opos);
2484 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002485 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002486 return obj;
2487 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002488
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002489 // Look backwards for it...
2490 while (opos > 0 && OBJS[opos] > DPOS) {
2491 opos--;
2492 }
2493 if (OBJS[opos] == DPOS) {
2494 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002495 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002496 this, DPOS, opos);
2497 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002498 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002499 return obj;
2500 }
2501 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002502 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 -07002503 this, DPOS);
2504 }
Yi Kong91635562018-06-07 14:38:36 -07002505 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002506}
2507
2508void Parcel::closeFileDescriptors()
2509{
2510 size_t i = mObjectsSize;
2511 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002512 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513 }
2514 while (i > 0) {
2515 i--;
2516 const flat_binder_object* flat
2517 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002518 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002519 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002520 close(flat->handle);
2521 }
2522 }
2523}
2524
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002525uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002526{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002527 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002528}
2529
2530size_t Parcel::ipcDataSize() const
2531{
2532 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2533}
2534
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002535uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002536{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002537 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002538}
2539
2540size_t Parcel::ipcObjectsCount() const
2541{
2542 return mObjectsSize;
2543}
2544
2545void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002546 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002547{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002548 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002549 freeDataNoInit();
2550 mError = NO_ERROR;
2551 mData = const_cast<uint8_t*>(data);
2552 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002553 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002554 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002555 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002556 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002557 mObjectsSize = mObjectsCapacity = objectsCount;
2558 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002559 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002560 mOwner = relFunc;
2561 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002562 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002563 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002564 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002565 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002566 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002567 mObjectsSize = 0;
2568 break;
2569 }
2570 minOffset = offset + sizeof(flat_binder_object);
2571 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002572 scanForFds();
2573}
2574
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002575void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002576{
2577 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002578
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002579 if (errorCheck() != NO_ERROR) {
2580 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002581 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002582 } else if (dataSize() > 0) {
2583 const uint8_t* DATA = data();
2584 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002585 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002586 const size_t N = objectsCount();
2587 for (size_t i=0; i<N; i++) {
2588 const flat_binder_object* flat
2589 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2590 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002591 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002592 << " = " << flat->binder;
2593 }
2594 } else {
2595 to << "NULL";
2596 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002597
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002598 to << ")";
2599}
2600
2601void Parcel::releaseObjects()
2602{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002603 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002604 if (i == 0) {
2605 return;
2606 }
2607 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002608 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002609 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002610 while (i > 0) {
2611 i--;
2612 const flat_binder_object* flat
2613 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002614 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002615 }
2616}
2617
2618void Parcel::acquireObjects()
2619{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002620 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002621 if (i == 0) {
2622 return;
2623 }
2624 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002625 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002626 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002627 while (i > 0) {
2628 i--;
2629 const flat_binder_object* flat
2630 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002631 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002632 }
2633}
2634
2635void Parcel::freeData()
2636{
2637 freeDataNoInit();
2638 initState();
2639}
2640
2641void Parcel::freeDataNoInit()
2642{
2643 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002644 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002645 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002646 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2647 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002648 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002649 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002650 if (mData) {
2651 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002652 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002653 if (mDataCapacity <= gParcelGlobalAllocSize) {
2654 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2655 } else {
2656 gParcelGlobalAllocSize = 0;
2657 }
2658 if (gParcelGlobalAllocCount > 0) {
2659 gParcelGlobalAllocCount--;
2660 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002661 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002662 free(mData);
2663 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002664 if (mObjects) free(mObjects);
2665 }
2666}
2667
2668status_t Parcel::growData(size_t len)
2669{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002670 if (len > INT32_MAX) {
2671 // don't accept size_t values which may have come from an
2672 // inadvertent conversion from a negative int.
2673 return BAD_VALUE;
2674 }
2675
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002676 size_t newSize = ((mDataSize+len)*3)/2;
2677 return (newSize <= mDataSize)
2678 ? (status_t) NO_MEMORY
2679 : continueWrite(newSize);
2680}
2681
2682status_t Parcel::restartWrite(size_t desired)
2683{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002684 if (desired > INT32_MAX) {
2685 // don't accept size_t values which may have come from an
2686 // inadvertent conversion from a negative int.
2687 return BAD_VALUE;
2688 }
2689
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002690 if (mOwner) {
2691 freeData();
2692 return continueWrite(desired);
2693 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002694
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002695 uint8_t* data = (uint8_t*)realloc(mData, desired);
2696 if (!data && desired > mDataCapacity) {
2697 mError = NO_MEMORY;
2698 return NO_MEMORY;
2699 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002700
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002701 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002702
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002703 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002704 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002705 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002706 gParcelGlobalAllocSize += desired;
2707 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002708 if (!mData) {
2709 gParcelGlobalAllocCount++;
2710 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002711 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002712 mData = data;
2713 mDataCapacity = desired;
2714 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002715
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002716 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002717 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2718 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2719
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002720 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002721 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002722 mObjectsSize = mObjectsCapacity = 0;
2723 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002724 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002725 mHasFds = false;
2726 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002727 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002728
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002729 return NO_ERROR;
2730}
2731
2732status_t Parcel::continueWrite(size_t desired)
2733{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002734 if (desired > INT32_MAX) {
2735 // don't accept size_t values which may have come from an
2736 // inadvertent conversion from a negative int.
2737 return BAD_VALUE;
2738 }
2739
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002740 // If shrinking, first adjust for any objects that appear
2741 // after the new data size.
2742 size_t objectsSize = mObjectsSize;
2743 if (desired < mDataSize) {
2744 if (desired == 0) {
2745 objectsSize = 0;
2746 } else {
2747 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002748 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002749 break;
2750 objectsSize--;
2751 }
2752 }
2753 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002754
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002755 if (mOwner) {
2756 // If the size is going to zero, just release the owner's data.
2757 if (desired == 0) {
2758 freeData();
2759 return NO_ERROR;
2760 }
2761
2762 // If there is a different owner, we need to take
2763 // posession.
2764 uint8_t* data = (uint8_t*)malloc(desired);
2765 if (!data) {
2766 mError = NO_MEMORY;
2767 return NO_MEMORY;
2768 }
Yi Kong91635562018-06-07 14:38:36 -07002769 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002770
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002771 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002772 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002773 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002774 free(data);
2775
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002776 mError = NO_MEMORY;
2777 return NO_MEMORY;
2778 }
2779
2780 // Little hack to only acquire references on objects
2781 // we will be keeping.
2782 size_t oldObjectsSize = mObjectsSize;
2783 mObjectsSize = objectsSize;
2784 acquireObjects();
2785 mObjectsSize = oldObjectsSize;
2786 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002787
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002788 if (mData) {
2789 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2790 }
2791 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002792 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002793 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002794 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002795 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002796 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002797
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002798 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002799 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002800 gParcelGlobalAllocSize += desired;
2801 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002802 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002803
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002804 mData = data;
2805 mObjects = objects;
2806 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002807 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002808 mDataCapacity = desired;
2809 mObjectsSize = mObjectsCapacity = objectsSize;
2810 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002811 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002812
2813 } else if (mData) {
2814 if (objectsSize < mObjectsSize) {
2815 // Need to release refs on any objects we are dropping.
2816 const sp<ProcessState> proc(ProcessState::self());
2817 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2818 const flat_binder_object* flat
2819 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002820 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002821 // will need to rescan because we may have lopped off the only FDs
2822 mFdsKnown = false;
2823 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002824 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002825 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002826 binder_size_t* objects =
2827 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002828 if (objects) {
2829 mObjects = objects;
2830 }
2831 mObjectsSize = objectsSize;
2832 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002833 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002834 }
2835
2836 // We own the data, so we can just do a realloc().
2837 if (desired > mDataCapacity) {
2838 uint8_t* data = (uint8_t*)realloc(mData, desired);
2839 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002840 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2841 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002842 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002843 gParcelGlobalAllocSize += desired;
2844 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002845 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002846 mData = data;
2847 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002848 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002849 mError = NO_MEMORY;
2850 return NO_MEMORY;
2851 }
2852 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002853 if (mDataSize > desired) {
2854 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002855 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002856 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002857 if (mDataPos > desired) {
2858 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002859 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002860 }
2861 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002862
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002863 } else {
2864 // This is the first data. Easy!
2865 uint8_t* data = (uint8_t*)malloc(desired);
2866 if (!data) {
2867 mError = NO_MEMORY;
2868 return NO_MEMORY;
2869 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002870
Yi Kong91635562018-06-07 14:38:36 -07002871 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002872 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002873 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002874 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002875
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002876 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002877 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002878 gParcelGlobalAllocSize += desired;
2879 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002880 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002881
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002882 mData = data;
2883 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002884 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2885 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002886 mDataCapacity = desired;
2887 }
2888
2889 return NO_ERROR;
2890}
2891
2892void Parcel::initState()
2893{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002894 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002895 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002896 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002897 mDataSize = 0;
2898 mDataCapacity = 0;
2899 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002900 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2901 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002902 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002903 mObjectsSize = 0;
2904 mObjectsCapacity = 0;
2905 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002906 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002907 mHasFds = false;
2908 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002909 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002910 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002911 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002912 mWorkSourceRequestHeaderPosition = 0;
2913 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002914
2915 // racing multiple init leads only to multiple identical write
2916 if (gMaxFds == 0) {
2917 struct rlimit result;
2918 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2919 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002920 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002921 } else {
2922 ALOGW("Unable to getrlimit: %s", strerror(errno));
2923 gMaxFds = 1024;
2924 }
2925 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002926}
2927
2928void Parcel::scanForFds() const
2929{
2930 bool hasFds = false;
2931 for (size_t i=0; i<mObjectsSize; i++) {
2932 const flat_binder_object* flat
2933 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002934 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002935 hasFds = true;
2936 break;
2937 }
2938 }
2939 mHasFds = hasFds;
2940 mFdsKnown = true;
2941}
2942
Dan Sandleraa5c2342015-04-10 10:08:45 -04002943size_t Parcel::getBlobAshmemSize() const
2944{
Adrian Roos6bb31142015-10-22 16:46:12 -07002945 // This used to return the size of all blobs that were written to ashmem, now we're returning
2946 // the ashmem currently referenced by this Parcel, which should be equivalent.
2947 // TODO: Remove method once ABI can be changed.
2948 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002949}
2950
Adrian Rooscbf37262015-10-22 16:12:53 -07002951size_t Parcel::getOpenAshmemSize() const
2952{
2953 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002954}
2955
2956// --- Parcel::Blob ---
2957
2958Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002959 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002960}
2961
2962Parcel::Blob::~Blob() {
2963 release();
2964}
2965
2966void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002967 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002968 ::munmap(mData, mSize);
2969 }
2970 clear();
2971}
2972
Jeff Brown13b16042014-11-11 16:44:25 -08002973void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2974 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002975 mData = data;
2976 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002977 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002978}
2979
2980void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002981 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002982 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002983 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002984 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002985}
2986
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002987}; // namespace android