blob: c5645d43ea8066495a7e7894853aa304f371a729 [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>
Steven Morelanda4853cd2019-07-12 15:44:37 -070051#include "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
Steven Morelandb1c81202019-04-05 18:49:55 -070096static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070097 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -070099 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700100 case BINDER_TYPE_BINDER:
101 if (obj.binder) {
102 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800103 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104 }
105 return;
106 case BINDER_TYPE_WEAK_BINDER:
107 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800108 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 return;
110 case BINDER_TYPE_HANDLE: {
111 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi 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 -0700138static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700139 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700140{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700141 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142 case BINDER_TYPE_BINDER:
143 if (obj.binder) {
144 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800145 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700146 }
147 return;
148 case BINDER_TYPE_WEAK_BINDER:
149 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800150 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700151 return;
152 case BINDER_TYPE_HANDLE: {
153 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kongfdd8da92018-06-07 17:52:27 -0700154 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700155 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
156 b->decStrong(who);
157 }
158 return;
159 }
160 case BINDER_TYPE_WEAK_HANDLE: {
161 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
Yi Kongfdd8da92018-06-07 17:52:27 -0700162 if (b != nullptr) b.get_refs()->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700163 return;
164 }
165 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800166 if (obj.cookie != 0) { // owned
Yi Kongfdd8da92018-06-07 17:52:27 -0700167 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700168 int size = ashmem_get_size_region(obj.handle);
169 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800170 // ashmem size might have changed since last time it was accounted for, e.g.
171 // in acquire_object(). Value of *outAshmemSize is not critical since we are
172 // releasing the object anyway. Check for integer overflow condition.
173 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700174 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700175 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800176
177 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700178 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700179 return;
180 }
181 }
182
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700183 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700184}
185
186inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800187 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700188{
189 return out->writeObject(flat, false);
190}
191
Steven Morelandb1c81202019-04-05 18:49:55 -0700192static status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700193 const sp<IBinder>& binder, Parcel* out)
194{
195 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700196
Martijn Coenen2b631742017-05-05 11:16:59 -0700197 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
198 /* minimum priority for all nodes is nice 0 */
199 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
200 } else {
201 /* minimum priority for all nodes is MAX_NICE(19) */
202 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
203 }
204
Yi Kongfdd8da92018-06-07 17:52:27 -0700205 if (binder != nullptr) {
Steven Moreland3085a472018-12-26 13:59:23 -0800206 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700207 if (!local) {
208 BpBinder *proxy = binder->remoteBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700209 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000210 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700211 }
212 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700213 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800214 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700215 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800216 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700217 } else {
Steven Moreland3085a472018-12-26 13:59:23 -0800218 if (local->isRequestingSid()) {
219 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
220 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700221 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800222 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
223 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700224 }
225 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700226 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800227 obj.binder = 0;
228 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700230
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 return finish_flatten_binder(binder, obj, out);
232}
233
Steven Morelandb1c81202019-04-05 18:49:55 -0700234static status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235 const wp<IBinder>& binder, Parcel* out)
236{
237 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700238
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700239 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Yi Kongfdd8da92018-06-07 17:52:27 -0700240 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700241 sp<IBinder> real = binder.promote();
Yi Kongfdd8da92018-06-07 17:52:27 -0700242 if (real != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243 IBinder *local = real->localBinder();
244 if (!local) {
245 BpBinder *proxy = real->remoteBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700246 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000247 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 }
249 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700250 obj.hdr.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800251 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700252 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800253 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700254 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700255 obj.hdr.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800256 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
257 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258 }
259 return finish_flatten_binder(real, obj, out);
260 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700261
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700262 // XXX How to deal? In order to flatten the given binder,
263 // we need to probe it for information, which requires a primary
264 // reference... but we don't have one.
265 //
266 // The OpenBinder implementation uses a dynamic_cast<> here,
267 // but we can't do that with the different reference counting
268 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000269 ALOGE("Unable to unflatten Binder weak reference!");
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700270 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800271 obj.binder = 0;
272 obj.cookie = 0;
Yi Kongfdd8da92018-06-07 17:52:27 -0700273 return finish_flatten_binder(nullptr, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700274
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700275 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700276 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800277 obj.binder = 0;
278 obj.cookie = 0;
Yi Kongfdd8da92018-06-07 17:52:27 -0700279 return finish_flatten_binder(nullptr, obj, out);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700280 }
281}
282
283inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800284 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
285 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700286{
287 return NO_ERROR;
288}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700289
Steven Morelandb1c81202019-04-05 18:49:55 -0700290static status_t unflatten_binder(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700291 const Parcel& in, sp<IBinder>* out)
292{
293 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700294
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700295 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700296 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700297 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800298 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kongfdd8da92018-06-07 17:52:27 -0700299 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700300 case BINDER_TYPE_HANDLE:
301 *out = proc->getStrongProxyForHandle(flat->handle);
302 return finish_unflatten_binder(
303 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700304 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700305 }
306 return BAD_TYPE;
307}
308
Steven Morelandb1c81202019-04-05 18:49:55 -0700309static status_t unflatten_binder(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700310 const Parcel& in, wp<IBinder>* out)
311{
312 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700313
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700314 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700315 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700316 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800317 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kongfdd8da92018-06-07 17:52:27 -0700318 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700319 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800320 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700321 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800322 reinterpret_cast<IBinder*>(flat->cookie),
323 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700324 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700325 *out = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700326 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700327 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700328 case BINDER_TYPE_HANDLE:
329 case BINDER_TYPE_WEAK_HANDLE:
330 *out = proc->getWeakProxyForHandle(flat->handle);
331 return finish_unflatten_binder(
332 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
333 }
334 }
335 return BAD_TYPE;
336}
337
338// ---------------------------------------------------------------------------
339
340Parcel::Parcel()
341{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800342 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700343 initState();
344}
345
346Parcel::~Parcel()
347{
348 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800349 LOG_ALLOC("Parcel %p: destroyed", this);
350}
351
352size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800353 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
354 size_t size = gParcelGlobalAllocSize;
355 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
356 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800357}
358
359size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800360 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
361 size_t count = gParcelGlobalAllocCount;
362 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
363 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700364}
365
366const uint8_t* Parcel::data() const
367{
368 return mData;
369}
370
371size_t Parcel::dataSize() const
372{
373 return (mDataSize > mDataPos ? mDataSize : mDataPos);
374}
375
376size_t Parcel::dataAvail() const
377{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700378 size_t result = dataSize() - dataPosition();
379 if (result > INT32_MAX) {
380 abort();
381 }
382 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700383}
384
385size_t Parcel::dataPosition() const
386{
387 return mDataPos;
388}
389
390size_t Parcel::dataCapacity() const
391{
392 return mDataCapacity;
393}
394
395status_t Parcel::setDataSize(size_t size)
396{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700397 if (size > INT32_MAX) {
398 // don't accept size_t values which may have come from an
399 // inadvertent conversion from a negative int.
400 return BAD_VALUE;
401 }
402
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700403 status_t err;
404 err = continueWrite(size);
405 if (err == NO_ERROR) {
406 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700407 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700408 }
409 return err;
410}
411
412void Parcel::setDataPosition(size_t pos) const
413{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700414 if (pos > INT32_MAX) {
415 // don't accept size_t values which may have come from an
416 // inadvertent conversion from a negative int.
417 abort();
418 }
419
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700420 mDataPos = pos;
421 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800422 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700423}
424
425status_t Parcel::setDataCapacity(size_t size)
426{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700427 if (size > INT32_MAX) {
428 // don't accept size_t values which may have come from an
429 // inadvertent conversion from a negative int.
430 return BAD_VALUE;
431 }
432
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700433 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 return NO_ERROR;
435}
436
437status_t Parcel::setData(const uint8_t* buffer, size_t len)
438{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700439 if (len > INT32_MAX) {
440 // don't accept size_t values which may have come from an
441 // inadvertent conversion from a negative int.
442 return BAD_VALUE;
443 }
444
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700445 status_t err = restartWrite(len);
446 if (err == NO_ERROR) {
447 memcpy(const_cast<uint8_t*>(data()), buffer, len);
448 mDataSize = len;
449 mFdsKnown = false;
450 }
451 return err;
452}
453
Andreas Huber51faf462011-04-13 10:21:56 -0700454status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700455{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700456 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700457 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800458 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 size_t size = parcel->mObjectsSize;
460 int startPos = mDataPos;
461 int firstIndex = -1, lastIndex = -2;
462
463 if (len == 0) {
464 return NO_ERROR;
465 }
466
Nick Kralevichb6b14232015-04-02 09:36:02 -0700467 if (len > INT32_MAX) {
468 // don't accept size_t values which may have come from an
469 // inadvertent conversion from a negative int.
470 return BAD_VALUE;
471 }
472
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473 // range checks against the source parcel size
474 if ((offset > parcel->mDataSize)
475 || (len > parcel->mDataSize)
476 || (offset + len > parcel->mDataSize)) {
477 return BAD_VALUE;
478 }
479
480 // Count objects in range
481 for (int i = 0; i < (int) size; i++) {
482 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700483 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700484 if (firstIndex == -1) {
485 firstIndex = i;
486 }
487 lastIndex = i;
488 }
489 }
490 int numObjects = lastIndex - firstIndex + 1;
491
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700492 if ((mDataSize+len) > mDataCapacity) {
493 // grow data
494 err = growData(len);
495 if (err != NO_ERROR) {
496 return err;
497 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700498 }
499
500 // append data
501 memcpy(mData + mDataPos, data + offset, len);
502 mDataPos += len;
503 mDataSize += len;
504
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400505 err = NO_ERROR;
506
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700507 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200508 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700509 // grow objects
510 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700511 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700512 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800513 binder_size_t *objects =
514 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kongfdd8da92018-06-07 17:52:27 -0700515 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700516 return NO_MEMORY;
517 }
518 mObjects = objects;
519 mObjectsCapacity = newSize;
520 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700521
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700522 // append and acquire objects
523 int idx = mObjectsSize;
524 for (int i = firstIndex; i <= lastIndex; i++) {
525 size_t off = objects[i] - offset + startPos;
526 mObjects[idx++] = off;
527 mObjectsSize++;
528
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700529 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700530 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700531 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700532
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700533 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700534 // If this is a file descriptor, we need to dup it so the
535 // new Parcel now owns its own fd, and can declare that we
536 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800537 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800538 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700539 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400540 if (!mAllowFds) {
541 err = FDS_NOT_ALLOWED;
542 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543 }
544 }
545 }
546
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400547 return err;
548}
549
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700550int Parcel::compareData(const Parcel& other) {
551 size_t size = dataSize();
552 if (size != other.dataSize()) {
553 return size < other.dataSize() ? -1 : 1;
554 }
555 return memcmp(data(), other.data(), size);
556}
557
Jeff Brown13b16042014-11-11 16:44:25 -0800558bool Parcel::allowFds() const
559{
560 return mAllowFds;
561}
562
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700563bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400564{
565 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700566 if (!allowFds) {
567 mAllowFds = false;
568 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400569 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700570}
571
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700572void Parcel::restoreAllowFds(bool lastValue)
573{
574 mAllowFds = lastValue;
575}
576
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700577bool Parcel::hasFileDescriptors() const
578{
579 if (!mFdsKnown) {
580 scanForFds();
581 }
582 return mHasFds;
583}
584
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700585// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700586status_t Parcel::writeInterfaceToken(const String16& interface)
587{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700588 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
589 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700590 // currently the interface identification token is just its name as a string
591 return writeString16(interface);
592}
593
Mathias Agopian83c04462009-05-22 19:00:22 -0700594bool Parcel::checkInterface(IBinder* binder) const
595{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700596 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700597}
598
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700599bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700600 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700601{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700602 int32_t strictPolicy = readInt32();
Yi Kongfdd8da92018-06-07 17:52:27 -0700603 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700604 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700605 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700606 if ((threadState->getLastTransactionBinderFlags() &
607 IBinder::FLAG_ONEWAY) != 0) {
608 // For one-way calls, the callee is running entirely
609 // disconnected from the caller, so disable StrictMode entirely.
610 // Not only does disk/network usage not impact the caller, but
611 // there's no way to commuicate back any violations anyway.
612 threadState->setStrictModePolicy(0);
613 } else {
614 threadState->setStrictModePolicy(strictPolicy);
615 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700616 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700617 if (str == interface) {
618 return true;
619 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700620 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700621 String8(interface).string(), String8(str).string());
622 return false;
623 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700624}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700625
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800626const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700627{
628 return mObjects;
629}
630
631size_t Parcel::objectsCount() const
632{
633 return mObjectsSize;
634}
635
636status_t Parcel::errorCheck() const
637{
638 return mError;
639}
640
641void Parcel::setError(status_t err)
642{
643 mError = err;
644}
645
646status_t Parcel::finishWrite(size_t len)
647{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700648 if (len > INT32_MAX) {
649 // don't accept size_t values which may have come from an
650 // inadvertent conversion from a negative int.
651 return BAD_VALUE;
652 }
653
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700654 //printf("Finish write of %d\n", len);
655 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700656 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700657 if (mDataPos > mDataSize) {
658 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700659 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700660 }
661 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
662 return NO_ERROR;
663}
664
665status_t Parcel::writeUnpadded(const void* data, size_t len)
666{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700667 if (len > INT32_MAX) {
668 // don't accept size_t values which may have come from an
669 // inadvertent conversion from a negative int.
670 return BAD_VALUE;
671 }
672
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700673 size_t end = mDataPos + len;
674 if (end < mDataPos) {
675 // integer overflow
676 return BAD_VALUE;
677 }
678
679 if (end <= mDataCapacity) {
680restart_write:
681 memcpy(mData+mDataPos, data, len);
682 return finishWrite(len);
683 }
684
685 status_t err = growData(len);
686 if (err == NO_ERROR) goto restart_write;
687 return err;
688}
689
690status_t Parcel::write(const void* data, size_t len)
691{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700692 if (len > INT32_MAX) {
693 // don't accept size_t values which may have come from an
694 // inadvertent conversion from a negative int.
695 return BAD_VALUE;
696 }
697
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700698 void* const d = writeInplace(len);
699 if (d) {
700 memcpy(d, data, len);
701 return NO_ERROR;
702 }
703 return mError;
704}
705
706void* Parcel::writeInplace(size_t len)
707{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700708 if (len > INT32_MAX) {
709 // don't accept size_t values which may have come from an
710 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -0700711 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700712 }
713
714 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700715
716 // sanity check for integer overflow
717 if (mDataPos+padded < mDataPos) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700718 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700719 }
720
721 if ((mDataPos+padded) <= mDataCapacity) {
722restart_write:
723 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
724 uint8_t* const data = mData+mDataPos;
725
726 // Need to pad at end?
727 if (padded != len) {
728#if BYTE_ORDER == BIG_ENDIAN
729 static const uint32_t mask[4] = {
730 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
731 };
732#endif
733#if BYTE_ORDER == LITTLE_ENDIAN
734 static const uint32_t mask[4] = {
735 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
736 };
737#endif
738 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
739 // *reinterpret_cast<void**>(data+padded-4));
740 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
741 }
742
743 finishWrite(padded);
744 return data;
745 }
746
747 status_t err = growData(padded);
748 if (err == NO_ERROR) goto restart_write;
Yi Kongfdd8da92018-06-07 17:52:27 -0700749 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700750}
751
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800752status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
753 const uint8_t* strData = (uint8_t*)str.data();
754 const size_t strLen= str.length();
755 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100756 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800757 return BAD_VALUE;
758 }
759
760 status_t err = writeInt32(utf16Len);
761 if (err) {
762 return err;
763 }
764
765 // Allocate enough bytes to hold our converted string and its terminating NULL.
766 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
767 if (!dst) {
768 return NO_MEMORY;
769 }
770
Sergio Girof4607432016-07-21 14:46:35 +0100771 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800772
773 return NO_ERROR;
774}
775
776status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
777 if (!str) {
778 return writeInt32(-1);
779 }
780 return writeUtf8AsUtf16(*str);
781}
782
Casey Dahlin185d3442016-02-09 11:08:35 -0800783namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800784
Casey Dahlin185d3442016-02-09 11:08:35 -0800785template<typename T>
786status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700787{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700788 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700789 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700790 status = BAD_VALUE;
791 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700792 }
793
Casey Dahlin185d3442016-02-09 11:08:35 -0800794 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700795 if (status != OK) {
796 return status;
797 }
798
Casey Dahlin185d3442016-02-09 11:08:35 -0800799 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700800 if (!data) {
801 status = BAD_VALUE;
802 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700803 }
804
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700805 memcpy(data, val.data(), val.size());
806 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700807}
808
Casey Dahlin185d3442016-02-09 11:08:35 -0800809template<typename T>
810status_t writeByteVectorInternalPtr(Parcel* parcel,
811 const std::unique_ptr<std::vector<T>>& val)
812{
813 if (!val) {
814 return parcel->writeInt32(-1);
815 }
816
817 return writeByteVectorInternal(parcel, *val);
818}
819
820} // namespace
821
822status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
823 return writeByteVectorInternal(this, val);
824}
825
826status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
827{
828 return writeByteVectorInternalPtr(this, val);
829}
830
831status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
832 return writeByteVectorInternal(this, val);
833}
834
835status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
836{
837 return writeByteVectorInternalPtr(this, val);
838}
839
Casey Dahlin451ff582015-10-19 18:12:18 -0700840status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
841{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800842 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700843}
844
Casey Dahlinb9872622015-11-25 15:09:45 -0800845status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
846{
847 return writeNullableTypedVector(val, &Parcel::writeInt32);
848}
849
Casey Dahlin451ff582015-10-19 18:12:18 -0700850status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
851{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800852 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700853}
854
Casey Dahlinb9872622015-11-25 15:09:45 -0800855status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
856{
857 return writeNullableTypedVector(val, &Parcel::writeInt64);
858}
859
Casey Dahlin451ff582015-10-19 18:12:18 -0700860status_t Parcel::writeFloatVector(const std::vector<float>& val)
861{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800862 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700863}
864
Casey Dahlinb9872622015-11-25 15:09:45 -0800865status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
866{
867 return writeNullableTypedVector(val, &Parcel::writeFloat);
868}
869
Casey Dahlin451ff582015-10-19 18:12:18 -0700870status_t Parcel::writeDoubleVector(const std::vector<double>& val)
871{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800872 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700873}
874
Casey Dahlinb9872622015-11-25 15:09:45 -0800875status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
876{
877 return writeNullableTypedVector(val, &Parcel::writeDouble);
878}
879
Casey Dahlin451ff582015-10-19 18:12:18 -0700880status_t Parcel::writeBoolVector(const std::vector<bool>& val)
881{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800882 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700883}
884
Casey Dahlinb9872622015-11-25 15:09:45 -0800885status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
886{
887 return writeNullableTypedVector(val, &Parcel::writeBool);
888}
889
Casey Dahlin451ff582015-10-19 18:12:18 -0700890status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
891{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800892 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700893}
894
Casey Dahlinb9872622015-11-25 15:09:45 -0800895status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
896{
897 return writeNullableTypedVector(val, &Parcel::writeChar);
898}
899
Casey Dahlin451ff582015-10-19 18:12:18 -0700900status_t Parcel::writeString16Vector(const std::vector<String16>& val)
901{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800902 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700903}
904
Casey Dahlinb9872622015-11-25 15:09:45 -0800905status_t Parcel::writeString16Vector(
906 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
907{
908 return writeNullableTypedVector(val, &Parcel::writeString16);
909}
910
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800911status_t Parcel::writeUtf8VectorAsUtf16Vector(
912 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
913 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
914}
915
916status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
917 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
918}
919
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700920status_t Parcel::writeInt32(int32_t val)
921{
Andreas Huber84a6d042009-08-17 13:33:27 -0700922 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700923}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800924
925status_t Parcel::writeUint32(uint32_t val)
926{
927 return writeAligned(val);
928}
929
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700930status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700931 if (len > INT32_MAX) {
932 // don't accept size_t values which may have come from an
933 // inadvertent conversion from a negative int.
934 return BAD_VALUE;
935 }
936
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700937 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700938 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700939 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700940 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700941 if (ret == NO_ERROR) {
942 ret = write(val, len * sizeof(*val));
943 }
944 return ret;
945}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700946status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700947 if (len > INT32_MAX) {
948 // don't accept size_t values which may have come from an
949 // inadvertent conversion from a negative int.
950 return BAD_VALUE;
951 }
952
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700953 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700954 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700955 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700956 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700957 if (ret == NO_ERROR) {
958 ret = write(val, len * sizeof(*val));
959 }
960 return ret;
961}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700962
Casey Dahlind6848f52015-10-15 15:44:59 -0700963status_t Parcel::writeBool(bool val)
964{
965 return writeInt32(int32_t(val));
966}
967
968status_t Parcel::writeChar(char16_t val)
969{
970 return writeInt32(int32_t(val));
971}
972
973status_t Parcel::writeByte(int8_t val)
974{
975 return writeInt32(int32_t(val));
976}
977
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700978status_t Parcel::writeInt64(int64_t val)
979{
Andreas Huber84a6d042009-08-17 13:33:27 -0700980 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700981}
982
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700983status_t Parcel::writeUint64(uint64_t val)
984{
985 return writeAligned(val);
986}
987
Serban Constantinescuf683e012013-11-05 16:53:55 +0000988status_t Parcel::writePointer(uintptr_t val)
989{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800990 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000991}
992
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700993status_t Parcel::writeFloat(float val)
994{
Andreas Huber84a6d042009-08-17 13:33:27 -0700995 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700996}
997
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800998#if defined(__mips__) && defined(__mips_hard_float)
999
1000status_t Parcel::writeDouble(double val)
1001{
1002 union {
1003 double d;
1004 unsigned long long ll;
1005 } u;
1006 u.d = val;
1007 return writeAligned(u.ll);
1008}
1009
1010#else
1011
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001012status_t Parcel::writeDouble(double val)
1013{
Andreas Huber84a6d042009-08-17 13:33:27 -07001014 return writeAligned(val);
1015}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001016
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001017#endif
1018
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001019status_t Parcel::writeCString(const char* str)
1020{
1021 return write(str, strlen(str)+1);
1022}
1023
1024status_t Parcel::writeString8(const String8& str)
1025{
1026 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001027 // only write string if its length is more than zero characters,
1028 // as readString8 will only read if the length field is non-zero.
1029 // this is slightly different from how writeString16 works.
1030 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001031 err = write(str.string(), str.bytes()+1);
1032 }
1033 return err;
1034}
1035
Casey Dahlinb9872622015-11-25 15:09:45 -08001036status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1037{
1038 if (!str) {
1039 return writeInt32(-1);
1040 }
1041
1042 return writeString16(*str);
1043}
1044
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001045status_t Parcel::writeString16(const String16& str)
1046{
1047 return writeString16(str.string(), str.size());
1048}
1049
1050status_t Parcel::writeString16(const char16_t* str, size_t len)
1051{
Yi Kongfdd8da92018-06-07 17:52:27 -07001052 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001053
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001054 status_t err = writeInt32(len);
1055 if (err == NO_ERROR) {
1056 len *= sizeof(char16_t);
1057 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1058 if (data) {
1059 memcpy(data, str, len);
1060 *reinterpret_cast<char16_t*>(data+len) = 0;
1061 return NO_ERROR;
1062 }
1063 err = mError;
1064 }
1065 return err;
1066}
1067
1068status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1069{
1070 return flatten_binder(ProcessState::self(), val, this);
1071}
1072
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001073status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1074{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001075 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001076}
1077
Casey Dahlinb9872622015-11-25 15:09:45 -08001078status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1079{
1080 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1081}
1082
1083status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001084 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001085}
1086
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001087status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001088 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001089}
1090
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001091status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1092{
1093 return flatten_binder(ProcessState::self(), val, this);
1094}
1095
Casey Dahlinb9872622015-11-25 15:09:45 -08001096status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1097 if (!parcelable) {
1098 return writeInt32(0);
1099 }
1100
1101 return writeParcelable(*parcelable);
1102}
1103
Christopher Wiley97f048d2015-11-19 06:49:05 -08001104status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1105 status_t status = writeInt32(1); // parcelable is not null.
1106 if (status != OK) {
1107 return status;
1108 }
1109 return parcelable.writeToParcel(this);
1110}
1111
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001112status_t Parcel::writeValue(const binder::Value& value) {
1113 return value.writeToParcel(this);
1114}
1115
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001116status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001117{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001118 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001119 return BAD_TYPE;
1120
1121 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001122 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001123 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001125 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001126 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001128 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1129 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130
1131 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001132 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001133 return err;
1134 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001135 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001136 return err;
1137}
1138
Jeff Brown93ff1f92011-11-04 19:01:44 -07001139status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001140{
1141 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001142 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001143 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001144 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001145 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001146 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001147 return writeObject(obj, true);
1148}
1149
1150status_t Parcel::writeDupFileDescriptor(int fd)
1151{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001152 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001153 if (dupFd < 0) {
1154 return -errno;
1155 }
1156 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001157 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001158 close(dupFd);
1159 }
1160 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001161}
1162
Dianne Hackborn1941a402016-08-29 12:30:43 -07001163status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1164{
1165 writeInt32(0);
1166 return writeFileDescriptor(fd, takeOwnership);
1167}
1168
Ryo Hashimotobf551892018-05-31 16:58:35 +09001169status_t Parcel::writeDupParcelFileDescriptor(int fd)
1170{
1171 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1172 if (dupFd < 0) {
1173 return -errno;
1174 }
1175 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1176 if (err != OK) {
1177 close(dupFd);
1178 }
1179 return err;
1180}
1181
Christopher Wiley2cf19952016-04-11 11:09:37 -07001182status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001183 return writeDupFileDescriptor(fd.get());
1184}
1185
Christopher Wiley2cf19952016-04-11 11:09:37 -07001186status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001187 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1188}
1189
Christopher Wiley2cf19952016-04-11 11:09:37 -07001190status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001191 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1192}
1193
Jeff Brown13b16042014-11-11 16:44:25 -08001194status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001195{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001196 if (len > INT32_MAX) {
1197 // don't accept size_t values which may have come from an
1198 // inadvertent conversion from a negative int.
1199 return BAD_VALUE;
1200 }
1201
Jeff Brown13b16042014-11-11 16:44:25 -08001202 status_t status;
1203 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001204 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001205 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001206 if (status) return status;
1207
1208 void* ptr = writeInplace(len);
1209 if (!ptr) return NO_MEMORY;
1210
Jeff Brown13b16042014-11-11 16:44:25 -08001211 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001212 return NO_ERROR;
1213 }
1214
Steve Block6807e592011-10-20 11:56:00 +01001215 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001216 int fd = ashmem_create_region("Parcel Blob", len);
1217 if (fd < 0) return NO_MEMORY;
1218
1219 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1220 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001221 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001222 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -07001223 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001224 if (ptr == MAP_FAILED) {
1225 status = -errno;
1226 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001227 if (!mutableCopy) {
1228 result = ashmem_set_prot_region(fd, PROT_READ);
1229 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001230 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001231 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001232 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001233 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001234 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001235 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001236 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001237 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001238 return NO_ERROR;
1239 }
1240 }
1241 }
1242 }
1243 ::munmap(ptr, len);
1244 }
1245 ::close(fd);
1246 return status;
1247}
1248
Jeff Brown13b16042014-11-11 16:44:25 -08001249status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1250{
1251 // Must match up with what's done in writeBlob.
1252 if (!mAllowFds) return FDS_NOT_ALLOWED;
1253 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1254 if (status) return status;
1255 return writeDupFileDescriptor(fd);
1256}
1257
Mathias Agopiane1424282013-07-29 21:24:40 -07001258status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001259{
1260 status_t err;
1261
1262 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001263 const size_t len = val.getFlattenedSize();
1264 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001265
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001266 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001267 // don't accept size_t values which may have come from an
1268 // inadvertent conversion from a negative int.
1269 return BAD_VALUE;
1270 }
1271
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001272 err = this->writeInt32(len);
1273 if (err) return err;
1274
1275 err = this->writeInt32(fd_count);
1276 if (err) return err;
1277
1278 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001279 void* const buf = this->writeInplace(len);
Yi Kongfdd8da92018-06-07 17:52:27 -07001280 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001281 return BAD_VALUE;
1282
Yi Kongfdd8da92018-06-07 17:52:27 -07001283 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001284 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001285 fds = new (std::nothrow) int[fd_count];
1286 if (fds == nullptr) {
1287 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1288 return BAD_VALUE;
1289 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001290 }
1291
1292 err = val.flatten(buf, len, fds, fd_count);
1293 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1294 err = this->writeDupFileDescriptor( fds[i] );
1295 }
1296
1297 if (fd_count) {
1298 delete [] fds;
1299 }
1300
1301 return err;
1302}
1303
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001304status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1305{
1306 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1307 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1308 if (enoughData && enoughObjects) {
1309restart_write:
1310 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001311
Christopher Tate98e67d32015-06-03 18:44:15 -07001312 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001313 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001314 if (!mAllowFds) {
1315 // fail before modifying our object index
1316 return FDS_NOT_ALLOWED;
1317 }
1318 mHasFds = mFdsKnown = true;
1319 }
1320
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001321 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001322 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001323 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001324 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001325 mObjectsSize++;
1326 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001327
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001328 return finishWrite(sizeof(flat_binder_object));
1329 }
1330
1331 if (!enoughData) {
1332 const status_t err = growData(sizeof(val));
1333 if (err != NO_ERROR) return err;
1334 }
1335 if (!enoughObjects) {
1336 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001337 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001338 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07001339 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001340 mObjects = objects;
1341 mObjectsCapacity = newSize;
1342 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001343
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001344 goto restart_write;
1345}
1346
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001347status_t Parcel::writeNoException()
1348{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001349 binder::Status status;
1350 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001351}
1352
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001353status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1354{
1355 using ::std::map;
1356 using ::android::binder::Value;
1357 using ::android::binder::Map;
1358
1359 Map::const_iterator iter;
1360 status_t ret;
1361
1362 ret = writeInt32(map_in.size());
1363
1364 if (ret != NO_ERROR) {
1365 return ret;
1366 }
1367
1368 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1369 ret = writeValue(Value(iter->first));
1370 if (ret != NO_ERROR) {
1371 return ret;
1372 }
1373
1374 ret = writeValue(iter->second);
1375 if (ret != NO_ERROR) {
1376 return ret;
1377 }
1378 }
1379
1380 return ret;
1381}
1382
1383status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1384{
Yi Kongfdd8da92018-06-07 17:52:27 -07001385 if (map == nullptr) {
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001386 return writeInt32(-1);
1387 }
1388
1389 return writeMap(*map.get());
1390}
1391
1392status_t Parcel::readMap(::android::binder::Map* map_out)const
1393{
1394 using ::std::map;
1395 using ::android::String16;
1396 using ::android::String8;
1397 using ::android::binder::Value;
1398 using ::android::binder::Map;
1399
1400 status_t ret = NO_ERROR;
1401 int32_t count;
1402
1403 ret = readInt32(&count);
1404 if (ret != NO_ERROR) {
1405 return ret;
1406 }
1407
1408 if (count < 0) {
1409 ALOGE("readMap: Unexpected count: %d", count);
1410 return (count == -1)
1411 ? UNEXPECTED_NULL
1412 : BAD_VALUE;
1413 }
1414
1415 map_out->clear();
1416
1417 while (count--) {
1418 Map::key_type key;
1419 Value value;
1420
1421 ret = readValue(&value);
1422 if (ret != NO_ERROR) {
1423 return ret;
1424 }
1425
1426 if (!value.getString(&key)) {
1427 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1428 return BAD_VALUE;
1429 }
1430
1431 ret = readValue(&value);
1432 if (ret != NO_ERROR) {
1433 return ret;
1434 }
1435
1436 (*map_out)[key] = value;
1437 }
1438
1439 return ret;
1440}
1441
1442status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1443{
1444 const size_t start = dataPosition();
1445 int32_t count;
1446 status_t status = readInt32(&count);
1447 map->reset();
1448
1449 if (status != OK || count == -1) {
1450 return status;
1451 }
1452
1453 setDataPosition(start);
1454 map->reset(new binder::Map());
1455
1456 status = readMap(map->get());
1457
1458 if (status != OK) {
1459 map->reset();
1460 }
1461
1462 return status;
1463}
1464
1465
1466
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001467void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001468{
1469 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1470}
1471
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001472status_t Parcel::validateReadData(size_t upperBound) const
1473{
1474 // Don't allow non-object reads on object data
1475 if (mObjectsSorted || mObjectsSize <= 1) {
1476data_sorted:
1477 // Expect to check only against the next object
1478 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1479 // For some reason the current read position is greater than the next object
1480 // hint. Iterate until we find the right object
1481 size_t nextObject = mNextObjectHint;
1482 do {
1483 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1484 // Requested info overlaps with an object
1485 ALOGE("Attempt to read from protected data in Parcel %p", this);
1486 return PERMISSION_DENIED;
1487 }
1488 nextObject++;
1489 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1490 mNextObjectHint = nextObject;
1491 }
1492 return NO_ERROR;
1493 }
1494 // Quickly determine if mObjects is sorted.
1495 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1496 binder_size_t* prevObj = currObj;
1497 while (currObj > mObjects) {
1498 prevObj--;
1499 if(*prevObj > *currObj) {
1500 goto data_unsorted;
1501 }
1502 currObj--;
1503 }
1504 mObjectsSorted = true;
1505 goto data_sorted;
1506
1507data_unsorted:
1508 // Insertion Sort mObjects
1509 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1510 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1511 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1512 binder_size_t temp = *iter0;
1513 binder_size_t* iter1 = iter0 - 1;
1514 while (iter1 >= mObjects && *iter1 > temp) {
1515 *(iter1 + 1) = *iter1;
1516 iter1--;
1517 }
1518 *(iter1 + 1) = temp;
1519 }
1520 mNextObjectHint = 0;
1521 mObjectsSorted = true;
1522 goto data_sorted;
1523}
1524
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001525status_t Parcel::read(void* outData, size_t len) const
1526{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001527 if (len > INT32_MAX) {
1528 // don't accept size_t values which may have come from an
1529 // inadvertent conversion from a negative int.
1530 return BAD_VALUE;
1531 }
1532
1533 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1534 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001535 if (mObjectsSize > 0) {
1536 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001537 if(err != NO_ERROR) {
1538 // Still increment the data position by the expected length
1539 mDataPos += pad_size(len);
1540 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1541 return err;
1542 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001543 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001544 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001545 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001546 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001547 return NO_ERROR;
1548 }
1549 return NOT_ENOUGH_DATA;
1550}
1551
1552const void* Parcel::readInplace(size_t len) const
1553{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001554 if (len > INT32_MAX) {
1555 // don't accept size_t values which may have come from an
1556 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -07001557 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001558 }
1559
1560 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1561 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001562 if (mObjectsSize > 0) {
1563 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001564 if(err != NO_ERROR) {
1565 // Still increment the data position by the expected length
1566 mDataPos += pad_size(len);
1567 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Xin Lif11e2bd2018-06-08 15:11:57 -07001568 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001569 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001570 }
1571
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001572 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001573 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001574 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001575 return data;
1576 }
Yi Kongfdd8da92018-06-07 17:52:27 -07001577 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001578}
1579
Andreas Huber84a6d042009-08-17 13:33:27 -07001580template<class T>
1581status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001582 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001583
1584 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001585 if (mObjectsSize > 0) {
1586 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001587 if(err != NO_ERROR) {
1588 // Still increment the data position by the expected length
1589 mDataPos += sizeof(T);
1590 return err;
1591 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001592 }
1593
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001594 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001595 mDataPos += sizeof(T);
1596 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001597 return NO_ERROR;
1598 } else {
1599 return NOT_ENOUGH_DATA;
1600 }
1601}
1602
Andreas Huber84a6d042009-08-17 13:33:27 -07001603template<class T>
1604T Parcel::readAligned() const {
1605 T result;
1606 if (readAligned(&result) != NO_ERROR) {
1607 result = 0;
1608 }
1609
1610 return result;
1611}
1612
1613template<class T>
1614status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001615 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001616
1617 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1618restart_write:
1619 *reinterpret_cast<T*>(mData+mDataPos) = val;
1620 return finishWrite(sizeof(val));
1621 }
1622
1623 status_t err = growData(sizeof(val));
1624 if (err == NO_ERROR) goto restart_write;
1625 return err;
1626}
1627
Casey Dahlin185d3442016-02-09 11:08:35 -08001628namespace {
1629
1630template<typename T>
1631status_t readByteVectorInternal(const Parcel* parcel,
1632 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001633 val->clear();
1634
1635 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001636 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001637
1638 if (status != OK) {
1639 return status;
1640 }
1641
Christopher Wiley4db672d2015-11-10 09:44:30 -08001642 if (size < 0) {
1643 status = UNEXPECTED_NULL;
1644 return status;
1645 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001646 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001647 status = BAD_VALUE;
1648 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001649 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001650
Paul Lietar433e87b2016-09-16 10:39:32 -07001651 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001652 if (!data) {
1653 status = BAD_VALUE;
1654 return status;
1655 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001656 val->reserve(size);
1657 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001658
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001659 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001660}
1661
Casey Dahlin185d3442016-02-09 11:08:35 -08001662template<typename T>
1663status_t readByteVectorInternalPtr(
1664 const Parcel* parcel,
1665 std::unique_ptr<std::vector<T>>* val) {
1666 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001667 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001668 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001669 val->reset();
1670
1671 if (status != OK || size < 0) {
1672 return status;
1673 }
1674
Casey Dahlin185d3442016-02-09 11:08:35 -08001675 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001676 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001677
Casey Dahlin185d3442016-02-09 11:08:35 -08001678 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001679
1680 if (status != OK) {
1681 val->reset();
1682 }
1683
1684 return status;
1685}
1686
Casey Dahlin185d3442016-02-09 11:08:35 -08001687} // namespace
1688
1689status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1690 return readByteVectorInternal(this, val);
1691}
1692
1693status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1694 return readByteVectorInternal(this, val);
1695}
1696
1697status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1698 return readByteVectorInternalPtr(this, val);
1699}
1700
1701status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1702 return readByteVectorInternalPtr(this, val);
1703}
1704
Casey Dahlinb9872622015-11-25 15:09:45 -08001705status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1706 return readNullableTypedVector(val, &Parcel::readInt32);
1707}
1708
Casey Dahlin451ff582015-10-19 18:12:18 -07001709status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001710 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001711}
1712
Casey Dahlinb9872622015-11-25 15:09:45 -08001713status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1714 return readNullableTypedVector(val, &Parcel::readInt64);
1715}
1716
Casey Dahlin451ff582015-10-19 18:12:18 -07001717status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001718 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001719}
1720
Casey Dahlinb9872622015-11-25 15:09:45 -08001721status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1722 return readNullableTypedVector(val, &Parcel::readFloat);
1723}
1724
Casey Dahlin451ff582015-10-19 18:12:18 -07001725status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001726 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001727}
1728
Casey Dahlinb9872622015-11-25 15:09:45 -08001729status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1730 return readNullableTypedVector(val, &Parcel::readDouble);
1731}
1732
Casey Dahlin451ff582015-10-19 18:12:18 -07001733status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001734 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001735}
1736
Casey Dahlinb9872622015-11-25 15:09:45 -08001737status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1738 const int32_t start = dataPosition();
1739 int32_t size;
1740 status_t status = readInt32(&size);
1741 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001742
Casey Dahlinb9872622015-11-25 15:09:45 -08001743 if (status != OK || size < 0) {
1744 return status;
1745 }
1746
1747 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001748 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001749
1750 status = readBoolVector(val->get());
1751
1752 if (status != OK) {
1753 val->reset();
1754 }
1755
1756 return status;
1757}
1758
1759status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001760 int32_t size;
1761 status_t status = readInt32(&size);
1762
1763 if (status != OK) {
1764 return status;
1765 }
1766
1767 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001768 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001769 }
1770
1771 val->resize(size);
1772
1773 /* C++ bool handling means a vector of bools isn't necessarily addressable
1774 * (we might use individual bits)
1775 */
Christopher Wiley97887982015-10-27 16:33:47 -07001776 bool data;
1777 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001778 status = readBool(&data);
1779 (*val)[i] = data;
1780
1781 if (status != OK) {
1782 return status;
1783 }
1784 }
1785
1786 return OK;
1787}
1788
Casey Dahlinb9872622015-11-25 15:09:45 -08001789status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1790 return readNullableTypedVector(val, &Parcel::readChar);
1791}
1792
Casey Dahlin451ff582015-10-19 18:12:18 -07001793status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001794 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001795}
1796
Casey Dahlinb9872622015-11-25 15:09:45 -08001797status_t Parcel::readString16Vector(
1798 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1799 return readNullableTypedVector(val, &Parcel::readString16);
1800}
1801
Casey Dahlin451ff582015-10-19 18:12:18 -07001802status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001803 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001804}
1805
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001806status_t Parcel::readUtf8VectorFromUtf16Vector(
1807 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1808 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1809}
1810
1811status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1812 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1813}
Casey Dahlin451ff582015-10-19 18:12:18 -07001814
Andreas Huber84a6d042009-08-17 13:33:27 -07001815status_t Parcel::readInt32(int32_t *pArg) const
1816{
1817 return readAligned(pArg);
1818}
1819
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001820int32_t Parcel::readInt32() const
1821{
Andreas Huber84a6d042009-08-17 13:33:27 -07001822 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001823}
1824
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001825status_t Parcel::readUint32(uint32_t *pArg) const
1826{
1827 return readAligned(pArg);
1828}
1829
1830uint32_t Parcel::readUint32() const
1831{
1832 return readAligned<uint32_t>();
1833}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001834
1835status_t Parcel::readInt64(int64_t *pArg) const
1836{
Andreas Huber84a6d042009-08-17 13:33:27 -07001837 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001838}
1839
1840
1841int64_t Parcel::readInt64() const
1842{
Andreas Huber84a6d042009-08-17 13:33:27 -07001843 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001844}
1845
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001846status_t Parcel::readUint64(uint64_t *pArg) const
1847{
1848 return readAligned(pArg);
1849}
1850
1851uint64_t Parcel::readUint64() const
1852{
1853 return readAligned<uint64_t>();
1854}
1855
Serban Constantinescuf683e012013-11-05 16:53:55 +00001856status_t Parcel::readPointer(uintptr_t *pArg) const
1857{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001858 status_t ret;
1859 binder_uintptr_t ptr;
1860 ret = readAligned(&ptr);
1861 if (!ret)
1862 *pArg = ptr;
1863 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001864}
1865
1866uintptr_t Parcel::readPointer() const
1867{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001868 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001869}
1870
1871
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001872status_t Parcel::readFloat(float *pArg) const
1873{
Andreas Huber84a6d042009-08-17 13:33:27 -07001874 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001875}
1876
1877
1878float Parcel::readFloat() const
1879{
Andreas Huber84a6d042009-08-17 13:33:27 -07001880 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001881}
1882
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001883#if defined(__mips__) && defined(__mips_hard_float)
1884
1885status_t Parcel::readDouble(double *pArg) const
1886{
1887 union {
1888 double d;
1889 unsigned long long ll;
1890 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001891 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001892 status_t status;
1893 status = readAligned(&u.ll);
1894 *pArg = u.d;
1895 return status;
1896}
1897
1898double Parcel::readDouble() const
1899{
1900 union {
1901 double d;
1902 unsigned long long ll;
1903 } u;
1904 u.ll = readAligned<unsigned long long>();
1905 return u.d;
1906}
1907
1908#else
1909
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001910status_t Parcel::readDouble(double *pArg) const
1911{
Andreas Huber84a6d042009-08-17 13:33:27 -07001912 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001913}
1914
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001915double Parcel::readDouble() const
1916{
Andreas Huber84a6d042009-08-17 13:33:27 -07001917 return readAligned<double>();
1918}
1919
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001920#endif
1921
Andreas Huber84a6d042009-08-17 13:33:27 -07001922status_t Parcel::readIntPtr(intptr_t *pArg) const
1923{
1924 return readAligned(pArg);
1925}
1926
1927
1928intptr_t Parcel::readIntPtr() const
1929{
1930 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001931}
1932
Casey Dahlind6848f52015-10-15 15:44:59 -07001933status_t Parcel::readBool(bool *pArg) const
1934{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001935 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001936 status_t ret = readInt32(&tmp);
1937 *pArg = (tmp != 0);
1938 return ret;
1939}
1940
1941bool Parcel::readBool() const
1942{
1943 return readInt32() != 0;
1944}
1945
1946status_t Parcel::readChar(char16_t *pArg) const
1947{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001948 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001949 status_t ret = readInt32(&tmp);
1950 *pArg = char16_t(tmp);
1951 return ret;
1952}
1953
1954char16_t Parcel::readChar() const
1955{
1956 return char16_t(readInt32());
1957}
1958
1959status_t Parcel::readByte(int8_t *pArg) const
1960{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001961 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001962 status_t ret = readInt32(&tmp);
1963 *pArg = int8_t(tmp);
1964 return ret;
1965}
1966
1967int8_t Parcel::readByte() const
1968{
1969 return int8_t(readInt32());
1970}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001971
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001972status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1973 size_t utf16Size = 0;
1974 const char16_t* src = readString16Inplace(&utf16Size);
1975 if (!src) {
1976 return UNEXPECTED_NULL;
1977 }
1978
1979 // Save ourselves the trouble, we're done.
1980 if (utf16Size == 0u) {
1981 str->clear();
1982 return NO_ERROR;
1983 }
1984
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001985 // Allow for closing '\0'
1986 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1987 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001988 return BAD_VALUE;
1989 }
1990 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001991 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001992 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001993 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1994 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001995 return NO_ERROR;
1996}
1997
1998status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1999 const int32_t start = dataPosition();
2000 int32_t size;
2001 status_t status = readInt32(&size);
2002 str->reset();
2003
2004 if (status != OK || size < 0) {
2005 return status;
2006 }
2007
2008 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002009 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002010 return readUtf8FromUtf16(str->get());
2011}
2012
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002013const char* Parcel::readCString() const
2014{
2015 const size_t avail = mDataSize-mDataPos;
2016 if (avail > 0) {
2017 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2018 // is the string's trailing NUL within the parcel's valid bounds?
2019 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2020 if (eos) {
2021 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002022 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002023 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002024 return str;
2025 }
2026 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002027 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002028}
2029
2030String8 Parcel::readString8() const
2031{
Roshan Pius87b64d22016-07-18 12:51:02 -07002032 String8 retString;
2033 status_t status = readString8(&retString);
2034 if (status != OK) {
2035 // We don't care about errors here, so just return an empty string.
2036 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002037 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002038 return retString;
2039}
2040
2041status_t Parcel::readString8(String8* pArg) const
2042{
2043 int32_t size;
2044 status_t status = readInt32(&size);
2045 if (status != OK) {
2046 return status;
2047 }
2048 // watch for potential int overflow from size+1
2049 if (size < 0 || size >= INT32_MAX) {
2050 return BAD_VALUE;
2051 }
2052 // |writeString8| writes nothing for empty string.
2053 if (size == 0) {
2054 *pArg = String8();
2055 return OK;
2056 }
2057 const char* str = (const char*)readInplace(size + 1);
Yi Kongfdd8da92018-06-07 17:52:27 -07002058 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002059 return BAD_VALUE;
2060 }
2061 pArg->setTo(str, size);
2062 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002063}
2064
2065String16 Parcel::readString16() const
2066{
2067 size_t len;
2068 const char16_t* str = readString16Inplace(&len);
2069 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002070 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002071 return String16();
2072}
2073
Casey Dahlinb9872622015-11-25 15:09:45 -08002074status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2075{
2076 const int32_t start = dataPosition();
2077 int32_t size;
2078 status_t status = readInt32(&size);
2079 pArg->reset();
2080
2081 if (status != OK || size < 0) {
2082 return status;
2083 }
2084
2085 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002086 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002087
2088 status = readString16(pArg->get());
2089
2090 if (status != OK) {
2091 pArg->reset();
2092 }
2093
2094 return status;
2095}
2096
Casey Dahlin451ff582015-10-19 18:12:18 -07002097status_t Parcel::readString16(String16* pArg) const
2098{
2099 size_t len;
2100 const char16_t* str = readString16Inplace(&len);
2101 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002102 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002103 return 0;
2104 } else {
2105 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002106 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002107 }
2108}
2109
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002110const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2111{
2112 int32_t size = readInt32();
2113 // watch for potential int overflow from size+1
2114 if (size >= 0 && size < INT32_MAX) {
2115 *outLen = size;
2116 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07002117 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002118 return str;
2119 }
2120 }
2121 *outLen = 0;
Yi Kongfdd8da92018-06-07 17:52:27 -07002122 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002123}
2124
Casey Dahlinf0c13772015-10-27 18:33:56 -07002125status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2126{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002127 status_t status = readNullableStrongBinder(val);
2128 if (status == OK && !val->get()) {
2129 status = UNEXPECTED_NULL;
2130 }
2131 return status;
2132}
2133
2134status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2135{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002136 return unflatten_binder(ProcessState::self(), *this, val);
2137}
2138
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002139sp<IBinder> Parcel::readStrongBinder() const
2140{
2141 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002142 // Note that a lot of code in Android reads binders by hand with this
2143 // method, and that code has historically been ok with getting nullptr
2144 // back (while ignoring error codes).
2145 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002146 return val;
2147}
2148
2149wp<IBinder> Parcel::readWeakBinder() const
2150{
2151 wp<IBinder> val;
2152 unflatten_binder(ProcessState::self(), *this, &val);
2153 return val;
2154}
2155
Christopher Wiley97f048d2015-11-19 06:49:05 -08002156status_t Parcel::readParcelable(Parcelable* parcelable) const {
2157 int32_t have_parcelable = 0;
2158 status_t status = readInt32(&have_parcelable);
2159 if (status != OK) {
2160 return status;
2161 }
2162 if (!have_parcelable) {
2163 return UNEXPECTED_NULL;
2164 }
2165 return parcelable->readFromParcel(this);
2166}
2167
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002168status_t Parcel::readValue(binder::Value* value) const {
2169 return value->readFromParcel(this);
2170}
2171
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002172int32_t Parcel::readExceptionCode() const
2173{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002174 binder::Status status;
2175 status.readFromParcel(*this);
2176 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002177}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002178
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002179native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002180{
2181 int numFds, numInts;
2182 status_t err;
2183 err = readInt32(&numFds);
Yi Kongfdd8da92018-06-07 17:52:27 -07002184 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002185 err = readInt32(&numInts);
Yi Kongfdd8da92018-06-07 17:52:27 -07002186 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002187
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002188 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002189 if (!h) {
Yi Kongfdd8da92018-06-07 17:52:27 -07002190 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002191 }
2192
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002193 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002194 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002195 if (h->data[i] < 0) {
2196 for (int j = 0; j < i; j++) {
2197 close(h->data[j]);
2198 }
2199 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07002200 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002201 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002202 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002203 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002204 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002205 native_handle_close(h);
2206 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07002207 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002208 }
2209 return h;
2210}
2211
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002212int Parcel::readFileDescriptor() const
2213{
2214 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002215
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002216 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002217 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002218 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002219
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002220 return BAD_TYPE;
2221}
2222
Dianne Hackborn1941a402016-08-29 12:30:43 -07002223int Parcel::readParcelFileDescriptor() const
2224{
2225 int32_t hasComm = readInt32();
2226 int fd = readFileDescriptor();
2227 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002228 // detach (owned by the binder driver)
2229 int comm = readFileDescriptor();
2230
2231 // warning: this must be kept in sync with:
2232 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2233 enum ParcelFileDescriptorStatus {
2234 DETACHED = 2,
2235 };
2236
2237#if BYTE_ORDER == BIG_ENDIAN
2238 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2239#endif
2240#if BYTE_ORDER == LITTLE_ENDIAN
2241 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2242#endif
2243
2244 ssize_t written = TEMP_FAILURE_RETRY(
2245 ::write(comm, &message, sizeof(message)));
2246
2247 if (written == -1 || written != sizeof(message)) {
2248 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2249 written, strerror(errno));
2250 return BAD_TYPE;
2251 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002252 }
2253 return fd;
2254}
2255
Christopher Wiley2cf19952016-04-11 11:09:37 -07002256status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002257{
2258 int got = readFileDescriptor();
2259
2260 if (got == BAD_TYPE) {
2261 return BAD_TYPE;
2262 }
2263
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002264 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002265
2266 if (val->get() < 0) {
2267 return BAD_VALUE;
2268 }
2269
2270 return OK;
2271}
2272
Ryo Hashimotobf551892018-05-31 16:58:35 +09002273status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2274{
2275 int got = readParcelFileDescriptor();
2276
2277 if (got == BAD_TYPE) {
2278 return BAD_TYPE;
2279 }
2280
2281 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2282
2283 if (val->get() < 0) {
2284 return BAD_VALUE;
2285 }
2286
2287 return OK;
2288}
Casey Dahlin06673e32015-11-23 13:24:23 -08002289
Christopher Wiley2cf19952016-04-11 11:09:37 -07002290status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002291 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2292}
2293
Christopher Wiley2cf19952016-04-11 11:09:37 -07002294status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002295 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2296}
2297
Jeff Brown5707dbf2011-09-23 21:17:56 -07002298status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2299{
Jeff Brown13b16042014-11-11 16:44:25 -08002300 int32_t blobType;
2301 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002302 if (status) return status;
2303
Jeff Brown13b16042014-11-11 16:44:25 -08002304 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002305 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002306 const void* ptr = readInplace(len);
2307 if (!ptr) return BAD_VALUE;
2308
Jeff Brown13b16042014-11-11 16:44:25 -08002309 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002310 return NO_ERROR;
2311 }
2312
Steve Block6807e592011-10-20 11:56:00 +01002313 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002314 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002315 int fd = readFileDescriptor();
2316 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2317
Yi Kongfdd8da92018-06-07 17:52:27 -07002318 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002319 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002320 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002321
Jeff Brown13b16042014-11-11 16:44:25 -08002322 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002323 return NO_ERROR;
2324}
2325
Mathias Agopiane1424282013-07-29 21:24:40 -07002326status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002327{
2328 // size
2329 const size_t len = this->readInt32();
2330 const size_t fd_count = this->readInt32();
2331
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002332 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002333 // don't accept size_t values which may have come from an
2334 // inadvertent conversion from a negative int.
2335 return BAD_VALUE;
2336 }
2337
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002338 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002339 void const* const buf = this->readInplace(pad_size(len));
Yi Kongfdd8da92018-06-07 17:52:27 -07002340 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002341 return BAD_VALUE;
2342
Yi Kongfdd8da92018-06-07 17:52:27 -07002343 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002344 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002345 fds = new (std::nothrow) int[fd_count];
2346 if (fds == nullptr) {
2347 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2348 return BAD_VALUE;
2349 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002350 }
2351
2352 status_t err = NO_ERROR;
2353 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002354 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002355 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002356 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002357 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 -07002358 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2359 // Close all the file descriptors that were dup-ed.
2360 for (size_t j=0; j<i ;j++) {
2361 close(fds[j]);
2362 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002363 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002364 }
2365
2366 if (err == NO_ERROR) {
2367 err = val.unflatten(buf, len, fds, fd_count);
2368 }
2369
2370 if (fd_count) {
2371 delete [] fds;
2372 }
2373
2374 return err;
2375}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002376const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2377{
2378 const size_t DPOS = mDataPos;
2379 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2380 const flat_binder_object* obj
2381 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2382 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002383 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002384 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002385 // the object list, so we don't want to check for it when
2386 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002387 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002388 return obj;
2389 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002390
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002391 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002392 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002393 const size_t N = mObjectsSize;
2394 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002395
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002396 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002397 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002398 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002399
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002400 // Start at the current hint position, looking for an object at
2401 // the current data position.
2402 if (opos < N) {
2403 while (opos < (N-1) && OBJS[opos] < DPOS) {
2404 opos++;
2405 }
2406 } else {
2407 opos = N-1;
2408 }
2409 if (OBJS[opos] == DPOS) {
2410 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002411 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002412 this, DPOS, opos);
2413 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002414 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002415 return obj;
2416 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002417
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002418 // Look backwards for it...
2419 while (opos > 0 && OBJS[opos] > DPOS) {
2420 opos--;
2421 }
2422 if (OBJS[opos] == DPOS) {
2423 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002424 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002425 this, DPOS, opos);
2426 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002427 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002428 return obj;
2429 }
2430 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002431 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 -07002432 this, DPOS);
2433 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002434 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435}
2436
2437void Parcel::closeFileDescriptors()
2438{
2439 size_t i = mObjectsSize;
2440 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002441 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002442 }
2443 while (i > 0) {
2444 i--;
2445 const flat_binder_object* flat
2446 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002447 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002448 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002449 close(flat->handle);
2450 }
2451 }
2452}
2453
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002454uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002455{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002456 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002457}
2458
2459size_t Parcel::ipcDataSize() const
2460{
2461 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2462}
2463
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002464uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002465{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002466 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002467}
2468
2469size_t Parcel::ipcObjectsCount() const
2470{
2471 return mObjectsSize;
2472}
2473
2474void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002475 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002476{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002477 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002478 freeDataNoInit();
2479 mError = NO_ERROR;
2480 mData = const_cast<uint8_t*>(data);
2481 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002482 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002483 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002484 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002485 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002486 mObjectsSize = mObjectsCapacity = objectsCount;
2487 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002488 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002489 mOwner = relFunc;
2490 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002491 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002492 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002493 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002494 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002495 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002496 mObjectsSize = 0;
2497 break;
2498 }
2499 minOffset = offset + sizeof(flat_binder_object);
2500 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002501 scanForFds();
2502}
2503
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002504void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002505{
2506 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002507
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508 if (errorCheck() != NO_ERROR) {
2509 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002510 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002511 } else if (dataSize() > 0) {
2512 const uint8_t* DATA = data();
2513 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002514 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002515 const size_t N = objectsCount();
2516 for (size_t i=0; i<N; i++) {
2517 const flat_binder_object* flat
2518 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2519 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002520 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002521 << " = " << flat->binder;
2522 }
2523 } else {
2524 to << "NULL";
2525 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002526
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002527 to << ")";
2528}
2529
2530void Parcel::releaseObjects()
2531{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002532 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002533 if (i == 0) {
2534 return;
2535 }
2536 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002537 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002538 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002539 while (i > 0) {
2540 i--;
2541 const flat_binder_object* flat
2542 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002543 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544 }
2545}
2546
2547void Parcel::acquireObjects()
2548{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002549 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002550 if (i == 0) {
2551 return;
2552 }
2553 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002554 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002555 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002556 while (i > 0) {
2557 i--;
2558 const flat_binder_object* flat
2559 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002560 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002561 }
2562}
2563
2564void Parcel::freeData()
2565{
2566 freeDataNoInit();
2567 initState();
2568}
2569
2570void Parcel::freeDataNoInit()
2571{
2572 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002573 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002574 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002575 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2576 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002577 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002578 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002579 if (mData) {
2580 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002581 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002582 if (mDataCapacity <= gParcelGlobalAllocSize) {
2583 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2584 } else {
2585 gParcelGlobalAllocSize = 0;
2586 }
2587 if (gParcelGlobalAllocCount > 0) {
2588 gParcelGlobalAllocCount--;
2589 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002590 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002591 free(mData);
2592 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002593 if (mObjects) free(mObjects);
2594 }
2595}
2596
2597status_t Parcel::growData(size_t len)
2598{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002599 if (len > INT32_MAX) {
2600 // don't accept size_t values which may have come from an
2601 // inadvertent conversion from a negative int.
2602 return BAD_VALUE;
2603 }
2604
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002605 size_t newSize = ((mDataSize+len)*3)/2;
2606 return (newSize <= mDataSize)
2607 ? (status_t) NO_MEMORY
2608 : continueWrite(newSize);
2609}
2610
2611status_t Parcel::restartWrite(size_t desired)
2612{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002613 if (desired > INT32_MAX) {
2614 // don't accept size_t values which may have come from an
2615 // inadvertent conversion from a negative int.
2616 return BAD_VALUE;
2617 }
2618
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002619 if (mOwner) {
2620 freeData();
2621 return continueWrite(desired);
2622 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002623
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002624 uint8_t* data = (uint8_t*)realloc(mData, desired);
2625 if (!data && desired > mDataCapacity) {
2626 mError = NO_MEMORY;
2627 return NO_MEMORY;
2628 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002629
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002630 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002631
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002632 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002633 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002634 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002635 gParcelGlobalAllocSize += desired;
2636 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002637 if (!mData) {
2638 gParcelGlobalAllocCount++;
2639 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002640 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002641 mData = data;
2642 mDataCapacity = desired;
2643 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002644
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002645 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002646 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2647 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2648
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002649 free(mObjects);
Yi Kongfdd8da92018-06-07 17:52:27 -07002650 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002651 mObjectsSize = mObjectsCapacity = 0;
2652 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002653 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002654 mHasFds = false;
2655 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002656 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002657
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002658 return NO_ERROR;
2659}
2660
2661status_t Parcel::continueWrite(size_t desired)
2662{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002663 if (desired > INT32_MAX) {
2664 // don't accept size_t values which may have come from an
2665 // inadvertent conversion from a negative int.
2666 return BAD_VALUE;
2667 }
2668
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002669 // If shrinking, first adjust for any objects that appear
2670 // after the new data size.
2671 size_t objectsSize = mObjectsSize;
2672 if (desired < mDataSize) {
2673 if (desired == 0) {
2674 objectsSize = 0;
2675 } else {
2676 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002677 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002678 break;
2679 objectsSize--;
2680 }
2681 }
2682 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002683
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002684 if (mOwner) {
2685 // If the size is going to zero, just release the owner's data.
2686 if (desired == 0) {
2687 freeData();
2688 return NO_ERROR;
2689 }
2690
2691 // If there is a different owner, we need to take
2692 // posession.
2693 uint8_t* data = (uint8_t*)malloc(desired);
2694 if (!data) {
2695 mError = NO_MEMORY;
2696 return NO_MEMORY;
2697 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002698 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002699
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002700 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002701 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002702 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002703 free(data);
2704
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002705 mError = NO_MEMORY;
2706 return NO_MEMORY;
2707 }
2708
2709 // Little hack to only acquire references on objects
2710 // we will be keeping.
2711 size_t oldObjectsSize = mObjectsSize;
2712 mObjectsSize = objectsSize;
2713 acquireObjects();
2714 mObjectsSize = oldObjectsSize;
2715 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002716
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002717 if (mData) {
2718 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2719 }
2720 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002721 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002722 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002723 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002724 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kongfdd8da92018-06-07 17:52:27 -07002725 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002726
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002727 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002728 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002729 gParcelGlobalAllocSize += desired;
2730 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002731 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002732
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002733 mData = data;
2734 mObjects = objects;
2735 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002736 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002737 mDataCapacity = desired;
2738 mObjectsSize = mObjectsCapacity = objectsSize;
2739 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002740 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002741
2742 } else if (mData) {
2743 if (objectsSize < mObjectsSize) {
2744 // Need to release refs on any objects we are dropping.
2745 const sp<ProcessState> proc(ProcessState::self());
2746 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2747 const flat_binder_object* flat
2748 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002749 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002750 // will need to rescan because we may have lopped off the only FDs
2751 mFdsKnown = false;
2752 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002753 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002754 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002755 binder_size_t* objects =
2756 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002757 if (objects) {
2758 mObjects = objects;
2759 }
2760 mObjectsSize = objectsSize;
2761 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002762 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002763 }
2764
2765 // We own the data, so we can just do a realloc().
2766 if (desired > mDataCapacity) {
2767 uint8_t* data = (uint8_t*)realloc(mData, desired);
2768 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002769 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2770 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002771 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002772 gParcelGlobalAllocSize += desired;
2773 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002774 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002775 mData = data;
2776 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002777 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002778 mError = NO_MEMORY;
2779 return NO_MEMORY;
2780 }
2781 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002782 if (mDataSize > desired) {
2783 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002784 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002785 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002786 if (mDataPos > desired) {
2787 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002788 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002789 }
2790 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002791
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002792 } else {
2793 // This is the first data. Easy!
2794 uint8_t* data = (uint8_t*)malloc(desired);
2795 if (!data) {
2796 mError = NO_MEMORY;
2797 return NO_MEMORY;
2798 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002799
Yi Kongfdd8da92018-06-07 17:52:27 -07002800 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002801 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002802 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002803 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002804
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002805 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002806 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002807 gParcelGlobalAllocSize += desired;
2808 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002809 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002810
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002811 mData = data;
2812 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002813 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2814 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002815 mDataCapacity = desired;
2816 }
2817
2818 return NO_ERROR;
2819}
2820
2821void Parcel::initState()
2822{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002823 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002824 mError = NO_ERROR;
Yi Kongfdd8da92018-06-07 17:52:27 -07002825 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002826 mDataSize = 0;
2827 mDataCapacity = 0;
2828 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002829 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2830 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kongfdd8da92018-06-07 17:52:27 -07002831 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002832 mObjectsSize = 0;
2833 mObjectsCapacity = 0;
2834 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002835 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002836 mHasFds = false;
2837 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002838 mAllowFds = true;
Yi Kongfdd8da92018-06-07 17:52:27 -07002839 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002840 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002841
2842 // racing multiple init leads only to multiple identical write
2843 if (gMaxFds == 0) {
2844 struct rlimit result;
2845 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2846 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002847 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002848 } else {
2849 ALOGW("Unable to getrlimit: %s", strerror(errno));
2850 gMaxFds = 1024;
2851 }
2852 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002853}
2854
2855void Parcel::scanForFds() const
2856{
2857 bool hasFds = false;
2858 for (size_t i=0; i<mObjectsSize; i++) {
2859 const flat_binder_object* flat
2860 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002861 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002862 hasFds = true;
2863 break;
2864 }
2865 }
2866 mHasFds = hasFds;
2867 mFdsKnown = true;
2868}
2869
Dan Sandleraa5c2342015-04-10 10:08:45 -04002870size_t Parcel::getBlobAshmemSize() const
2871{
Adrian Roos6bb31142015-10-22 16:46:12 -07002872 // This used to return the size of all blobs that were written to ashmem, now we're returning
2873 // the ashmem currently referenced by this Parcel, which should be equivalent.
2874 // TODO: Remove method once ABI can be changed.
2875 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002876}
2877
Adrian Rooscbf37262015-10-22 16:12:53 -07002878size_t Parcel::getOpenAshmemSize() const
2879{
2880 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002881}
2882
2883// --- Parcel::Blob ---
2884
2885Parcel::Blob::Blob() :
Yi Kongfdd8da92018-06-07 17:52:27 -07002886 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002887}
2888
2889Parcel::Blob::~Blob() {
2890 release();
2891}
2892
2893void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002894 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002895 ::munmap(mData, mSize);
2896 }
2897 clear();
2898}
2899
Jeff Brown13b16042014-11-11 16:44:25 -08002900void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2901 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002902 mData = data;
2903 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002904 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002905}
2906
2907void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002908 mFd = -1;
Yi Kongfdd8da92018-06-07 17:52:27 -07002909 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002910 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002911 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002912}
2913
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002914}; // namespace android