blob: 9f8c40876e9be840ef6e537dca7f52aad5dfe7cc [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 Sharkey0c1f5cb2014-12-18 10:26:57 -080077#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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: {
Yi Kongfdd8da92018-06-07 17:52:27 -0700124 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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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
Yi Kongfdd8da92018-06-07 17:52:27 -0700173 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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -0700217 if (binder != nullptr) {
Steven Moreland3085a472018-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 Kongfdd8da92018-06-07 17:52:27 -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 Moreland3085a472018-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 Kongfdd8da92018-06-07 17:52:27 -0700252 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700253 sp<IBinder> real = binder.promote();
Yi Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -0700337 *out = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700338 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700339 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700340 case BINDER_TYPE_HANDLE:
341 case BINDER_TYPE_WEAK_HANDLE:
342 *out = proc->getWeakProxyForHandle(flat->handle);
343 return finish_unflatten_binder(
344 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
345 }
346 }
347 return BAD_TYPE;
348}
349
350// ---------------------------------------------------------------------------
351
352Parcel::Parcel()
353{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800354 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700355 initState();
356}
357
358Parcel::~Parcel()
359{
360 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800361 LOG_ALLOC("Parcel %p: destroyed", this);
362}
363
364size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800365 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
366 size_t size = gParcelGlobalAllocSize;
367 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
368 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800369}
370
371size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800372 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
373 size_t count = gParcelGlobalAllocCount;
374 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
375 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700376}
377
378const uint8_t* Parcel::data() const
379{
380 return mData;
381}
382
383size_t Parcel::dataSize() const
384{
385 return (mDataSize > mDataPos ? mDataSize : mDataPos);
386}
387
388size_t Parcel::dataAvail() const
389{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700390 size_t result = dataSize() - dataPosition();
391 if (result > INT32_MAX) {
392 abort();
393 }
394 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700395}
396
397size_t Parcel::dataPosition() const
398{
399 return mDataPos;
400}
401
402size_t Parcel::dataCapacity() const
403{
404 return mDataCapacity;
405}
406
407status_t Parcel::setDataSize(size_t size)
408{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700409 if (size > INT32_MAX) {
410 // don't accept size_t values which may have come from an
411 // inadvertent conversion from a negative int.
412 return BAD_VALUE;
413 }
414
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700415 status_t err;
416 err = continueWrite(size);
417 if (err == NO_ERROR) {
418 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700419 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700420 }
421 return err;
422}
423
424void Parcel::setDataPosition(size_t pos) const
425{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700426 if (pos > INT32_MAX) {
427 // don't accept size_t values which may have come from an
428 // inadvertent conversion from a negative int.
429 abort();
430 }
431
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700432 mDataPos = pos;
433 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800434 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700435}
436
437status_t Parcel::setDataCapacity(size_t size)
438{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700439 if (size > INT32_MAX) {
440 // don't accept size_t values which may have come from an
441 // inadvertent conversion from a negative int.
442 return BAD_VALUE;
443 }
444
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700445 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700446 return NO_ERROR;
447}
448
449status_t Parcel::setData(const uint8_t* buffer, size_t len)
450{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700451 if (len > INT32_MAX) {
452 // don't accept size_t values which may have come from an
453 // inadvertent conversion from a negative int.
454 return BAD_VALUE;
455 }
456
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700457 status_t err = restartWrite(len);
458 if (err == NO_ERROR) {
459 memcpy(const_cast<uint8_t*>(data()), buffer, len);
460 mDataSize = len;
461 mFdsKnown = false;
462 }
463 return err;
464}
465
Andreas Huber51faf462011-04-13 10:21:56 -0700466status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700467{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700468 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700469 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800470 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700471 size_t size = parcel->mObjectsSize;
472 int startPos = mDataPos;
473 int firstIndex = -1, lastIndex = -2;
474
475 if (len == 0) {
476 return NO_ERROR;
477 }
478
Nick Kralevichb6b14232015-04-02 09:36:02 -0700479 if (len > INT32_MAX) {
480 // don't accept size_t values which may have come from an
481 // inadvertent conversion from a negative int.
482 return BAD_VALUE;
483 }
484
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700485 // range checks against the source parcel size
486 if ((offset > parcel->mDataSize)
487 || (len > parcel->mDataSize)
488 || (offset + len > parcel->mDataSize)) {
489 return BAD_VALUE;
490 }
491
492 // Count objects in range
493 for (int i = 0; i < (int) size; i++) {
494 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700495 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700496 if (firstIndex == -1) {
497 firstIndex = i;
498 }
499 lastIndex = i;
500 }
501 }
502 int numObjects = lastIndex - firstIndex + 1;
503
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700504 if ((mDataSize+len) > mDataCapacity) {
505 // grow data
506 err = growData(len);
507 if (err != NO_ERROR) {
508 return err;
509 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700510 }
511
512 // append data
513 memcpy(mData + mDataPos, data + offset, len);
514 mDataPos += len;
515 mDataSize += len;
516
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400517 err = NO_ERROR;
518
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700519 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200520 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521 // grow objects
522 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700523 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700524 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800525 binder_size_t *objects =
526 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kongfdd8da92018-06-07 17:52:27 -0700527 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700528 return NO_MEMORY;
529 }
530 mObjects = objects;
531 mObjectsCapacity = newSize;
532 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700533
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700534 // append and acquire objects
535 int idx = mObjectsSize;
536 for (int i = firstIndex; i <= lastIndex; i++) {
537 size_t off = objects[i] - offset + startPos;
538 mObjects[idx++] = off;
539 mObjectsSize++;
540
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700541 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700542 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700543 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700544
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700545 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700546 // If this is a file descriptor, we need to dup it so the
547 // new Parcel now owns its own fd, and can declare that we
548 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800549 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800550 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700551 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400552 if (!mAllowFds) {
553 err = FDS_NOT_ALLOWED;
554 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700555 }
556 }
557 }
558
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400559 return err;
560}
561
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700562int Parcel::compareData(const Parcel& other) {
563 size_t size = dataSize();
564 if (size != other.dataSize()) {
565 return size < other.dataSize() ? -1 : 1;
566 }
567 return memcmp(data(), other.data(), size);
568}
569
Jeff Brown13b16042014-11-11 16:44:25 -0800570bool Parcel::allowFds() const
571{
572 return mAllowFds;
573}
574
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700575bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400576{
577 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700578 if (!allowFds) {
579 mAllowFds = false;
580 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400581 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700582}
583
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700584void Parcel::restoreAllowFds(bool lastValue)
585{
586 mAllowFds = lastValue;
587}
588
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700589bool Parcel::hasFileDescriptors() const
590{
591 if (!mFdsKnown) {
592 scanForFds();
593 }
594 return mHasFds;
595}
596
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700597// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700598status_t Parcel::writeInterfaceToken(const String16& interface)
599{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700600 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
601 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700602 // currently the interface identification token is just its name as a string
603 return writeString16(interface);
604}
605
Mathias Agopian83c04462009-05-22 19:00:22 -0700606bool Parcel::checkInterface(IBinder* binder) const
607{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700608 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700609}
610
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700611bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700612 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700613{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700614 int32_t strictPolicy = readInt32();
Yi Kongfdd8da92018-06-07 17:52:27 -0700615 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700616 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700617 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700618 if ((threadState->getLastTransactionBinderFlags() &
619 IBinder::FLAG_ONEWAY) != 0) {
620 // For one-way calls, the callee is running entirely
621 // disconnected from the caller, so disable StrictMode entirely.
622 // Not only does disk/network usage not impact the caller, but
623 // there's no way to commuicate back any violations anyway.
624 threadState->setStrictModePolicy(0);
625 } else {
626 threadState->setStrictModePolicy(strictPolicy);
627 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700628 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700629 if (str == interface) {
630 return true;
631 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700632 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700633 String8(interface).string(), String8(str).string());
634 return false;
635 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700636}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700637
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800638const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700639{
640 return mObjects;
641}
642
643size_t Parcel::objectsCount() const
644{
645 return mObjectsSize;
646}
647
648status_t Parcel::errorCheck() const
649{
650 return mError;
651}
652
653void Parcel::setError(status_t err)
654{
655 mError = err;
656}
657
658status_t Parcel::finishWrite(size_t len)
659{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700660 if (len > INT32_MAX) {
661 // don't accept size_t values which may have come from an
662 // inadvertent conversion from a negative int.
663 return BAD_VALUE;
664 }
665
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700666 //printf("Finish write of %d\n", len);
667 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700668 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700669 if (mDataPos > mDataSize) {
670 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700671 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700672 }
673 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
674 return NO_ERROR;
675}
676
677status_t Parcel::writeUnpadded(const void* data, size_t len)
678{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700679 if (len > INT32_MAX) {
680 // don't accept size_t values which may have come from an
681 // inadvertent conversion from a negative int.
682 return BAD_VALUE;
683 }
684
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700685 size_t end = mDataPos + len;
686 if (end < mDataPos) {
687 // integer overflow
688 return BAD_VALUE;
689 }
690
691 if (end <= mDataCapacity) {
692restart_write:
693 memcpy(mData+mDataPos, data, len);
694 return finishWrite(len);
695 }
696
697 status_t err = growData(len);
698 if (err == NO_ERROR) goto restart_write;
699 return err;
700}
701
702status_t Parcel::write(const void* data, size_t len)
703{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700704 if (len > INT32_MAX) {
705 // don't accept size_t values which may have come from an
706 // inadvertent conversion from a negative int.
707 return BAD_VALUE;
708 }
709
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700710 void* const d = writeInplace(len);
711 if (d) {
712 memcpy(d, data, len);
713 return NO_ERROR;
714 }
715 return mError;
716}
717
718void* Parcel::writeInplace(size_t len)
719{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700720 if (len > INT32_MAX) {
721 // don't accept size_t values which may have come from an
722 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -0700723 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700724 }
725
726 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700727
728 // sanity check for integer overflow
729 if (mDataPos+padded < mDataPos) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700730 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700731 }
732
733 if ((mDataPos+padded) <= mDataCapacity) {
734restart_write:
735 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
736 uint8_t* const data = mData+mDataPos;
737
738 // Need to pad at end?
739 if (padded != len) {
740#if BYTE_ORDER == BIG_ENDIAN
741 static const uint32_t mask[4] = {
742 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
743 };
744#endif
745#if BYTE_ORDER == LITTLE_ENDIAN
746 static const uint32_t mask[4] = {
747 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
748 };
749#endif
750 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
751 // *reinterpret_cast<void**>(data+padded-4));
752 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
753 }
754
755 finishWrite(padded);
756 return data;
757 }
758
759 status_t err = growData(padded);
760 if (err == NO_ERROR) goto restart_write;
Yi Kongfdd8da92018-06-07 17:52:27 -0700761 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700762}
763
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800764status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
765 const uint8_t* strData = (uint8_t*)str.data();
766 const size_t strLen= str.length();
767 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100768 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800769 return BAD_VALUE;
770 }
771
772 status_t err = writeInt32(utf16Len);
773 if (err) {
774 return err;
775 }
776
777 // Allocate enough bytes to hold our converted string and its terminating NULL.
778 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
779 if (!dst) {
780 return NO_MEMORY;
781 }
782
Sergio Girof4607432016-07-21 14:46:35 +0100783 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800784
785 return NO_ERROR;
786}
787
788status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
789 if (!str) {
790 return writeInt32(-1);
791 }
792 return writeUtf8AsUtf16(*str);
793}
794
Casey Dahlin185d3442016-02-09 11:08:35 -0800795namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800796
Casey Dahlin185d3442016-02-09 11:08:35 -0800797template<typename T>
798status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700799{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700800 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700801 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700802 status = BAD_VALUE;
803 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700804 }
805
Casey Dahlin185d3442016-02-09 11:08:35 -0800806 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700807 if (status != OK) {
808 return status;
809 }
810
Casey Dahlin185d3442016-02-09 11:08:35 -0800811 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700812 if (!data) {
813 status = BAD_VALUE;
814 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700815 }
816
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700817 memcpy(data, val.data(), val.size());
818 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700819}
820
Casey Dahlin185d3442016-02-09 11:08:35 -0800821template<typename T>
822status_t writeByteVectorInternalPtr(Parcel* parcel,
823 const std::unique_ptr<std::vector<T>>& val)
824{
825 if (!val) {
826 return parcel->writeInt32(-1);
827 }
828
829 return writeByteVectorInternal(parcel, *val);
830}
831
832} // namespace
833
834status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
835 return writeByteVectorInternal(this, val);
836}
837
838status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
839{
840 return writeByteVectorInternalPtr(this, val);
841}
842
843status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
844 return writeByteVectorInternal(this, val);
845}
846
847status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
848{
849 return writeByteVectorInternalPtr(this, val);
850}
851
Casey Dahlin451ff582015-10-19 18:12:18 -0700852status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
853{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800854 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700855}
856
Casey Dahlinb9872622015-11-25 15:09:45 -0800857status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
858{
859 return writeNullableTypedVector(val, &Parcel::writeInt32);
860}
861
Casey Dahlin451ff582015-10-19 18:12:18 -0700862status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
863{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800864 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700865}
866
Casey Dahlinb9872622015-11-25 15:09:45 -0800867status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
868{
869 return writeNullableTypedVector(val, &Parcel::writeInt64);
870}
871
Casey Dahlin451ff582015-10-19 18:12:18 -0700872status_t Parcel::writeFloatVector(const std::vector<float>& val)
873{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800874 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700875}
876
Casey Dahlinb9872622015-11-25 15:09:45 -0800877status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
878{
879 return writeNullableTypedVector(val, &Parcel::writeFloat);
880}
881
Casey Dahlin451ff582015-10-19 18:12:18 -0700882status_t Parcel::writeDoubleVector(const std::vector<double>& val)
883{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800884 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700885}
886
Casey Dahlinb9872622015-11-25 15:09:45 -0800887status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
888{
889 return writeNullableTypedVector(val, &Parcel::writeDouble);
890}
891
Casey Dahlin451ff582015-10-19 18:12:18 -0700892status_t Parcel::writeBoolVector(const std::vector<bool>& val)
893{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800894 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700895}
896
Casey Dahlinb9872622015-11-25 15:09:45 -0800897status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
898{
899 return writeNullableTypedVector(val, &Parcel::writeBool);
900}
901
Casey Dahlin451ff582015-10-19 18:12:18 -0700902status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
903{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800904 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700905}
906
Casey Dahlinb9872622015-11-25 15:09:45 -0800907status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
908{
909 return writeNullableTypedVector(val, &Parcel::writeChar);
910}
911
Casey Dahlin451ff582015-10-19 18:12:18 -0700912status_t Parcel::writeString16Vector(const std::vector<String16>& val)
913{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800914 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700915}
916
Casey Dahlinb9872622015-11-25 15:09:45 -0800917status_t Parcel::writeString16Vector(
918 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
919{
920 return writeNullableTypedVector(val, &Parcel::writeString16);
921}
922
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800923status_t Parcel::writeUtf8VectorAsUtf16Vector(
924 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
925 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
926}
927
928status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
929 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
930}
931
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700932status_t Parcel::writeInt32(int32_t val)
933{
Andreas Huber84a6d042009-08-17 13:33:27 -0700934 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700935}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800936
937status_t Parcel::writeUint32(uint32_t val)
938{
939 return writeAligned(val);
940}
941
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700942status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700943 if (len > INT32_MAX) {
944 // don't accept size_t values which may have come from an
945 // inadvertent conversion from a negative int.
946 return BAD_VALUE;
947 }
948
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700949 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700950 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700951 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700952 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700953 if (ret == NO_ERROR) {
954 ret = write(val, len * sizeof(*val));
955 }
956 return ret;
957}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700958status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700959 if (len > INT32_MAX) {
960 // don't accept size_t values which may have come from an
961 // inadvertent conversion from a negative int.
962 return BAD_VALUE;
963 }
964
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700965 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700966 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700967 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700968 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700969 if (ret == NO_ERROR) {
970 ret = write(val, len * sizeof(*val));
971 }
972 return ret;
973}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700974
Casey Dahlind6848f52015-10-15 15:44:59 -0700975status_t Parcel::writeBool(bool val)
976{
977 return writeInt32(int32_t(val));
978}
979
980status_t Parcel::writeChar(char16_t val)
981{
982 return writeInt32(int32_t(val));
983}
984
985status_t Parcel::writeByte(int8_t val)
986{
987 return writeInt32(int32_t(val));
988}
989
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700990status_t Parcel::writeInt64(int64_t val)
991{
Andreas Huber84a6d042009-08-17 13:33:27 -0700992 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700993}
994
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700995status_t Parcel::writeUint64(uint64_t val)
996{
997 return writeAligned(val);
998}
999
Serban Constantinescuf683e012013-11-05 16:53:55 +00001000status_t Parcel::writePointer(uintptr_t val)
1001{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001002 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001003}
1004
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001005status_t Parcel::writeFloat(float val)
1006{
Andreas Huber84a6d042009-08-17 13:33:27 -07001007 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001008}
1009
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001010#if defined(__mips__) && defined(__mips_hard_float)
1011
1012status_t Parcel::writeDouble(double val)
1013{
1014 union {
1015 double d;
1016 unsigned long long ll;
1017 } u;
1018 u.d = val;
1019 return writeAligned(u.ll);
1020}
1021
1022#else
1023
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001024status_t Parcel::writeDouble(double val)
1025{
Andreas Huber84a6d042009-08-17 13:33:27 -07001026 return writeAligned(val);
1027}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001028
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001029#endif
1030
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001031status_t Parcel::writeCString(const char* str)
1032{
1033 return write(str, strlen(str)+1);
1034}
1035
1036status_t Parcel::writeString8(const String8& str)
1037{
1038 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001039 // only write string if its length is more than zero characters,
1040 // as readString8 will only read if the length field is non-zero.
1041 // this is slightly different from how writeString16 works.
1042 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001043 err = write(str.string(), str.bytes()+1);
1044 }
1045 return err;
1046}
1047
Casey Dahlinb9872622015-11-25 15:09:45 -08001048status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1049{
1050 if (!str) {
1051 return writeInt32(-1);
1052 }
1053
1054 return writeString16(*str);
1055}
1056
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001057status_t Parcel::writeString16(const String16& str)
1058{
1059 return writeString16(str.string(), str.size());
1060}
1061
1062status_t Parcel::writeString16(const char16_t* str, size_t len)
1063{
Yi Kongfdd8da92018-06-07 17:52:27 -07001064 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001065
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001066 status_t err = writeInt32(len);
1067 if (err == NO_ERROR) {
1068 len *= sizeof(char16_t);
1069 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1070 if (data) {
1071 memcpy(data, str, len);
1072 *reinterpret_cast<char16_t*>(data+len) = 0;
1073 return NO_ERROR;
1074 }
1075 err = mError;
1076 }
1077 return err;
1078}
1079
1080status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1081{
1082 return flatten_binder(ProcessState::self(), val, this);
1083}
1084
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001085status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1086{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001087 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001088}
1089
Casey Dahlinb9872622015-11-25 15:09:45 -08001090status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1091{
1092 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1093}
1094
1095status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001096 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001097}
1098
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001099status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001100 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001101}
1102
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001103status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1104{
1105 return flatten_binder(ProcessState::self(), val, this);
1106}
1107
Casey Dahlinb9872622015-11-25 15:09:45 -08001108status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1109 if (!parcelable) {
1110 return writeInt32(0);
1111 }
1112
1113 return writeParcelable(*parcelable);
1114}
1115
Christopher Wiley97f048d2015-11-19 06:49:05 -08001116status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1117 status_t status = writeInt32(1); // parcelable is not null.
1118 if (status != OK) {
1119 return status;
1120 }
1121 return parcelable.writeToParcel(this);
1122}
1123
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001124status_t Parcel::writeValue(const binder::Value& value) {
1125 return value.writeToParcel(this);
1126}
1127
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001128status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001129{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001130 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001131 return BAD_TYPE;
1132
1133 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001134 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001135 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001137 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001138 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001140 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1141 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001142
1143 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001144 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145 return err;
1146 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001147 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001148 return err;
1149}
1150
Jeff Brown93ff1f92011-11-04 19:01:44 -07001151status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001152{
1153 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001154 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001155 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001156 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001157 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001158 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001159 return writeObject(obj, true);
1160}
1161
1162status_t Parcel::writeDupFileDescriptor(int fd)
1163{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001164 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001165 if (dupFd < 0) {
1166 return -errno;
1167 }
1168 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001169 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001170 close(dupFd);
1171 }
1172 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001173}
1174
Dianne Hackborn1941a402016-08-29 12:30:43 -07001175status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1176{
1177 writeInt32(0);
1178 return writeFileDescriptor(fd, takeOwnership);
1179}
1180
Ryo Hashimotobf551892018-05-31 16:58:35 +09001181status_t Parcel::writeDupParcelFileDescriptor(int fd)
1182{
1183 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1184 if (dupFd < 0) {
1185 return -errno;
1186 }
1187 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1188 if (err != OK) {
1189 close(dupFd);
1190 }
1191 return err;
1192}
1193
Christopher Wiley2cf19952016-04-11 11:09:37 -07001194status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001195 return writeDupFileDescriptor(fd.get());
1196}
1197
Christopher Wiley2cf19952016-04-11 11:09:37 -07001198status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001199 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1200}
1201
Christopher Wiley2cf19952016-04-11 11:09:37 -07001202status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001203 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1204}
1205
Jeff Brown13b16042014-11-11 16:44:25 -08001206status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001207{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001208 if (len > INT32_MAX) {
1209 // don't accept size_t values which may have come from an
1210 // inadvertent conversion from a negative int.
1211 return BAD_VALUE;
1212 }
1213
Jeff Brown13b16042014-11-11 16:44:25 -08001214 status_t status;
1215 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001216 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001217 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001218 if (status) return status;
1219
1220 void* ptr = writeInplace(len);
1221 if (!ptr) return NO_MEMORY;
1222
Jeff Brown13b16042014-11-11 16:44:25 -08001223 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001224 return NO_ERROR;
1225 }
1226
Steve Block6807e592011-10-20 11:56:00 +01001227 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001228 int fd = ashmem_create_region("Parcel Blob", len);
1229 if (fd < 0) return NO_MEMORY;
1230
1231 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1232 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001233 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001234 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -07001235 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001236 if (ptr == MAP_FAILED) {
1237 status = -errno;
1238 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001239 if (!mutableCopy) {
1240 result = ashmem_set_prot_region(fd, PROT_READ);
1241 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001242 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001243 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001244 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001245 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001246 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001247 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001248 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001249 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001250 return NO_ERROR;
1251 }
1252 }
1253 }
1254 }
1255 ::munmap(ptr, len);
1256 }
1257 ::close(fd);
1258 return status;
1259}
1260
Jeff Brown13b16042014-11-11 16:44:25 -08001261status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1262{
1263 // Must match up with what's done in writeBlob.
1264 if (!mAllowFds) return FDS_NOT_ALLOWED;
1265 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1266 if (status) return status;
1267 return writeDupFileDescriptor(fd);
1268}
1269
Mathias Agopiane1424282013-07-29 21:24:40 -07001270status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001271{
1272 status_t err;
1273
1274 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001275 const size_t len = val.getFlattenedSize();
1276 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001277
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001278 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001279 // don't accept size_t values which may have come from an
1280 // inadvertent conversion from a negative int.
1281 return BAD_VALUE;
1282 }
1283
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001284 err = this->writeInt32(len);
1285 if (err) return err;
1286
1287 err = this->writeInt32(fd_count);
1288 if (err) return err;
1289
1290 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001291 void* const buf = this->writeInplace(len);
Yi Kongfdd8da92018-06-07 17:52:27 -07001292 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001293 return BAD_VALUE;
1294
Yi Kongfdd8da92018-06-07 17:52:27 -07001295 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001296 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001297 fds = new (std::nothrow) int[fd_count];
1298 if (fds == nullptr) {
1299 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1300 return BAD_VALUE;
1301 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001302 }
1303
1304 err = val.flatten(buf, len, fds, fd_count);
1305 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1306 err = this->writeDupFileDescriptor( fds[i] );
1307 }
1308
1309 if (fd_count) {
1310 delete [] fds;
1311 }
1312
1313 return err;
1314}
1315
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001316status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1317{
1318 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1319 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1320 if (enoughData && enoughObjects) {
1321restart_write:
1322 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001323
Christopher Tate98e67d32015-06-03 18:44:15 -07001324 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001325 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001326 if (!mAllowFds) {
1327 // fail before modifying our object index
1328 return FDS_NOT_ALLOWED;
1329 }
1330 mHasFds = mFdsKnown = true;
1331 }
1332
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001333 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001334 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001335 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001336 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001337 mObjectsSize++;
1338 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001339
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001340 return finishWrite(sizeof(flat_binder_object));
1341 }
1342
1343 if (!enoughData) {
1344 const status_t err = growData(sizeof(val));
1345 if (err != NO_ERROR) return err;
1346 }
1347 if (!enoughObjects) {
1348 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001349 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001350 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07001351 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001352 mObjects = objects;
1353 mObjectsCapacity = newSize;
1354 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001355
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001356 goto restart_write;
1357}
1358
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001359status_t Parcel::writeNoException()
1360{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001361 binder::Status status;
1362 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001363}
1364
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001365status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1366{
1367 using ::std::map;
1368 using ::android::binder::Value;
1369 using ::android::binder::Map;
1370
1371 Map::const_iterator iter;
1372 status_t ret;
1373
1374 ret = writeInt32(map_in.size());
1375
1376 if (ret != NO_ERROR) {
1377 return ret;
1378 }
1379
1380 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1381 ret = writeValue(Value(iter->first));
1382 if (ret != NO_ERROR) {
1383 return ret;
1384 }
1385
1386 ret = writeValue(iter->second);
1387 if (ret != NO_ERROR) {
1388 return ret;
1389 }
1390 }
1391
1392 return ret;
1393}
1394
1395status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1396{
Yi Kongfdd8da92018-06-07 17:52:27 -07001397 if (map == nullptr) {
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001398 return writeInt32(-1);
1399 }
1400
1401 return writeMap(*map.get());
1402}
1403
1404status_t Parcel::readMap(::android::binder::Map* map_out)const
1405{
1406 using ::std::map;
1407 using ::android::String16;
1408 using ::android::String8;
1409 using ::android::binder::Value;
1410 using ::android::binder::Map;
1411
1412 status_t ret = NO_ERROR;
1413 int32_t count;
1414
1415 ret = readInt32(&count);
1416 if (ret != NO_ERROR) {
1417 return ret;
1418 }
1419
1420 if (count < 0) {
1421 ALOGE("readMap: Unexpected count: %d", count);
1422 return (count == -1)
1423 ? UNEXPECTED_NULL
1424 : BAD_VALUE;
1425 }
1426
1427 map_out->clear();
1428
1429 while (count--) {
1430 Map::key_type key;
1431 Value value;
1432
1433 ret = readValue(&value);
1434 if (ret != NO_ERROR) {
1435 return ret;
1436 }
1437
1438 if (!value.getString(&key)) {
1439 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1440 return BAD_VALUE;
1441 }
1442
1443 ret = readValue(&value);
1444 if (ret != NO_ERROR) {
1445 return ret;
1446 }
1447
1448 (*map_out)[key] = value;
1449 }
1450
1451 return ret;
1452}
1453
1454status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1455{
1456 const size_t start = dataPosition();
1457 int32_t count;
1458 status_t status = readInt32(&count);
1459 map->reset();
1460
1461 if (status != OK || count == -1) {
1462 return status;
1463 }
1464
1465 setDataPosition(start);
1466 map->reset(new binder::Map());
1467
1468 status = readMap(map->get());
1469
1470 if (status != OK) {
1471 map->reset();
1472 }
1473
1474 return status;
1475}
1476
1477
1478
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001479void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001480{
1481 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1482}
1483
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001484status_t Parcel::validateReadData(size_t upperBound) const
1485{
1486 // Don't allow non-object reads on object data
1487 if (mObjectsSorted || mObjectsSize <= 1) {
1488data_sorted:
1489 // Expect to check only against the next object
1490 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1491 // For some reason the current read position is greater than the next object
1492 // hint. Iterate until we find the right object
1493 size_t nextObject = mNextObjectHint;
1494 do {
1495 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1496 // Requested info overlaps with an object
1497 ALOGE("Attempt to read from protected data in Parcel %p", this);
1498 return PERMISSION_DENIED;
1499 }
1500 nextObject++;
1501 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1502 mNextObjectHint = nextObject;
1503 }
1504 return NO_ERROR;
1505 }
1506 // Quickly determine if mObjects is sorted.
1507 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1508 binder_size_t* prevObj = currObj;
1509 while (currObj > mObjects) {
1510 prevObj--;
1511 if(*prevObj > *currObj) {
1512 goto data_unsorted;
1513 }
1514 currObj--;
1515 }
1516 mObjectsSorted = true;
1517 goto data_sorted;
1518
1519data_unsorted:
1520 // Insertion Sort mObjects
1521 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1522 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1523 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1524 binder_size_t temp = *iter0;
1525 binder_size_t* iter1 = iter0 - 1;
1526 while (iter1 >= mObjects && *iter1 > temp) {
1527 *(iter1 + 1) = *iter1;
1528 iter1--;
1529 }
1530 *(iter1 + 1) = temp;
1531 }
1532 mNextObjectHint = 0;
1533 mObjectsSorted = true;
1534 goto data_sorted;
1535}
1536
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001537status_t Parcel::read(void* outData, size_t len) const
1538{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001539 if (len > INT32_MAX) {
1540 // don't accept size_t values which may have come from an
1541 // inadvertent conversion from a negative int.
1542 return BAD_VALUE;
1543 }
1544
1545 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1546 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001547 if (mObjectsSize > 0) {
1548 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001549 if(err != NO_ERROR) {
1550 // Still increment the data position by the expected length
1551 mDataPos += pad_size(len);
1552 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1553 return err;
1554 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001555 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001556 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001557 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001558 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001559 return NO_ERROR;
1560 }
1561 return NOT_ENOUGH_DATA;
1562}
1563
1564const void* Parcel::readInplace(size_t len) const
1565{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001566 if (len > INT32_MAX) {
1567 // don't accept size_t values which may have come from an
1568 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -07001569 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001570 }
1571
1572 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1573 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001574 if (mObjectsSize > 0) {
1575 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001576 if(err != NO_ERROR) {
1577 // Still increment the data position by the expected length
1578 mDataPos += pad_size(len);
1579 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Xin Lif11e2bd2018-06-08 15:11:57 -07001580 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001581 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001582 }
1583
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001584 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001585 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001586 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001587 return data;
1588 }
Yi Kongfdd8da92018-06-07 17:52:27 -07001589 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001590}
1591
Andreas Huber84a6d042009-08-17 13:33:27 -07001592template<class T>
1593status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001594 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001595
1596 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001597 if (mObjectsSize > 0) {
1598 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001599 if(err != NO_ERROR) {
1600 // Still increment the data position by the expected length
1601 mDataPos += sizeof(T);
1602 return err;
1603 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001604 }
1605
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001606 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001607 mDataPos += sizeof(T);
1608 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001609 return NO_ERROR;
1610 } else {
1611 return NOT_ENOUGH_DATA;
1612 }
1613}
1614
Andreas Huber84a6d042009-08-17 13:33:27 -07001615template<class T>
1616T Parcel::readAligned() const {
1617 T result;
1618 if (readAligned(&result) != NO_ERROR) {
1619 result = 0;
1620 }
1621
1622 return result;
1623}
1624
1625template<class T>
1626status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001627 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001628
1629 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1630restart_write:
1631 *reinterpret_cast<T*>(mData+mDataPos) = val;
1632 return finishWrite(sizeof(val));
1633 }
1634
1635 status_t err = growData(sizeof(val));
1636 if (err == NO_ERROR) goto restart_write;
1637 return err;
1638}
1639
Casey Dahlin185d3442016-02-09 11:08:35 -08001640namespace {
1641
1642template<typename T>
1643status_t readByteVectorInternal(const Parcel* parcel,
1644 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001645 val->clear();
1646
1647 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001648 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001649
1650 if (status != OK) {
1651 return status;
1652 }
1653
Christopher Wiley4db672d2015-11-10 09:44:30 -08001654 if (size < 0) {
1655 status = UNEXPECTED_NULL;
1656 return status;
1657 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001658 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001659 status = BAD_VALUE;
1660 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001661 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001662
Paul Lietar433e87b2016-09-16 10:39:32 -07001663 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001664 if (!data) {
1665 status = BAD_VALUE;
1666 return status;
1667 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001668 val->reserve(size);
1669 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001670
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001671 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001672}
1673
Casey Dahlin185d3442016-02-09 11:08:35 -08001674template<typename T>
1675status_t readByteVectorInternalPtr(
1676 const Parcel* parcel,
1677 std::unique_ptr<std::vector<T>>* val) {
1678 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001679 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001680 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001681 val->reset();
1682
1683 if (status != OK || size < 0) {
1684 return status;
1685 }
1686
Casey Dahlin185d3442016-02-09 11:08:35 -08001687 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001688 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001689
Casey Dahlin185d3442016-02-09 11:08:35 -08001690 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001691
1692 if (status != OK) {
1693 val->reset();
1694 }
1695
1696 return status;
1697}
1698
Casey Dahlin185d3442016-02-09 11:08:35 -08001699} // namespace
1700
1701status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1702 return readByteVectorInternal(this, val);
1703}
1704
1705status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1706 return readByteVectorInternal(this, val);
1707}
1708
1709status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1710 return readByteVectorInternalPtr(this, val);
1711}
1712
1713status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1714 return readByteVectorInternalPtr(this, val);
1715}
1716
Casey Dahlinb9872622015-11-25 15:09:45 -08001717status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1718 return readNullableTypedVector(val, &Parcel::readInt32);
1719}
1720
Casey Dahlin451ff582015-10-19 18:12:18 -07001721status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001722 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001723}
1724
Casey Dahlinb9872622015-11-25 15:09:45 -08001725status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1726 return readNullableTypedVector(val, &Parcel::readInt64);
1727}
1728
Casey Dahlin451ff582015-10-19 18:12:18 -07001729status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001730 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001731}
1732
Casey Dahlinb9872622015-11-25 15:09:45 -08001733status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1734 return readNullableTypedVector(val, &Parcel::readFloat);
1735}
1736
Casey Dahlin451ff582015-10-19 18:12:18 -07001737status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001738 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001739}
1740
Casey Dahlinb9872622015-11-25 15:09:45 -08001741status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1742 return readNullableTypedVector(val, &Parcel::readDouble);
1743}
1744
Casey Dahlin451ff582015-10-19 18:12:18 -07001745status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001746 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001747}
1748
Casey Dahlinb9872622015-11-25 15:09:45 -08001749status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1750 const int32_t start = dataPosition();
1751 int32_t size;
1752 status_t status = readInt32(&size);
1753 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001754
Casey Dahlinb9872622015-11-25 15:09:45 -08001755 if (status != OK || size < 0) {
1756 return status;
1757 }
1758
1759 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001760 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001761
1762 status = readBoolVector(val->get());
1763
1764 if (status != OK) {
1765 val->reset();
1766 }
1767
1768 return status;
1769}
1770
1771status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001772 int32_t size;
1773 status_t status = readInt32(&size);
1774
1775 if (status != OK) {
1776 return status;
1777 }
1778
1779 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001780 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001781 }
1782
1783 val->resize(size);
1784
1785 /* C++ bool handling means a vector of bools isn't necessarily addressable
1786 * (we might use individual bits)
1787 */
Christopher Wiley97887982015-10-27 16:33:47 -07001788 bool data;
1789 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001790 status = readBool(&data);
1791 (*val)[i] = data;
1792
1793 if (status != OK) {
1794 return status;
1795 }
1796 }
1797
1798 return OK;
1799}
1800
Casey Dahlinb9872622015-11-25 15:09:45 -08001801status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1802 return readNullableTypedVector(val, &Parcel::readChar);
1803}
1804
Casey Dahlin451ff582015-10-19 18:12:18 -07001805status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001806 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001807}
1808
Casey Dahlinb9872622015-11-25 15:09:45 -08001809status_t Parcel::readString16Vector(
1810 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1811 return readNullableTypedVector(val, &Parcel::readString16);
1812}
1813
Casey Dahlin451ff582015-10-19 18:12:18 -07001814status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001815 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001816}
1817
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001818status_t Parcel::readUtf8VectorFromUtf16Vector(
1819 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1820 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1821}
1822
1823status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1824 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1825}
Casey Dahlin451ff582015-10-19 18:12:18 -07001826
Andreas Huber84a6d042009-08-17 13:33:27 -07001827status_t Parcel::readInt32(int32_t *pArg) const
1828{
1829 return readAligned(pArg);
1830}
1831
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001832int32_t Parcel::readInt32() const
1833{
Andreas Huber84a6d042009-08-17 13:33:27 -07001834 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001835}
1836
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001837status_t Parcel::readUint32(uint32_t *pArg) const
1838{
1839 return readAligned(pArg);
1840}
1841
1842uint32_t Parcel::readUint32() const
1843{
1844 return readAligned<uint32_t>();
1845}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001846
1847status_t Parcel::readInt64(int64_t *pArg) const
1848{
Andreas Huber84a6d042009-08-17 13:33:27 -07001849 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001850}
1851
1852
1853int64_t Parcel::readInt64() const
1854{
Andreas Huber84a6d042009-08-17 13:33:27 -07001855 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001856}
1857
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001858status_t Parcel::readUint64(uint64_t *pArg) const
1859{
1860 return readAligned(pArg);
1861}
1862
1863uint64_t Parcel::readUint64() const
1864{
1865 return readAligned<uint64_t>();
1866}
1867
Serban Constantinescuf683e012013-11-05 16:53:55 +00001868status_t Parcel::readPointer(uintptr_t *pArg) const
1869{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001870 status_t ret;
1871 binder_uintptr_t ptr;
1872 ret = readAligned(&ptr);
1873 if (!ret)
1874 *pArg = ptr;
1875 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001876}
1877
1878uintptr_t Parcel::readPointer() const
1879{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001880 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001881}
1882
1883
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001884status_t Parcel::readFloat(float *pArg) const
1885{
Andreas Huber84a6d042009-08-17 13:33:27 -07001886 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001887}
1888
1889
1890float Parcel::readFloat() const
1891{
Andreas Huber84a6d042009-08-17 13:33:27 -07001892 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001893}
1894
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001895#if defined(__mips__) && defined(__mips_hard_float)
1896
1897status_t Parcel::readDouble(double *pArg) const
1898{
1899 union {
1900 double d;
1901 unsigned long long ll;
1902 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001903 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001904 status_t status;
1905 status = readAligned(&u.ll);
1906 *pArg = u.d;
1907 return status;
1908}
1909
1910double Parcel::readDouble() const
1911{
1912 union {
1913 double d;
1914 unsigned long long ll;
1915 } u;
1916 u.ll = readAligned<unsigned long long>();
1917 return u.d;
1918}
1919
1920#else
1921
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001922status_t Parcel::readDouble(double *pArg) const
1923{
Andreas Huber84a6d042009-08-17 13:33:27 -07001924 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001925}
1926
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001927double Parcel::readDouble() const
1928{
Andreas Huber84a6d042009-08-17 13:33:27 -07001929 return readAligned<double>();
1930}
1931
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001932#endif
1933
Andreas Huber84a6d042009-08-17 13:33:27 -07001934status_t Parcel::readIntPtr(intptr_t *pArg) const
1935{
1936 return readAligned(pArg);
1937}
1938
1939
1940intptr_t Parcel::readIntPtr() const
1941{
1942 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001943}
1944
Casey Dahlind6848f52015-10-15 15:44:59 -07001945status_t Parcel::readBool(bool *pArg) const
1946{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001947 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001948 status_t ret = readInt32(&tmp);
1949 *pArg = (tmp != 0);
1950 return ret;
1951}
1952
1953bool Parcel::readBool() const
1954{
1955 return readInt32() != 0;
1956}
1957
1958status_t Parcel::readChar(char16_t *pArg) const
1959{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001960 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001961 status_t ret = readInt32(&tmp);
1962 *pArg = char16_t(tmp);
1963 return ret;
1964}
1965
1966char16_t Parcel::readChar() const
1967{
1968 return char16_t(readInt32());
1969}
1970
1971status_t Parcel::readByte(int8_t *pArg) const
1972{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001973 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001974 status_t ret = readInt32(&tmp);
1975 *pArg = int8_t(tmp);
1976 return ret;
1977}
1978
1979int8_t Parcel::readByte() const
1980{
1981 return int8_t(readInt32());
1982}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001983
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001984status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1985 size_t utf16Size = 0;
1986 const char16_t* src = readString16Inplace(&utf16Size);
1987 if (!src) {
1988 return UNEXPECTED_NULL;
1989 }
1990
1991 // Save ourselves the trouble, we're done.
1992 if (utf16Size == 0u) {
1993 str->clear();
1994 return NO_ERROR;
1995 }
1996
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001997 // Allow for closing '\0'
1998 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1999 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002000 return BAD_VALUE;
2001 }
2002 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002003 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002004 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002005 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2006 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002007 return NO_ERROR;
2008}
2009
2010status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2011 const int32_t start = dataPosition();
2012 int32_t size;
2013 status_t status = readInt32(&size);
2014 str->reset();
2015
2016 if (status != OK || size < 0) {
2017 return status;
2018 }
2019
2020 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002021 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002022 return readUtf8FromUtf16(str->get());
2023}
2024
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002025const char* Parcel::readCString() const
2026{
2027 const size_t avail = mDataSize-mDataPos;
2028 if (avail > 0) {
2029 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2030 // is the string's trailing NUL within the parcel's valid bounds?
2031 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2032 if (eos) {
2033 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002034 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002035 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002036 return str;
2037 }
2038 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002039 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002040}
2041
2042String8 Parcel::readString8() const
2043{
Roshan Pius87b64d22016-07-18 12:51:02 -07002044 String8 retString;
2045 status_t status = readString8(&retString);
2046 if (status != OK) {
2047 // We don't care about errors here, so just return an empty string.
2048 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002049 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002050 return retString;
2051}
2052
2053status_t Parcel::readString8(String8* pArg) const
2054{
2055 int32_t size;
2056 status_t status = readInt32(&size);
2057 if (status != OK) {
2058 return status;
2059 }
2060 // watch for potential int overflow from size+1
2061 if (size < 0 || size >= INT32_MAX) {
2062 return BAD_VALUE;
2063 }
2064 // |writeString8| writes nothing for empty string.
2065 if (size == 0) {
2066 *pArg = String8();
2067 return OK;
2068 }
2069 const char* str = (const char*)readInplace(size + 1);
Yi Kongfdd8da92018-06-07 17:52:27 -07002070 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002071 return BAD_VALUE;
2072 }
2073 pArg->setTo(str, size);
2074 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002075}
2076
2077String16 Parcel::readString16() const
2078{
2079 size_t len;
2080 const char16_t* str = readString16Inplace(&len);
2081 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002082 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002083 return String16();
2084}
2085
Casey Dahlinb9872622015-11-25 15:09:45 -08002086status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2087{
2088 const int32_t start = dataPosition();
2089 int32_t size;
2090 status_t status = readInt32(&size);
2091 pArg->reset();
2092
2093 if (status != OK || size < 0) {
2094 return status;
2095 }
2096
2097 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002098 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002099
2100 status = readString16(pArg->get());
2101
2102 if (status != OK) {
2103 pArg->reset();
2104 }
2105
2106 return status;
2107}
2108
Casey Dahlin451ff582015-10-19 18:12:18 -07002109status_t Parcel::readString16(String16* pArg) const
2110{
2111 size_t len;
2112 const char16_t* str = readString16Inplace(&len);
2113 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002114 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002115 return 0;
2116 } else {
2117 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002118 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002119 }
2120}
2121
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002122const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2123{
2124 int32_t size = readInt32();
2125 // watch for potential int overflow from size+1
2126 if (size >= 0 && size < INT32_MAX) {
2127 *outLen = size;
2128 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07002129 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002130 return str;
2131 }
2132 }
2133 *outLen = 0;
Yi Kongfdd8da92018-06-07 17:52:27 -07002134 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002135}
2136
Casey Dahlinf0c13772015-10-27 18:33:56 -07002137status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2138{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002139 status_t status = readNullableStrongBinder(val);
2140 if (status == OK && !val->get()) {
2141 status = UNEXPECTED_NULL;
2142 }
2143 return status;
2144}
2145
2146status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2147{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002148 return unflatten_binder(ProcessState::self(), *this, val);
2149}
2150
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002151sp<IBinder> Parcel::readStrongBinder() const
2152{
2153 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002154 // Note that a lot of code in Android reads binders by hand with this
2155 // method, and that code has historically been ok with getting nullptr
2156 // back (while ignoring error codes).
2157 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002158 return val;
2159}
2160
2161wp<IBinder> Parcel::readWeakBinder() const
2162{
2163 wp<IBinder> val;
2164 unflatten_binder(ProcessState::self(), *this, &val);
2165 return val;
2166}
2167
Christopher Wiley97f048d2015-11-19 06:49:05 -08002168status_t Parcel::readParcelable(Parcelable* parcelable) const {
2169 int32_t have_parcelable = 0;
2170 status_t status = readInt32(&have_parcelable);
2171 if (status != OK) {
2172 return status;
2173 }
2174 if (!have_parcelable) {
2175 return UNEXPECTED_NULL;
2176 }
2177 return parcelable->readFromParcel(this);
2178}
2179
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002180status_t Parcel::readValue(binder::Value* value) const {
2181 return value->readFromParcel(this);
2182}
2183
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002184int32_t Parcel::readExceptionCode() const
2185{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002186 binder::Status status;
2187 status.readFromParcel(*this);
2188 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002189}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002190
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002191native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002192{
2193 int numFds, numInts;
2194 status_t err;
2195 err = readInt32(&numFds);
Yi Kongfdd8da92018-06-07 17:52:27 -07002196 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002197 err = readInt32(&numInts);
Yi Kongfdd8da92018-06-07 17:52:27 -07002198 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002199
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002200 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002201 if (!h) {
Yi Kongfdd8da92018-06-07 17:52:27 -07002202 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002203 }
2204
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002205 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002206 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002207 if (h->data[i] < 0) {
2208 for (int j = 0; j < i; j++) {
2209 close(h->data[j]);
2210 }
2211 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07002212 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002213 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002214 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002215 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002216 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002217 native_handle_close(h);
2218 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07002219 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002220 }
2221 return h;
2222}
2223
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002224int Parcel::readFileDescriptor() const
2225{
2226 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002227
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002228 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002229 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002230 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002231
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002232 return BAD_TYPE;
2233}
2234
Dianne Hackborn1941a402016-08-29 12:30:43 -07002235int Parcel::readParcelFileDescriptor() const
2236{
2237 int32_t hasComm = readInt32();
2238 int fd = readFileDescriptor();
2239 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002240 // detach (owned by the binder driver)
2241 int comm = readFileDescriptor();
2242
2243 // warning: this must be kept in sync with:
2244 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2245 enum ParcelFileDescriptorStatus {
2246 DETACHED = 2,
2247 };
2248
2249#if BYTE_ORDER == BIG_ENDIAN
2250 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2251#endif
2252#if BYTE_ORDER == LITTLE_ENDIAN
2253 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2254#endif
2255
2256 ssize_t written = TEMP_FAILURE_RETRY(
2257 ::write(comm, &message, sizeof(message)));
2258
2259 if (written == -1 || written != sizeof(message)) {
2260 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2261 written, strerror(errno));
2262 return BAD_TYPE;
2263 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002264 }
2265 return fd;
2266}
2267
Christopher Wiley2cf19952016-04-11 11:09:37 -07002268status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002269{
2270 int got = readFileDescriptor();
2271
2272 if (got == BAD_TYPE) {
2273 return BAD_TYPE;
2274 }
2275
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002276 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002277
2278 if (val->get() < 0) {
2279 return BAD_VALUE;
2280 }
2281
2282 return OK;
2283}
2284
Ryo Hashimotobf551892018-05-31 16:58:35 +09002285status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2286{
2287 int got = readParcelFileDescriptor();
2288
2289 if (got == BAD_TYPE) {
2290 return BAD_TYPE;
2291 }
2292
2293 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2294
2295 if (val->get() < 0) {
2296 return BAD_VALUE;
2297 }
2298
2299 return OK;
2300}
Casey Dahlin06673e32015-11-23 13:24:23 -08002301
Christopher Wiley2cf19952016-04-11 11:09:37 -07002302status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002303 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2304}
2305
Christopher Wiley2cf19952016-04-11 11:09:37 -07002306status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002307 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2308}
2309
Jeff Brown5707dbf2011-09-23 21:17:56 -07002310status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2311{
Jeff Brown13b16042014-11-11 16:44:25 -08002312 int32_t blobType;
2313 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002314 if (status) return status;
2315
Jeff Brown13b16042014-11-11 16:44:25 -08002316 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002317 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002318 const void* ptr = readInplace(len);
2319 if (!ptr) return BAD_VALUE;
2320
Jeff Brown13b16042014-11-11 16:44:25 -08002321 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002322 return NO_ERROR;
2323 }
2324
Steve Block6807e592011-10-20 11:56:00 +01002325 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002326 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002327 int fd = readFileDescriptor();
2328 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2329
Yi Kongfdd8da92018-06-07 17:52:27 -07002330 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002331 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002332 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002333
Jeff Brown13b16042014-11-11 16:44:25 -08002334 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002335 return NO_ERROR;
2336}
2337
Mathias Agopiane1424282013-07-29 21:24:40 -07002338status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002339{
2340 // size
2341 const size_t len = this->readInt32();
2342 const size_t fd_count = this->readInt32();
2343
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002344 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002345 // don't accept size_t values which may have come from an
2346 // inadvertent conversion from a negative int.
2347 return BAD_VALUE;
2348 }
2349
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002350 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002351 void const* const buf = this->readInplace(pad_size(len));
Yi Kongfdd8da92018-06-07 17:52:27 -07002352 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002353 return BAD_VALUE;
2354
Yi Kongfdd8da92018-06-07 17:52:27 -07002355 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002356 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002357 fds = new (std::nothrow) int[fd_count];
2358 if (fds == nullptr) {
2359 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2360 return BAD_VALUE;
2361 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002362 }
2363
2364 status_t err = NO_ERROR;
2365 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002366 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002367 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002368 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002369 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 -07002370 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2371 // Close all the file descriptors that were dup-ed.
2372 for (size_t j=0; j<i ;j++) {
2373 close(fds[j]);
2374 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002375 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002376 }
2377
2378 if (err == NO_ERROR) {
2379 err = val.unflatten(buf, len, fds, fd_count);
2380 }
2381
2382 if (fd_count) {
2383 delete [] fds;
2384 }
2385
2386 return err;
2387}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002388const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2389{
2390 const size_t DPOS = mDataPos;
2391 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2392 const flat_binder_object* obj
2393 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2394 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002395 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002396 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002397 // the object list, so we don't want to check for it when
2398 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002399 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002400 return obj;
2401 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002402
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002403 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002404 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002405 const size_t N = mObjectsSize;
2406 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002407
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002408 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002409 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002410 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002411
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002412 // Start at the current hint position, looking for an object at
2413 // the current data position.
2414 if (opos < N) {
2415 while (opos < (N-1) && OBJS[opos] < DPOS) {
2416 opos++;
2417 }
2418 } else {
2419 opos = N-1;
2420 }
2421 if (OBJS[opos] == DPOS) {
2422 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002423 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002424 this, DPOS, opos);
2425 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002426 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427 return obj;
2428 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002429
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002430 // Look backwards for it...
2431 while (opos > 0 && OBJS[opos] > DPOS) {
2432 opos--;
2433 }
2434 if (OBJS[opos] == DPOS) {
2435 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002436 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002437 this, DPOS, opos);
2438 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002439 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002440 return obj;
2441 }
2442 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002443 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 -07002444 this, DPOS);
2445 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002446 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002447}
2448
2449void Parcel::closeFileDescriptors()
2450{
2451 size_t i = mObjectsSize;
2452 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002453 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002454 }
2455 while (i > 0) {
2456 i--;
2457 const flat_binder_object* flat
2458 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002459 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002460 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002461 close(flat->handle);
2462 }
2463 }
2464}
2465
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002466uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002467{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002468 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002469}
2470
2471size_t Parcel::ipcDataSize() const
2472{
2473 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2474}
2475
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002476uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002477{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002478 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479}
2480
2481size_t Parcel::ipcObjectsCount() const
2482{
2483 return mObjectsSize;
2484}
2485
2486void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002487 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002488{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002489 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002490 freeDataNoInit();
2491 mError = NO_ERROR;
2492 mData = const_cast<uint8_t*>(data);
2493 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002494 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002495 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002496 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002497 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002498 mObjectsSize = mObjectsCapacity = objectsCount;
2499 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002500 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002501 mOwner = relFunc;
2502 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002503 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002504 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002505 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002506 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002507 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002508 mObjectsSize = 0;
2509 break;
2510 }
2511 minOffset = offset + sizeof(flat_binder_object);
2512 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513 scanForFds();
2514}
2515
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002516void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002517{
2518 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002519
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002520 if (errorCheck() != NO_ERROR) {
2521 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002522 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002523 } else if (dataSize() > 0) {
2524 const uint8_t* DATA = data();
2525 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002526 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002527 const size_t N = objectsCount();
2528 for (size_t i=0; i<N; i++) {
2529 const flat_binder_object* flat
2530 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2531 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002532 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002533 << " = " << flat->binder;
2534 }
2535 } else {
2536 to << "NULL";
2537 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002538
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002539 to << ")";
2540}
2541
2542void Parcel::releaseObjects()
2543{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002545 if (i == 0) {
2546 return;
2547 }
2548 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002549 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002550 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002551 while (i > 0) {
2552 i--;
2553 const flat_binder_object* flat
2554 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002555 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002556 }
2557}
2558
2559void Parcel::acquireObjects()
2560{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002561 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002562 if (i == 0) {
2563 return;
2564 }
2565 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002566 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002567 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002568 while (i > 0) {
2569 i--;
2570 const flat_binder_object* flat
2571 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002572 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002573 }
2574}
2575
2576void Parcel::freeData()
2577{
2578 freeDataNoInit();
2579 initState();
2580}
2581
2582void Parcel::freeDataNoInit()
2583{
2584 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002585 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002586 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002587 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2588 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002589 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002590 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002591 if (mData) {
2592 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002593 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002594 if (mDataCapacity <= gParcelGlobalAllocSize) {
2595 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2596 } else {
2597 gParcelGlobalAllocSize = 0;
2598 }
2599 if (gParcelGlobalAllocCount > 0) {
2600 gParcelGlobalAllocCount--;
2601 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002602 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002603 free(mData);
2604 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002605 if (mObjects) free(mObjects);
2606 }
2607}
2608
2609status_t Parcel::growData(size_t len)
2610{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002611 if (len > INT32_MAX) {
2612 // don't accept size_t values which may have come from an
2613 // inadvertent conversion from a negative int.
2614 return BAD_VALUE;
2615 }
2616
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002617 size_t newSize = ((mDataSize+len)*3)/2;
2618 return (newSize <= mDataSize)
2619 ? (status_t) NO_MEMORY
2620 : continueWrite(newSize);
2621}
2622
2623status_t Parcel::restartWrite(size_t desired)
2624{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002625 if (desired > INT32_MAX) {
2626 // don't accept size_t values which may have come from an
2627 // inadvertent conversion from a negative int.
2628 return BAD_VALUE;
2629 }
2630
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002631 if (mOwner) {
2632 freeData();
2633 return continueWrite(desired);
2634 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002635
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002636 uint8_t* data = (uint8_t*)realloc(mData, desired);
2637 if (!data && desired > mDataCapacity) {
2638 mError = NO_MEMORY;
2639 return NO_MEMORY;
2640 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002641
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002642 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002643
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002644 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002645 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002646 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002647 gParcelGlobalAllocSize += desired;
2648 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002649 if (!mData) {
2650 gParcelGlobalAllocCount++;
2651 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002652 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002653 mData = data;
2654 mDataCapacity = desired;
2655 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002656
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002657 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002658 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2659 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2660
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002661 free(mObjects);
Yi Kongfdd8da92018-06-07 17:52:27 -07002662 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002663 mObjectsSize = mObjectsCapacity = 0;
2664 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002665 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002666 mHasFds = false;
2667 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002668 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002669
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002670 return NO_ERROR;
2671}
2672
2673status_t Parcel::continueWrite(size_t desired)
2674{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002675 if (desired > INT32_MAX) {
2676 // don't accept size_t values which may have come from an
2677 // inadvertent conversion from a negative int.
2678 return BAD_VALUE;
2679 }
2680
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002681 // If shrinking, first adjust for any objects that appear
2682 // after the new data size.
2683 size_t objectsSize = mObjectsSize;
2684 if (desired < mDataSize) {
2685 if (desired == 0) {
2686 objectsSize = 0;
2687 } else {
2688 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002689 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002690 break;
2691 objectsSize--;
2692 }
2693 }
2694 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002695
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002696 if (mOwner) {
2697 // If the size is going to zero, just release the owner's data.
2698 if (desired == 0) {
2699 freeData();
2700 return NO_ERROR;
2701 }
2702
2703 // If there is a different owner, we need to take
2704 // posession.
2705 uint8_t* data = (uint8_t*)malloc(desired);
2706 if (!data) {
2707 mError = NO_MEMORY;
2708 return NO_MEMORY;
2709 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002710 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002711
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002712 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002713 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002714 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002715 free(data);
2716
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002717 mError = NO_MEMORY;
2718 return NO_MEMORY;
2719 }
2720
2721 // Little hack to only acquire references on objects
2722 // we will be keeping.
2723 size_t oldObjectsSize = mObjectsSize;
2724 mObjectsSize = objectsSize;
2725 acquireObjects();
2726 mObjectsSize = oldObjectsSize;
2727 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002728
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002729 if (mData) {
2730 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2731 }
2732 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002733 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002734 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002735 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002736 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kongfdd8da92018-06-07 17:52:27 -07002737 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002738
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002739 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002740 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002741 gParcelGlobalAllocSize += desired;
2742 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002743 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002744
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002745 mData = data;
2746 mObjects = objects;
2747 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002748 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002749 mDataCapacity = desired;
2750 mObjectsSize = mObjectsCapacity = objectsSize;
2751 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002752 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002753
2754 } else if (mData) {
2755 if (objectsSize < mObjectsSize) {
2756 // Need to release refs on any objects we are dropping.
2757 const sp<ProcessState> proc(ProcessState::self());
2758 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2759 const flat_binder_object* flat
2760 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002761 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002762 // will need to rescan because we may have lopped off the only FDs
2763 mFdsKnown = false;
2764 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002765 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002766 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002767 binder_size_t* objects =
2768 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002769 if (objects) {
2770 mObjects = objects;
2771 }
2772 mObjectsSize = objectsSize;
2773 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002774 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002775 }
2776
2777 // We own the data, so we can just do a realloc().
2778 if (desired > mDataCapacity) {
2779 uint8_t* data = (uint8_t*)realloc(mData, desired);
2780 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002781 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2782 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002783 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002784 gParcelGlobalAllocSize += desired;
2785 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002786 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002787 mData = data;
2788 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002789 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002790 mError = NO_MEMORY;
2791 return NO_MEMORY;
2792 }
2793 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002794 if (mDataSize > desired) {
2795 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002796 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002797 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002798 if (mDataPos > desired) {
2799 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002800 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002801 }
2802 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002803
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002804 } else {
2805 // This is the first data. Easy!
2806 uint8_t* data = (uint8_t*)malloc(desired);
2807 if (!data) {
2808 mError = NO_MEMORY;
2809 return NO_MEMORY;
2810 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002811
Yi Kongfdd8da92018-06-07 17:52:27 -07002812 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002813 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002814 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002815 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002816
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002817 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002818 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002819 gParcelGlobalAllocSize += desired;
2820 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002821 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002822
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002823 mData = data;
2824 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002825 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2826 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002827 mDataCapacity = desired;
2828 }
2829
2830 return NO_ERROR;
2831}
2832
2833void Parcel::initState()
2834{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002835 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002836 mError = NO_ERROR;
Yi Kongfdd8da92018-06-07 17:52:27 -07002837 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002838 mDataSize = 0;
2839 mDataCapacity = 0;
2840 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002841 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2842 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kongfdd8da92018-06-07 17:52:27 -07002843 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002844 mObjectsSize = 0;
2845 mObjectsCapacity = 0;
2846 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002847 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002848 mHasFds = false;
2849 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002850 mAllowFds = true;
Yi Kongfdd8da92018-06-07 17:52:27 -07002851 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002852 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002853
2854 // racing multiple init leads only to multiple identical write
2855 if (gMaxFds == 0) {
2856 struct rlimit result;
2857 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2858 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002859 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002860 } else {
2861 ALOGW("Unable to getrlimit: %s", strerror(errno));
2862 gMaxFds = 1024;
2863 }
2864 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002865}
2866
2867void Parcel::scanForFds() const
2868{
2869 bool hasFds = false;
2870 for (size_t i=0; i<mObjectsSize; i++) {
2871 const flat_binder_object* flat
2872 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002873 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002874 hasFds = true;
2875 break;
2876 }
2877 }
2878 mHasFds = hasFds;
2879 mFdsKnown = true;
2880}
2881
Dan Sandleraa5c2342015-04-10 10:08:45 -04002882size_t Parcel::getBlobAshmemSize() const
2883{
Adrian Roos6bb31142015-10-22 16:46:12 -07002884 // This used to return the size of all blobs that were written to ashmem, now we're returning
2885 // the ashmem currently referenced by this Parcel, which should be equivalent.
2886 // TODO: Remove method once ABI can be changed.
2887 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002888}
2889
Adrian Rooscbf37262015-10-22 16:12:53 -07002890size_t Parcel::getOpenAshmemSize() const
2891{
2892 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002893}
2894
2895// --- Parcel::Blob ---
2896
2897Parcel::Blob::Blob() :
Yi Kongfdd8da92018-06-07 17:52:27 -07002898 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002899}
2900
2901Parcel::Blob::~Blob() {
2902 release();
2903}
2904
2905void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002906 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002907 ::munmap(mData, mSize);
2908 }
2909 clear();
2910}
2911
Jeff Brown13b16042014-11-11 16:44:25 -08002912void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2913 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002914 mData = data;
2915 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002916 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002917}
2918
2919void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002920 mFd = -1;
Yi Kongfdd8da92018-06-07 17:52:27 -07002921 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002922 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002923 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002924}
2925
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002926}; // namespace android