blob: d5cdf07444e8a573cd988f549ffcb4831e2f9dc9 [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
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070096void 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 -0700138void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 const flat_binder_object& obj, const void* who)
140{
Yi Kong91635562018-06-07 14:38:36 -0700141 acquire_object(proc, obj, who, nullptr);
Adrian Roos6bb31142015-10-22 16:46:12 -0700142}
143
144static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700145 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700146{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700147 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700148 case BINDER_TYPE_BINDER:
149 if (obj.binder) {
150 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800151 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700152 }
153 return;
154 case BINDER_TYPE_WEAK_BINDER:
155 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800156 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700157 return;
158 case BINDER_TYPE_HANDLE: {
159 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700160 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700161 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
162 b->decStrong(who);
163 }
164 return;
165 }
166 case BINDER_TYPE_WEAK_HANDLE: {
167 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700168 if (b != nullptr) b.get_refs()->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169 return;
170 }
171 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800172 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000173 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700174 int size = ashmem_get_size_region(obj.handle);
175 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800176 // ashmem size might have changed since last time it was accounted for, e.g.
177 // in acquire_object(). Value of *outAshmemSize is not critical since we are
178 // releasing the object anyway. Check for integer overflow condition.
179 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700180 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700181 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800182
183 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700184 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700185 return;
186 }
187 }
188
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700189 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700190}
191
Adrian Roos6bb31142015-10-22 16:46:12 -0700192void release_object(const sp<ProcessState>& proc,
193 const flat_binder_object& obj, const void* who)
194{
Yi Kong91635562018-06-07 14:38:36 -0700195 release_object(proc, obj, who, nullptr);
Adrian Roos6bb31142015-10-22 16:46:12 -0700196}
197
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700198inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800199 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700200{
201 return out->writeObject(flat, false);
202}
203
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800204status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205 const sp<IBinder>& binder, Parcel* out)
206{
207 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700208
Martijn Coenen2b631742017-05-05 11:16:59 -0700209 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
210 /* minimum priority for all nodes is nice 0 */
211 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
212 } else {
213 /* minimum priority for all nodes is MAX_NICE(19) */
214 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
215 }
216
Yi Kong91635562018-06-07 14:38:36 -0700217 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800218 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700219 if (!local) {
220 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700221 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000222 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 }
224 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700225 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800226 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800228 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229 } else {
Steven Morelandf0212002018-12-26 13:59:23 -0800230 if (local->isRequestingSid()) {
231 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
232 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700233 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800234 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
235 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700236 }
237 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700238 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800239 obj.binder = 0;
240 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700241 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700242
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243 return finish_flatten_binder(binder, obj, out);
244}
245
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800246status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700247 const wp<IBinder>& binder, Parcel* out)
248{
249 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700250
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700251 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Yi Kong91635562018-06-07 14:38:36 -0700252 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700253 sp<IBinder> real = binder.promote();
Yi Kong91635562018-06-07 14:38:36 -0700254 if (real != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255 IBinder *local = real->localBinder();
256 if (!local) {
257 BpBinder *proxy = real->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700258 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000259 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700260 }
261 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700262 obj.hdr.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800263 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700264 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800265 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700266 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700267 obj.hdr.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800268 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
269 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700270 }
271 return finish_flatten_binder(real, obj, out);
272 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700273
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700274 // XXX How to deal? In order to flatten the given binder,
275 // we need to probe it for information, which requires a primary
276 // reference... but we don't have one.
277 //
278 // The OpenBinder implementation uses a dynamic_cast<> here,
279 // but we can't do that with the different reference counting
280 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000281 ALOGE("Unable to unflatten Binder weak reference!");
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700282 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800283 obj.binder = 0;
284 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700285 return finish_flatten_binder(nullptr, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700286
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700287 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700288 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800289 obj.binder = 0;
290 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700291 return finish_flatten_binder(nullptr, obj, out);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700292 }
293}
294
295inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800296 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
297 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700298{
299 return NO_ERROR;
300}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700301
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700302status_t unflatten_binder(const sp<ProcessState>& proc,
303 const Parcel& in, sp<IBinder>* out)
304{
305 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700306
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700307 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700308 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700309 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800310 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700311 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700312 case BINDER_TYPE_HANDLE:
313 *out = proc->getStrongProxyForHandle(flat->handle);
314 return finish_unflatten_binder(
315 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700316 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700317 }
318 return BAD_TYPE;
319}
320
321status_t unflatten_binder(const sp<ProcessState>& proc,
322 const Parcel& in, wp<IBinder>* out)
323{
324 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700325
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700326 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700327 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700328 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800329 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700330 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700331 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800332 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700333 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800334 reinterpret_cast<IBinder*>(flat->cookie),
335 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700336 } else {
Yi Kong91635562018-06-07 14:38:36 -0700337 *out = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700338 }
Yi Kong91635562018-06-07 14:38:36 -0700339 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700340 case BINDER_TYPE_HANDLE:
341 case BINDER_TYPE_WEAK_HANDLE:
342 *out = proc->getWeakProxyForHandle(flat->handle);
343 return finish_unflatten_binder(
344 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
345 }
346 }
347 return BAD_TYPE;
348}
349
350// ---------------------------------------------------------------------------
351
352Parcel::Parcel()
353{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800354 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700355 initState();
356}
357
358Parcel::~Parcel()
359{
360 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800361 LOG_ALLOC("Parcel %p: destroyed", this);
362}
363
364size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800365 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
366 size_t size = gParcelGlobalAllocSize;
367 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
368 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800369}
370
371size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800372 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
373 size_t count = gParcelGlobalAllocCount;
374 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
375 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700376}
377
378const uint8_t* Parcel::data() const
379{
380 return mData;
381}
382
383size_t Parcel::dataSize() const
384{
385 return (mDataSize > mDataPos ? mDataSize : mDataPos);
386}
387
388size_t Parcel::dataAvail() const
389{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700390 size_t result = dataSize() - dataPosition();
391 if (result > INT32_MAX) {
392 abort();
393 }
394 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700395}
396
397size_t Parcel::dataPosition() const
398{
399 return mDataPos;
400}
401
402size_t Parcel::dataCapacity() const
403{
404 return mDataCapacity;
405}
406
407status_t Parcel::setDataSize(size_t size)
408{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700409 if (size > INT32_MAX) {
410 // don't accept size_t values which may have come from an
411 // inadvertent conversion from a negative int.
412 return BAD_VALUE;
413 }
414
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700415 status_t err;
416 err = continueWrite(size);
417 if (err == NO_ERROR) {
418 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700419 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700420 }
421 return err;
422}
423
424void Parcel::setDataPosition(size_t pos) const
425{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700426 if (pos > INT32_MAX) {
427 // don't accept size_t values which may have come from an
428 // inadvertent conversion from a negative int.
429 abort();
430 }
431
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700432 mDataPos = pos;
433 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800434 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700435}
436
437status_t Parcel::setDataCapacity(size_t size)
438{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700439 if (size > 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
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700445 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700446 return NO_ERROR;
447}
448
449status_t Parcel::setData(const uint8_t* buffer, size_t len)
450{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700451 if (len > INT32_MAX) {
452 // don't accept size_t values which may have come from an
453 // inadvertent conversion from a negative int.
454 return BAD_VALUE;
455 }
456
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700457 status_t err = restartWrite(len);
458 if (err == NO_ERROR) {
459 memcpy(const_cast<uint8_t*>(data()), buffer, len);
460 mDataSize = len;
461 mFdsKnown = false;
462 }
463 return err;
464}
465
Andreas Huber51faf462011-04-13 10:21:56 -0700466status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700467{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700468 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700469 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800470 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700471 size_t size = parcel->mObjectsSize;
472 int startPos = mDataPos;
473 int firstIndex = -1, lastIndex = -2;
474
475 if (len == 0) {
476 return NO_ERROR;
477 }
478
Nick Kralevichb6b14232015-04-02 09:36:02 -0700479 if (len > INT32_MAX) {
480 // don't accept size_t values which may have come from an
481 // inadvertent conversion from a negative int.
482 return BAD_VALUE;
483 }
484
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700485 // range checks against the source parcel size
486 if ((offset > parcel->mDataSize)
487 || (len > parcel->mDataSize)
488 || (offset + len > parcel->mDataSize)) {
489 return BAD_VALUE;
490 }
491
492 // Count objects in range
493 for (int i = 0; i < (int) size; i++) {
494 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700495 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700496 if (firstIndex == -1) {
497 firstIndex = i;
498 }
499 lastIndex = i;
500 }
501 }
502 int numObjects = lastIndex - firstIndex + 1;
503
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700504 if ((mDataSize+len) > mDataCapacity) {
505 // grow data
506 err = growData(len);
507 if (err != NO_ERROR) {
508 return err;
509 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700510 }
511
512 // append data
513 memcpy(mData + mDataPos, data + offset, len);
514 mDataPos += len;
515 mDataSize += len;
516
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400517 err = NO_ERROR;
518
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700519 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200520 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521 // grow objects
522 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700523 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700524 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800525 binder_size_t *objects =
526 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700527 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700528 return NO_MEMORY;
529 }
530 mObjects = objects;
531 mObjectsCapacity = newSize;
532 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700533
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700534 // append and acquire objects
535 int idx = mObjectsSize;
536 for (int i = firstIndex; i <= lastIndex; i++) {
537 size_t off = objects[i] - offset + startPos;
538 mObjects[idx++] = off;
539 mObjectsSize++;
540
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700541 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700542 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700543 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700544
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700545 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700546 // If this is a file descriptor, we need to dup it so the
547 // new Parcel now owns its own fd, and can declare that we
548 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800549 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800550 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700551 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400552 if (!mAllowFds) {
553 err = FDS_NOT_ALLOWED;
554 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700555 }
556 }
557 }
558
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400559 return err;
560}
561
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700562int Parcel::compareData(const Parcel& other) {
563 size_t size = dataSize();
564 if (size != other.dataSize()) {
565 return size < other.dataSize() ? -1 : 1;
566 }
567 return memcmp(data(), other.data(), size);
568}
569
Jeff Brown13b16042014-11-11 16:44:25 -0800570bool Parcel::allowFds() const
571{
572 return mAllowFds;
573}
574
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700575bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400576{
577 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700578 if (!allowFds) {
579 mAllowFds = false;
580 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400581 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700582}
583
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700584void Parcel::restoreAllowFds(bool lastValue)
585{
586 mAllowFds = lastValue;
587}
588
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700589bool Parcel::hasFileDescriptors() const
590{
591 if (!mFdsKnown) {
592 scanForFds();
593 }
594 return mHasFds;
595}
596
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000597void Parcel::updateWorkSourceRequestHeaderPosition() const {
598 // Only update the request headers once. We only want to point
599 // to the first headers read/written.
600 if (!mRequestHeaderPresent) {
601 mWorkSourceRequestHeaderPosition = dataPosition();
602 mRequestHeaderPresent = true;
603 }
604}
605
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700606// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700607status_t Parcel::writeInterfaceToken(const String16& interface)
608{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000609 const IPCThreadState* threadState = IPCThreadState::self();
610 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000611 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000612 writeInt32(threadState->shouldPropagateWorkSource() ?
613 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700614 // currently the interface identification token is just its name as a string
615 return writeString16(interface);
616}
617
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000618bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
619{
620 if (!mRequestHeaderPresent) {
621 return false;
622 }
623
624 const size_t initialPosition = dataPosition();
625 setDataPosition(mWorkSourceRequestHeaderPosition);
626 status_t err = writeInt32(uid);
627 setDataPosition(initialPosition);
628 return err == NO_ERROR;
629}
630
631uid_t Parcel::readCallingWorkSourceUid()
632{
633 if (!mRequestHeaderPresent) {
634 return IPCThreadState::kUnsetWorkSource;
635 }
636
637 const size_t initialPosition = dataPosition();
638 setDataPosition(mWorkSourceRequestHeaderPosition);
639 uid_t uid = readInt32();
640 setDataPosition(initialPosition);
641 return uid;
642}
643
Mathias Agopian83c04462009-05-22 19:00:22 -0700644bool Parcel::checkInterface(IBinder* binder) const
645{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700646 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700647}
648
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700649bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700650 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700651{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100652 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700653 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700654 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700655 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700656 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700657 if ((threadState->getLastTransactionBinderFlags() &
658 IBinder::FLAG_ONEWAY) != 0) {
659 // For one-way calls, the callee is running entirely
660 // disconnected from the caller, so disable StrictMode entirely.
661 // Not only does disk/network usage not impact the caller, but
662 // there's no way to commuicate back any violations anyway.
663 threadState->setStrictModePolicy(0);
664 } else {
665 threadState->setStrictModePolicy(strictPolicy);
666 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100667 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000668 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100669 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000670 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100671 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700672 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700673 if (str == interface) {
674 return true;
675 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700676 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700677 String8(interface).string(), String8(str).string());
678 return false;
679 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700680}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700681
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800682const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700683{
684 return mObjects;
685}
686
687size_t Parcel::objectsCount() const
688{
689 return mObjectsSize;
690}
691
692status_t Parcel::errorCheck() const
693{
694 return mError;
695}
696
697void Parcel::setError(status_t err)
698{
699 mError = err;
700}
701
702status_t Parcel::finishWrite(size_t len)
703{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700704 if (len > INT32_MAX) {
705 // don't accept size_t values which may have come from an
706 // inadvertent conversion from a negative int.
707 return BAD_VALUE;
708 }
709
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700710 //printf("Finish write of %d\n", len);
711 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700712 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700713 if (mDataPos > mDataSize) {
714 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700715 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700716 }
717 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
718 return NO_ERROR;
719}
720
721status_t Parcel::writeUnpadded(const void* data, size_t len)
722{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700723 if (len > INT32_MAX) {
724 // don't accept size_t values which may have come from an
725 // inadvertent conversion from a negative int.
726 return BAD_VALUE;
727 }
728
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700729 size_t end = mDataPos + len;
730 if (end < mDataPos) {
731 // integer overflow
732 return BAD_VALUE;
733 }
734
735 if (end <= mDataCapacity) {
736restart_write:
737 memcpy(mData+mDataPos, data, len);
738 return finishWrite(len);
739 }
740
741 status_t err = growData(len);
742 if (err == NO_ERROR) goto restart_write;
743 return err;
744}
745
746status_t Parcel::write(const void* data, size_t len)
747{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700748 if (len > INT32_MAX) {
749 // don't accept size_t values which may have come from an
750 // inadvertent conversion from a negative int.
751 return BAD_VALUE;
752 }
753
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700754 void* const d = writeInplace(len);
755 if (d) {
756 memcpy(d, data, len);
757 return NO_ERROR;
758 }
759 return mError;
760}
761
762void* Parcel::writeInplace(size_t len)
763{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700764 if (len > INT32_MAX) {
765 // don't accept size_t values which may have come from an
766 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700767 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700768 }
769
770 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700771
772 // sanity check for integer overflow
773 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700774 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700775 }
776
777 if ((mDataPos+padded) <= mDataCapacity) {
778restart_write:
779 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
780 uint8_t* const data = mData+mDataPos;
781
782 // Need to pad at end?
783 if (padded != len) {
784#if BYTE_ORDER == BIG_ENDIAN
785 static const uint32_t mask[4] = {
786 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
787 };
788#endif
789#if BYTE_ORDER == LITTLE_ENDIAN
790 static const uint32_t mask[4] = {
791 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
792 };
793#endif
794 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
795 // *reinterpret_cast<void**>(data+padded-4));
796 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
797 }
798
799 finishWrite(padded);
800 return data;
801 }
802
803 status_t err = growData(padded);
804 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700805 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700806}
807
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800808status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
809 const uint8_t* strData = (uint8_t*)str.data();
810 const size_t strLen= str.length();
811 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100812 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800813 return BAD_VALUE;
814 }
815
816 status_t err = writeInt32(utf16Len);
817 if (err) {
818 return err;
819 }
820
821 // Allocate enough bytes to hold our converted string and its terminating NULL.
822 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
823 if (!dst) {
824 return NO_MEMORY;
825 }
826
Sergio Girof4607432016-07-21 14:46:35 +0100827 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800828
829 return NO_ERROR;
830}
831
832status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
833 if (!str) {
834 return writeInt32(-1);
835 }
836 return writeUtf8AsUtf16(*str);
837}
838
Casey Dahlin185d3442016-02-09 11:08:35 -0800839namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800840
Casey Dahlin185d3442016-02-09 11:08:35 -0800841template<typename T>
842status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700843{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700844 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700845 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700846 status = BAD_VALUE;
847 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700848 }
849
Casey Dahlin185d3442016-02-09 11:08:35 -0800850 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700851 if (status != OK) {
852 return status;
853 }
854
Casey Dahlin185d3442016-02-09 11:08:35 -0800855 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700856 if (!data) {
857 status = BAD_VALUE;
858 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700859 }
860
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700861 memcpy(data, val.data(), val.size());
862 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700863}
864
Casey Dahlin185d3442016-02-09 11:08:35 -0800865template<typename T>
866status_t writeByteVectorInternalPtr(Parcel* parcel,
867 const std::unique_ptr<std::vector<T>>& val)
868{
869 if (!val) {
870 return parcel->writeInt32(-1);
871 }
872
873 return writeByteVectorInternal(parcel, *val);
874}
875
876} // namespace
877
878status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
879 return writeByteVectorInternal(this, val);
880}
881
882status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
883{
884 return writeByteVectorInternalPtr(this, val);
885}
886
887status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
888 return writeByteVectorInternal(this, val);
889}
890
891status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
892{
893 return writeByteVectorInternalPtr(this, val);
894}
895
Casey Dahlin451ff582015-10-19 18:12:18 -0700896status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
897{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800898 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700899}
900
Casey Dahlinb9872622015-11-25 15:09:45 -0800901status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
902{
903 return writeNullableTypedVector(val, &Parcel::writeInt32);
904}
905
Casey Dahlin451ff582015-10-19 18:12:18 -0700906status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
907{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800908 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700909}
910
Casey Dahlinb9872622015-11-25 15:09:45 -0800911status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
912{
913 return writeNullableTypedVector(val, &Parcel::writeInt64);
914}
915
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800916status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
917{
918 return writeTypedVector(val, &Parcel::writeUint64);
919}
920
921status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
922{
923 return writeNullableTypedVector(val, &Parcel::writeUint64);
924}
925
Casey Dahlin451ff582015-10-19 18:12:18 -0700926status_t Parcel::writeFloatVector(const std::vector<float>& val)
927{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800928 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700929}
930
Casey Dahlinb9872622015-11-25 15:09:45 -0800931status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
932{
933 return writeNullableTypedVector(val, &Parcel::writeFloat);
934}
935
Casey Dahlin451ff582015-10-19 18:12:18 -0700936status_t Parcel::writeDoubleVector(const std::vector<double>& val)
937{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800938 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700939}
940
Casey Dahlinb9872622015-11-25 15:09:45 -0800941status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
942{
943 return writeNullableTypedVector(val, &Parcel::writeDouble);
944}
945
Casey Dahlin451ff582015-10-19 18:12:18 -0700946status_t Parcel::writeBoolVector(const std::vector<bool>& val)
947{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800948 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700949}
950
Casey Dahlinb9872622015-11-25 15:09:45 -0800951status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
952{
953 return writeNullableTypedVector(val, &Parcel::writeBool);
954}
955
Casey Dahlin451ff582015-10-19 18:12:18 -0700956status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
957{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800958 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700959}
960
Casey Dahlinb9872622015-11-25 15:09:45 -0800961status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
962{
963 return writeNullableTypedVector(val, &Parcel::writeChar);
964}
965
Casey Dahlin451ff582015-10-19 18:12:18 -0700966status_t Parcel::writeString16Vector(const std::vector<String16>& val)
967{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800968 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700969}
970
Casey Dahlinb9872622015-11-25 15:09:45 -0800971status_t Parcel::writeString16Vector(
972 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
973{
974 return writeNullableTypedVector(val, &Parcel::writeString16);
975}
976
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800977status_t Parcel::writeUtf8VectorAsUtf16Vector(
978 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
979 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
980}
981
982status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
983 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
984}
985
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700986status_t Parcel::writeInt32(int32_t val)
987{
Andreas Huber84a6d042009-08-17 13:33:27 -0700988 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700989}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800990
991status_t Parcel::writeUint32(uint32_t val)
992{
993 return writeAligned(val);
994}
995
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700996status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700997 if (len > INT32_MAX) {
998 // don't accept size_t values which may have come from an
999 // inadvertent conversion from a negative int.
1000 return BAD_VALUE;
1001 }
1002
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001003 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001004 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001005 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001006 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001007 if (ret == NO_ERROR) {
1008 ret = write(val, len * sizeof(*val));
1009 }
1010 return ret;
1011}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001012status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001013 if (len > INT32_MAX) {
1014 // don't accept size_t values which may have come from an
1015 // inadvertent conversion from a negative int.
1016 return BAD_VALUE;
1017 }
1018
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001019 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001020 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001021 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001022 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001023 if (ret == NO_ERROR) {
1024 ret = write(val, len * sizeof(*val));
1025 }
1026 return ret;
1027}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001028
Casey Dahlind6848f52015-10-15 15:44:59 -07001029status_t Parcel::writeBool(bool val)
1030{
1031 return writeInt32(int32_t(val));
1032}
1033
1034status_t Parcel::writeChar(char16_t val)
1035{
1036 return writeInt32(int32_t(val));
1037}
1038
1039status_t Parcel::writeByte(int8_t val)
1040{
1041 return writeInt32(int32_t(val));
1042}
1043
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001044status_t Parcel::writeInt64(int64_t val)
1045{
Andreas Huber84a6d042009-08-17 13:33:27 -07001046 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001047}
1048
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001049status_t Parcel::writeUint64(uint64_t val)
1050{
1051 return writeAligned(val);
1052}
1053
Serban Constantinescuf683e012013-11-05 16:53:55 +00001054status_t Parcel::writePointer(uintptr_t val)
1055{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001056 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001057}
1058
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001059status_t Parcel::writeFloat(float val)
1060{
Andreas Huber84a6d042009-08-17 13:33:27 -07001061 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001062}
1063
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001064#if defined(__mips__) && defined(__mips_hard_float)
1065
1066status_t Parcel::writeDouble(double val)
1067{
1068 union {
1069 double d;
1070 unsigned long long ll;
1071 } u;
1072 u.d = val;
1073 return writeAligned(u.ll);
1074}
1075
1076#else
1077
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001078status_t Parcel::writeDouble(double val)
1079{
Andreas Huber84a6d042009-08-17 13:33:27 -07001080 return writeAligned(val);
1081}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001082
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001083#endif
1084
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001085status_t Parcel::writeCString(const char* str)
1086{
1087 return write(str, strlen(str)+1);
1088}
1089
1090status_t Parcel::writeString8(const String8& str)
1091{
1092 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001093 // only write string if its length is more than zero characters,
1094 // as readString8 will only read if the length field is non-zero.
1095 // this is slightly different from how writeString16 works.
1096 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001097 err = write(str.string(), str.bytes()+1);
1098 }
1099 return err;
1100}
1101
Casey Dahlinb9872622015-11-25 15:09:45 -08001102status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1103{
1104 if (!str) {
1105 return writeInt32(-1);
1106 }
1107
1108 return writeString16(*str);
1109}
1110
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001111status_t Parcel::writeString16(const String16& str)
1112{
1113 return writeString16(str.string(), str.size());
1114}
1115
1116status_t Parcel::writeString16(const char16_t* str, size_t len)
1117{
Yi Kong91635562018-06-07 14:38:36 -07001118 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001119
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001120 status_t err = writeInt32(len);
1121 if (err == NO_ERROR) {
1122 len *= sizeof(char16_t);
1123 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1124 if (data) {
1125 memcpy(data, str, len);
1126 *reinterpret_cast<char16_t*>(data+len) = 0;
1127 return NO_ERROR;
1128 }
1129 err = mError;
1130 }
1131 return err;
1132}
1133
1134status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1135{
1136 return flatten_binder(ProcessState::self(), val, this);
1137}
1138
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001139status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1140{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001141 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001142}
1143
Casey Dahlinb9872622015-11-25 15:09:45 -08001144status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1145{
1146 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1147}
1148
1149status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001150 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001151}
1152
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001153status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001154 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001155}
1156
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001157status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1158{
1159 return flatten_binder(ProcessState::self(), val, this);
1160}
1161
Casey Dahlinb9872622015-11-25 15:09:45 -08001162status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1163 if (!parcelable) {
1164 return writeInt32(0);
1165 }
1166
1167 return writeParcelable(*parcelable);
1168}
1169
Christopher Wiley97f048d2015-11-19 06:49:05 -08001170status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1171 status_t status = writeInt32(1); // parcelable is not null.
1172 if (status != OK) {
1173 return status;
1174 }
1175 return parcelable.writeToParcel(this);
1176}
1177
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001178status_t Parcel::writeValue(const binder::Value& value) {
1179 return value.writeToParcel(this);
1180}
1181
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001182status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001183{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001184 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001185 return BAD_TYPE;
1186
1187 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001188 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001189 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001191 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001192 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001194 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1195 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196
1197 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001198 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199 return err;
1200 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001201 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001202 return err;
1203}
1204
Jeff Brown93ff1f92011-11-04 19:01:44 -07001205status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001206{
1207 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001208 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001209 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001210 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001211 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001212 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001213 return writeObject(obj, true);
1214}
1215
1216status_t Parcel::writeDupFileDescriptor(int fd)
1217{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001218 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001219 if (dupFd < 0) {
1220 return -errno;
1221 }
1222 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001223 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001224 close(dupFd);
1225 }
1226 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001227}
1228
Dianne Hackborn1941a402016-08-29 12:30:43 -07001229status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1230{
1231 writeInt32(0);
1232 return writeFileDescriptor(fd, takeOwnership);
1233}
1234
Ryo Hashimotobf551892018-05-31 16:58:35 +09001235status_t Parcel::writeDupParcelFileDescriptor(int fd)
1236{
1237 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1238 if (dupFd < 0) {
1239 return -errno;
1240 }
1241 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1242 if (err != OK) {
1243 close(dupFd);
1244 }
1245 return err;
1246}
1247
Christopher Wiley2cf19952016-04-11 11:09:37 -07001248status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001249 return writeDupFileDescriptor(fd.get());
1250}
1251
Christopher Wiley2cf19952016-04-11 11:09:37 -07001252status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001253 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1254}
1255
Christopher Wiley2cf19952016-04-11 11:09:37 -07001256status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001257 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1258}
1259
Jeff Brown13b16042014-11-11 16:44:25 -08001260status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001261{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001262 if (len > INT32_MAX) {
1263 // don't accept size_t values which may have come from an
1264 // inadvertent conversion from a negative int.
1265 return BAD_VALUE;
1266 }
1267
Jeff Brown13b16042014-11-11 16:44:25 -08001268 status_t status;
1269 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001270 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001271 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001272 if (status) return status;
1273
1274 void* ptr = writeInplace(len);
1275 if (!ptr) return NO_MEMORY;
1276
Jeff Brown13b16042014-11-11 16:44:25 -08001277 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001278 return NO_ERROR;
1279 }
1280
Steve Block6807e592011-10-20 11:56:00 +01001281 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001282 int fd = ashmem_create_region("Parcel Blob", len);
1283 if (fd < 0) return NO_MEMORY;
1284
1285 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1286 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001287 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001288 } else {
Yi Kong91635562018-06-07 14:38:36 -07001289 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001290 if (ptr == MAP_FAILED) {
1291 status = -errno;
1292 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001293 if (!mutableCopy) {
1294 result = ashmem_set_prot_region(fd, PROT_READ);
1295 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001296 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001297 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001298 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001299 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001300 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001301 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001302 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001303 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001304 return NO_ERROR;
1305 }
1306 }
1307 }
1308 }
1309 ::munmap(ptr, len);
1310 }
1311 ::close(fd);
1312 return status;
1313}
1314
Jeff Brown13b16042014-11-11 16:44:25 -08001315status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1316{
1317 // Must match up with what's done in writeBlob.
1318 if (!mAllowFds) return FDS_NOT_ALLOWED;
1319 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1320 if (status) return status;
1321 return writeDupFileDescriptor(fd);
1322}
1323
Mathias Agopiane1424282013-07-29 21:24:40 -07001324status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001325{
1326 status_t err;
1327
1328 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001329 const size_t len = val.getFlattenedSize();
1330 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001331
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001332 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001333 // don't accept size_t values which may have come from an
1334 // inadvertent conversion from a negative int.
1335 return BAD_VALUE;
1336 }
1337
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001338 err = this->writeInt32(len);
1339 if (err) return err;
1340
1341 err = this->writeInt32(fd_count);
1342 if (err) return err;
1343
1344 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001345 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001346 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001347 return BAD_VALUE;
1348
Yi Kong91635562018-06-07 14:38:36 -07001349 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001350 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001351 fds = new (std::nothrow) int[fd_count];
1352 if (fds == nullptr) {
1353 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1354 return BAD_VALUE;
1355 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001356 }
1357
1358 err = val.flatten(buf, len, fds, fd_count);
1359 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1360 err = this->writeDupFileDescriptor( fds[i] );
1361 }
1362
1363 if (fd_count) {
1364 delete [] fds;
1365 }
1366
1367 return err;
1368}
1369
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001370status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1371{
1372 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1373 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1374 if (enoughData && enoughObjects) {
1375restart_write:
1376 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001377
Christopher Tate98e67d32015-06-03 18:44:15 -07001378 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001379 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001380 if (!mAllowFds) {
1381 // fail before modifying our object index
1382 return FDS_NOT_ALLOWED;
1383 }
1384 mHasFds = mFdsKnown = true;
1385 }
1386
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001387 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001388 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001389 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001390 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001391 mObjectsSize++;
1392 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001393
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001394 return finishWrite(sizeof(flat_binder_object));
1395 }
1396
1397 if (!enoughData) {
1398 const status_t err = growData(sizeof(val));
1399 if (err != NO_ERROR) return err;
1400 }
1401 if (!enoughObjects) {
1402 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001403 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001404 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001405 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001406 mObjects = objects;
1407 mObjectsCapacity = newSize;
1408 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001409
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001410 goto restart_write;
1411}
1412
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001413status_t Parcel::writeNoException()
1414{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001415 binder::Status status;
1416 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001417}
1418
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001419status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1420{
1421 using ::std::map;
1422 using ::android::binder::Value;
1423 using ::android::binder::Map;
1424
1425 Map::const_iterator iter;
1426 status_t ret;
1427
1428 ret = writeInt32(map_in.size());
1429
1430 if (ret != NO_ERROR) {
1431 return ret;
1432 }
1433
1434 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1435 ret = writeValue(Value(iter->first));
1436 if (ret != NO_ERROR) {
1437 return ret;
1438 }
1439
1440 ret = writeValue(iter->second);
1441 if (ret != NO_ERROR) {
1442 return ret;
1443 }
1444 }
1445
1446 return ret;
1447}
1448
1449status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1450{
Yi Kong91635562018-06-07 14:38:36 -07001451 if (map == nullptr) {
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001452 return writeInt32(-1);
1453 }
1454
1455 return writeMap(*map.get());
1456}
1457
1458status_t Parcel::readMap(::android::binder::Map* map_out)const
1459{
1460 using ::std::map;
1461 using ::android::String16;
1462 using ::android::String8;
1463 using ::android::binder::Value;
1464 using ::android::binder::Map;
1465
1466 status_t ret = NO_ERROR;
1467 int32_t count;
1468
1469 ret = readInt32(&count);
1470 if (ret != NO_ERROR) {
1471 return ret;
1472 }
1473
1474 if (count < 0) {
1475 ALOGE("readMap: Unexpected count: %d", count);
1476 return (count == -1)
1477 ? UNEXPECTED_NULL
1478 : BAD_VALUE;
1479 }
1480
1481 map_out->clear();
1482
1483 while (count--) {
1484 Map::key_type key;
1485 Value value;
1486
1487 ret = readValue(&value);
1488 if (ret != NO_ERROR) {
1489 return ret;
1490 }
1491
1492 if (!value.getString(&key)) {
1493 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1494 return BAD_VALUE;
1495 }
1496
1497 ret = readValue(&value);
1498 if (ret != NO_ERROR) {
1499 return ret;
1500 }
1501
1502 (*map_out)[key] = value;
1503 }
1504
1505 return ret;
1506}
1507
1508status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1509{
1510 const size_t start = dataPosition();
1511 int32_t count;
1512 status_t status = readInt32(&count);
1513 map->reset();
1514
1515 if (status != OK || count == -1) {
1516 return status;
1517 }
1518
1519 setDataPosition(start);
1520 map->reset(new binder::Map());
1521
1522 status = readMap(map->get());
1523
1524 if (status != OK) {
1525 map->reset();
1526 }
1527
1528 return status;
1529}
1530
1531
1532
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001533void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001534{
1535 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1536}
1537
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001538status_t Parcel::validateReadData(size_t upperBound) const
1539{
1540 // Don't allow non-object reads on object data
1541 if (mObjectsSorted || mObjectsSize <= 1) {
1542data_sorted:
1543 // Expect to check only against the next object
1544 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1545 // For some reason the current read position is greater than the next object
1546 // hint. Iterate until we find the right object
1547 size_t nextObject = mNextObjectHint;
1548 do {
1549 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1550 // Requested info overlaps with an object
1551 ALOGE("Attempt to read from protected data in Parcel %p", this);
1552 return PERMISSION_DENIED;
1553 }
1554 nextObject++;
1555 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1556 mNextObjectHint = nextObject;
1557 }
1558 return NO_ERROR;
1559 }
1560 // Quickly determine if mObjects is sorted.
1561 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1562 binder_size_t* prevObj = currObj;
1563 while (currObj > mObjects) {
1564 prevObj--;
1565 if(*prevObj > *currObj) {
1566 goto data_unsorted;
1567 }
1568 currObj--;
1569 }
1570 mObjectsSorted = true;
1571 goto data_sorted;
1572
1573data_unsorted:
1574 // Insertion Sort mObjects
1575 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1576 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1577 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1578 binder_size_t temp = *iter0;
1579 binder_size_t* iter1 = iter0 - 1;
1580 while (iter1 >= mObjects && *iter1 > temp) {
1581 *(iter1 + 1) = *iter1;
1582 iter1--;
1583 }
1584 *(iter1 + 1) = temp;
1585 }
1586 mNextObjectHint = 0;
1587 mObjectsSorted = true;
1588 goto data_sorted;
1589}
1590
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001591status_t Parcel::read(void* outData, size_t len) const
1592{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001593 if (len > INT32_MAX) {
1594 // don't accept size_t values which may have come from an
1595 // inadvertent conversion from a negative int.
1596 return BAD_VALUE;
1597 }
1598
1599 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1600 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001601 if (mObjectsSize > 0) {
1602 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001603 if(err != NO_ERROR) {
1604 // Still increment the data position by the expected length
1605 mDataPos += pad_size(len);
1606 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1607 return err;
1608 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001609 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001610 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001611 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001612 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001613 return NO_ERROR;
1614 }
1615 return NOT_ENOUGH_DATA;
1616}
1617
1618const void* Parcel::readInplace(size_t len) const
1619{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001620 if (len > INT32_MAX) {
1621 // don't accept size_t values which may have come from an
1622 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001623 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001624 }
1625
1626 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1627 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001628 if (mObjectsSize > 0) {
1629 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001630 if(err != NO_ERROR) {
1631 // Still increment the data position by the expected length
1632 mDataPos += pad_size(len);
1633 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001634 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001635 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001636 }
1637
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001638 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001639 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001640 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001641 return data;
1642 }
Yi Kong91635562018-06-07 14:38:36 -07001643 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001644}
1645
Andreas Huber84a6d042009-08-17 13:33:27 -07001646template<class T>
1647status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001648 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001649
1650 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001651 if (mObjectsSize > 0) {
1652 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001653 if(err != NO_ERROR) {
1654 // Still increment the data position by the expected length
1655 mDataPos += sizeof(T);
1656 return err;
1657 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001658 }
1659
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001660 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001661 mDataPos += sizeof(T);
1662 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001663 return NO_ERROR;
1664 } else {
1665 return NOT_ENOUGH_DATA;
1666 }
1667}
1668
Andreas Huber84a6d042009-08-17 13:33:27 -07001669template<class T>
1670T Parcel::readAligned() const {
1671 T result;
1672 if (readAligned(&result) != NO_ERROR) {
1673 result = 0;
1674 }
1675
1676 return result;
1677}
1678
1679template<class T>
1680status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001681 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001682
1683 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1684restart_write:
1685 *reinterpret_cast<T*>(mData+mDataPos) = val;
1686 return finishWrite(sizeof(val));
1687 }
1688
1689 status_t err = growData(sizeof(val));
1690 if (err == NO_ERROR) goto restart_write;
1691 return err;
1692}
1693
Casey Dahlin185d3442016-02-09 11:08:35 -08001694namespace {
1695
1696template<typename T>
1697status_t readByteVectorInternal(const Parcel* parcel,
1698 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001699 val->clear();
1700
1701 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001702 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001703
1704 if (status != OK) {
1705 return status;
1706 }
1707
Christopher Wiley4db672d2015-11-10 09:44:30 -08001708 if (size < 0) {
1709 status = UNEXPECTED_NULL;
1710 return status;
1711 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001712 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001713 status = BAD_VALUE;
1714 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001715 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001716
Paul Lietar433e87b2016-09-16 10:39:32 -07001717 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001718 if (!data) {
1719 status = BAD_VALUE;
1720 return status;
1721 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001722 val->reserve(size);
1723 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001724
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001725 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001726}
1727
Casey Dahlin185d3442016-02-09 11:08:35 -08001728template<typename T>
1729status_t readByteVectorInternalPtr(
1730 const Parcel* parcel,
1731 std::unique_ptr<std::vector<T>>* val) {
1732 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001733 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001734 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001735 val->reset();
1736
1737 if (status != OK || size < 0) {
1738 return status;
1739 }
1740
Casey Dahlin185d3442016-02-09 11:08:35 -08001741 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001742 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001743
Casey Dahlin185d3442016-02-09 11:08:35 -08001744 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001745
1746 if (status != OK) {
1747 val->reset();
1748 }
1749
1750 return status;
1751}
1752
Casey Dahlin185d3442016-02-09 11:08:35 -08001753} // namespace
1754
1755status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1756 return readByteVectorInternal(this, val);
1757}
1758
1759status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1760 return readByteVectorInternal(this, val);
1761}
1762
1763status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1764 return readByteVectorInternalPtr(this, val);
1765}
1766
1767status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1768 return readByteVectorInternalPtr(this, val);
1769}
1770
Casey Dahlinb9872622015-11-25 15:09:45 -08001771status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1772 return readNullableTypedVector(val, &Parcel::readInt32);
1773}
1774
Casey Dahlin451ff582015-10-19 18:12:18 -07001775status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001776 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001777}
1778
Casey Dahlinb9872622015-11-25 15:09:45 -08001779status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1780 return readNullableTypedVector(val, &Parcel::readInt64);
1781}
1782
Casey Dahlin451ff582015-10-19 18:12:18 -07001783status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001784 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001785}
1786
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001787status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1788 return readNullableTypedVector(val, &Parcel::readUint64);
1789}
1790
1791status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1792 return readTypedVector(val, &Parcel::readUint64);
1793}
1794
Casey Dahlinb9872622015-11-25 15:09:45 -08001795status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1796 return readNullableTypedVector(val, &Parcel::readFloat);
1797}
1798
Casey Dahlin451ff582015-10-19 18:12:18 -07001799status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001800 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001801}
1802
Casey Dahlinb9872622015-11-25 15:09:45 -08001803status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1804 return readNullableTypedVector(val, &Parcel::readDouble);
1805}
1806
Casey Dahlin451ff582015-10-19 18:12:18 -07001807status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001808 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001809}
1810
Casey Dahlinb9872622015-11-25 15:09:45 -08001811status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1812 const int32_t start = dataPosition();
1813 int32_t size;
1814 status_t status = readInt32(&size);
1815 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001816
Casey Dahlinb9872622015-11-25 15:09:45 -08001817 if (status != OK || size < 0) {
1818 return status;
1819 }
1820
1821 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001822 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001823
1824 status = readBoolVector(val->get());
1825
1826 if (status != OK) {
1827 val->reset();
1828 }
1829
1830 return status;
1831}
1832
1833status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001834 int32_t size;
1835 status_t status = readInt32(&size);
1836
1837 if (status != OK) {
1838 return status;
1839 }
1840
1841 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001842 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001843 }
1844
1845 val->resize(size);
1846
1847 /* C++ bool handling means a vector of bools isn't necessarily addressable
1848 * (we might use individual bits)
1849 */
Christopher Wiley97887982015-10-27 16:33:47 -07001850 bool data;
1851 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001852 status = readBool(&data);
1853 (*val)[i] = data;
1854
1855 if (status != OK) {
1856 return status;
1857 }
1858 }
1859
1860 return OK;
1861}
1862
Casey Dahlinb9872622015-11-25 15:09:45 -08001863status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1864 return readNullableTypedVector(val, &Parcel::readChar);
1865}
1866
Casey Dahlin451ff582015-10-19 18:12:18 -07001867status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001868 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001869}
1870
Casey Dahlinb9872622015-11-25 15:09:45 -08001871status_t Parcel::readString16Vector(
1872 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1873 return readNullableTypedVector(val, &Parcel::readString16);
1874}
1875
Casey Dahlin451ff582015-10-19 18:12:18 -07001876status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001877 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001878}
1879
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001880status_t Parcel::readUtf8VectorFromUtf16Vector(
1881 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1882 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1883}
1884
1885status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1886 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1887}
Casey Dahlin451ff582015-10-19 18:12:18 -07001888
Andreas Huber84a6d042009-08-17 13:33:27 -07001889status_t Parcel::readInt32(int32_t *pArg) const
1890{
1891 return readAligned(pArg);
1892}
1893
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001894int32_t Parcel::readInt32() const
1895{
Andreas Huber84a6d042009-08-17 13:33:27 -07001896 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001897}
1898
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001899status_t Parcel::readUint32(uint32_t *pArg) const
1900{
1901 return readAligned(pArg);
1902}
1903
1904uint32_t Parcel::readUint32() const
1905{
1906 return readAligned<uint32_t>();
1907}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001908
1909status_t Parcel::readInt64(int64_t *pArg) const
1910{
Andreas Huber84a6d042009-08-17 13:33:27 -07001911 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001912}
1913
1914
1915int64_t Parcel::readInt64() const
1916{
Andreas Huber84a6d042009-08-17 13:33:27 -07001917 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001918}
1919
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001920status_t Parcel::readUint64(uint64_t *pArg) const
1921{
1922 return readAligned(pArg);
1923}
1924
1925uint64_t Parcel::readUint64() const
1926{
1927 return readAligned<uint64_t>();
1928}
1929
Serban Constantinescuf683e012013-11-05 16:53:55 +00001930status_t Parcel::readPointer(uintptr_t *pArg) const
1931{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001932 status_t ret;
1933 binder_uintptr_t ptr;
1934 ret = readAligned(&ptr);
1935 if (!ret)
1936 *pArg = ptr;
1937 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001938}
1939
1940uintptr_t Parcel::readPointer() const
1941{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001942 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001943}
1944
1945
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001946status_t Parcel::readFloat(float *pArg) const
1947{
Andreas Huber84a6d042009-08-17 13:33:27 -07001948 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001949}
1950
1951
1952float Parcel::readFloat() const
1953{
Andreas Huber84a6d042009-08-17 13:33:27 -07001954 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001955}
1956
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001957#if defined(__mips__) && defined(__mips_hard_float)
1958
1959status_t Parcel::readDouble(double *pArg) const
1960{
1961 union {
1962 double d;
1963 unsigned long long ll;
1964 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001965 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001966 status_t status;
1967 status = readAligned(&u.ll);
1968 *pArg = u.d;
1969 return status;
1970}
1971
1972double Parcel::readDouble() const
1973{
1974 union {
1975 double d;
1976 unsigned long long ll;
1977 } u;
1978 u.ll = readAligned<unsigned long long>();
1979 return u.d;
1980}
1981
1982#else
1983
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001984status_t Parcel::readDouble(double *pArg) const
1985{
Andreas Huber84a6d042009-08-17 13:33:27 -07001986 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001987}
1988
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001989double Parcel::readDouble() const
1990{
Andreas Huber84a6d042009-08-17 13:33:27 -07001991 return readAligned<double>();
1992}
1993
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001994#endif
1995
Andreas Huber84a6d042009-08-17 13:33:27 -07001996status_t Parcel::readIntPtr(intptr_t *pArg) const
1997{
1998 return readAligned(pArg);
1999}
2000
2001
2002intptr_t Parcel::readIntPtr() const
2003{
2004 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002005}
2006
Casey Dahlind6848f52015-10-15 15:44:59 -07002007status_t Parcel::readBool(bool *pArg) const
2008{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002009 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002010 status_t ret = readInt32(&tmp);
2011 *pArg = (tmp != 0);
2012 return ret;
2013}
2014
2015bool Parcel::readBool() const
2016{
2017 return readInt32() != 0;
2018}
2019
2020status_t Parcel::readChar(char16_t *pArg) const
2021{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002022 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002023 status_t ret = readInt32(&tmp);
2024 *pArg = char16_t(tmp);
2025 return ret;
2026}
2027
2028char16_t Parcel::readChar() const
2029{
2030 return char16_t(readInt32());
2031}
2032
2033status_t Parcel::readByte(int8_t *pArg) const
2034{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002035 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002036 status_t ret = readInt32(&tmp);
2037 *pArg = int8_t(tmp);
2038 return ret;
2039}
2040
2041int8_t Parcel::readByte() const
2042{
2043 return int8_t(readInt32());
2044}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002045
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002046status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2047 size_t utf16Size = 0;
2048 const char16_t* src = readString16Inplace(&utf16Size);
2049 if (!src) {
2050 return UNEXPECTED_NULL;
2051 }
2052
2053 // Save ourselves the trouble, we're done.
2054 if (utf16Size == 0u) {
2055 str->clear();
2056 return NO_ERROR;
2057 }
2058
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002059 // Allow for closing '\0'
2060 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2061 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002062 return BAD_VALUE;
2063 }
2064 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002065 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002066 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002067 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2068 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002069 return NO_ERROR;
2070}
2071
2072status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2073 const int32_t start = dataPosition();
2074 int32_t size;
2075 status_t status = readInt32(&size);
2076 str->reset();
2077
2078 if (status != OK || size < 0) {
2079 return status;
2080 }
2081
2082 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002083 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002084 return readUtf8FromUtf16(str->get());
2085}
2086
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002087const char* Parcel::readCString() const
2088{
2089 const size_t avail = mDataSize-mDataPos;
2090 if (avail > 0) {
2091 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2092 // is the string's trailing NUL within the parcel's valid bounds?
2093 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2094 if (eos) {
2095 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002096 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002097 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002098 return str;
2099 }
2100 }
Yi Kong91635562018-06-07 14:38:36 -07002101 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002102}
2103
2104String8 Parcel::readString8() const
2105{
Roshan Pius87b64d22016-07-18 12:51:02 -07002106 String8 retString;
2107 status_t status = readString8(&retString);
2108 if (status != OK) {
2109 // We don't care about errors here, so just return an empty string.
2110 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002111 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002112 return retString;
2113}
2114
2115status_t Parcel::readString8(String8* pArg) const
2116{
2117 int32_t size;
2118 status_t status = readInt32(&size);
2119 if (status != OK) {
2120 return status;
2121 }
2122 // watch for potential int overflow from size+1
2123 if (size < 0 || size >= INT32_MAX) {
2124 return BAD_VALUE;
2125 }
2126 // |writeString8| writes nothing for empty string.
2127 if (size == 0) {
2128 *pArg = String8();
2129 return OK;
2130 }
2131 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07002132 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002133 return BAD_VALUE;
2134 }
2135 pArg->setTo(str, size);
2136 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002137}
2138
2139String16 Parcel::readString16() const
2140{
2141 size_t len;
2142 const char16_t* str = readString16Inplace(&len);
2143 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002144 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002145 return String16();
2146}
2147
Casey Dahlinb9872622015-11-25 15:09:45 -08002148status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2149{
2150 const int32_t start = dataPosition();
2151 int32_t size;
2152 status_t status = readInt32(&size);
2153 pArg->reset();
2154
2155 if (status != OK || size < 0) {
2156 return status;
2157 }
2158
2159 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002160 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002161
2162 status = readString16(pArg->get());
2163
2164 if (status != OK) {
2165 pArg->reset();
2166 }
2167
2168 return status;
2169}
2170
Casey Dahlin451ff582015-10-19 18:12:18 -07002171status_t Parcel::readString16(String16* pArg) const
2172{
2173 size_t len;
2174 const char16_t* str = readString16Inplace(&len);
2175 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002176 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002177 return 0;
2178 } else {
2179 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002180 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002181 }
2182}
2183
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002184const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2185{
2186 int32_t size = readInt32();
2187 // watch for potential int overflow from size+1
2188 if (size >= 0 && size < INT32_MAX) {
2189 *outLen = size;
2190 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002191 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002192 return str;
2193 }
2194 }
2195 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002196 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002197}
2198
Casey Dahlinf0c13772015-10-27 18:33:56 -07002199status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2200{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002201 status_t status = readNullableStrongBinder(val);
2202 if (status == OK && !val->get()) {
2203 status = UNEXPECTED_NULL;
2204 }
2205 return status;
2206}
2207
2208status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2209{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002210 return unflatten_binder(ProcessState::self(), *this, val);
2211}
2212
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002213sp<IBinder> Parcel::readStrongBinder() const
2214{
2215 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002216 // Note that a lot of code in Android reads binders by hand with this
2217 // method, and that code has historically been ok with getting nullptr
2218 // back (while ignoring error codes).
2219 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002220 return val;
2221}
2222
2223wp<IBinder> Parcel::readWeakBinder() const
2224{
2225 wp<IBinder> val;
2226 unflatten_binder(ProcessState::self(), *this, &val);
2227 return val;
2228}
2229
Christopher Wiley97f048d2015-11-19 06:49:05 -08002230status_t Parcel::readParcelable(Parcelable* parcelable) const {
2231 int32_t have_parcelable = 0;
2232 status_t status = readInt32(&have_parcelable);
2233 if (status != OK) {
2234 return status;
2235 }
2236 if (!have_parcelable) {
2237 return UNEXPECTED_NULL;
2238 }
2239 return parcelable->readFromParcel(this);
2240}
2241
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002242status_t Parcel::readValue(binder::Value* value) const {
2243 return value->readFromParcel(this);
2244}
2245
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002246int32_t Parcel::readExceptionCode() const
2247{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002248 binder::Status status;
2249 status.readFromParcel(*this);
2250 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002251}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002252
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002253native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002254{
2255 int numFds, numInts;
2256 status_t err;
2257 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002258 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002259 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002260 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002261
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002262 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002263 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002264 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002265 }
2266
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002267 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002268 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002269 if (h->data[i] < 0) {
2270 for (int j = 0; j < i; j++) {
2271 close(h->data[j]);
2272 }
2273 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002274 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002275 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002276 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002277 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002278 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002279 native_handle_close(h);
2280 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002281 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002282 }
2283 return h;
2284}
2285
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002286int Parcel::readFileDescriptor() const
2287{
2288 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002289
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002290 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002291 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002292 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002293
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002294 return BAD_TYPE;
2295}
2296
Dianne Hackborn1941a402016-08-29 12:30:43 -07002297int Parcel::readParcelFileDescriptor() const
2298{
2299 int32_t hasComm = readInt32();
2300 int fd = readFileDescriptor();
2301 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002302 // detach (owned by the binder driver)
2303 int comm = readFileDescriptor();
2304
2305 // warning: this must be kept in sync with:
2306 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2307 enum ParcelFileDescriptorStatus {
2308 DETACHED = 2,
2309 };
2310
2311#if BYTE_ORDER == BIG_ENDIAN
2312 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2313#endif
2314#if BYTE_ORDER == LITTLE_ENDIAN
2315 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2316#endif
2317
2318 ssize_t written = TEMP_FAILURE_RETRY(
2319 ::write(comm, &message, sizeof(message)));
2320
2321 if (written == -1 || written != sizeof(message)) {
2322 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2323 written, strerror(errno));
2324 return BAD_TYPE;
2325 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002326 }
2327 return fd;
2328}
2329
Christopher Wiley2cf19952016-04-11 11:09:37 -07002330status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002331{
2332 int got = readFileDescriptor();
2333
2334 if (got == BAD_TYPE) {
2335 return BAD_TYPE;
2336 }
2337
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002338 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002339
2340 if (val->get() < 0) {
2341 return BAD_VALUE;
2342 }
2343
2344 return OK;
2345}
2346
Ryo Hashimotobf551892018-05-31 16:58:35 +09002347status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2348{
2349 int got = readParcelFileDescriptor();
2350
2351 if (got == BAD_TYPE) {
2352 return BAD_TYPE;
2353 }
2354
2355 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2356
2357 if (val->get() < 0) {
2358 return BAD_VALUE;
2359 }
2360
2361 return OK;
2362}
Casey Dahlin06673e32015-11-23 13:24:23 -08002363
Christopher Wiley2cf19952016-04-11 11:09:37 -07002364status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002365 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2366}
2367
Christopher Wiley2cf19952016-04-11 11:09:37 -07002368status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002369 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2370}
2371
Jeff Brown5707dbf2011-09-23 21:17:56 -07002372status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2373{
Jeff Brown13b16042014-11-11 16:44:25 -08002374 int32_t blobType;
2375 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002376 if (status) return status;
2377
Jeff Brown13b16042014-11-11 16:44:25 -08002378 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002379 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002380 const void* ptr = readInplace(len);
2381 if (!ptr) return BAD_VALUE;
2382
Jeff Brown13b16042014-11-11 16:44:25 -08002383 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002384 return NO_ERROR;
2385 }
2386
Steve Block6807e592011-10-20 11:56:00 +01002387 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002388 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002389 int fd = readFileDescriptor();
2390 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2391
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002392 if (!ashmem_valid(fd)) {
2393 ALOGE("invalid fd");
2394 return BAD_VALUE;
2395 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002396 int size = ashmem_get_size_region(fd);
2397 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002398 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002399 return BAD_VALUE;
2400 }
Yi Kong91635562018-06-07 14:38:36 -07002401 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002402 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002403 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002404
Jeff Brown13b16042014-11-11 16:44:25 -08002405 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002406 return NO_ERROR;
2407}
2408
Mathias Agopiane1424282013-07-29 21:24:40 -07002409status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002410{
2411 // size
2412 const size_t len = this->readInt32();
2413 const size_t fd_count = this->readInt32();
2414
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002415 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002416 // don't accept size_t values which may have come from an
2417 // inadvertent conversion from a negative int.
2418 return BAD_VALUE;
2419 }
2420
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002421 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002422 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002423 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002424 return BAD_VALUE;
2425
Yi Kong91635562018-06-07 14:38:36 -07002426 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002427 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002428 fds = new (std::nothrow) int[fd_count];
2429 if (fds == nullptr) {
2430 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2431 return BAD_VALUE;
2432 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002433 }
2434
2435 status_t err = NO_ERROR;
2436 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002437 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002438 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002439 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002440 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 -07002441 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2442 // Close all the file descriptors that were dup-ed.
2443 for (size_t j=0; j<i ;j++) {
2444 close(fds[j]);
2445 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002446 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002447 }
2448
2449 if (err == NO_ERROR) {
2450 err = val.unflatten(buf, len, fds, fd_count);
2451 }
2452
2453 if (fd_count) {
2454 delete [] fds;
2455 }
2456
2457 return err;
2458}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002459const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2460{
2461 const size_t DPOS = mDataPos;
2462 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2463 const flat_binder_object* obj
2464 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2465 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002466 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002467 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002468 // the object list, so we don't want to check for it when
2469 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002470 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002471 return obj;
2472 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002473
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002474 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002475 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002476 const size_t N = mObjectsSize;
2477 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002478
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002480 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002481 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002482
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002483 // Start at the current hint position, looking for an object at
2484 // the current data position.
2485 if (opos < N) {
2486 while (opos < (N-1) && OBJS[opos] < DPOS) {
2487 opos++;
2488 }
2489 } else {
2490 opos = N-1;
2491 }
2492 if (OBJS[opos] == DPOS) {
2493 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002494 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002495 this, DPOS, opos);
2496 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002497 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002498 return obj;
2499 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002500
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002501 // Look backwards for it...
2502 while (opos > 0 && OBJS[opos] > DPOS) {
2503 opos--;
2504 }
2505 if (OBJS[opos] == DPOS) {
2506 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002507 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508 this, DPOS, opos);
2509 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002510 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002511 return obj;
2512 }
2513 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002514 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 -07002515 this, DPOS);
2516 }
Yi Kong91635562018-06-07 14:38:36 -07002517 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002518}
2519
2520void Parcel::closeFileDescriptors()
2521{
2522 size_t i = mObjectsSize;
2523 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002524 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002525 }
2526 while (i > 0) {
2527 i--;
2528 const flat_binder_object* flat
2529 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002530 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002531 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002532 close(flat->handle);
2533 }
2534 }
2535}
2536
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002537uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002538{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002539 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002540}
2541
2542size_t Parcel::ipcDataSize() const
2543{
2544 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2545}
2546
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002547uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002548{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002549 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002550}
2551
2552size_t Parcel::ipcObjectsCount() const
2553{
2554 return mObjectsSize;
2555}
2556
2557void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002558 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002559{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002560 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002561 freeDataNoInit();
2562 mError = NO_ERROR;
2563 mData = const_cast<uint8_t*>(data);
2564 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002565 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002566 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002567 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002568 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002569 mObjectsSize = mObjectsCapacity = objectsCount;
2570 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002571 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002572 mOwner = relFunc;
2573 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002574 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002575 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002576 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002577 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002578 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002579 mObjectsSize = 0;
2580 break;
2581 }
2582 minOffset = offset + sizeof(flat_binder_object);
2583 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002584 scanForFds();
2585}
2586
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002587void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002588{
2589 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002590
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002591 if (errorCheck() != NO_ERROR) {
2592 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002593 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002594 } else if (dataSize() > 0) {
2595 const uint8_t* DATA = data();
2596 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002597 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002598 const size_t N = objectsCount();
2599 for (size_t i=0; i<N; i++) {
2600 const flat_binder_object* flat
2601 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2602 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002603 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002604 << " = " << flat->binder;
2605 }
2606 } else {
2607 to << "NULL";
2608 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002609
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002610 to << ")";
2611}
2612
2613void Parcel::releaseObjects()
2614{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002615 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002616 if (i == 0) {
2617 return;
2618 }
2619 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002620 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002621 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002622 while (i > 0) {
2623 i--;
2624 const flat_binder_object* flat
2625 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002626 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002627 }
2628}
2629
2630void Parcel::acquireObjects()
2631{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002632 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002633 if (i == 0) {
2634 return;
2635 }
2636 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002637 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002638 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002639 while (i > 0) {
2640 i--;
2641 const flat_binder_object* flat
2642 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002643 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002644 }
2645}
2646
2647void Parcel::freeData()
2648{
2649 freeDataNoInit();
2650 initState();
2651}
2652
2653void Parcel::freeDataNoInit()
2654{
2655 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002656 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002657 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002658 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2659 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002660 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002661 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002662 if (mData) {
2663 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002664 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002665 if (mDataCapacity <= gParcelGlobalAllocSize) {
2666 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2667 } else {
2668 gParcelGlobalAllocSize = 0;
2669 }
2670 if (gParcelGlobalAllocCount > 0) {
2671 gParcelGlobalAllocCount--;
2672 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002673 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002674 free(mData);
2675 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002676 if (mObjects) free(mObjects);
2677 }
2678}
2679
2680status_t Parcel::growData(size_t len)
2681{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002682 if (len > INT32_MAX) {
2683 // don't accept size_t values which may have come from an
2684 // inadvertent conversion from a negative int.
2685 return BAD_VALUE;
2686 }
2687
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002688 size_t newSize = ((mDataSize+len)*3)/2;
2689 return (newSize <= mDataSize)
2690 ? (status_t) NO_MEMORY
2691 : continueWrite(newSize);
2692}
2693
2694status_t Parcel::restartWrite(size_t desired)
2695{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002696 if (desired > INT32_MAX) {
2697 // don't accept size_t values which may have come from an
2698 // inadvertent conversion from a negative int.
2699 return BAD_VALUE;
2700 }
2701
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002702 if (mOwner) {
2703 freeData();
2704 return continueWrite(desired);
2705 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002706
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002707 uint8_t* data = (uint8_t*)realloc(mData, desired);
2708 if (!data && desired > mDataCapacity) {
2709 mError = NO_MEMORY;
2710 return NO_MEMORY;
2711 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002712
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002713 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002714
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002715 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002716 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002717 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002718 gParcelGlobalAllocSize += desired;
2719 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002720 if (!mData) {
2721 gParcelGlobalAllocCount++;
2722 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002723 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002724 mData = data;
2725 mDataCapacity = desired;
2726 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002727
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002728 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002729 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2730 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2731
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002732 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002733 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002734 mObjectsSize = mObjectsCapacity = 0;
2735 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002736 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002737 mHasFds = false;
2738 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002739 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002740
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002741 return NO_ERROR;
2742}
2743
2744status_t Parcel::continueWrite(size_t desired)
2745{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002746 if (desired > INT32_MAX) {
2747 // don't accept size_t values which may have come from an
2748 // inadvertent conversion from a negative int.
2749 return BAD_VALUE;
2750 }
2751
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002752 // If shrinking, first adjust for any objects that appear
2753 // after the new data size.
2754 size_t objectsSize = mObjectsSize;
2755 if (desired < mDataSize) {
2756 if (desired == 0) {
2757 objectsSize = 0;
2758 } else {
2759 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002760 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002761 break;
2762 objectsSize--;
2763 }
2764 }
2765 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002766
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002767 if (mOwner) {
2768 // If the size is going to zero, just release the owner's data.
2769 if (desired == 0) {
2770 freeData();
2771 return NO_ERROR;
2772 }
2773
2774 // If there is a different owner, we need to take
2775 // posession.
2776 uint8_t* data = (uint8_t*)malloc(desired);
2777 if (!data) {
2778 mError = NO_MEMORY;
2779 return NO_MEMORY;
2780 }
Yi Kong91635562018-06-07 14:38:36 -07002781 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002782
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002783 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002784 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002785 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002786 free(data);
2787
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002788 mError = NO_MEMORY;
2789 return NO_MEMORY;
2790 }
2791
2792 // Little hack to only acquire references on objects
2793 // we will be keeping.
2794 size_t oldObjectsSize = mObjectsSize;
2795 mObjectsSize = objectsSize;
2796 acquireObjects();
2797 mObjectsSize = oldObjectsSize;
2798 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002799
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002800 if (mData) {
2801 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2802 }
2803 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002804 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002805 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002806 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002807 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002808 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002809
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002810 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002811 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002812 gParcelGlobalAllocSize += desired;
2813 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002814 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002815
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002816 mData = data;
2817 mObjects = objects;
2818 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002819 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002820 mDataCapacity = desired;
2821 mObjectsSize = mObjectsCapacity = objectsSize;
2822 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002823 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002824
2825 } else if (mData) {
2826 if (objectsSize < mObjectsSize) {
2827 // Need to release refs on any objects we are dropping.
2828 const sp<ProcessState> proc(ProcessState::self());
2829 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2830 const flat_binder_object* flat
2831 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002832 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002833 // will need to rescan because we may have lopped off the only FDs
2834 mFdsKnown = false;
2835 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002836 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002837 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002838
2839 if (objectsSize == 0) {
2840 free(mObjects);
2841 mObjects = nullptr;
2842 } else {
2843 binder_size_t* objects =
2844 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2845 if (objects) {
2846 mObjects = objects;
2847 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002848 }
2849 mObjectsSize = objectsSize;
2850 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002851 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002852 }
2853
2854 // We own the data, so we can just do a realloc().
2855 if (desired > mDataCapacity) {
2856 uint8_t* data = (uint8_t*)realloc(mData, desired);
2857 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002858 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2859 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002860 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002861 gParcelGlobalAllocSize += desired;
2862 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002863 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002864 mData = data;
2865 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002866 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002867 mError = NO_MEMORY;
2868 return NO_MEMORY;
2869 }
2870 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002871 if (mDataSize > desired) {
2872 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002873 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002874 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002875 if (mDataPos > desired) {
2876 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002877 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002878 }
2879 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002880
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002881 } else {
2882 // This is the first data. Easy!
2883 uint8_t* data = (uint8_t*)malloc(desired);
2884 if (!data) {
2885 mError = NO_MEMORY;
2886 return NO_MEMORY;
2887 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002888
Yi Kong91635562018-06-07 14:38:36 -07002889 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002890 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002891 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002892 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002893
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002894 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002895 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002896 gParcelGlobalAllocSize += desired;
2897 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002898 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002899
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002900 mData = data;
2901 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002902 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2903 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002904 mDataCapacity = desired;
2905 }
2906
2907 return NO_ERROR;
2908}
2909
2910void Parcel::initState()
2911{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002912 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002913 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002914 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002915 mDataSize = 0;
2916 mDataCapacity = 0;
2917 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002918 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2919 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002920 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002921 mObjectsSize = 0;
2922 mObjectsCapacity = 0;
2923 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002924 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002925 mHasFds = false;
2926 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002927 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002928 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002929 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002930 mWorkSourceRequestHeaderPosition = 0;
2931 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002932
2933 // racing multiple init leads only to multiple identical write
2934 if (gMaxFds == 0) {
2935 struct rlimit result;
2936 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2937 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002938 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002939 } else {
2940 ALOGW("Unable to getrlimit: %s", strerror(errno));
2941 gMaxFds = 1024;
2942 }
2943 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002944}
2945
2946void Parcel::scanForFds() const
2947{
2948 bool hasFds = false;
2949 for (size_t i=0; i<mObjectsSize; i++) {
2950 const flat_binder_object* flat
2951 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002952 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002953 hasFds = true;
2954 break;
2955 }
2956 }
2957 mHasFds = hasFds;
2958 mFdsKnown = true;
2959}
2960
Dan Sandleraa5c2342015-04-10 10:08:45 -04002961size_t Parcel::getBlobAshmemSize() const
2962{
Adrian Roos6bb31142015-10-22 16:46:12 -07002963 // This used to return the size of all blobs that were written to ashmem, now we're returning
2964 // the ashmem currently referenced by this Parcel, which should be equivalent.
2965 // TODO: Remove method once ABI can be changed.
2966 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002967}
2968
Adrian Rooscbf37262015-10-22 16:12:53 -07002969size_t Parcel::getOpenAshmemSize() const
2970{
2971 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002972}
2973
2974// --- Parcel::Blob ---
2975
2976Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002977 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002978}
2979
2980Parcel::Blob::~Blob() {
2981 release();
2982}
2983
2984void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002985 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002986 ::munmap(mData, mSize);
2987 }
2988 clear();
2989}
2990
Jeff Brown13b16042014-11-11 16:44:25 -08002991void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2992 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002993 mData = data;
2994 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002995 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002996}
2997
2998void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002999 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07003000 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003001 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08003002 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003003}
3004
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003005}; // namespace android