blob: 11441eca64b2f66a292da90b32999f44cbd018df [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) {
Martijn Coenen93fe5182020-01-22 10:46:25 +0100523 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
524 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700525 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +0100526 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800527 binder_size_t *objects =
528 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700529 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700530 return NO_MEMORY;
531 }
532 mObjects = objects;
533 mObjectsCapacity = newSize;
534 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700535
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700536 // append and acquire objects
537 int idx = mObjectsSize;
538 for (int i = firstIndex; i <= lastIndex; i++) {
539 size_t off = objects[i] - offset + startPos;
540 mObjects[idx++] = off;
541 mObjectsSize++;
542
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700543 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700544 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700545 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700546
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700547 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700548 // If this is a file descriptor, we need to dup it so the
549 // new Parcel now owns its own fd, and can declare that we
550 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800551 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800552 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700553 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400554 if (!mAllowFds) {
555 err = FDS_NOT_ALLOWED;
556 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700557 }
558 }
559 }
560
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400561 return err;
562}
563
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700564int Parcel::compareData(const Parcel& other) {
565 size_t size = dataSize();
566 if (size != other.dataSize()) {
567 return size < other.dataSize() ? -1 : 1;
568 }
569 return memcmp(data(), other.data(), size);
570}
571
Jeff Brown13b16042014-11-11 16:44:25 -0800572bool Parcel::allowFds() const
573{
574 return mAllowFds;
575}
576
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700577bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400578{
579 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700580 if (!allowFds) {
581 mAllowFds = false;
582 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400583 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700584}
585
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700586void Parcel::restoreAllowFds(bool lastValue)
587{
588 mAllowFds = lastValue;
589}
590
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700591bool Parcel::hasFileDescriptors() const
592{
593 if (!mFdsKnown) {
594 scanForFds();
595 }
596 return mHasFds;
597}
598
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000599void Parcel::updateWorkSourceRequestHeaderPosition() const {
600 // Only update the request headers once. We only want to point
601 // to the first headers read/written.
602 if (!mRequestHeaderPresent) {
603 mWorkSourceRequestHeaderPosition = dataPosition();
604 mRequestHeaderPresent = true;
605 }
606}
607
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700608// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700609status_t Parcel::writeInterfaceToken(const String16& interface)
610{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000611 const IPCThreadState* threadState = IPCThreadState::self();
612 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000613 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000614 writeInt32(threadState->shouldPropagateWorkSource() ?
615 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700616 // currently the interface identification token is just its name as a string
617 return writeString16(interface);
618}
619
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000620bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
621{
622 if (!mRequestHeaderPresent) {
623 return false;
624 }
625
626 const size_t initialPosition = dataPosition();
627 setDataPosition(mWorkSourceRequestHeaderPosition);
628 status_t err = writeInt32(uid);
629 setDataPosition(initialPosition);
630 return err == NO_ERROR;
631}
632
633uid_t Parcel::readCallingWorkSourceUid()
634{
635 if (!mRequestHeaderPresent) {
636 return IPCThreadState::kUnsetWorkSource;
637 }
638
639 const size_t initialPosition = dataPosition();
640 setDataPosition(mWorkSourceRequestHeaderPosition);
641 uid_t uid = readInt32();
642 setDataPosition(initialPosition);
643 return uid;
644}
645
Mathias Agopian83c04462009-05-22 19:00:22 -0700646bool Parcel::checkInterface(IBinder* binder) const
647{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700648 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700649}
650
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700651bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700652 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700653{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100654 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700655 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700656 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700657 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700658 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700659 if ((threadState->getLastTransactionBinderFlags() &
660 IBinder::FLAG_ONEWAY) != 0) {
661 // For one-way calls, the callee is running entirely
662 // disconnected from the caller, so disable StrictMode entirely.
663 // Not only does disk/network usage not impact the caller, but
664 // there's no way to commuicate back any violations anyway.
665 threadState->setStrictModePolicy(0);
666 } else {
667 threadState->setStrictModePolicy(strictPolicy);
668 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100669 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000670 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100671 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000672 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100673 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700674 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700675 if (str == interface) {
676 return true;
677 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700678 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700679 String8(interface).string(), String8(str).string());
680 return false;
681 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700682}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700683
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800684const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700685{
686 return mObjects;
687}
688
689size_t Parcel::objectsCount() const
690{
691 return mObjectsSize;
692}
693
694status_t Parcel::errorCheck() const
695{
696 return mError;
697}
698
699void Parcel::setError(status_t err)
700{
701 mError = err;
702}
703
704status_t Parcel::finishWrite(size_t len)
705{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700706 if (len > INT32_MAX) {
707 // don't accept size_t values which may have come from an
708 // inadvertent conversion from a negative int.
709 return BAD_VALUE;
710 }
711
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700712 //printf("Finish write of %d\n", len);
713 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700714 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700715 if (mDataPos > mDataSize) {
716 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700717 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700718 }
719 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
720 return NO_ERROR;
721}
722
723status_t Parcel::writeUnpadded(const void* data, size_t len)
724{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700725 if (len > INT32_MAX) {
726 // don't accept size_t values which may have come from an
727 // inadvertent conversion from a negative int.
728 return BAD_VALUE;
729 }
730
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700731 size_t end = mDataPos + len;
732 if (end < mDataPos) {
733 // integer overflow
734 return BAD_VALUE;
735 }
736
737 if (end <= mDataCapacity) {
738restart_write:
739 memcpy(mData+mDataPos, data, len);
740 return finishWrite(len);
741 }
742
743 status_t err = growData(len);
744 if (err == NO_ERROR) goto restart_write;
745 return err;
746}
747
748status_t Parcel::write(const void* data, size_t len)
749{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700750 if (len > INT32_MAX) {
751 // don't accept size_t values which may have come from an
752 // inadvertent conversion from a negative int.
753 return BAD_VALUE;
754 }
755
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700756 void* const d = writeInplace(len);
757 if (d) {
758 memcpy(d, data, len);
759 return NO_ERROR;
760 }
761 return mError;
762}
763
764void* Parcel::writeInplace(size_t len)
765{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700766 if (len > INT32_MAX) {
767 // don't accept size_t values which may have come from an
768 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700769 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700770 }
771
772 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700773
774 // sanity check for integer overflow
775 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700776 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700777 }
778
779 if ((mDataPos+padded) <= mDataCapacity) {
780restart_write:
781 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
782 uint8_t* const data = mData+mDataPos;
783
784 // Need to pad at end?
785 if (padded != len) {
786#if BYTE_ORDER == BIG_ENDIAN
787 static const uint32_t mask[4] = {
788 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
789 };
790#endif
791#if BYTE_ORDER == LITTLE_ENDIAN
792 static const uint32_t mask[4] = {
793 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
794 };
795#endif
796 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
797 // *reinterpret_cast<void**>(data+padded-4));
798 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
799 }
800
801 finishWrite(padded);
802 return data;
803 }
804
805 status_t err = growData(padded);
806 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700807 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700808}
809
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800810status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
811 const uint8_t* strData = (uint8_t*)str.data();
812 const size_t strLen= str.length();
813 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100814 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800815 return BAD_VALUE;
816 }
817
818 status_t err = writeInt32(utf16Len);
819 if (err) {
820 return err;
821 }
822
823 // Allocate enough bytes to hold our converted string and its terminating NULL.
824 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
825 if (!dst) {
826 return NO_MEMORY;
827 }
828
Sergio Girof4607432016-07-21 14:46:35 +0100829 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800830
831 return NO_ERROR;
832}
833
834status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
835 if (!str) {
836 return writeInt32(-1);
837 }
838 return writeUtf8AsUtf16(*str);
839}
840
Casey Dahlin185d3442016-02-09 11:08:35 -0800841namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800842
Casey Dahlin185d3442016-02-09 11:08:35 -0800843template<typename T>
844status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700845{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700846 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700847 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700848 status = BAD_VALUE;
849 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700850 }
851
Casey Dahlin185d3442016-02-09 11:08:35 -0800852 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700853 if (status != OK) {
854 return status;
855 }
856
Casey Dahlin185d3442016-02-09 11:08:35 -0800857 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700858 if (!data) {
859 status = BAD_VALUE;
860 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700861 }
862
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700863 memcpy(data, val.data(), val.size());
864 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700865}
866
Casey Dahlin185d3442016-02-09 11:08:35 -0800867template<typename T>
868status_t writeByteVectorInternalPtr(Parcel* parcel,
869 const std::unique_ptr<std::vector<T>>& val)
870{
871 if (!val) {
872 return parcel->writeInt32(-1);
873 }
874
875 return writeByteVectorInternal(parcel, *val);
876}
877
878} // namespace
879
880status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
881 return writeByteVectorInternal(this, val);
882}
883
884status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
885{
886 return writeByteVectorInternalPtr(this, val);
887}
888
889status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
890 return writeByteVectorInternal(this, val);
891}
892
893status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
894{
895 return writeByteVectorInternalPtr(this, val);
896}
897
Casey Dahlin451ff582015-10-19 18:12:18 -0700898status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
899{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800900 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700901}
902
Casey Dahlinb9872622015-11-25 15:09:45 -0800903status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
904{
905 return writeNullableTypedVector(val, &Parcel::writeInt32);
906}
907
Casey Dahlin451ff582015-10-19 18:12:18 -0700908status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
909{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800910 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700911}
912
Casey Dahlinb9872622015-11-25 15:09:45 -0800913status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
914{
915 return writeNullableTypedVector(val, &Parcel::writeInt64);
916}
917
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800918status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
919{
920 return writeTypedVector(val, &Parcel::writeUint64);
921}
922
923status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
924{
925 return writeNullableTypedVector(val, &Parcel::writeUint64);
926}
927
Casey Dahlin451ff582015-10-19 18:12:18 -0700928status_t Parcel::writeFloatVector(const std::vector<float>& val)
929{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800930 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700931}
932
Casey Dahlinb9872622015-11-25 15:09:45 -0800933status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
934{
935 return writeNullableTypedVector(val, &Parcel::writeFloat);
936}
937
Casey Dahlin451ff582015-10-19 18:12:18 -0700938status_t Parcel::writeDoubleVector(const std::vector<double>& val)
939{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800940 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700941}
942
Casey Dahlinb9872622015-11-25 15:09:45 -0800943status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
944{
945 return writeNullableTypedVector(val, &Parcel::writeDouble);
946}
947
Casey Dahlin451ff582015-10-19 18:12:18 -0700948status_t Parcel::writeBoolVector(const std::vector<bool>& val)
949{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800950 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700951}
952
Casey Dahlinb9872622015-11-25 15:09:45 -0800953status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
954{
955 return writeNullableTypedVector(val, &Parcel::writeBool);
956}
957
Casey Dahlin451ff582015-10-19 18:12:18 -0700958status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
959{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800960 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700961}
962
Casey Dahlinb9872622015-11-25 15:09:45 -0800963status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
964{
965 return writeNullableTypedVector(val, &Parcel::writeChar);
966}
967
Casey Dahlin451ff582015-10-19 18:12:18 -0700968status_t Parcel::writeString16Vector(const std::vector<String16>& val)
969{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800970 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700971}
972
Casey Dahlinb9872622015-11-25 15:09:45 -0800973status_t Parcel::writeString16Vector(
974 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
975{
976 return writeNullableTypedVector(val, &Parcel::writeString16);
977}
978
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800979status_t Parcel::writeUtf8VectorAsUtf16Vector(
980 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
981 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
982}
983
984status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
985 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
986}
987
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700988status_t Parcel::writeInt32(int32_t val)
989{
Andreas Huber84a6d042009-08-17 13:33:27 -0700990 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700991}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800992
993status_t Parcel::writeUint32(uint32_t val)
994{
995 return writeAligned(val);
996}
997
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700998status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700999 if (len > INT32_MAX) {
1000 // don't accept size_t values which may have come from an
1001 // inadvertent conversion from a negative int.
1002 return BAD_VALUE;
1003 }
1004
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001005 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001006 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001007 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001008 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001009 if (ret == NO_ERROR) {
1010 ret = write(val, len * sizeof(*val));
1011 }
1012 return ret;
1013}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001014status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001015 if (len > INT32_MAX) {
1016 // don't accept size_t values which may have come from an
1017 // inadvertent conversion from a negative int.
1018 return BAD_VALUE;
1019 }
1020
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001021 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001022 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001023 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001024 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001025 if (ret == NO_ERROR) {
1026 ret = write(val, len * sizeof(*val));
1027 }
1028 return ret;
1029}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001030
Casey Dahlind6848f52015-10-15 15:44:59 -07001031status_t Parcel::writeBool(bool val)
1032{
1033 return writeInt32(int32_t(val));
1034}
1035
1036status_t Parcel::writeChar(char16_t val)
1037{
1038 return writeInt32(int32_t(val));
1039}
1040
1041status_t Parcel::writeByte(int8_t val)
1042{
1043 return writeInt32(int32_t(val));
1044}
1045
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001046status_t Parcel::writeInt64(int64_t val)
1047{
Andreas Huber84a6d042009-08-17 13:33:27 -07001048 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001049}
1050
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001051status_t Parcel::writeUint64(uint64_t val)
1052{
1053 return writeAligned(val);
1054}
1055
Serban Constantinescuf683e012013-11-05 16:53:55 +00001056status_t Parcel::writePointer(uintptr_t val)
1057{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001058 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001059}
1060
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001061status_t Parcel::writeFloat(float val)
1062{
Andreas Huber84a6d042009-08-17 13:33:27 -07001063 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001064}
1065
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001066#if defined(__mips__) && defined(__mips_hard_float)
1067
1068status_t Parcel::writeDouble(double val)
1069{
1070 union {
1071 double d;
1072 unsigned long long ll;
1073 } u;
1074 u.d = val;
1075 return writeAligned(u.ll);
1076}
1077
1078#else
1079
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001080status_t Parcel::writeDouble(double val)
1081{
Andreas Huber84a6d042009-08-17 13:33:27 -07001082 return writeAligned(val);
1083}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001084
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001085#endif
1086
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001087status_t Parcel::writeCString(const char* str)
1088{
1089 return write(str, strlen(str)+1);
1090}
1091
1092status_t Parcel::writeString8(const String8& str)
1093{
1094 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001095 // only write string if its length is more than zero characters,
1096 // as readString8 will only read if the length field is non-zero.
1097 // this is slightly different from how writeString16 works.
1098 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001099 err = write(str.string(), str.bytes()+1);
1100 }
1101 return err;
1102}
1103
Casey Dahlinb9872622015-11-25 15:09:45 -08001104status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1105{
1106 if (!str) {
1107 return writeInt32(-1);
1108 }
1109
1110 return writeString16(*str);
1111}
1112
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001113status_t Parcel::writeString16(const String16& str)
1114{
1115 return writeString16(str.string(), str.size());
1116}
1117
1118status_t Parcel::writeString16(const char16_t* str, size_t len)
1119{
Yi Kong91635562018-06-07 14:38:36 -07001120 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001121
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001122 status_t err = writeInt32(len);
1123 if (err == NO_ERROR) {
1124 len *= sizeof(char16_t);
1125 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1126 if (data) {
1127 memcpy(data, str, len);
1128 *reinterpret_cast<char16_t*>(data+len) = 0;
1129 return NO_ERROR;
1130 }
1131 err = mError;
1132 }
1133 return err;
1134}
1135
1136status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1137{
1138 return flatten_binder(ProcessState::self(), val, this);
1139}
1140
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001141status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1142{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001143 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001144}
1145
Casey Dahlinb9872622015-11-25 15:09:45 -08001146status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1147{
1148 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1149}
1150
1151status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001152 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001153}
1154
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001155status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001156 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001157}
1158
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001159status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1160{
1161 return flatten_binder(ProcessState::self(), val, this);
1162}
1163
Casey Dahlinb9872622015-11-25 15:09:45 -08001164status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1165 if (!parcelable) {
1166 return writeInt32(0);
1167 }
1168
1169 return writeParcelable(*parcelable);
1170}
1171
Christopher Wiley97f048d2015-11-19 06:49:05 -08001172status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1173 status_t status = writeInt32(1); // parcelable is not null.
1174 if (status != OK) {
1175 return status;
1176 }
1177 return parcelable.writeToParcel(this);
1178}
1179
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001180status_t Parcel::writeValue(const binder::Value& value) {
1181 return value.writeToParcel(this);
1182}
1183
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001184status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001185{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001186 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001187 return BAD_TYPE;
1188
1189 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001190 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001191 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001193 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001194 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001196 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1197 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198
1199 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001200 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201 return err;
1202 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001203 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001204 return err;
1205}
1206
Jeff Brown93ff1f92011-11-04 19:01:44 -07001207status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001208{
1209 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001210 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001211 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001212 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001213 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001214 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001215 return writeObject(obj, true);
1216}
1217
1218status_t Parcel::writeDupFileDescriptor(int fd)
1219{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001220 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001221 if (dupFd < 0) {
1222 return -errno;
1223 }
1224 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001225 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001226 close(dupFd);
1227 }
1228 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001229}
1230
Dianne Hackborn1941a402016-08-29 12:30:43 -07001231status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1232{
1233 writeInt32(0);
1234 return writeFileDescriptor(fd, takeOwnership);
1235}
1236
Ryo Hashimotobf551892018-05-31 16:58:35 +09001237status_t Parcel::writeDupParcelFileDescriptor(int fd)
1238{
1239 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1240 if (dupFd < 0) {
1241 return -errno;
1242 }
1243 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1244 if (err != OK) {
1245 close(dupFd);
1246 }
1247 return err;
1248}
1249
Christopher Wiley2cf19952016-04-11 11:09:37 -07001250status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001251 return writeDupFileDescriptor(fd.get());
1252}
1253
Christopher Wiley2cf19952016-04-11 11:09:37 -07001254status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001255 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1256}
1257
Christopher Wiley2cf19952016-04-11 11:09:37 -07001258status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001259 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1260}
1261
Jeff Brown13b16042014-11-11 16:44:25 -08001262status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001263{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001264 if (len > INT32_MAX) {
1265 // don't accept size_t values which may have come from an
1266 // inadvertent conversion from a negative int.
1267 return BAD_VALUE;
1268 }
1269
Jeff Brown13b16042014-11-11 16:44:25 -08001270 status_t status;
1271 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001272 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001273 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001274 if (status) return status;
1275
1276 void* ptr = writeInplace(len);
1277 if (!ptr) return NO_MEMORY;
1278
Jeff Brown13b16042014-11-11 16:44:25 -08001279 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001280 return NO_ERROR;
1281 }
1282
Steve Block6807e592011-10-20 11:56:00 +01001283 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001284 int fd = ashmem_create_region("Parcel Blob", len);
1285 if (fd < 0) return NO_MEMORY;
1286
1287 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1288 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001289 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001290 } else {
Yi Kong91635562018-06-07 14:38:36 -07001291 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001292 if (ptr == MAP_FAILED) {
1293 status = -errno;
1294 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001295 if (!mutableCopy) {
1296 result = ashmem_set_prot_region(fd, PROT_READ);
1297 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001298 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001299 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001300 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001301 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001302 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001303 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001304 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001305 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001306 return NO_ERROR;
1307 }
1308 }
1309 }
1310 }
1311 ::munmap(ptr, len);
1312 }
1313 ::close(fd);
1314 return status;
1315}
1316
Jeff Brown13b16042014-11-11 16:44:25 -08001317status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1318{
1319 // Must match up with what's done in writeBlob.
1320 if (!mAllowFds) return FDS_NOT_ALLOWED;
1321 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1322 if (status) return status;
1323 return writeDupFileDescriptor(fd);
1324}
1325
Mathias Agopiane1424282013-07-29 21:24:40 -07001326status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001327{
1328 status_t err;
1329
1330 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001331 const size_t len = val.getFlattenedSize();
1332 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001333
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001334 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001335 // don't accept size_t values which may have come from an
1336 // inadvertent conversion from a negative int.
1337 return BAD_VALUE;
1338 }
1339
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001340 err = this->writeInt32(len);
1341 if (err) return err;
1342
1343 err = this->writeInt32(fd_count);
1344 if (err) return err;
1345
1346 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001347 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001348 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001349 return BAD_VALUE;
1350
Yi Kong91635562018-06-07 14:38:36 -07001351 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001352 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001353 fds = new (std::nothrow) int[fd_count];
1354 if (fds == nullptr) {
1355 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1356 return BAD_VALUE;
1357 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001358 }
1359
1360 err = val.flatten(buf, len, fds, fd_count);
1361 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1362 err = this->writeDupFileDescriptor( fds[i] );
1363 }
1364
1365 if (fd_count) {
1366 delete [] fds;
1367 }
1368
1369 return err;
1370}
1371
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001372status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1373{
1374 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1375 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1376 if (enoughData && enoughObjects) {
1377restart_write:
1378 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001379
Christopher Tate98e67d32015-06-03 18:44:15 -07001380 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001381 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001382 if (!mAllowFds) {
1383 // fail before modifying our object index
1384 return FDS_NOT_ALLOWED;
1385 }
1386 mHasFds = mFdsKnown = true;
1387 }
1388
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001389 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001390 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001391 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001392 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001393 mObjectsSize++;
1394 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001395
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001396 return finishWrite(sizeof(flat_binder_object));
1397 }
1398
1399 if (!enoughData) {
1400 const status_t err = growData(sizeof(val));
1401 if (err != NO_ERROR) return err;
1402 }
1403 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001404 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1405 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001406 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001407 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001408 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001409 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001410 mObjects = objects;
1411 mObjectsCapacity = newSize;
1412 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001413
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001414 goto restart_write;
1415}
1416
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001417status_t Parcel::writeNoException()
1418{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001419 binder::Status status;
1420 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001421}
1422
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001423status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1424{
1425 using ::std::map;
1426 using ::android::binder::Value;
1427 using ::android::binder::Map;
1428
1429 Map::const_iterator iter;
1430 status_t ret;
1431
1432 ret = writeInt32(map_in.size());
1433
1434 if (ret != NO_ERROR) {
1435 return ret;
1436 }
1437
1438 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1439 ret = writeValue(Value(iter->first));
1440 if (ret != NO_ERROR) {
1441 return ret;
1442 }
1443
1444 ret = writeValue(iter->second);
1445 if (ret != NO_ERROR) {
1446 return ret;
1447 }
1448 }
1449
1450 return ret;
1451}
1452
1453status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1454{
Yi Kong91635562018-06-07 14:38:36 -07001455 if (map == nullptr) {
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001456 return writeInt32(-1);
1457 }
1458
1459 return writeMap(*map.get());
1460}
1461
1462status_t Parcel::readMap(::android::binder::Map* map_out)const
1463{
1464 using ::std::map;
1465 using ::android::String16;
1466 using ::android::String8;
1467 using ::android::binder::Value;
1468 using ::android::binder::Map;
1469
1470 status_t ret = NO_ERROR;
1471 int32_t count;
1472
1473 ret = readInt32(&count);
1474 if (ret != NO_ERROR) {
1475 return ret;
1476 }
1477
1478 if (count < 0) {
1479 ALOGE("readMap: Unexpected count: %d", count);
1480 return (count == -1)
1481 ? UNEXPECTED_NULL
1482 : BAD_VALUE;
1483 }
1484
1485 map_out->clear();
1486
1487 while (count--) {
1488 Map::key_type key;
1489 Value value;
1490
1491 ret = readValue(&value);
1492 if (ret != NO_ERROR) {
1493 return ret;
1494 }
1495
1496 if (!value.getString(&key)) {
1497 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1498 return BAD_VALUE;
1499 }
1500
1501 ret = readValue(&value);
1502 if (ret != NO_ERROR) {
1503 return ret;
1504 }
1505
1506 (*map_out)[key] = value;
1507 }
1508
1509 return ret;
1510}
1511
1512status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1513{
1514 const size_t start = dataPosition();
1515 int32_t count;
1516 status_t status = readInt32(&count);
1517 map->reset();
1518
1519 if (status != OK || count == -1) {
1520 return status;
1521 }
1522
1523 setDataPosition(start);
1524 map->reset(new binder::Map());
1525
1526 status = readMap(map->get());
1527
1528 if (status != OK) {
1529 map->reset();
1530 }
1531
1532 return status;
1533}
1534
1535
1536
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001537void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001538{
1539 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1540}
1541
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001542status_t Parcel::validateReadData(size_t upperBound) const
1543{
1544 // Don't allow non-object reads on object data
1545 if (mObjectsSorted || mObjectsSize <= 1) {
1546data_sorted:
1547 // Expect to check only against the next object
1548 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1549 // For some reason the current read position is greater than the next object
1550 // hint. Iterate until we find the right object
1551 size_t nextObject = mNextObjectHint;
1552 do {
1553 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1554 // Requested info overlaps with an object
1555 ALOGE("Attempt to read from protected data in Parcel %p", this);
1556 return PERMISSION_DENIED;
1557 }
1558 nextObject++;
1559 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1560 mNextObjectHint = nextObject;
1561 }
1562 return NO_ERROR;
1563 }
1564 // Quickly determine if mObjects is sorted.
1565 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1566 binder_size_t* prevObj = currObj;
1567 while (currObj > mObjects) {
1568 prevObj--;
1569 if(*prevObj > *currObj) {
1570 goto data_unsorted;
1571 }
1572 currObj--;
1573 }
1574 mObjectsSorted = true;
1575 goto data_sorted;
1576
1577data_unsorted:
1578 // Insertion Sort mObjects
1579 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1580 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1581 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1582 binder_size_t temp = *iter0;
1583 binder_size_t* iter1 = iter0 - 1;
1584 while (iter1 >= mObjects && *iter1 > temp) {
1585 *(iter1 + 1) = *iter1;
1586 iter1--;
1587 }
1588 *(iter1 + 1) = temp;
1589 }
1590 mNextObjectHint = 0;
1591 mObjectsSorted = true;
1592 goto data_sorted;
1593}
1594
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001595status_t Parcel::read(void* outData, size_t len) const
1596{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001597 if (len > INT32_MAX) {
1598 // don't accept size_t values which may have come from an
1599 // inadvertent conversion from a negative int.
1600 return BAD_VALUE;
1601 }
1602
1603 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1604 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001605 if (mObjectsSize > 0) {
1606 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001607 if(err != NO_ERROR) {
1608 // Still increment the data position by the expected length
1609 mDataPos += pad_size(len);
1610 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1611 return err;
1612 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001613 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001614 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001615 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001616 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001617 return NO_ERROR;
1618 }
1619 return NOT_ENOUGH_DATA;
1620}
1621
1622const void* Parcel::readInplace(size_t len) const
1623{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001624 if (len > INT32_MAX) {
1625 // don't accept size_t values which may have come from an
1626 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001627 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001628 }
1629
1630 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1631 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001632 if (mObjectsSize > 0) {
1633 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001634 if(err != NO_ERROR) {
1635 // Still increment the data position by the expected length
1636 mDataPos += pad_size(len);
1637 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001638 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001639 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001640 }
1641
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001642 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001643 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001644 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001645 return data;
1646 }
Yi Kong91635562018-06-07 14:38:36 -07001647 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001648}
1649
Andreas Huber84a6d042009-08-17 13:33:27 -07001650template<class T>
1651status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001652 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001653
1654 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001655 if (mObjectsSize > 0) {
1656 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001657 if(err != NO_ERROR) {
1658 // Still increment the data position by the expected length
1659 mDataPos += sizeof(T);
1660 return err;
1661 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001662 }
1663
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001664 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001665 mDataPos += sizeof(T);
1666 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001667 return NO_ERROR;
1668 } else {
1669 return NOT_ENOUGH_DATA;
1670 }
1671}
1672
Andreas Huber84a6d042009-08-17 13:33:27 -07001673template<class T>
1674T Parcel::readAligned() const {
1675 T result;
1676 if (readAligned(&result) != NO_ERROR) {
1677 result = 0;
1678 }
1679
1680 return result;
1681}
1682
1683template<class T>
1684status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001685 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001686
1687 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1688restart_write:
1689 *reinterpret_cast<T*>(mData+mDataPos) = val;
1690 return finishWrite(sizeof(val));
1691 }
1692
1693 status_t err = growData(sizeof(val));
1694 if (err == NO_ERROR) goto restart_write;
1695 return err;
1696}
1697
Casey Dahlin185d3442016-02-09 11:08:35 -08001698namespace {
1699
1700template<typename T>
1701status_t readByteVectorInternal(const Parcel* parcel,
1702 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001703 val->clear();
1704
1705 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001706 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001707
1708 if (status != OK) {
1709 return status;
1710 }
1711
Christopher Wiley4db672d2015-11-10 09:44:30 -08001712 if (size < 0) {
1713 status = UNEXPECTED_NULL;
1714 return status;
1715 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001716 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001717 status = BAD_VALUE;
1718 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001719 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001720
Paul Lietar433e87b2016-09-16 10:39:32 -07001721 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001722 if (!data) {
1723 status = BAD_VALUE;
1724 return status;
1725 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001726 val->reserve(size);
1727 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001728
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001729 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001730}
1731
Casey Dahlin185d3442016-02-09 11:08:35 -08001732template<typename T>
1733status_t readByteVectorInternalPtr(
1734 const Parcel* parcel,
1735 std::unique_ptr<std::vector<T>>* val) {
1736 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001737 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001738 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001739 val->reset();
1740
1741 if (status != OK || size < 0) {
1742 return status;
1743 }
1744
Casey Dahlin185d3442016-02-09 11:08:35 -08001745 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001746 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001747
Casey Dahlin185d3442016-02-09 11:08:35 -08001748 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001749
1750 if (status != OK) {
1751 val->reset();
1752 }
1753
1754 return status;
1755}
1756
Casey Dahlin185d3442016-02-09 11:08:35 -08001757} // namespace
1758
1759status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1760 return readByteVectorInternal(this, val);
1761}
1762
1763status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1764 return readByteVectorInternal(this, val);
1765}
1766
1767status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1768 return readByteVectorInternalPtr(this, val);
1769}
1770
1771status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1772 return readByteVectorInternalPtr(this, val);
1773}
1774
Casey Dahlinb9872622015-11-25 15:09:45 -08001775status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1776 return readNullableTypedVector(val, &Parcel::readInt32);
1777}
1778
Casey Dahlin451ff582015-10-19 18:12:18 -07001779status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001780 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001781}
1782
Casey Dahlinb9872622015-11-25 15:09:45 -08001783status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1784 return readNullableTypedVector(val, &Parcel::readInt64);
1785}
1786
Casey Dahlin451ff582015-10-19 18:12:18 -07001787status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001788 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001789}
1790
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001791status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1792 return readNullableTypedVector(val, &Parcel::readUint64);
1793}
1794
1795status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1796 return readTypedVector(val, &Parcel::readUint64);
1797}
1798
Casey Dahlinb9872622015-11-25 15:09:45 -08001799status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1800 return readNullableTypedVector(val, &Parcel::readFloat);
1801}
1802
Casey Dahlin451ff582015-10-19 18:12:18 -07001803status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001804 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001805}
1806
Casey Dahlinb9872622015-11-25 15:09:45 -08001807status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1808 return readNullableTypedVector(val, &Parcel::readDouble);
1809}
1810
Casey Dahlin451ff582015-10-19 18:12:18 -07001811status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001812 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001813}
1814
Casey Dahlinb9872622015-11-25 15:09:45 -08001815status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1816 const int32_t start = dataPosition();
1817 int32_t size;
1818 status_t status = readInt32(&size);
1819 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001820
Casey Dahlinb9872622015-11-25 15:09:45 -08001821 if (status != OK || size < 0) {
1822 return status;
1823 }
1824
1825 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001826 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001827
1828 status = readBoolVector(val->get());
1829
1830 if (status != OK) {
1831 val->reset();
1832 }
1833
1834 return status;
1835}
1836
1837status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001838 int32_t size;
1839 status_t status = readInt32(&size);
1840
1841 if (status != OK) {
1842 return status;
1843 }
1844
1845 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001846 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001847 }
1848
1849 val->resize(size);
1850
1851 /* C++ bool handling means a vector of bools isn't necessarily addressable
1852 * (we might use individual bits)
1853 */
Christopher Wiley97887982015-10-27 16:33:47 -07001854 bool data;
1855 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001856 status = readBool(&data);
1857 (*val)[i] = data;
1858
1859 if (status != OK) {
1860 return status;
1861 }
1862 }
1863
1864 return OK;
1865}
1866
Casey Dahlinb9872622015-11-25 15:09:45 -08001867status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1868 return readNullableTypedVector(val, &Parcel::readChar);
1869}
1870
Casey Dahlin451ff582015-10-19 18:12:18 -07001871status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001872 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001873}
1874
Casey Dahlinb9872622015-11-25 15:09:45 -08001875status_t Parcel::readString16Vector(
1876 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1877 return readNullableTypedVector(val, &Parcel::readString16);
1878}
1879
Casey Dahlin451ff582015-10-19 18:12:18 -07001880status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001881 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001882}
1883
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001884status_t Parcel::readUtf8VectorFromUtf16Vector(
1885 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1886 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1887}
1888
1889status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1890 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1891}
Casey Dahlin451ff582015-10-19 18:12:18 -07001892
Andreas Huber84a6d042009-08-17 13:33:27 -07001893status_t Parcel::readInt32(int32_t *pArg) const
1894{
1895 return readAligned(pArg);
1896}
1897
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001898int32_t Parcel::readInt32() const
1899{
Andreas Huber84a6d042009-08-17 13:33:27 -07001900 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001901}
1902
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001903status_t Parcel::readUint32(uint32_t *pArg) const
1904{
1905 return readAligned(pArg);
1906}
1907
1908uint32_t Parcel::readUint32() const
1909{
1910 return readAligned<uint32_t>();
1911}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001912
1913status_t Parcel::readInt64(int64_t *pArg) const
1914{
Andreas Huber84a6d042009-08-17 13:33:27 -07001915 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001916}
1917
1918
1919int64_t Parcel::readInt64() const
1920{
Andreas Huber84a6d042009-08-17 13:33:27 -07001921 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001922}
1923
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001924status_t Parcel::readUint64(uint64_t *pArg) const
1925{
1926 return readAligned(pArg);
1927}
1928
1929uint64_t Parcel::readUint64() const
1930{
1931 return readAligned<uint64_t>();
1932}
1933
Serban Constantinescuf683e012013-11-05 16:53:55 +00001934status_t Parcel::readPointer(uintptr_t *pArg) const
1935{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001936 status_t ret;
1937 binder_uintptr_t ptr;
1938 ret = readAligned(&ptr);
1939 if (!ret)
1940 *pArg = ptr;
1941 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001942}
1943
1944uintptr_t Parcel::readPointer() const
1945{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001946 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001947}
1948
1949
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001950status_t Parcel::readFloat(float *pArg) const
1951{
Andreas Huber84a6d042009-08-17 13:33:27 -07001952 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001953}
1954
1955
1956float Parcel::readFloat() const
1957{
Andreas Huber84a6d042009-08-17 13:33:27 -07001958 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001959}
1960
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001961#if defined(__mips__) && defined(__mips_hard_float)
1962
1963status_t Parcel::readDouble(double *pArg) const
1964{
1965 union {
1966 double d;
1967 unsigned long long ll;
1968 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001969 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001970 status_t status;
1971 status = readAligned(&u.ll);
1972 *pArg = u.d;
1973 return status;
1974}
1975
1976double Parcel::readDouble() const
1977{
1978 union {
1979 double d;
1980 unsigned long long ll;
1981 } u;
1982 u.ll = readAligned<unsigned long long>();
1983 return u.d;
1984}
1985
1986#else
1987
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001988status_t Parcel::readDouble(double *pArg) const
1989{
Andreas Huber84a6d042009-08-17 13:33:27 -07001990 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001991}
1992
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001993double Parcel::readDouble() const
1994{
Andreas Huber84a6d042009-08-17 13:33:27 -07001995 return readAligned<double>();
1996}
1997
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001998#endif
1999
Andreas Huber84a6d042009-08-17 13:33:27 -07002000status_t Parcel::readIntPtr(intptr_t *pArg) const
2001{
2002 return readAligned(pArg);
2003}
2004
2005
2006intptr_t Parcel::readIntPtr() const
2007{
2008 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002009}
2010
Casey Dahlind6848f52015-10-15 15:44:59 -07002011status_t Parcel::readBool(bool *pArg) const
2012{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002013 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002014 status_t ret = readInt32(&tmp);
2015 *pArg = (tmp != 0);
2016 return ret;
2017}
2018
2019bool Parcel::readBool() const
2020{
2021 return readInt32() != 0;
2022}
2023
2024status_t Parcel::readChar(char16_t *pArg) const
2025{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002026 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002027 status_t ret = readInt32(&tmp);
2028 *pArg = char16_t(tmp);
2029 return ret;
2030}
2031
2032char16_t Parcel::readChar() const
2033{
2034 return char16_t(readInt32());
2035}
2036
2037status_t Parcel::readByte(int8_t *pArg) const
2038{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002039 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002040 status_t ret = readInt32(&tmp);
2041 *pArg = int8_t(tmp);
2042 return ret;
2043}
2044
2045int8_t Parcel::readByte() const
2046{
2047 return int8_t(readInt32());
2048}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002049
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002050status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2051 size_t utf16Size = 0;
2052 const char16_t* src = readString16Inplace(&utf16Size);
2053 if (!src) {
2054 return UNEXPECTED_NULL;
2055 }
2056
2057 // Save ourselves the trouble, we're done.
2058 if (utf16Size == 0u) {
2059 str->clear();
2060 return NO_ERROR;
2061 }
2062
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002063 // Allow for closing '\0'
2064 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2065 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002066 return BAD_VALUE;
2067 }
2068 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002069 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002070 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002071 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2072 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002073 return NO_ERROR;
2074}
2075
2076status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2077 const int32_t start = dataPosition();
2078 int32_t size;
2079 status_t status = readInt32(&size);
2080 str->reset();
2081
2082 if (status != OK || size < 0) {
2083 return status;
2084 }
2085
2086 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002087 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002088 return readUtf8FromUtf16(str->get());
2089}
2090
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002091const char* Parcel::readCString() const
2092{
Steven Morelandd0d4b582019-05-17 13:14:06 -07002093 if (mDataPos < mDataSize) {
2094 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002095 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2096 // is the string's trailing NUL within the parcel's valid bounds?
2097 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2098 if (eos) {
2099 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002100 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002101 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002102 return str;
2103 }
2104 }
Yi Kong91635562018-06-07 14:38:36 -07002105 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002106}
2107
2108String8 Parcel::readString8() const
2109{
Roshan Pius87b64d22016-07-18 12:51:02 -07002110 String8 retString;
2111 status_t status = readString8(&retString);
2112 if (status != OK) {
2113 // We don't care about errors here, so just return an empty string.
2114 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002115 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002116 return retString;
2117}
2118
2119status_t Parcel::readString8(String8* pArg) const
2120{
2121 int32_t size;
2122 status_t status = readInt32(&size);
2123 if (status != OK) {
2124 return status;
2125 }
2126 // watch for potential int overflow from size+1
2127 if (size < 0 || size >= INT32_MAX) {
2128 return BAD_VALUE;
2129 }
2130 // |writeString8| writes nothing for empty string.
2131 if (size == 0) {
2132 *pArg = String8();
2133 return OK;
2134 }
2135 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07002136 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002137 return BAD_VALUE;
2138 }
2139 pArg->setTo(str, size);
2140 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141}
2142
2143String16 Parcel::readString16() const
2144{
2145 size_t len;
2146 const char16_t* str = readString16Inplace(&len);
2147 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002148 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002149 return String16();
2150}
2151
Casey Dahlinb9872622015-11-25 15:09:45 -08002152status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2153{
2154 const int32_t start = dataPosition();
2155 int32_t size;
2156 status_t status = readInt32(&size);
2157 pArg->reset();
2158
2159 if (status != OK || size < 0) {
2160 return status;
2161 }
2162
2163 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002164 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002165
2166 status = readString16(pArg->get());
2167
2168 if (status != OK) {
2169 pArg->reset();
2170 }
2171
2172 return status;
2173}
2174
Casey Dahlin451ff582015-10-19 18:12:18 -07002175status_t Parcel::readString16(String16* pArg) const
2176{
2177 size_t len;
2178 const char16_t* str = readString16Inplace(&len);
2179 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002180 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002181 return 0;
2182 } else {
2183 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002184 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002185 }
2186}
2187
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002188const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2189{
2190 int32_t size = readInt32();
2191 // watch for potential int overflow from size+1
2192 if (size >= 0 && size < INT32_MAX) {
2193 *outLen = size;
2194 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002195 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002196 return str;
2197 }
2198 }
2199 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002200 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002201}
2202
Casey Dahlinf0c13772015-10-27 18:33:56 -07002203status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2204{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002205 status_t status = readNullableStrongBinder(val);
2206 if (status == OK && !val->get()) {
2207 status = UNEXPECTED_NULL;
2208 }
2209 return status;
2210}
2211
2212status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2213{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002214 return unflatten_binder(ProcessState::self(), *this, val);
2215}
2216
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002217sp<IBinder> Parcel::readStrongBinder() const
2218{
2219 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002220 // Note that a lot of code in Android reads binders by hand with this
2221 // method, and that code has historically been ok with getting nullptr
2222 // back (while ignoring error codes).
2223 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002224 return val;
2225}
2226
2227wp<IBinder> Parcel::readWeakBinder() const
2228{
2229 wp<IBinder> val;
2230 unflatten_binder(ProcessState::self(), *this, &val);
2231 return val;
2232}
2233
Christopher Wiley97f048d2015-11-19 06:49:05 -08002234status_t Parcel::readParcelable(Parcelable* parcelable) const {
2235 int32_t have_parcelable = 0;
2236 status_t status = readInt32(&have_parcelable);
2237 if (status != OK) {
2238 return status;
2239 }
2240 if (!have_parcelable) {
2241 return UNEXPECTED_NULL;
2242 }
2243 return parcelable->readFromParcel(this);
2244}
2245
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002246status_t Parcel::readValue(binder::Value* value) const {
2247 return value->readFromParcel(this);
2248}
2249
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002250int32_t Parcel::readExceptionCode() const
2251{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002252 binder::Status status;
2253 status.readFromParcel(*this);
2254 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002255}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002256
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002257native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002258{
2259 int numFds, numInts;
2260 status_t err;
2261 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002262 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002263 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002264 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002265
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002266 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002267 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002268 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002269 }
2270
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002271 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002272 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002273 if (h->data[i] < 0) {
2274 for (int j = 0; j < i; j++) {
2275 close(h->data[j]);
2276 }
2277 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002278 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002279 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002280 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002281 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002282 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002283 native_handle_close(h);
2284 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002285 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002286 }
2287 return h;
2288}
2289
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002290int Parcel::readFileDescriptor() const
2291{
2292 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002293
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002294 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002295 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002296 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002297
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002298 return BAD_TYPE;
2299}
2300
Dianne Hackborn1941a402016-08-29 12:30:43 -07002301int Parcel::readParcelFileDescriptor() const
2302{
2303 int32_t hasComm = readInt32();
2304 int fd = readFileDescriptor();
2305 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002306 // detach (owned by the binder driver)
2307 int comm = readFileDescriptor();
2308
2309 // warning: this must be kept in sync with:
2310 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2311 enum ParcelFileDescriptorStatus {
2312 DETACHED = 2,
2313 };
2314
2315#if BYTE_ORDER == BIG_ENDIAN
2316 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2317#endif
2318#if BYTE_ORDER == LITTLE_ENDIAN
2319 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2320#endif
2321
2322 ssize_t written = TEMP_FAILURE_RETRY(
2323 ::write(comm, &message, sizeof(message)));
2324
2325 if (written == -1 || written != sizeof(message)) {
2326 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2327 written, strerror(errno));
2328 return BAD_TYPE;
2329 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002330 }
2331 return fd;
2332}
2333
Christopher Wiley2cf19952016-04-11 11:09:37 -07002334status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002335{
2336 int got = readFileDescriptor();
2337
2338 if (got == BAD_TYPE) {
2339 return BAD_TYPE;
2340 }
2341
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002342 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002343
2344 if (val->get() < 0) {
2345 return BAD_VALUE;
2346 }
2347
2348 return OK;
2349}
2350
Ryo Hashimotobf551892018-05-31 16:58:35 +09002351status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2352{
2353 int got = readParcelFileDescriptor();
2354
2355 if (got == BAD_TYPE) {
2356 return BAD_TYPE;
2357 }
2358
2359 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2360
2361 if (val->get() < 0) {
2362 return BAD_VALUE;
2363 }
2364
2365 return OK;
2366}
Casey Dahlin06673e32015-11-23 13:24:23 -08002367
Christopher Wiley2cf19952016-04-11 11:09:37 -07002368status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002369 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2370}
2371
Christopher Wiley2cf19952016-04-11 11:09:37 -07002372status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002373 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2374}
2375
Jeff Brown5707dbf2011-09-23 21:17:56 -07002376status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2377{
Jeff Brown13b16042014-11-11 16:44:25 -08002378 int32_t blobType;
2379 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002380 if (status) return status;
2381
Jeff Brown13b16042014-11-11 16:44:25 -08002382 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002383 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002384 const void* ptr = readInplace(len);
2385 if (!ptr) return BAD_VALUE;
2386
Jeff Brown13b16042014-11-11 16:44:25 -08002387 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002388 return NO_ERROR;
2389 }
2390
Steve Block6807e592011-10-20 11:56:00 +01002391 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002392 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002393 int fd = readFileDescriptor();
2394 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2395
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002396 if (!ashmem_valid(fd)) {
2397 ALOGE("invalid fd");
2398 return BAD_VALUE;
2399 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002400 int size = ashmem_get_size_region(fd);
2401 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002402 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002403 return BAD_VALUE;
2404 }
Yi Kong91635562018-06-07 14:38:36 -07002405 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002406 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002407 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002408
Jeff Brown13b16042014-11-11 16:44:25 -08002409 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002410 return NO_ERROR;
2411}
2412
Mathias Agopiane1424282013-07-29 21:24:40 -07002413status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002414{
2415 // size
2416 const size_t len = this->readInt32();
2417 const size_t fd_count = this->readInt32();
2418
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002419 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002420 // don't accept size_t values which may have come from an
2421 // inadvertent conversion from a negative int.
2422 return BAD_VALUE;
2423 }
2424
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002425 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002426 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002427 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002428 return BAD_VALUE;
2429
Yi Kong91635562018-06-07 14:38:36 -07002430 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002431 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002432 fds = new (std::nothrow) int[fd_count];
2433 if (fds == nullptr) {
2434 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2435 return BAD_VALUE;
2436 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002437 }
2438
2439 status_t err = NO_ERROR;
2440 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002441 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002442 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002443 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002444 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 -07002445 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2446 // Close all the file descriptors that were dup-ed.
2447 for (size_t j=0; j<i ;j++) {
2448 close(fds[j]);
2449 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002450 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002451 }
2452
2453 if (err == NO_ERROR) {
2454 err = val.unflatten(buf, len, fds, fd_count);
2455 }
2456
2457 if (fd_count) {
2458 delete [] fds;
2459 }
2460
2461 return err;
2462}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002463const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2464{
2465 const size_t DPOS = mDataPos;
2466 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2467 const flat_binder_object* obj
2468 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2469 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002470 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002471 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002472 // the object list, so we don't want to check for it when
2473 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002474 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002475 return obj;
2476 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002477
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002478 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002479 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002480 const size_t N = mObjectsSize;
2481 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002482
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002483 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002484 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002485 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002486
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002487 // Start at the current hint position, looking for an object at
2488 // the current data position.
2489 if (opos < N) {
2490 while (opos < (N-1) && OBJS[opos] < DPOS) {
2491 opos++;
2492 }
2493 } else {
2494 opos = N-1;
2495 }
2496 if (OBJS[opos] == DPOS) {
2497 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002498 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002499 this, DPOS, opos);
2500 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002501 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002502 return obj;
2503 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002504
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002505 // Look backwards for it...
2506 while (opos > 0 && OBJS[opos] > DPOS) {
2507 opos--;
2508 }
2509 if (OBJS[opos] == DPOS) {
2510 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002511 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002512 this, DPOS, opos);
2513 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002514 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002515 return obj;
2516 }
2517 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002518 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 -07002519 this, DPOS);
2520 }
Yi Kong91635562018-06-07 14:38:36 -07002521 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002522}
2523
2524void Parcel::closeFileDescriptors()
2525{
2526 size_t i = mObjectsSize;
2527 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002528 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002529 }
2530 while (i > 0) {
2531 i--;
2532 const flat_binder_object* flat
2533 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002534 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002535 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002536 close(flat->handle);
2537 }
2538 }
2539}
2540
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002541uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002542{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002543 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544}
2545
2546size_t Parcel::ipcDataSize() const
2547{
2548 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2549}
2550
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002551uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002552{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002553 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002554}
2555
2556size_t Parcel::ipcObjectsCount() const
2557{
2558 return mObjectsSize;
2559}
2560
2561void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002562 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002563{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002564 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002565 freeDataNoInit();
2566 mError = NO_ERROR;
2567 mData = const_cast<uint8_t*>(data);
2568 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002569 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002570 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002571 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002572 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002573 mObjectsSize = mObjectsCapacity = objectsCount;
2574 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002575 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002576 mOwner = relFunc;
2577 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002578 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002579 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002580 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002581 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002582 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002583 mObjectsSize = 0;
2584 break;
2585 }
2586 minOffset = offset + sizeof(flat_binder_object);
2587 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002588 scanForFds();
2589}
2590
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002591void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002592{
2593 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002594
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002595 if (errorCheck() != NO_ERROR) {
2596 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002597 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002598 } else if (dataSize() > 0) {
2599 const uint8_t* DATA = data();
2600 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002601 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002602 const size_t N = objectsCount();
2603 for (size_t i=0; i<N; i++) {
2604 const flat_binder_object* flat
2605 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2606 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002607 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002608 << " = " << flat->binder;
2609 }
2610 } else {
2611 to << "NULL";
2612 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002613
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002614 to << ")";
2615}
2616
2617void Parcel::releaseObjects()
2618{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002619 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002620 if (i == 0) {
2621 return;
2622 }
2623 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002624 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002625 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002626 while (i > 0) {
2627 i--;
2628 const flat_binder_object* flat
2629 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002630 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002631 }
2632}
2633
2634void Parcel::acquireObjects()
2635{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002636 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002637 if (i == 0) {
2638 return;
2639 }
2640 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002641 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002642 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002643 while (i > 0) {
2644 i--;
2645 const flat_binder_object* flat
2646 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002647 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002648 }
2649}
2650
2651void Parcel::freeData()
2652{
2653 freeDataNoInit();
2654 initState();
2655}
2656
2657void Parcel::freeDataNoInit()
2658{
2659 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002660 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002661 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002662 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2663 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002664 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002665 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002666 if (mData) {
2667 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002668 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002669 if (mDataCapacity <= gParcelGlobalAllocSize) {
2670 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2671 } else {
2672 gParcelGlobalAllocSize = 0;
2673 }
2674 if (gParcelGlobalAllocCount > 0) {
2675 gParcelGlobalAllocCount--;
2676 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002677 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002678 free(mData);
2679 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002680 if (mObjects) free(mObjects);
2681 }
2682}
2683
2684status_t Parcel::growData(size_t len)
2685{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002686 if (len > INT32_MAX) {
2687 // don't accept size_t values which may have come from an
2688 // inadvertent conversion from a negative int.
2689 return BAD_VALUE;
2690 }
2691
Martijn Coenen93fe5182020-01-22 10:46:25 +01002692 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2693 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002694 size_t newSize = ((mDataSize+len)*3)/2;
2695 return (newSize <= mDataSize)
2696 ? (status_t) NO_MEMORY
2697 : continueWrite(newSize);
2698}
2699
2700status_t Parcel::restartWrite(size_t desired)
2701{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002702 if (desired > INT32_MAX) {
2703 // don't accept size_t values which may have come from an
2704 // inadvertent conversion from a negative int.
2705 return BAD_VALUE;
2706 }
2707
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002708 if (mOwner) {
2709 freeData();
2710 return continueWrite(desired);
2711 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002712
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002713 uint8_t* data = (uint8_t*)realloc(mData, desired);
2714 if (!data && desired > mDataCapacity) {
2715 mError = NO_MEMORY;
2716 return NO_MEMORY;
2717 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002718
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002719 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002720
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002721 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002722 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002723 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002724 gParcelGlobalAllocSize += desired;
2725 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002726 if (!mData) {
2727 gParcelGlobalAllocCount++;
2728 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002729 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002730 mData = data;
2731 mDataCapacity = desired;
2732 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002733
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002734 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002735 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2736 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2737
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002738 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002739 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002740 mObjectsSize = mObjectsCapacity = 0;
2741 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002742 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002743 mHasFds = false;
2744 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002745 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002746
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002747 return NO_ERROR;
2748}
2749
2750status_t Parcel::continueWrite(size_t desired)
2751{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002752 if (desired > INT32_MAX) {
2753 // don't accept size_t values which may have come from an
2754 // inadvertent conversion from a negative int.
2755 return BAD_VALUE;
2756 }
2757
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002758 // If shrinking, first adjust for any objects that appear
2759 // after the new data size.
2760 size_t objectsSize = mObjectsSize;
2761 if (desired < mDataSize) {
2762 if (desired == 0) {
2763 objectsSize = 0;
2764 } else {
2765 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002766 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002767 break;
2768 objectsSize--;
2769 }
2770 }
2771 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002772
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002773 if (mOwner) {
2774 // If the size is going to zero, just release the owner's data.
2775 if (desired == 0) {
2776 freeData();
2777 return NO_ERROR;
2778 }
2779
2780 // If there is a different owner, we need to take
2781 // posession.
2782 uint8_t* data = (uint8_t*)malloc(desired);
2783 if (!data) {
2784 mError = NO_MEMORY;
2785 return NO_MEMORY;
2786 }
Yi Kong91635562018-06-07 14:38:36 -07002787 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002788
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002789 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002790 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002791 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002792 free(data);
2793
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002794 mError = NO_MEMORY;
2795 return NO_MEMORY;
2796 }
2797
2798 // Little hack to only acquire references on objects
2799 // we will be keeping.
2800 size_t oldObjectsSize = mObjectsSize;
2801 mObjectsSize = objectsSize;
2802 acquireObjects();
2803 mObjectsSize = oldObjectsSize;
2804 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002805
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002806 if (mData) {
2807 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2808 }
2809 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002810 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002811 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002812 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002813 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002814 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002815
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002816 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002817 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002818 gParcelGlobalAllocSize += desired;
2819 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002820 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002821
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002822 mData = data;
2823 mObjects = objects;
2824 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002825 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002826 mDataCapacity = desired;
2827 mObjectsSize = mObjectsCapacity = objectsSize;
2828 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002829 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002830
2831 } else if (mData) {
2832 if (objectsSize < mObjectsSize) {
2833 // Need to release refs on any objects we are dropping.
2834 const sp<ProcessState> proc(ProcessState::self());
2835 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2836 const flat_binder_object* flat
2837 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002838 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002839 // will need to rescan because we may have lopped off the only FDs
2840 mFdsKnown = false;
2841 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002842 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002843 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002844
2845 if (objectsSize == 0) {
2846 free(mObjects);
2847 mObjects = nullptr;
Michael Wachenschwanzd9d10db2019-10-15 11:49:22 -07002848 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002849 } else {
2850 binder_size_t* objects =
2851 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2852 if (objects) {
2853 mObjects = objects;
Michael Wachenschwanzd9d10db2019-10-15 11:49:22 -07002854 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002855 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002856 }
2857 mObjectsSize = objectsSize;
2858 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002859 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002860 }
2861
2862 // We own the data, so we can just do a realloc().
2863 if (desired > mDataCapacity) {
2864 uint8_t* data = (uint8_t*)realloc(mData, desired);
2865 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002866 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2867 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002868 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002869 gParcelGlobalAllocSize += desired;
2870 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002871 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002872 mData = data;
2873 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002874 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002875 mError = NO_MEMORY;
2876 return NO_MEMORY;
2877 }
2878 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002879 if (mDataSize > desired) {
2880 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002881 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002882 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002883 if (mDataPos > desired) {
2884 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002885 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002886 }
2887 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002888
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002889 } else {
2890 // This is the first data. Easy!
2891 uint8_t* data = (uint8_t*)malloc(desired);
2892 if (!data) {
2893 mError = NO_MEMORY;
2894 return NO_MEMORY;
2895 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002896
Yi Kong91635562018-06-07 14:38:36 -07002897 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002898 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002899 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002900 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002901
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002902 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002903 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002904 gParcelGlobalAllocSize += desired;
2905 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002906 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002907
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002908 mData = data;
2909 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002910 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2911 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002912 mDataCapacity = desired;
2913 }
2914
2915 return NO_ERROR;
2916}
2917
2918void Parcel::initState()
2919{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002920 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002921 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002922 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002923 mDataSize = 0;
2924 mDataCapacity = 0;
2925 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002926 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2927 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002928 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002929 mObjectsSize = 0;
2930 mObjectsCapacity = 0;
2931 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002932 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002933 mHasFds = false;
2934 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002935 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002936 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002937 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002938 mWorkSourceRequestHeaderPosition = 0;
2939 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002940
2941 // racing multiple init leads only to multiple identical write
2942 if (gMaxFds == 0) {
2943 struct rlimit result;
2944 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2945 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002946 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002947 } else {
2948 ALOGW("Unable to getrlimit: %s", strerror(errno));
2949 gMaxFds = 1024;
2950 }
2951 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002952}
2953
2954void Parcel::scanForFds() const
2955{
2956 bool hasFds = false;
2957 for (size_t i=0; i<mObjectsSize; i++) {
2958 const flat_binder_object* flat
2959 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002960 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002961 hasFds = true;
2962 break;
2963 }
2964 }
2965 mHasFds = hasFds;
2966 mFdsKnown = true;
2967}
2968
Dan Sandleraa5c2342015-04-10 10:08:45 -04002969size_t Parcel::getBlobAshmemSize() const
2970{
Adrian Roos6bb31142015-10-22 16:46:12 -07002971 // This used to return the size of all blobs that were written to ashmem, now we're returning
2972 // the ashmem currently referenced by this Parcel, which should be equivalent.
2973 // TODO: Remove method once ABI can be changed.
2974 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002975}
2976
Adrian Rooscbf37262015-10-22 16:12:53 -07002977size_t Parcel::getOpenAshmemSize() const
2978{
2979 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002980}
2981
2982// --- Parcel::Blob ---
2983
2984Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002985 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002986}
2987
2988Parcel::Blob::~Blob() {
2989 release();
2990}
2991
2992void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002993 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002994 ::munmap(mData, mSize);
2995 }
2996 clear();
2997}
2998
Jeff Brown13b16042014-11-11 16:44:25 -08002999void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
3000 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003001 mData = data;
3002 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08003003 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003004}
3005
3006void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08003007 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07003008 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003009 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08003010 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003011}
3012
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003013}; // namespace android