blob: eb30581dba4c8d031b1e0dfcb7ce2bf462774fc4 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080023#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080028#include <sys/stat.h>
29#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070030#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080031#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070032
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/Binder.h>
34#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080035#include <binder/IPCThreadState.h>
36#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070037#include <binder/ProcessState.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080038#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070039#include <binder/TextOutput.h>
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -080040#include <binder/Value.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041
Mark Salyzynabed7f72016-01-27 08:02:48 -080042#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070043#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080044#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080046#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String8.h>
48#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049
Mathias Agopian208059f2009-05-18 15:08:03 -070050#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080051#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053#ifndef INT32_MAX
54#define INT32_MAX ((int32_t)(2147483647))
55#endif
56
57#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080058//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080059#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080060//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070061
62// ---------------------------------------------------------------------------
63
Nick Kralevichb6b14232015-04-02 09:36:02 -070064// This macro should never be used at runtime, as a too large value
65// of s could cause an integer overflow. Instead, you should always
66// use the wrapper function pad_size()
67#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
68
69static size_t pad_size(size_t s) {
70 if (s > (SIZE_T_MAX - 3)) {
71 abort();
72 }
73 return PAD_SIZE_UNSAFE(s);
74}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070076// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080077#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070078
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070079namespace android {
80
Dianne Hackborna4cff882014-11-13 17:07:40 -080081static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
82static size_t gParcelGlobalAllocSize = 0;
83static size_t gParcelGlobalAllocCount = 0;
84
Christopher Tatee4e0ae82016-03-24 16:03:44 -070085static size_t gMaxFds = 0;
86
Jeff Brown13b16042014-11-11 16:44:25 -080087// Maximum size of a blob to transfer in-place.
88static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
89
90enum {
91 BLOB_INPLACE = 0,
92 BLOB_ASHMEM_IMMUTABLE = 1,
93 BLOB_ASHMEM_MUTABLE = 2,
94};
95
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
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700626size_t Parcel::objectsCount() const
627{
628 return mObjectsSize;
629}
630
631status_t Parcel::errorCheck() const
632{
633 return mError;
634}
635
636void Parcel::setError(status_t err)
637{
638 mError = err;
639}
640
641status_t Parcel::finishWrite(size_t len)
642{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700643 if (len > INT32_MAX) {
644 // don't accept size_t values which may have come from an
645 // inadvertent conversion from a negative int.
646 return BAD_VALUE;
647 }
648
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700649 //printf("Finish write of %d\n", len);
650 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700651 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700652 if (mDataPos > mDataSize) {
653 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700654 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700655 }
656 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
657 return NO_ERROR;
658}
659
660status_t Parcel::writeUnpadded(const void* data, size_t len)
661{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700662 if (len > INT32_MAX) {
663 // don't accept size_t values which may have come from an
664 // inadvertent conversion from a negative int.
665 return BAD_VALUE;
666 }
667
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700668 size_t end = mDataPos + len;
669 if (end < mDataPos) {
670 // integer overflow
671 return BAD_VALUE;
672 }
673
674 if (end <= mDataCapacity) {
675restart_write:
676 memcpy(mData+mDataPos, data, len);
677 return finishWrite(len);
678 }
679
680 status_t err = growData(len);
681 if (err == NO_ERROR) goto restart_write;
682 return err;
683}
684
685status_t Parcel::write(const void* data, size_t len)
686{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700687 if (len > INT32_MAX) {
688 // don't accept size_t values which may have come from an
689 // inadvertent conversion from a negative int.
690 return BAD_VALUE;
691 }
692
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700693 void* const d = writeInplace(len);
694 if (d) {
695 memcpy(d, data, len);
696 return NO_ERROR;
697 }
698 return mError;
699}
700
701void* Parcel::writeInplace(size_t len)
702{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700703 if (len > INT32_MAX) {
704 // don't accept size_t values which may have come from an
705 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -0700706 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700707 }
708
709 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700710
711 // sanity check for integer overflow
712 if (mDataPos+padded < mDataPos) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700713 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700714 }
715
716 if ((mDataPos+padded) <= mDataCapacity) {
717restart_write:
718 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
719 uint8_t* const data = mData+mDataPos;
720
721 // Need to pad at end?
722 if (padded != len) {
723#if BYTE_ORDER == BIG_ENDIAN
724 static const uint32_t mask[4] = {
725 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
726 };
727#endif
728#if BYTE_ORDER == LITTLE_ENDIAN
729 static const uint32_t mask[4] = {
730 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
731 };
732#endif
733 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
734 // *reinterpret_cast<void**>(data+padded-4));
735 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
736 }
737
738 finishWrite(padded);
739 return data;
740 }
741
742 status_t err = growData(padded);
743 if (err == NO_ERROR) goto restart_write;
Yi Kongfdd8da92018-06-07 17:52:27 -0700744 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700745}
746
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800747status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
748 const uint8_t* strData = (uint8_t*)str.data();
749 const size_t strLen= str.length();
750 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100751 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800752 return BAD_VALUE;
753 }
754
755 status_t err = writeInt32(utf16Len);
756 if (err) {
757 return err;
758 }
759
760 // Allocate enough bytes to hold our converted string and its terminating NULL.
761 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
762 if (!dst) {
763 return NO_MEMORY;
764 }
765
Sergio Girof4607432016-07-21 14:46:35 +0100766 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800767
768 return NO_ERROR;
769}
770
771status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
772 if (!str) {
773 return writeInt32(-1);
774 }
775 return writeUtf8AsUtf16(*str);
776}
777
Casey Dahlin185d3442016-02-09 11:08:35 -0800778namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800779
Casey Dahlin185d3442016-02-09 11:08:35 -0800780template<typename T>
781status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700782{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700783 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700784 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700785 status = BAD_VALUE;
786 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700787 }
788
Casey Dahlin185d3442016-02-09 11:08:35 -0800789 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700790 if (status != OK) {
791 return status;
792 }
793
Casey Dahlin185d3442016-02-09 11:08:35 -0800794 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700795 if (!data) {
796 status = BAD_VALUE;
797 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700798 }
799
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700800 memcpy(data, val.data(), val.size());
801 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700802}
803
Casey Dahlin185d3442016-02-09 11:08:35 -0800804template<typename T>
805status_t writeByteVectorInternalPtr(Parcel* parcel,
806 const std::unique_ptr<std::vector<T>>& val)
807{
808 if (!val) {
809 return parcel->writeInt32(-1);
810 }
811
812 return writeByteVectorInternal(parcel, *val);
813}
814
815} // namespace
816
817status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
818 return writeByteVectorInternal(this, val);
819}
820
821status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
822{
823 return writeByteVectorInternalPtr(this, val);
824}
825
826status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
827 return writeByteVectorInternal(this, val);
828}
829
830status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
831{
832 return writeByteVectorInternalPtr(this, val);
833}
834
Casey Dahlin451ff582015-10-19 18:12:18 -0700835status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
836{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800837 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700838}
839
Casey Dahlinb9872622015-11-25 15:09:45 -0800840status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
841{
842 return writeNullableTypedVector(val, &Parcel::writeInt32);
843}
844
Casey Dahlin451ff582015-10-19 18:12:18 -0700845status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
846{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800847 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700848}
849
Casey Dahlinb9872622015-11-25 15:09:45 -0800850status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
851{
852 return writeNullableTypedVector(val, &Parcel::writeInt64);
853}
854
Casey Dahlin451ff582015-10-19 18:12:18 -0700855status_t Parcel::writeFloatVector(const std::vector<float>& val)
856{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800857 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700858}
859
Casey Dahlinb9872622015-11-25 15:09:45 -0800860status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
861{
862 return writeNullableTypedVector(val, &Parcel::writeFloat);
863}
864
Casey Dahlin451ff582015-10-19 18:12:18 -0700865status_t Parcel::writeDoubleVector(const std::vector<double>& val)
866{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800867 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700868}
869
Casey Dahlinb9872622015-11-25 15:09:45 -0800870status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
871{
872 return writeNullableTypedVector(val, &Parcel::writeDouble);
873}
874
Casey Dahlin451ff582015-10-19 18:12:18 -0700875status_t Parcel::writeBoolVector(const std::vector<bool>& val)
876{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800877 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700878}
879
Casey Dahlinb9872622015-11-25 15:09:45 -0800880status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
881{
882 return writeNullableTypedVector(val, &Parcel::writeBool);
883}
884
Casey Dahlin451ff582015-10-19 18:12:18 -0700885status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
886{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800887 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700888}
889
Casey Dahlinb9872622015-11-25 15:09:45 -0800890status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
891{
892 return writeNullableTypedVector(val, &Parcel::writeChar);
893}
894
Casey Dahlin451ff582015-10-19 18:12:18 -0700895status_t Parcel::writeString16Vector(const std::vector<String16>& val)
896{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800897 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700898}
899
Casey Dahlinb9872622015-11-25 15:09:45 -0800900status_t Parcel::writeString16Vector(
901 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
902{
903 return writeNullableTypedVector(val, &Parcel::writeString16);
904}
905
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800906status_t Parcel::writeUtf8VectorAsUtf16Vector(
907 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
908 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
909}
910
911status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
912 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
913}
914
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700915status_t Parcel::writeInt32(int32_t val)
916{
Andreas Huber84a6d042009-08-17 13:33:27 -0700917 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700918}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800919
920status_t Parcel::writeUint32(uint32_t val)
921{
922 return writeAligned(val);
923}
924
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700925status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700926 if (len > INT32_MAX) {
927 // don't accept size_t values which may have come from an
928 // inadvertent conversion from a negative int.
929 return BAD_VALUE;
930 }
931
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700932 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700933 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700934 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700935 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700936 if (ret == NO_ERROR) {
937 ret = write(val, len * sizeof(*val));
938 }
939 return ret;
940}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700941status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700942 if (len > INT32_MAX) {
943 // don't accept size_t values which may have come from an
944 // inadvertent conversion from a negative int.
945 return BAD_VALUE;
946 }
947
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700948 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700949 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700950 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700951 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700952 if (ret == NO_ERROR) {
953 ret = write(val, len * sizeof(*val));
954 }
955 return ret;
956}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700957
Casey Dahlind6848f52015-10-15 15:44:59 -0700958status_t Parcel::writeBool(bool val)
959{
960 return writeInt32(int32_t(val));
961}
962
963status_t Parcel::writeChar(char16_t val)
964{
965 return writeInt32(int32_t(val));
966}
967
968status_t Parcel::writeByte(int8_t val)
969{
970 return writeInt32(int32_t(val));
971}
972
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700973status_t Parcel::writeInt64(int64_t val)
974{
Andreas Huber84a6d042009-08-17 13:33:27 -0700975 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700976}
977
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700978status_t Parcel::writeUint64(uint64_t val)
979{
980 return writeAligned(val);
981}
982
Serban Constantinescuf683e012013-11-05 16:53:55 +0000983status_t Parcel::writePointer(uintptr_t val)
984{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800985 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000986}
987
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700988status_t Parcel::writeFloat(float val)
989{
Andreas Huber84a6d042009-08-17 13:33:27 -0700990 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700991}
992
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800993#if defined(__mips__) && defined(__mips_hard_float)
994
995status_t Parcel::writeDouble(double val)
996{
997 union {
998 double d;
999 unsigned long long ll;
1000 } u;
1001 u.d = val;
1002 return writeAligned(u.ll);
1003}
1004
1005#else
1006
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001007status_t Parcel::writeDouble(double val)
1008{
Andreas Huber84a6d042009-08-17 13:33:27 -07001009 return writeAligned(val);
1010}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001011
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001012#endif
1013
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001014status_t Parcel::writeCString(const char* str)
1015{
1016 return write(str, strlen(str)+1);
1017}
1018
1019status_t Parcel::writeString8(const String8& str)
1020{
1021 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001022 // only write string if its length is more than zero characters,
1023 // as readString8 will only read if the length field is non-zero.
1024 // this is slightly different from how writeString16 works.
1025 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001026 err = write(str.string(), str.bytes()+1);
1027 }
1028 return err;
1029}
1030
Casey Dahlinb9872622015-11-25 15:09:45 -08001031status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1032{
1033 if (!str) {
1034 return writeInt32(-1);
1035 }
1036
1037 return writeString16(*str);
1038}
1039
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001040status_t Parcel::writeString16(const String16& str)
1041{
1042 return writeString16(str.string(), str.size());
1043}
1044
1045status_t Parcel::writeString16(const char16_t* str, size_t len)
1046{
Yi Kongfdd8da92018-06-07 17:52:27 -07001047 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001048
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001049 status_t err = writeInt32(len);
1050 if (err == NO_ERROR) {
1051 len *= sizeof(char16_t);
1052 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1053 if (data) {
1054 memcpy(data, str, len);
1055 *reinterpret_cast<char16_t*>(data+len) = 0;
1056 return NO_ERROR;
1057 }
1058 err = mError;
1059 }
1060 return err;
1061}
1062
1063status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1064{
1065 return flatten_binder(ProcessState::self(), val, this);
1066}
1067
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001068status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1069{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001070 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001071}
1072
Casey Dahlinb9872622015-11-25 15:09:45 -08001073status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1074{
1075 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1076}
1077
1078status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001079 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001080}
1081
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001082status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001083 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001084}
1085
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001086status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1087{
1088 return flatten_binder(ProcessState::self(), val, this);
1089}
1090
Casey Dahlinb9872622015-11-25 15:09:45 -08001091status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1092 if (!parcelable) {
1093 return writeInt32(0);
1094 }
1095
1096 return writeParcelable(*parcelable);
1097}
1098
Christopher Wiley97f048d2015-11-19 06:49:05 -08001099status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1100 status_t status = writeInt32(1); // parcelable is not null.
1101 if (status != OK) {
1102 return status;
1103 }
1104 return parcelable.writeToParcel(this);
1105}
1106
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001107status_t Parcel::writeValue(const binder::Value& value) {
1108 return value.writeToParcel(this);
1109}
1110
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001111status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001112{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001113 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001114 return BAD_TYPE;
1115
1116 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001117 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001118 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001120 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001121 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001123 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1124 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125
1126 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001127 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128 return err;
1129 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001130 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001131 return err;
1132}
1133
Jeff Brown93ff1f92011-11-04 19:01:44 -07001134status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001135{
1136 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001137 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001138 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001139 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001140 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001141 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001142 return writeObject(obj, true);
1143}
1144
1145status_t Parcel::writeDupFileDescriptor(int fd)
1146{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001147 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001148 if (dupFd < 0) {
1149 return -errno;
1150 }
1151 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001152 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001153 close(dupFd);
1154 }
1155 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001156}
1157
Dianne Hackborn1941a402016-08-29 12:30:43 -07001158status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1159{
1160 writeInt32(0);
1161 return writeFileDescriptor(fd, takeOwnership);
1162}
1163
Ryo Hashimotobf551892018-05-31 16:58:35 +09001164status_t Parcel::writeDupParcelFileDescriptor(int fd)
1165{
1166 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1167 if (dupFd < 0) {
1168 return -errno;
1169 }
1170 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1171 if (err != OK) {
1172 close(dupFd);
1173 }
1174 return err;
1175}
1176
Christopher Wiley2cf19952016-04-11 11:09:37 -07001177status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001178 return writeDupFileDescriptor(fd.get());
1179}
1180
Christopher Wiley2cf19952016-04-11 11:09:37 -07001181status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001182 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1183}
1184
Christopher Wiley2cf19952016-04-11 11:09:37 -07001185status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001186 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1187}
1188
Jeff Brown13b16042014-11-11 16:44:25 -08001189status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001190{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001191 if (len > INT32_MAX) {
1192 // don't accept size_t values which may have come from an
1193 // inadvertent conversion from a negative int.
1194 return BAD_VALUE;
1195 }
1196
Jeff Brown13b16042014-11-11 16:44:25 -08001197 status_t status;
1198 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001199 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001200 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001201 if (status) return status;
1202
1203 void* ptr = writeInplace(len);
1204 if (!ptr) return NO_MEMORY;
1205
Jeff Brown13b16042014-11-11 16:44:25 -08001206 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001207 return NO_ERROR;
1208 }
1209
Steve Block6807e592011-10-20 11:56:00 +01001210 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001211 int fd = ashmem_create_region("Parcel Blob", len);
1212 if (fd < 0) return NO_MEMORY;
1213
1214 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1215 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001216 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001217 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -07001218 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001219 if (ptr == MAP_FAILED) {
1220 status = -errno;
1221 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001222 if (!mutableCopy) {
1223 result = ashmem_set_prot_region(fd, PROT_READ);
1224 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001225 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001226 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001227 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001228 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001229 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001230 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001231 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001232 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001233 return NO_ERROR;
1234 }
1235 }
1236 }
1237 }
1238 ::munmap(ptr, len);
1239 }
1240 ::close(fd);
1241 return status;
1242}
1243
Jeff Brown13b16042014-11-11 16:44:25 -08001244status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1245{
1246 // Must match up with what's done in writeBlob.
1247 if (!mAllowFds) return FDS_NOT_ALLOWED;
1248 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1249 if (status) return status;
1250 return writeDupFileDescriptor(fd);
1251}
1252
Mathias Agopiane1424282013-07-29 21:24:40 -07001253status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001254{
1255 status_t err;
1256
1257 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001258 const size_t len = val.getFlattenedSize();
1259 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001260
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001261 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001262 // don't accept size_t values which may have come from an
1263 // inadvertent conversion from a negative int.
1264 return BAD_VALUE;
1265 }
1266
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001267 err = this->writeInt32(len);
1268 if (err) return err;
1269
1270 err = this->writeInt32(fd_count);
1271 if (err) return err;
1272
1273 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001274 void* const buf = this->writeInplace(len);
Yi Kongfdd8da92018-06-07 17:52:27 -07001275 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001276 return BAD_VALUE;
1277
Yi Kongfdd8da92018-06-07 17:52:27 -07001278 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001279 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001280 fds = new (std::nothrow) int[fd_count];
1281 if (fds == nullptr) {
1282 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1283 return BAD_VALUE;
1284 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001285 }
1286
1287 err = val.flatten(buf, len, fds, fd_count);
1288 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1289 err = this->writeDupFileDescriptor( fds[i] );
1290 }
1291
1292 if (fd_count) {
1293 delete [] fds;
1294 }
1295
1296 return err;
1297}
1298
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001299status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1300{
1301 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1302 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1303 if (enoughData && enoughObjects) {
1304restart_write:
1305 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001306
Christopher Tate98e67d32015-06-03 18:44:15 -07001307 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001308 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001309 if (!mAllowFds) {
1310 // fail before modifying our object index
1311 return FDS_NOT_ALLOWED;
1312 }
1313 mHasFds = mFdsKnown = true;
1314 }
1315
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001316 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001317 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001318 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001319 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001320 mObjectsSize++;
1321 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001322
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001323 return finishWrite(sizeof(flat_binder_object));
1324 }
1325
1326 if (!enoughData) {
1327 const status_t err = growData(sizeof(val));
1328 if (err != NO_ERROR) return err;
1329 }
1330 if (!enoughObjects) {
1331 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001332 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001333 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07001334 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001335 mObjects = objects;
1336 mObjectsCapacity = newSize;
1337 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001338
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001339 goto restart_write;
1340}
1341
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001342status_t Parcel::writeNoException()
1343{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001344 binder::Status status;
1345 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001346}
1347
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001348status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1349{
1350 using ::std::map;
1351 using ::android::binder::Value;
1352 using ::android::binder::Map;
1353
1354 Map::const_iterator iter;
1355 status_t ret;
1356
1357 ret = writeInt32(map_in.size());
1358
1359 if (ret != NO_ERROR) {
1360 return ret;
1361 }
1362
1363 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1364 ret = writeValue(Value(iter->first));
1365 if (ret != NO_ERROR) {
1366 return ret;
1367 }
1368
1369 ret = writeValue(iter->second);
1370 if (ret != NO_ERROR) {
1371 return ret;
1372 }
1373 }
1374
1375 return ret;
1376}
1377
1378status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1379{
Yi Kongfdd8da92018-06-07 17:52:27 -07001380 if (map == nullptr) {
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001381 return writeInt32(-1);
1382 }
1383
1384 return writeMap(*map.get());
1385}
1386
1387status_t Parcel::readMap(::android::binder::Map* map_out)const
1388{
1389 using ::std::map;
1390 using ::android::String16;
1391 using ::android::String8;
1392 using ::android::binder::Value;
1393 using ::android::binder::Map;
1394
1395 status_t ret = NO_ERROR;
1396 int32_t count;
1397
1398 ret = readInt32(&count);
1399 if (ret != NO_ERROR) {
1400 return ret;
1401 }
1402
1403 if (count < 0) {
1404 ALOGE("readMap: Unexpected count: %d", count);
1405 return (count == -1)
1406 ? UNEXPECTED_NULL
1407 : BAD_VALUE;
1408 }
1409
1410 map_out->clear();
1411
1412 while (count--) {
1413 Map::key_type key;
1414 Value value;
1415
1416 ret = readValue(&value);
1417 if (ret != NO_ERROR) {
1418 return ret;
1419 }
1420
1421 if (!value.getString(&key)) {
1422 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1423 return BAD_VALUE;
1424 }
1425
1426 ret = readValue(&value);
1427 if (ret != NO_ERROR) {
1428 return ret;
1429 }
1430
1431 (*map_out)[key] = value;
1432 }
1433
1434 return ret;
1435}
1436
1437status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1438{
1439 const size_t start = dataPosition();
1440 int32_t count;
1441 status_t status = readInt32(&count);
1442 map->reset();
1443
1444 if (status != OK || count == -1) {
1445 return status;
1446 }
1447
1448 setDataPosition(start);
1449 map->reset(new binder::Map());
1450
1451 status = readMap(map->get());
1452
1453 if (status != OK) {
1454 map->reset();
1455 }
1456
1457 return status;
1458}
1459
1460
1461
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001462void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001463{
1464 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1465}
1466
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001467status_t Parcel::validateReadData(size_t upperBound) const
1468{
1469 // Don't allow non-object reads on object data
1470 if (mObjectsSorted || mObjectsSize <= 1) {
1471data_sorted:
1472 // Expect to check only against the next object
1473 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1474 // For some reason the current read position is greater than the next object
1475 // hint. Iterate until we find the right object
1476 size_t nextObject = mNextObjectHint;
1477 do {
1478 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1479 // Requested info overlaps with an object
1480 ALOGE("Attempt to read from protected data in Parcel %p", this);
1481 return PERMISSION_DENIED;
1482 }
1483 nextObject++;
1484 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1485 mNextObjectHint = nextObject;
1486 }
1487 return NO_ERROR;
1488 }
1489 // Quickly determine if mObjects is sorted.
1490 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1491 binder_size_t* prevObj = currObj;
1492 while (currObj > mObjects) {
1493 prevObj--;
1494 if(*prevObj > *currObj) {
1495 goto data_unsorted;
1496 }
1497 currObj--;
1498 }
1499 mObjectsSorted = true;
1500 goto data_sorted;
1501
1502data_unsorted:
1503 // Insertion Sort mObjects
1504 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1505 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1506 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1507 binder_size_t temp = *iter0;
1508 binder_size_t* iter1 = iter0 - 1;
1509 while (iter1 >= mObjects && *iter1 > temp) {
1510 *(iter1 + 1) = *iter1;
1511 iter1--;
1512 }
1513 *(iter1 + 1) = temp;
1514 }
1515 mNextObjectHint = 0;
1516 mObjectsSorted = true;
1517 goto data_sorted;
1518}
1519
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001520status_t Parcel::read(void* outData, size_t len) const
1521{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001522 if (len > INT32_MAX) {
1523 // don't accept size_t values which may have come from an
1524 // inadvertent conversion from a negative int.
1525 return BAD_VALUE;
1526 }
1527
1528 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1529 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001530 if (mObjectsSize > 0) {
1531 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001532 if(err != NO_ERROR) {
1533 // Still increment the data position by the expected length
1534 mDataPos += pad_size(len);
1535 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1536 return err;
1537 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001538 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001539 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001540 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001541 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001542 return NO_ERROR;
1543 }
1544 return NOT_ENOUGH_DATA;
1545}
1546
1547const void* Parcel::readInplace(size_t len) const
1548{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001549 if (len > INT32_MAX) {
1550 // don't accept size_t values which may have come from an
1551 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -07001552 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001553 }
1554
1555 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1556 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001557 if (mObjectsSize > 0) {
1558 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001559 if(err != NO_ERROR) {
1560 // Still increment the data position by the expected length
1561 mDataPos += pad_size(len);
1562 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Xin Lif11e2bd2018-06-08 15:11:57 -07001563 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001564 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001565 }
1566
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001567 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001568 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001569 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001570 return data;
1571 }
Yi Kongfdd8da92018-06-07 17:52:27 -07001572 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001573}
1574
Andreas Huber84a6d042009-08-17 13:33:27 -07001575template<class T>
1576status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001577 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001578
1579 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001580 if (mObjectsSize > 0) {
1581 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001582 if(err != NO_ERROR) {
1583 // Still increment the data position by the expected length
1584 mDataPos += sizeof(T);
1585 return err;
1586 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001587 }
1588
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001589 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001590 mDataPos += sizeof(T);
1591 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001592 return NO_ERROR;
1593 } else {
1594 return NOT_ENOUGH_DATA;
1595 }
1596}
1597
Andreas Huber84a6d042009-08-17 13:33:27 -07001598template<class T>
1599T Parcel::readAligned() const {
1600 T result;
1601 if (readAligned(&result) != NO_ERROR) {
1602 result = 0;
1603 }
1604
1605 return result;
1606}
1607
1608template<class T>
1609status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001610 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001611
1612 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1613restart_write:
1614 *reinterpret_cast<T*>(mData+mDataPos) = val;
1615 return finishWrite(sizeof(val));
1616 }
1617
1618 status_t err = growData(sizeof(val));
1619 if (err == NO_ERROR) goto restart_write;
1620 return err;
1621}
1622
Casey Dahlin185d3442016-02-09 11:08:35 -08001623namespace {
1624
1625template<typename T>
1626status_t readByteVectorInternal(const Parcel* parcel,
1627 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001628 val->clear();
1629
1630 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001631 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001632
1633 if (status != OK) {
1634 return status;
1635 }
1636
Christopher Wiley4db672d2015-11-10 09:44:30 -08001637 if (size < 0) {
1638 status = UNEXPECTED_NULL;
1639 return status;
1640 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001641 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001642 status = BAD_VALUE;
1643 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001644 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001645
Paul Lietar433e87b2016-09-16 10:39:32 -07001646 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001647 if (!data) {
1648 status = BAD_VALUE;
1649 return status;
1650 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001651 val->reserve(size);
1652 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001653
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001654 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001655}
1656
Casey Dahlin185d3442016-02-09 11:08:35 -08001657template<typename T>
1658status_t readByteVectorInternalPtr(
1659 const Parcel* parcel,
1660 std::unique_ptr<std::vector<T>>* val) {
1661 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001662 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001663 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001664 val->reset();
1665
1666 if (status != OK || size < 0) {
1667 return status;
1668 }
1669
Casey Dahlin185d3442016-02-09 11:08:35 -08001670 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001671 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001672
Casey Dahlin185d3442016-02-09 11:08:35 -08001673 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001674
1675 if (status != OK) {
1676 val->reset();
1677 }
1678
1679 return status;
1680}
1681
Casey Dahlin185d3442016-02-09 11:08:35 -08001682} // namespace
1683
1684status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1685 return readByteVectorInternal(this, val);
1686}
1687
1688status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1689 return readByteVectorInternal(this, val);
1690}
1691
1692status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1693 return readByteVectorInternalPtr(this, val);
1694}
1695
1696status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1697 return readByteVectorInternalPtr(this, val);
1698}
1699
Casey Dahlinb9872622015-11-25 15:09:45 -08001700status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1701 return readNullableTypedVector(val, &Parcel::readInt32);
1702}
1703
Casey Dahlin451ff582015-10-19 18:12:18 -07001704status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001705 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001706}
1707
Casey Dahlinb9872622015-11-25 15:09:45 -08001708status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1709 return readNullableTypedVector(val, &Parcel::readInt64);
1710}
1711
Casey Dahlin451ff582015-10-19 18:12:18 -07001712status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001713 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001714}
1715
Casey Dahlinb9872622015-11-25 15:09:45 -08001716status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1717 return readNullableTypedVector(val, &Parcel::readFloat);
1718}
1719
Casey Dahlin451ff582015-10-19 18:12:18 -07001720status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001721 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001722}
1723
Casey Dahlinb9872622015-11-25 15:09:45 -08001724status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1725 return readNullableTypedVector(val, &Parcel::readDouble);
1726}
1727
Casey Dahlin451ff582015-10-19 18:12:18 -07001728status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001729 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001730}
1731
Casey Dahlinb9872622015-11-25 15:09:45 -08001732status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1733 const int32_t start = dataPosition();
1734 int32_t size;
1735 status_t status = readInt32(&size);
1736 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001737
Casey Dahlinb9872622015-11-25 15:09:45 -08001738 if (status != OK || size < 0) {
1739 return status;
1740 }
1741
1742 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001743 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001744
1745 status = readBoolVector(val->get());
1746
1747 if (status != OK) {
1748 val->reset();
1749 }
1750
1751 return status;
1752}
1753
1754status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001755 int32_t size;
1756 status_t status = readInt32(&size);
1757
1758 if (status != OK) {
1759 return status;
1760 }
1761
1762 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001763 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001764 }
1765
1766 val->resize(size);
1767
1768 /* C++ bool handling means a vector of bools isn't necessarily addressable
1769 * (we might use individual bits)
1770 */
Christopher Wiley97887982015-10-27 16:33:47 -07001771 bool data;
1772 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001773 status = readBool(&data);
1774 (*val)[i] = data;
1775
1776 if (status != OK) {
1777 return status;
1778 }
1779 }
1780
1781 return OK;
1782}
1783
Casey Dahlinb9872622015-11-25 15:09:45 -08001784status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1785 return readNullableTypedVector(val, &Parcel::readChar);
1786}
1787
Casey Dahlin451ff582015-10-19 18:12:18 -07001788status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001789 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001790}
1791
Casey Dahlinb9872622015-11-25 15:09:45 -08001792status_t Parcel::readString16Vector(
1793 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1794 return readNullableTypedVector(val, &Parcel::readString16);
1795}
1796
Casey Dahlin451ff582015-10-19 18:12:18 -07001797status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001798 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001799}
1800
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001801status_t Parcel::readUtf8VectorFromUtf16Vector(
1802 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1803 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1804}
1805
1806status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1807 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1808}
Casey Dahlin451ff582015-10-19 18:12:18 -07001809
Andreas Huber84a6d042009-08-17 13:33:27 -07001810status_t Parcel::readInt32(int32_t *pArg) const
1811{
1812 return readAligned(pArg);
1813}
1814
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001815int32_t Parcel::readInt32() const
1816{
Andreas Huber84a6d042009-08-17 13:33:27 -07001817 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001818}
1819
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001820status_t Parcel::readUint32(uint32_t *pArg) const
1821{
1822 return readAligned(pArg);
1823}
1824
1825uint32_t Parcel::readUint32() const
1826{
1827 return readAligned<uint32_t>();
1828}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001829
1830status_t Parcel::readInt64(int64_t *pArg) const
1831{
Andreas Huber84a6d042009-08-17 13:33:27 -07001832 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001833}
1834
1835
1836int64_t Parcel::readInt64() const
1837{
Andreas Huber84a6d042009-08-17 13:33:27 -07001838 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001839}
1840
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001841status_t Parcel::readUint64(uint64_t *pArg) const
1842{
1843 return readAligned(pArg);
1844}
1845
1846uint64_t Parcel::readUint64() const
1847{
1848 return readAligned<uint64_t>();
1849}
1850
Serban Constantinescuf683e012013-11-05 16:53:55 +00001851status_t Parcel::readPointer(uintptr_t *pArg) const
1852{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001853 status_t ret;
1854 binder_uintptr_t ptr;
1855 ret = readAligned(&ptr);
1856 if (!ret)
1857 *pArg = ptr;
1858 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001859}
1860
1861uintptr_t Parcel::readPointer() const
1862{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001863 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001864}
1865
1866
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001867status_t Parcel::readFloat(float *pArg) const
1868{
Andreas Huber84a6d042009-08-17 13:33:27 -07001869 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001870}
1871
1872
1873float Parcel::readFloat() const
1874{
Andreas Huber84a6d042009-08-17 13:33:27 -07001875 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001876}
1877
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001878#if defined(__mips__) && defined(__mips_hard_float)
1879
1880status_t Parcel::readDouble(double *pArg) const
1881{
1882 union {
1883 double d;
1884 unsigned long long ll;
1885 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001886 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001887 status_t status;
1888 status = readAligned(&u.ll);
1889 *pArg = u.d;
1890 return status;
1891}
1892
1893double Parcel::readDouble() const
1894{
1895 union {
1896 double d;
1897 unsigned long long ll;
1898 } u;
1899 u.ll = readAligned<unsigned long long>();
1900 return u.d;
1901}
1902
1903#else
1904
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001905status_t Parcel::readDouble(double *pArg) const
1906{
Andreas Huber84a6d042009-08-17 13:33:27 -07001907 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001908}
1909
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001910double Parcel::readDouble() const
1911{
Andreas Huber84a6d042009-08-17 13:33:27 -07001912 return readAligned<double>();
1913}
1914
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001915#endif
1916
Andreas Huber84a6d042009-08-17 13:33:27 -07001917status_t Parcel::readIntPtr(intptr_t *pArg) const
1918{
1919 return readAligned(pArg);
1920}
1921
1922
1923intptr_t Parcel::readIntPtr() const
1924{
1925 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001926}
1927
Casey Dahlind6848f52015-10-15 15:44:59 -07001928status_t Parcel::readBool(bool *pArg) const
1929{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001930 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001931 status_t ret = readInt32(&tmp);
1932 *pArg = (tmp != 0);
1933 return ret;
1934}
1935
1936bool Parcel::readBool() const
1937{
1938 return readInt32() != 0;
1939}
1940
1941status_t Parcel::readChar(char16_t *pArg) const
1942{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001943 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001944 status_t ret = readInt32(&tmp);
1945 *pArg = char16_t(tmp);
1946 return ret;
1947}
1948
1949char16_t Parcel::readChar() const
1950{
1951 return char16_t(readInt32());
1952}
1953
1954status_t Parcel::readByte(int8_t *pArg) const
1955{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001956 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001957 status_t ret = readInt32(&tmp);
1958 *pArg = int8_t(tmp);
1959 return ret;
1960}
1961
1962int8_t Parcel::readByte() const
1963{
1964 return int8_t(readInt32());
1965}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001966
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001967status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1968 size_t utf16Size = 0;
1969 const char16_t* src = readString16Inplace(&utf16Size);
1970 if (!src) {
1971 return UNEXPECTED_NULL;
1972 }
1973
1974 // Save ourselves the trouble, we're done.
1975 if (utf16Size == 0u) {
1976 str->clear();
1977 return NO_ERROR;
1978 }
1979
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001980 // Allow for closing '\0'
1981 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1982 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001983 return BAD_VALUE;
1984 }
1985 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001986 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001987 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001988 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1989 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001990 return NO_ERROR;
1991}
1992
1993status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1994 const int32_t start = dataPosition();
1995 int32_t size;
1996 status_t status = readInt32(&size);
1997 str->reset();
1998
1999 if (status != OK || size < 0) {
2000 return status;
2001 }
2002
2003 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002004 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002005 return readUtf8FromUtf16(str->get());
2006}
2007
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002008const char* Parcel::readCString() const
2009{
2010 const size_t avail = mDataSize-mDataPos;
2011 if (avail > 0) {
2012 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2013 // is the string's trailing NUL within the parcel's valid bounds?
2014 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2015 if (eos) {
2016 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002017 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002018 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002019 return str;
2020 }
2021 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002022 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002023}
2024
2025String8 Parcel::readString8() const
2026{
Roshan Pius87b64d22016-07-18 12:51:02 -07002027 String8 retString;
2028 status_t status = readString8(&retString);
2029 if (status != OK) {
2030 // We don't care about errors here, so just return an empty string.
2031 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002032 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002033 return retString;
2034}
2035
2036status_t Parcel::readString8(String8* pArg) const
2037{
2038 int32_t size;
2039 status_t status = readInt32(&size);
2040 if (status != OK) {
2041 return status;
2042 }
2043 // watch for potential int overflow from size+1
2044 if (size < 0 || size >= INT32_MAX) {
2045 return BAD_VALUE;
2046 }
2047 // |writeString8| writes nothing for empty string.
2048 if (size == 0) {
2049 *pArg = String8();
2050 return OK;
2051 }
2052 const char* str = (const char*)readInplace(size + 1);
Yi Kongfdd8da92018-06-07 17:52:27 -07002053 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002054 return BAD_VALUE;
2055 }
2056 pArg->setTo(str, size);
2057 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002058}
2059
2060String16 Parcel::readString16() const
2061{
2062 size_t len;
2063 const char16_t* str = readString16Inplace(&len);
2064 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002065 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002066 return String16();
2067}
2068
Casey Dahlinb9872622015-11-25 15:09:45 -08002069status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2070{
2071 const int32_t start = dataPosition();
2072 int32_t size;
2073 status_t status = readInt32(&size);
2074 pArg->reset();
2075
2076 if (status != OK || size < 0) {
2077 return status;
2078 }
2079
2080 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002081 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002082
2083 status = readString16(pArg->get());
2084
2085 if (status != OK) {
2086 pArg->reset();
2087 }
2088
2089 return status;
2090}
2091
Casey Dahlin451ff582015-10-19 18:12:18 -07002092status_t Parcel::readString16(String16* pArg) const
2093{
2094 size_t len;
2095 const char16_t* str = readString16Inplace(&len);
2096 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002097 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002098 return 0;
2099 } else {
2100 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002101 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002102 }
2103}
2104
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002105const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2106{
2107 int32_t size = readInt32();
2108 // watch for potential int overflow from size+1
2109 if (size >= 0 && size < INT32_MAX) {
2110 *outLen = size;
2111 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07002112 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002113 return str;
2114 }
2115 }
2116 *outLen = 0;
Yi Kongfdd8da92018-06-07 17:52:27 -07002117 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002118}
2119
Casey Dahlinf0c13772015-10-27 18:33:56 -07002120status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2121{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002122 status_t status = readNullableStrongBinder(val);
2123 if (status == OK && !val->get()) {
2124 status = UNEXPECTED_NULL;
2125 }
2126 return status;
2127}
2128
2129status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2130{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002131 return unflatten_binder(ProcessState::self(), *this, val);
2132}
2133
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002134sp<IBinder> Parcel::readStrongBinder() const
2135{
2136 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002137 // Note that a lot of code in Android reads binders by hand with this
2138 // method, and that code has historically been ok with getting nullptr
2139 // back (while ignoring error codes).
2140 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141 return val;
2142}
2143
2144wp<IBinder> Parcel::readWeakBinder() const
2145{
2146 wp<IBinder> val;
2147 unflatten_binder(ProcessState::self(), *this, &val);
2148 return val;
2149}
2150
Christopher Wiley97f048d2015-11-19 06:49:05 -08002151status_t Parcel::readParcelable(Parcelable* parcelable) const {
2152 int32_t have_parcelable = 0;
2153 status_t status = readInt32(&have_parcelable);
2154 if (status != OK) {
2155 return status;
2156 }
2157 if (!have_parcelable) {
2158 return UNEXPECTED_NULL;
2159 }
2160 return parcelable->readFromParcel(this);
2161}
2162
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002163status_t Parcel::readValue(binder::Value* value) const {
2164 return value->readFromParcel(this);
2165}
2166
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002167int32_t Parcel::readExceptionCode() const
2168{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002169 binder::Status status;
2170 status.readFromParcel(*this);
2171 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002172}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002173
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002174native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002175{
2176 int numFds, numInts;
2177 status_t err;
2178 err = readInt32(&numFds);
Yi Kongfdd8da92018-06-07 17:52:27 -07002179 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002180 err = readInt32(&numInts);
Yi Kongfdd8da92018-06-07 17:52:27 -07002181 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002182
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002183 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002184 if (!h) {
Yi Kongfdd8da92018-06-07 17:52:27 -07002185 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002186 }
2187
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002188 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002189 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002190 if (h->data[i] < 0) {
2191 for (int j = 0; j < i; j++) {
2192 close(h->data[j]);
2193 }
2194 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07002195 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002196 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002197 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002198 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002199 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002200 native_handle_close(h);
2201 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07002202 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002203 }
2204 return h;
2205}
2206
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002207int Parcel::readFileDescriptor() const
2208{
2209 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002210
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002211 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002212 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002213 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002214
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002215 return BAD_TYPE;
2216}
2217
Dianne Hackborn1941a402016-08-29 12:30:43 -07002218int Parcel::readParcelFileDescriptor() const
2219{
2220 int32_t hasComm = readInt32();
2221 int fd = readFileDescriptor();
2222 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002223 // detach (owned by the binder driver)
2224 int comm = readFileDescriptor();
2225
2226 // warning: this must be kept in sync with:
2227 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2228 enum ParcelFileDescriptorStatus {
2229 DETACHED = 2,
2230 };
2231
2232#if BYTE_ORDER == BIG_ENDIAN
2233 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2234#endif
2235#if BYTE_ORDER == LITTLE_ENDIAN
2236 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2237#endif
2238
2239 ssize_t written = TEMP_FAILURE_RETRY(
2240 ::write(comm, &message, sizeof(message)));
2241
2242 if (written == -1 || written != sizeof(message)) {
2243 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2244 written, strerror(errno));
2245 return BAD_TYPE;
2246 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002247 }
2248 return fd;
2249}
2250
Christopher Wiley2cf19952016-04-11 11:09:37 -07002251status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002252{
2253 int got = readFileDescriptor();
2254
2255 if (got == BAD_TYPE) {
2256 return BAD_TYPE;
2257 }
2258
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002259 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002260
2261 if (val->get() < 0) {
2262 return BAD_VALUE;
2263 }
2264
2265 return OK;
2266}
2267
Ryo Hashimotobf551892018-05-31 16:58:35 +09002268status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2269{
2270 int got = readParcelFileDescriptor();
2271
2272 if (got == BAD_TYPE) {
2273 return BAD_TYPE;
2274 }
2275
2276 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2277
2278 if (val->get() < 0) {
2279 return BAD_VALUE;
2280 }
2281
2282 return OK;
2283}
Casey Dahlin06673e32015-11-23 13:24:23 -08002284
Christopher Wiley2cf19952016-04-11 11:09:37 -07002285status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002286 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2287}
2288
Christopher Wiley2cf19952016-04-11 11:09:37 -07002289status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002290 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2291}
2292
Jeff Brown5707dbf2011-09-23 21:17:56 -07002293status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2294{
Jeff Brown13b16042014-11-11 16:44:25 -08002295 int32_t blobType;
2296 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002297 if (status) return status;
2298
Jeff Brown13b16042014-11-11 16:44:25 -08002299 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002300 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002301 const void* ptr = readInplace(len);
2302 if (!ptr) return BAD_VALUE;
2303
Jeff Brown13b16042014-11-11 16:44:25 -08002304 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002305 return NO_ERROR;
2306 }
2307
Steve Block6807e592011-10-20 11:56:00 +01002308 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002309 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002310 int fd = readFileDescriptor();
2311 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2312
Yi Kongfdd8da92018-06-07 17:52:27 -07002313 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002314 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002315 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002316
Jeff Brown13b16042014-11-11 16:44:25 -08002317 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002318 return NO_ERROR;
2319}
2320
Mathias Agopiane1424282013-07-29 21:24:40 -07002321status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002322{
2323 // size
2324 const size_t len = this->readInt32();
2325 const size_t fd_count = this->readInt32();
2326
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002327 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002328 // don't accept size_t values which may have come from an
2329 // inadvertent conversion from a negative int.
2330 return BAD_VALUE;
2331 }
2332
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002333 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002334 void const* const buf = this->readInplace(pad_size(len));
Yi Kongfdd8da92018-06-07 17:52:27 -07002335 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002336 return BAD_VALUE;
2337
Yi Kongfdd8da92018-06-07 17:52:27 -07002338 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002339 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002340 fds = new (std::nothrow) int[fd_count];
2341 if (fds == nullptr) {
2342 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2343 return BAD_VALUE;
2344 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002345 }
2346
2347 status_t err = NO_ERROR;
2348 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002349 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002350 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002351 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002352 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 -07002353 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2354 // Close all the file descriptors that were dup-ed.
2355 for (size_t j=0; j<i ;j++) {
2356 close(fds[j]);
2357 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002358 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002359 }
2360
2361 if (err == NO_ERROR) {
2362 err = val.unflatten(buf, len, fds, fd_count);
2363 }
2364
2365 if (fd_count) {
2366 delete [] fds;
2367 }
2368
2369 return err;
2370}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002371const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2372{
2373 const size_t DPOS = mDataPos;
2374 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2375 const flat_binder_object* obj
2376 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2377 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002378 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002379 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002380 // the object list, so we don't want to check for it when
2381 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002382 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002383 return obj;
2384 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002385
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002386 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002387 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002388 const size_t N = mObjectsSize;
2389 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002390
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002391 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002392 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002393 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002394
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002395 // Start at the current hint position, looking for an object at
2396 // the current data position.
2397 if (opos < N) {
2398 while (opos < (N-1) && OBJS[opos] < DPOS) {
2399 opos++;
2400 }
2401 } else {
2402 opos = N-1;
2403 }
2404 if (OBJS[opos] == DPOS) {
2405 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002406 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002407 this, DPOS, opos);
2408 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002409 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002410 return obj;
2411 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002412
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002413 // Look backwards for it...
2414 while (opos > 0 && OBJS[opos] > DPOS) {
2415 opos--;
2416 }
2417 if (OBJS[opos] == DPOS) {
2418 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002419 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002420 this, DPOS, opos);
2421 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002422 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002423 return obj;
2424 }
2425 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002426 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 -07002427 this, DPOS);
2428 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002429 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002430}
2431
2432void Parcel::closeFileDescriptors()
2433{
2434 size_t i = mObjectsSize;
2435 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002436 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002437 }
2438 while (i > 0) {
2439 i--;
2440 const flat_binder_object* flat
2441 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002442 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002443 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002444 close(flat->handle);
2445 }
2446 }
2447}
2448
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002449uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002450{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002451 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002452}
2453
2454size_t Parcel::ipcDataSize() const
2455{
2456 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2457}
2458
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002459uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002460{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002461 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002462}
2463
2464size_t Parcel::ipcObjectsCount() const
2465{
2466 return mObjectsSize;
2467}
2468
2469void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002470 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002471{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002472 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002473 freeDataNoInit();
2474 mError = NO_ERROR;
2475 mData = const_cast<uint8_t*>(data);
2476 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002477 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002478 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002479 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002480 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002481 mObjectsSize = mObjectsCapacity = objectsCount;
2482 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002483 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002484 mOwner = relFunc;
2485 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002486 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002487 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002488 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002489 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002490 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002491 mObjectsSize = 0;
2492 break;
2493 }
2494 minOffset = offset + sizeof(flat_binder_object);
2495 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002496 scanForFds();
2497}
2498
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002499void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002500{
2501 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002502
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002503 if (errorCheck() != NO_ERROR) {
2504 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002505 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002506 } else if (dataSize() > 0) {
2507 const uint8_t* DATA = data();
2508 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002509 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002510 const size_t N = objectsCount();
2511 for (size_t i=0; i<N; i++) {
2512 const flat_binder_object* flat
2513 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2514 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002515 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516 << " = " << flat->binder;
2517 }
2518 } else {
2519 to << "NULL";
2520 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002521
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002522 to << ")";
2523}
2524
2525void Parcel::releaseObjects()
2526{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002527 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002528 if (i == 0) {
2529 return;
2530 }
2531 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002532 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002533 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002534 while (i > 0) {
2535 i--;
2536 const flat_binder_object* flat
2537 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002538 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002539 }
2540}
2541
2542void Parcel::acquireObjects()
2543{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002545 if (i == 0) {
2546 return;
2547 }
2548 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002549 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002550 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002551 while (i > 0) {
2552 i--;
2553 const flat_binder_object* flat
2554 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002555 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002556 }
2557}
2558
2559void Parcel::freeData()
2560{
2561 freeDataNoInit();
2562 initState();
2563}
2564
2565void Parcel::freeDataNoInit()
2566{
2567 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002568 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002569 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002570 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2571 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002572 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002573 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002574 if (mData) {
2575 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002576 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002577 if (mDataCapacity <= gParcelGlobalAllocSize) {
2578 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2579 } else {
2580 gParcelGlobalAllocSize = 0;
2581 }
2582 if (gParcelGlobalAllocCount > 0) {
2583 gParcelGlobalAllocCount--;
2584 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002585 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002586 free(mData);
2587 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002588 if (mObjects) free(mObjects);
2589 }
2590}
2591
2592status_t Parcel::growData(size_t len)
2593{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002594 if (len > INT32_MAX) {
2595 // don't accept size_t values which may have come from an
2596 // inadvertent conversion from a negative int.
2597 return BAD_VALUE;
2598 }
2599
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002600 size_t newSize = ((mDataSize+len)*3)/2;
2601 return (newSize <= mDataSize)
2602 ? (status_t) NO_MEMORY
2603 : continueWrite(newSize);
2604}
2605
2606status_t Parcel::restartWrite(size_t desired)
2607{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002608 if (desired > INT32_MAX) {
2609 // don't accept size_t values which may have come from an
2610 // inadvertent conversion from a negative int.
2611 return BAD_VALUE;
2612 }
2613
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002614 if (mOwner) {
2615 freeData();
2616 return continueWrite(desired);
2617 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002618
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002619 uint8_t* data = (uint8_t*)realloc(mData, desired);
2620 if (!data && desired > mDataCapacity) {
2621 mError = NO_MEMORY;
2622 return NO_MEMORY;
2623 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002624
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002625 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002626
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002627 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002628 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002629 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002630 gParcelGlobalAllocSize += desired;
2631 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002632 if (!mData) {
2633 gParcelGlobalAllocCount++;
2634 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002635 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002636 mData = data;
2637 mDataCapacity = desired;
2638 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002639
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002640 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002641 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2642 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2643
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002644 free(mObjects);
Yi Kongfdd8da92018-06-07 17:52:27 -07002645 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002646 mObjectsSize = mObjectsCapacity = 0;
2647 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002648 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002649 mHasFds = false;
2650 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002651 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002652
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002653 return NO_ERROR;
2654}
2655
2656status_t Parcel::continueWrite(size_t desired)
2657{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002658 if (desired > INT32_MAX) {
2659 // don't accept size_t values which may have come from an
2660 // inadvertent conversion from a negative int.
2661 return BAD_VALUE;
2662 }
2663
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002664 // If shrinking, first adjust for any objects that appear
2665 // after the new data size.
2666 size_t objectsSize = mObjectsSize;
2667 if (desired < mDataSize) {
2668 if (desired == 0) {
2669 objectsSize = 0;
2670 } else {
2671 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002672 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002673 break;
2674 objectsSize--;
2675 }
2676 }
2677 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002678
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002679 if (mOwner) {
2680 // If the size is going to zero, just release the owner's data.
2681 if (desired == 0) {
2682 freeData();
2683 return NO_ERROR;
2684 }
2685
2686 // If there is a different owner, we need to take
2687 // posession.
2688 uint8_t* data = (uint8_t*)malloc(desired);
2689 if (!data) {
2690 mError = NO_MEMORY;
2691 return NO_MEMORY;
2692 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002693 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002694
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002695 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002696 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002697 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002698 free(data);
2699
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002700 mError = NO_MEMORY;
2701 return NO_MEMORY;
2702 }
2703
2704 // Little hack to only acquire references on objects
2705 // we will be keeping.
2706 size_t oldObjectsSize = mObjectsSize;
2707 mObjectsSize = objectsSize;
2708 acquireObjects();
2709 mObjectsSize = oldObjectsSize;
2710 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002711
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002712 if (mData) {
2713 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2714 }
2715 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002716 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002717 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002718 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002719 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kongfdd8da92018-06-07 17:52:27 -07002720 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002721
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002722 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002723 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002724 gParcelGlobalAllocSize += desired;
2725 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002726 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002727
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002728 mData = data;
2729 mObjects = objects;
2730 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002731 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002732 mDataCapacity = desired;
2733 mObjectsSize = mObjectsCapacity = objectsSize;
2734 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002735 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002736
2737 } else if (mData) {
2738 if (objectsSize < mObjectsSize) {
2739 // Need to release refs on any objects we are dropping.
2740 const sp<ProcessState> proc(ProcessState::self());
2741 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2742 const flat_binder_object* flat
2743 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002744 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002745 // will need to rescan because we may have lopped off the only FDs
2746 mFdsKnown = false;
2747 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002748 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002749 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002750 binder_size_t* objects =
2751 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002752 if (objects) {
2753 mObjects = objects;
2754 }
2755 mObjectsSize = objectsSize;
2756 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002757 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002758 }
2759
2760 // We own the data, so we can just do a realloc().
2761 if (desired > mDataCapacity) {
2762 uint8_t* data = (uint8_t*)realloc(mData, desired);
2763 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002764 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2765 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002766 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002767 gParcelGlobalAllocSize += desired;
2768 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002769 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002770 mData = data;
2771 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002772 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002773 mError = NO_MEMORY;
2774 return NO_MEMORY;
2775 }
2776 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002777 if (mDataSize > desired) {
2778 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002779 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002780 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002781 if (mDataPos > desired) {
2782 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002783 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002784 }
2785 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002786
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002787 } else {
2788 // This is the first data. Easy!
2789 uint8_t* data = (uint8_t*)malloc(desired);
2790 if (!data) {
2791 mError = NO_MEMORY;
2792 return NO_MEMORY;
2793 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002794
Yi Kongfdd8da92018-06-07 17:52:27 -07002795 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002796 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002797 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002798 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002799
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002800 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002801 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002802 gParcelGlobalAllocSize += desired;
2803 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002804 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002805
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002806 mData = data;
2807 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002808 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2809 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002810 mDataCapacity = desired;
2811 }
2812
2813 return NO_ERROR;
2814}
2815
2816void Parcel::initState()
2817{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002818 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002819 mError = NO_ERROR;
Yi Kongfdd8da92018-06-07 17:52:27 -07002820 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002821 mDataSize = 0;
2822 mDataCapacity = 0;
2823 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002824 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2825 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kongfdd8da92018-06-07 17:52:27 -07002826 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002827 mObjectsSize = 0;
2828 mObjectsCapacity = 0;
2829 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002830 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002831 mHasFds = false;
2832 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002833 mAllowFds = true;
Yi Kongfdd8da92018-06-07 17:52:27 -07002834 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002835 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002836
2837 // racing multiple init leads only to multiple identical write
2838 if (gMaxFds == 0) {
2839 struct rlimit result;
2840 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2841 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002842 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002843 } else {
2844 ALOGW("Unable to getrlimit: %s", strerror(errno));
2845 gMaxFds = 1024;
2846 }
2847 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002848}
2849
2850void Parcel::scanForFds() const
2851{
2852 bool hasFds = false;
2853 for (size_t i=0; i<mObjectsSize; i++) {
2854 const flat_binder_object* flat
2855 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002856 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002857 hasFds = true;
2858 break;
2859 }
2860 }
2861 mHasFds = hasFds;
2862 mFdsKnown = true;
2863}
2864
Dan Sandleraa5c2342015-04-10 10:08:45 -04002865size_t Parcel::getBlobAshmemSize() const
2866{
Adrian Roos6bb31142015-10-22 16:46:12 -07002867 // This used to return the size of all blobs that were written to ashmem, now we're returning
2868 // the ashmem currently referenced by this Parcel, which should be equivalent.
2869 // TODO: Remove method once ABI can be changed.
2870 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002871}
2872
Adrian Rooscbf37262015-10-22 16:12:53 -07002873size_t Parcel::getOpenAshmemSize() const
2874{
2875 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002876}
2877
2878// --- Parcel::Blob ---
2879
2880Parcel::Blob::Blob() :
Yi Kongfdd8da92018-06-07 17:52:27 -07002881 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002882}
2883
2884Parcel::Blob::~Blob() {
2885 release();
2886}
2887
2888void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002889 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002890 ::munmap(mData, mSize);
2891 }
2892 clear();
2893}
2894
Jeff Brown13b16042014-11-11 16:44:25 -08002895void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2896 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002897 mData = data;
2898 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002899 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002900}
2901
2902void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002903 mFd = -1;
Yi Kongfdd8da92018-06-07 17:52:27 -07002904 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002905 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002906 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002907}
2908
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002909}; // namespace android