blob: d5ad8c864a6326c49e44f86c40fe7ebfde6d3a01 [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>
40
Mark Salyzynabed7f72016-01-27 08:02:48 -080041#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070042#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080043#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070044#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080045#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#include <utils/String8.h>
47#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070048
Mathias Agopian208059f2009-05-18 15:08:03 -070049#include <private/binder/binder_module.h>
Steven Morelanda4853cd2019-07-12 15:44:37 -070050#include "Static.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070051
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052#ifndef INT32_MAX
53#define INT32_MAX ((int32_t)(2147483647))
54#endif
55
56#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080057//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080058#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080059//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070060
61// ---------------------------------------------------------------------------
62
Nick Kralevichb6b14232015-04-02 09:36:02 -070063// This macro should never be used at runtime, as a too large value
64// of s could cause an integer overflow. Instead, you should always
65// use the wrapper function pad_size()
66#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
67
68static size_t pad_size(size_t s) {
69 if (s > (SIZE_T_MAX - 3)) {
70 abort();
71 }
72 return PAD_SIZE_UNSAFE(s);
73}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070074
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070075// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060076#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070077
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070078namespace android {
79
Dianne Hackborna4cff882014-11-13 17:07:40 -080080static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
81static size_t gParcelGlobalAllocSize = 0;
82static size_t gParcelGlobalAllocCount = 0;
83
Christopher Tatee4e0ae82016-03-24 16:03:44 -070084static size_t gMaxFds = 0;
85
Jeff Brown13b16042014-11-11 16:44:25 -080086// Maximum size of a blob to transfer in-place.
87static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
88
89enum {
90 BLOB_INPLACE = 0,
91 BLOB_ASHMEM_IMMUTABLE = 1,
92 BLOB_ASHMEM_MUTABLE = 2,
93};
94
Steven Morelandb1c81202019-04-05 18:49:55 -070095static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070096 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070097{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -070098 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070099 case BINDER_TYPE_BINDER:
100 if (obj.binder) {
101 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800102 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700103 }
104 return;
105 case BINDER_TYPE_WEAK_BINDER:
106 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800107 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108 return;
109 case BINDER_TYPE_HANDLE: {
110 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700111 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700112 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
113 b->incStrong(who);
114 }
115 return;
116 }
117 case BINDER_TYPE_WEAK_HANDLE: {
118 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700119 if (b != nullptr) b.get_refs()->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700120 return;
121 }
122 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000123 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700124 // If we own an ashmem fd, keep track of how much memory it refers to.
125 int size = ashmem_get_size_region(obj.handle);
126 if (size > 0) {
127 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700128 }
129 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700130 return;
131 }
132 }
133
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700134 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700135}
136
Adrian Roos6bb31142015-10-22 16:46:12 -0700137static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700138 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700140 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700141 case BINDER_TYPE_BINDER:
142 if (obj.binder) {
143 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800144 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700145 }
146 return;
147 case BINDER_TYPE_WEAK_BINDER:
148 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800149 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700150 return;
151 case BINDER_TYPE_HANDLE: {
152 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700153 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700154 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
155 b->decStrong(who);
156 }
157 return;
158 }
159 case BINDER_TYPE_WEAK_HANDLE: {
160 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700161 if (b != nullptr) b.get_refs()->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700162 return;
163 }
164 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800165 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000166 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700167 int size = ashmem_get_size_region(obj.handle);
168 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800169 // ashmem size might have changed since last time it was accounted for, e.g.
170 // in acquire_object(). Value of *outAshmemSize is not critical since we are
171 // releasing the object anyway. Check for integer overflow condition.
172 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700173 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700174 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800175
176 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700177 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700178 return;
179 }
180 }
181
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700182 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700183}
184
185inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800186 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700187{
188 return out->writeObject(flat, false);
189}
190
Steven Morelandb1c81202019-04-05 18:49:55 -0700191static status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700192 const sp<IBinder>& binder, Parcel* out)
193{
194 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700195
Martijn Coenen2b631742017-05-05 11:16:59 -0700196 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
197 /* minimum priority for all nodes is nice 0 */
198 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
199 } else {
200 /* minimum priority for all nodes is MAX_NICE(19) */
201 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
202 }
203
Yi Kong91635562018-06-07 14:38:36 -0700204 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800205 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700206 if (!local) {
207 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700208 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000209 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700210 }
211 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700212 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800213 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700214 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800215 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700216 } else {
Steven Morelandf0212002018-12-26 13:59:23 -0800217 if (local->isRequestingSid()) {
218 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
219 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700220 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800221 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
222 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 }
224 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700225 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800226 obj.binder = 0;
227 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700228 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700229
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230 return finish_flatten_binder(binder, obj, out);
231}
232
Steven Morelandb1c81202019-04-05 18:49:55 -0700233static status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700234 const wp<IBinder>& binder, Parcel* out)
235{
236 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700237
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Yi Kong91635562018-06-07 14:38:36 -0700239 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700240 sp<IBinder> real = binder.promote();
Yi Kong91635562018-06-07 14:38:36 -0700241 if (real != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700242 IBinder *local = real->localBinder();
243 if (!local) {
244 BpBinder *proxy = real->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700245 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000246 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700247 }
248 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700249 obj.hdr.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800250 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700251 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800252 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700253 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700254 obj.hdr.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800255 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
256 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 }
258 return finish_flatten_binder(real, obj, out);
259 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700260
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261 // XXX How to deal? In order to flatten the given binder,
262 // we need to probe it for information, which requires a primary
263 // reference... but we don't have one.
264 //
265 // The OpenBinder implementation uses a dynamic_cast<> here,
266 // but we can't do that with the different reference counting
267 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000268 ALOGE("Unable to unflatten Binder weak reference!");
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700269 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800270 obj.binder = 0;
271 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700272 return finish_flatten_binder(nullptr, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700273
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700274 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700275 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800276 obj.binder = 0;
277 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700278 return finish_flatten_binder(nullptr, obj, out);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700279 }
280}
281
282inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800283 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
284 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700285{
286 return NO_ERROR;
287}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700288
Steven Morelandb1c81202019-04-05 18:49:55 -0700289static status_t unflatten_binder(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700290 const Parcel& in, sp<IBinder>* out)
291{
292 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700293
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700294 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700295 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700296 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800297 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700298 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700299 case BINDER_TYPE_HANDLE:
300 *out = proc->getStrongProxyForHandle(flat->handle);
301 return finish_unflatten_binder(
302 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700303 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700304 }
305 return BAD_TYPE;
306}
307
Steven Morelandb1c81202019-04-05 18:49:55 -0700308static status_t unflatten_binder(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700309 const Parcel& in, wp<IBinder>* out)
310{
311 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700312
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700313 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700314 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700315 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800316 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700317 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700318 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800319 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700320 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800321 reinterpret_cast<IBinder*>(flat->cookie),
322 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700323 } else {
Yi Kong91635562018-06-07 14:38:36 -0700324 *out = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700325 }
Yi Kong91635562018-06-07 14:38:36 -0700326 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700327 case BINDER_TYPE_HANDLE:
328 case BINDER_TYPE_WEAK_HANDLE:
329 *out = proc->getWeakProxyForHandle(flat->handle);
330 return finish_unflatten_binder(
331 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
332 }
333 }
334 return BAD_TYPE;
335}
336
337// ---------------------------------------------------------------------------
338
339Parcel::Parcel()
340{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800341 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700342 initState();
343}
344
345Parcel::~Parcel()
346{
347 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800348 LOG_ALLOC("Parcel %p: destroyed", this);
349}
350
351size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800352 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
353 size_t size = gParcelGlobalAllocSize;
354 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
355 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800356}
357
358size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800359 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
360 size_t count = gParcelGlobalAllocCount;
361 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
362 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700363}
364
365const uint8_t* Parcel::data() const
366{
367 return mData;
368}
369
370size_t Parcel::dataSize() const
371{
372 return (mDataSize > mDataPos ? mDataSize : mDataPos);
373}
374
375size_t Parcel::dataAvail() const
376{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700377 size_t result = dataSize() - dataPosition();
378 if (result > INT32_MAX) {
379 abort();
380 }
381 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700382}
383
384size_t Parcel::dataPosition() const
385{
386 return mDataPos;
387}
388
389size_t Parcel::dataCapacity() const
390{
391 return mDataCapacity;
392}
393
394status_t Parcel::setDataSize(size_t size)
395{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700396 if (size > INT32_MAX) {
397 // don't accept size_t values which may have come from an
398 // inadvertent conversion from a negative int.
399 return BAD_VALUE;
400 }
401
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700402 status_t err;
403 err = continueWrite(size);
404 if (err == NO_ERROR) {
405 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700406 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700407 }
408 return err;
409}
410
411void Parcel::setDataPosition(size_t pos) const
412{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700413 if (pos > INT32_MAX) {
414 // don't accept size_t values which may have come from an
415 // inadvertent conversion from a negative int.
416 abort();
417 }
418
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700419 mDataPos = pos;
420 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800421 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700422}
423
424status_t Parcel::setDataCapacity(size_t size)
425{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700426 if (size > INT32_MAX) {
427 // don't accept size_t values which may have come from an
428 // inadvertent conversion from a negative int.
429 return BAD_VALUE;
430 }
431
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700432 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700433 return NO_ERROR;
434}
435
436status_t Parcel::setData(const uint8_t* buffer, size_t len)
437{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700438 if (len > INT32_MAX) {
439 // don't accept size_t values which may have come from an
440 // inadvertent conversion from a negative int.
441 return BAD_VALUE;
442 }
443
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700444 status_t err = restartWrite(len);
445 if (err == NO_ERROR) {
446 memcpy(const_cast<uint8_t*>(data()), buffer, len);
447 mDataSize = len;
448 mFdsKnown = false;
449 }
450 return err;
451}
452
Andreas Huber51faf462011-04-13 10:21:56 -0700453status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700454{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700455 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700456 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800457 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700458 size_t size = parcel->mObjectsSize;
459 int startPos = mDataPos;
460 int firstIndex = -1, lastIndex = -2;
461
462 if (len == 0) {
463 return NO_ERROR;
464 }
465
Nick Kralevichb6b14232015-04-02 09:36:02 -0700466 if (len > INT32_MAX) {
467 // don't accept size_t values which may have come from an
468 // inadvertent conversion from a negative int.
469 return BAD_VALUE;
470 }
471
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700472 // range checks against the source parcel size
473 if ((offset > parcel->mDataSize)
474 || (len > parcel->mDataSize)
475 || (offset + len > parcel->mDataSize)) {
476 return BAD_VALUE;
477 }
478
479 // Count objects in range
480 for (int i = 0; i < (int) size; i++) {
481 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700482 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700483 if (firstIndex == -1) {
484 firstIndex = i;
485 }
486 lastIndex = i;
487 }
488 }
489 int numObjects = lastIndex - firstIndex + 1;
490
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700491 if ((mDataSize+len) > mDataCapacity) {
492 // grow data
493 err = growData(len);
494 if (err != NO_ERROR) {
495 return err;
496 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700497 }
498
499 // append data
500 memcpy(mData + mDataPos, data + offset, len);
501 mDataPos += len;
502 mDataSize += len;
503
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400504 err = NO_ERROR;
505
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700506 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200507 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700508 // grow objects
509 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700510 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700511 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800512 binder_size_t *objects =
513 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700514 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700515 return NO_MEMORY;
516 }
517 mObjects = objects;
518 mObjectsCapacity = newSize;
519 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700520
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521 // append and acquire objects
522 int idx = mObjectsSize;
523 for (int i = firstIndex; i <= lastIndex; i++) {
524 size_t off = objects[i] - offset + startPos;
525 mObjects[idx++] = off;
526 mObjectsSize++;
527
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700528 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700529 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700530 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700531
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700532 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700533 // If this is a file descriptor, we need to dup it so the
534 // new Parcel now owns its own fd, and can declare that we
535 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800536 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800537 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700538 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400539 if (!mAllowFds) {
540 err = FDS_NOT_ALLOWED;
541 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700542 }
543 }
544 }
545
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400546 return err;
547}
548
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700549int Parcel::compareData(const Parcel& other) {
550 size_t size = dataSize();
551 if (size != other.dataSize()) {
552 return size < other.dataSize() ? -1 : 1;
553 }
554 return memcmp(data(), other.data(), size);
555}
556
Jeff Brown13b16042014-11-11 16:44:25 -0800557bool Parcel::allowFds() const
558{
559 return mAllowFds;
560}
561
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700562bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400563{
564 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700565 if (!allowFds) {
566 mAllowFds = false;
567 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400568 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700569}
570
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700571void Parcel::restoreAllowFds(bool lastValue)
572{
573 mAllowFds = lastValue;
574}
575
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700576bool Parcel::hasFileDescriptors() const
577{
578 if (!mFdsKnown) {
579 scanForFds();
580 }
581 return mHasFds;
582}
583
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000584void Parcel::updateWorkSourceRequestHeaderPosition() const {
585 // Only update the request headers once. We only want to point
586 // to the first headers read/written.
587 if (!mRequestHeaderPresent) {
588 mWorkSourceRequestHeaderPosition = dataPosition();
589 mRequestHeaderPresent = true;
590 }
591}
592
Steven Morelandd70160f2019-07-23 10:20:38 -0700593#ifdef __ANDROID_VNDK__
594constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
595#else
596constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
597#endif
598
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700599// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700600status_t Parcel::writeInterfaceToken(const String16& interface)
601{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000602 const IPCThreadState* threadState = IPCThreadState::self();
603 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000604 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000605 writeInt32(threadState->shouldPropagateWorkSource() ?
606 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700607 writeInt32(kHeader);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700608 // currently the interface identification token is just its name as a string
609 return writeString16(interface);
610}
611
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000612bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
613{
614 if (!mRequestHeaderPresent) {
615 return false;
616 }
617
618 const size_t initialPosition = dataPosition();
619 setDataPosition(mWorkSourceRequestHeaderPosition);
620 status_t err = writeInt32(uid);
621 setDataPosition(initialPosition);
622 return err == NO_ERROR;
623}
624
625uid_t Parcel::readCallingWorkSourceUid()
626{
627 if (!mRequestHeaderPresent) {
628 return IPCThreadState::kUnsetWorkSource;
629 }
630
631 const size_t initialPosition = dataPosition();
632 setDataPosition(mWorkSourceRequestHeaderPosition);
633 uid_t uid = readInt32();
634 setDataPosition(initialPosition);
635 return uid;
636}
637
Mathias Agopian83c04462009-05-22 19:00:22 -0700638bool Parcel::checkInterface(IBinder* binder) const
639{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700640 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700641}
642
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700643bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700644 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700645{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100646 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700647 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700648 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700649 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700650 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700651 if ((threadState->getLastTransactionBinderFlags() &
652 IBinder::FLAG_ONEWAY) != 0) {
653 // For one-way calls, the callee is running entirely
654 // disconnected from the caller, so disable StrictMode entirely.
655 // Not only does disk/network usage not impact the caller, but
656 // there's no way to commuicate back any violations anyway.
657 threadState->setStrictModePolicy(0);
658 } else {
659 threadState->setStrictModePolicy(strictPolicy);
660 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100661 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000662 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100663 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000664 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Morelandd70160f2019-07-23 10:20:38 -0700665 // vendor header
666 int32_t header = readInt32();
667 if (header != kHeader) {
668 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
669 return false;
670 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100671 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700672 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700673 if (str == interface) {
674 return true;
675 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700676 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700677 String8(interface).string(), String8(str).string());
678 return false;
679 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700680}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700681
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700682size_t Parcel::objectsCount() const
683{
684 return mObjectsSize;
685}
686
687status_t Parcel::errorCheck() const
688{
689 return mError;
690}
691
692void Parcel::setError(status_t err)
693{
694 mError = err;
695}
696
697status_t Parcel::finishWrite(size_t len)
698{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700699 if (len > INT32_MAX) {
700 // don't accept size_t values which may have come from an
701 // inadvertent conversion from a negative int.
702 return BAD_VALUE;
703 }
704
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700705 //printf("Finish write of %d\n", len);
706 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700707 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700708 if (mDataPos > mDataSize) {
709 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700710 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700711 }
712 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
713 return NO_ERROR;
714}
715
716status_t Parcel::writeUnpadded(const void* data, size_t len)
717{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700718 if (len > INT32_MAX) {
719 // don't accept size_t values which may have come from an
720 // inadvertent conversion from a negative int.
721 return BAD_VALUE;
722 }
723
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700724 size_t end = mDataPos + len;
725 if (end < mDataPos) {
726 // integer overflow
727 return BAD_VALUE;
728 }
729
730 if (end <= mDataCapacity) {
731restart_write:
732 memcpy(mData+mDataPos, data, len);
733 return finishWrite(len);
734 }
735
736 status_t err = growData(len);
737 if (err == NO_ERROR) goto restart_write;
738 return err;
739}
740
741status_t Parcel::write(const void* data, size_t len)
742{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700743 if (len > INT32_MAX) {
744 // don't accept size_t values which may have come from an
745 // inadvertent conversion from a negative int.
746 return BAD_VALUE;
747 }
748
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700749 void* const d = writeInplace(len);
750 if (d) {
751 memcpy(d, data, len);
752 return NO_ERROR;
753 }
754 return mError;
755}
756
757void* Parcel::writeInplace(size_t len)
758{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700759 if (len > INT32_MAX) {
760 // don't accept size_t values which may have come from an
761 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700762 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700763 }
764
765 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700766
767 // sanity check for integer overflow
768 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700769 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700770 }
771
772 if ((mDataPos+padded) <= mDataCapacity) {
773restart_write:
774 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
775 uint8_t* const data = mData+mDataPos;
776
777 // Need to pad at end?
778 if (padded != len) {
779#if BYTE_ORDER == BIG_ENDIAN
780 static const uint32_t mask[4] = {
781 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
782 };
783#endif
784#if BYTE_ORDER == LITTLE_ENDIAN
785 static const uint32_t mask[4] = {
786 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
787 };
788#endif
789 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
790 // *reinterpret_cast<void**>(data+padded-4));
791 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
792 }
793
794 finishWrite(padded);
795 return data;
796 }
797
798 status_t err = growData(padded);
799 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700800 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700801}
802
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800803status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
804 const uint8_t* strData = (uint8_t*)str.data();
805 const size_t strLen= str.length();
806 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100807 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800808 return BAD_VALUE;
809 }
810
811 status_t err = writeInt32(utf16Len);
812 if (err) {
813 return err;
814 }
815
816 // Allocate enough bytes to hold our converted string and its terminating NULL.
817 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
818 if (!dst) {
819 return NO_MEMORY;
820 }
821
Sergio Girof4607432016-07-21 14:46:35 +0100822 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800823
824 return NO_ERROR;
825}
826
827status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
828 if (!str) {
829 return writeInt32(-1);
830 }
831 return writeUtf8AsUtf16(*str);
832}
833
Casey Dahlin185d3442016-02-09 11:08:35 -0800834namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800835
Casey Dahlin185d3442016-02-09 11:08:35 -0800836template<typename T>
837status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700838{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700839 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700840 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700841 status = BAD_VALUE;
842 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700843 }
844
Casey Dahlin185d3442016-02-09 11:08:35 -0800845 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700846 if (status != OK) {
847 return status;
848 }
849
Casey Dahlin185d3442016-02-09 11:08:35 -0800850 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700851 if (!data) {
852 status = BAD_VALUE;
853 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700854 }
855
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700856 memcpy(data, val.data(), val.size());
857 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700858}
859
Casey Dahlin185d3442016-02-09 11:08:35 -0800860template<typename T>
861status_t writeByteVectorInternalPtr(Parcel* parcel,
862 const std::unique_ptr<std::vector<T>>& val)
863{
864 if (!val) {
865 return parcel->writeInt32(-1);
866 }
867
868 return writeByteVectorInternal(parcel, *val);
869}
870
871} // namespace
872
873status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
874 return writeByteVectorInternal(this, val);
875}
876
877status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
878{
879 return writeByteVectorInternalPtr(this, val);
880}
881
882status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
883 return writeByteVectorInternal(this, val);
884}
885
886status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
887{
888 return writeByteVectorInternalPtr(this, val);
889}
890
Casey Dahlin451ff582015-10-19 18:12:18 -0700891status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
892{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800893 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700894}
895
Casey Dahlinb9872622015-11-25 15:09:45 -0800896status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
897{
898 return writeNullableTypedVector(val, &Parcel::writeInt32);
899}
900
Casey Dahlin451ff582015-10-19 18:12:18 -0700901status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
902{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800903 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700904}
905
Casey Dahlinb9872622015-11-25 15:09:45 -0800906status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
907{
908 return writeNullableTypedVector(val, &Parcel::writeInt64);
909}
910
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800911status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
912{
913 return writeTypedVector(val, &Parcel::writeUint64);
914}
915
916status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
917{
918 return writeNullableTypedVector(val, &Parcel::writeUint64);
919}
920
Casey Dahlin451ff582015-10-19 18:12:18 -0700921status_t Parcel::writeFloatVector(const std::vector<float>& val)
922{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800923 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700924}
925
Casey Dahlinb9872622015-11-25 15:09:45 -0800926status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
927{
928 return writeNullableTypedVector(val, &Parcel::writeFloat);
929}
930
Casey Dahlin451ff582015-10-19 18:12:18 -0700931status_t Parcel::writeDoubleVector(const std::vector<double>& val)
932{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800933 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700934}
935
Casey Dahlinb9872622015-11-25 15:09:45 -0800936status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
937{
938 return writeNullableTypedVector(val, &Parcel::writeDouble);
939}
940
Casey Dahlin451ff582015-10-19 18:12:18 -0700941status_t Parcel::writeBoolVector(const std::vector<bool>& val)
942{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800943 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700944}
945
Casey Dahlinb9872622015-11-25 15:09:45 -0800946status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
947{
948 return writeNullableTypedVector(val, &Parcel::writeBool);
949}
950
Casey Dahlin451ff582015-10-19 18:12:18 -0700951status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
952{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800953 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700954}
955
Casey Dahlinb9872622015-11-25 15:09:45 -0800956status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
957{
958 return writeNullableTypedVector(val, &Parcel::writeChar);
959}
960
Casey Dahlin451ff582015-10-19 18:12:18 -0700961status_t Parcel::writeString16Vector(const std::vector<String16>& val)
962{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800963 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700964}
965
Casey Dahlinb9872622015-11-25 15:09:45 -0800966status_t Parcel::writeString16Vector(
967 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
968{
969 return writeNullableTypedVector(val, &Parcel::writeString16);
970}
971
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800972status_t Parcel::writeUtf8VectorAsUtf16Vector(
973 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
974 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
975}
976
977status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
978 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
979}
980
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700981status_t Parcel::writeInt32(int32_t val)
982{
Andreas Huber84a6d042009-08-17 13:33:27 -0700983 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700984}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800985
986status_t Parcel::writeUint32(uint32_t val)
987{
988 return writeAligned(val);
989}
990
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700991status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700992 if (len > INT32_MAX) {
993 // don't accept size_t values which may have come from an
994 // inadvertent conversion from a negative int.
995 return BAD_VALUE;
996 }
997
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700998 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700999 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001000 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001001 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001002 if (ret == NO_ERROR) {
1003 ret = write(val, len * sizeof(*val));
1004 }
1005 return ret;
1006}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001007status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001008 if (len > INT32_MAX) {
1009 // don't accept size_t values which may have come from an
1010 // inadvertent conversion from a negative int.
1011 return BAD_VALUE;
1012 }
1013
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001014 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001015 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001016 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001017 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001018 if (ret == NO_ERROR) {
1019 ret = write(val, len * sizeof(*val));
1020 }
1021 return ret;
1022}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001023
Casey Dahlind6848f52015-10-15 15:44:59 -07001024status_t Parcel::writeBool(bool val)
1025{
1026 return writeInt32(int32_t(val));
1027}
1028
1029status_t Parcel::writeChar(char16_t val)
1030{
1031 return writeInt32(int32_t(val));
1032}
1033
1034status_t Parcel::writeByte(int8_t val)
1035{
1036 return writeInt32(int32_t(val));
1037}
1038
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001039status_t Parcel::writeInt64(int64_t val)
1040{
Andreas Huber84a6d042009-08-17 13:33:27 -07001041 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001042}
1043
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001044status_t Parcel::writeUint64(uint64_t val)
1045{
1046 return writeAligned(val);
1047}
1048
Serban Constantinescuf683e012013-11-05 16:53:55 +00001049status_t Parcel::writePointer(uintptr_t val)
1050{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001051 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001052}
1053
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001054status_t Parcel::writeFloat(float val)
1055{
Andreas Huber84a6d042009-08-17 13:33:27 -07001056 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001057}
1058
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001059#if defined(__mips__) && defined(__mips_hard_float)
1060
1061status_t Parcel::writeDouble(double val)
1062{
1063 union {
1064 double d;
1065 unsigned long long ll;
1066 } u;
1067 u.d = val;
1068 return writeAligned(u.ll);
1069}
1070
1071#else
1072
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001073status_t Parcel::writeDouble(double val)
1074{
Andreas Huber84a6d042009-08-17 13:33:27 -07001075 return writeAligned(val);
1076}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001077
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001078#endif
1079
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001080status_t Parcel::writeCString(const char* str)
1081{
1082 return write(str, strlen(str)+1);
1083}
1084
1085status_t Parcel::writeString8(const String8& str)
1086{
1087 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001088 // only write string if its length is more than zero characters,
1089 // as readString8 will only read if the length field is non-zero.
1090 // this is slightly different from how writeString16 works.
1091 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001092 err = write(str.string(), str.bytes()+1);
1093 }
1094 return err;
1095}
1096
Casey Dahlinb9872622015-11-25 15:09:45 -08001097status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1098{
1099 if (!str) {
1100 return writeInt32(-1);
1101 }
1102
1103 return writeString16(*str);
1104}
1105
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001106status_t Parcel::writeString16(const String16& str)
1107{
1108 return writeString16(str.string(), str.size());
1109}
1110
1111status_t Parcel::writeString16(const char16_t* str, size_t len)
1112{
Yi Kong91635562018-06-07 14:38:36 -07001113 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001114
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001115 status_t err = writeInt32(len);
1116 if (err == NO_ERROR) {
1117 len *= sizeof(char16_t);
1118 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1119 if (data) {
1120 memcpy(data, str, len);
1121 *reinterpret_cast<char16_t*>(data+len) = 0;
1122 return NO_ERROR;
1123 }
1124 err = mError;
1125 }
1126 return err;
1127}
1128
1129status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1130{
1131 return flatten_binder(ProcessState::self(), val, this);
1132}
1133
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001134status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1135{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001136 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001137}
1138
Casey Dahlinb9872622015-11-25 15:09:45 -08001139status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1140{
1141 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1142}
1143
1144status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001145 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001146}
1147
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001148status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001149 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001150}
1151
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001152status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1153{
1154 return flatten_binder(ProcessState::self(), val, this);
1155}
1156
Casey Dahlinb9872622015-11-25 15:09:45 -08001157status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1158 if (!parcelable) {
1159 return writeInt32(0);
1160 }
1161
1162 return writeParcelable(*parcelable);
1163}
1164
Christopher Wiley97f048d2015-11-19 06:49:05 -08001165status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1166 status_t status = writeInt32(1); // parcelable is not null.
1167 if (status != OK) {
1168 return status;
1169 }
1170 return parcelable.writeToParcel(this);
1171}
1172
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001173status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001174{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001175 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001176 return BAD_TYPE;
1177
1178 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001179 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001180 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001182 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001183 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001185 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1186 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187
1188 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001189 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 return err;
1191 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001192 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001193 return err;
1194}
1195
Jeff Brown93ff1f92011-11-04 19:01:44 -07001196status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001197{
1198 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001199 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001200 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001201 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001202 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001203 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001204 return writeObject(obj, true);
1205}
1206
1207status_t Parcel::writeDupFileDescriptor(int fd)
1208{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001209 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001210 if (dupFd < 0) {
1211 return -errno;
1212 }
1213 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001214 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001215 close(dupFd);
1216 }
1217 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001218}
1219
Dianne Hackborn1941a402016-08-29 12:30:43 -07001220status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1221{
1222 writeInt32(0);
1223 return writeFileDescriptor(fd, takeOwnership);
1224}
1225
Ryo Hashimotobf551892018-05-31 16:58:35 +09001226status_t Parcel::writeDupParcelFileDescriptor(int fd)
1227{
1228 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1229 if (dupFd < 0) {
1230 return -errno;
1231 }
1232 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1233 if (err != OK) {
1234 close(dupFd);
1235 }
1236 return err;
1237}
1238
Christopher Wiley2cf19952016-04-11 11:09:37 -07001239status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001240 return writeDupFileDescriptor(fd.get());
1241}
1242
Christopher Wiley2cf19952016-04-11 11:09:37 -07001243status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001244 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1245}
1246
Christopher Wiley2cf19952016-04-11 11:09:37 -07001247status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001248 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1249}
1250
Jeff Brown13b16042014-11-11 16:44:25 -08001251status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001252{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001253 if (len > INT32_MAX) {
1254 // don't accept size_t values which may have come from an
1255 // inadvertent conversion from a negative int.
1256 return BAD_VALUE;
1257 }
1258
Jeff Brown13b16042014-11-11 16:44:25 -08001259 status_t status;
1260 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001261 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001262 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001263 if (status) return status;
1264
1265 void* ptr = writeInplace(len);
1266 if (!ptr) return NO_MEMORY;
1267
Jeff Brown13b16042014-11-11 16:44:25 -08001268 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001269 return NO_ERROR;
1270 }
1271
Steve Block6807e592011-10-20 11:56:00 +01001272 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001273 int fd = ashmem_create_region("Parcel Blob", len);
1274 if (fd < 0) return NO_MEMORY;
1275
1276 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1277 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001278 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001279 } else {
Yi Kong91635562018-06-07 14:38:36 -07001280 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001281 if (ptr == MAP_FAILED) {
1282 status = -errno;
1283 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001284 if (!mutableCopy) {
1285 result = ashmem_set_prot_region(fd, PROT_READ);
1286 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001287 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001288 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001289 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001290 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001291 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001292 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001293 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001294 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001295 return NO_ERROR;
1296 }
1297 }
1298 }
1299 }
1300 ::munmap(ptr, len);
1301 }
1302 ::close(fd);
1303 return status;
1304}
1305
Jeff Brown13b16042014-11-11 16:44:25 -08001306status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1307{
1308 // Must match up with what's done in writeBlob.
1309 if (!mAllowFds) return FDS_NOT_ALLOWED;
1310 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1311 if (status) return status;
1312 return writeDupFileDescriptor(fd);
1313}
1314
Mathias Agopiane1424282013-07-29 21:24:40 -07001315status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001316{
1317 status_t err;
1318
1319 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001320 const size_t len = val.getFlattenedSize();
1321 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001322
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001323 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001324 // don't accept size_t values which may have come from an
1325 // inadvertent conversion from a negative int.
1326 return BAD_VALUE;
1327 }
1328
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001329 err = this->writeInt32(len);
1330 if (err) return err;
1331
1332 err = this->writeInt32(fd_count);
1333 if (err) return err;
1334
1335 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001336 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001337 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001338 return BAD_VALUE;
1339
Yi Kong91635562018-06-07 14:38:36 -07001340 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001341 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001342 fds = new (std::nothrow) int[fd_count];
1343 if (fds == nullptr) {
1344 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1345 return BAD_VALUE;
1346 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001347 }
1348
1349 err = val.flatten(buf, len, fds, fd_count);
1350 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1351 err = this->writeDupFileDescriptor( fds[i] );
1352 }
1353
1354 if (fd_count) {
1355 delete [] fds;
1356 }
1357
1358 return err;
1359}
1360
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001361status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1362{
1363 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1364 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1365 if (enoughData && enoughObjects) {
1366restart_write:
1367 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001368
Christopher Tate98e67d32015-06-03 18:44:15 -07001369 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001370 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001371 if (!mAllowFds) {
1372 // fail before modifying our object index
1373 return FDS_NOT_ALLOWED;
1374 }
1375 mHasFds = mFdsKnown = true;
1376 }
1377
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001378 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001379 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001380 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001381 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001382 mObjectsSize++;
1383 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001384
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001385 return finishWrite(sizeof(flat_binder_object));
1386 }
1387
1388 if (!enoughData) {
1389 const status_t err = growData(sizeof(val));
1390 if (err != NO_ERROR) return err;
1391 }
1392 if (!enoughObjects) {
1393 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001394 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001395 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001396 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001397 mObjects = objects;
1398 mObjectsCapacity = newSize;
1399 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001400
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001401 goto restart_write;
1402}
1403
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001404status_t Parcel::writeNoException()
1405{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001406 binder::Status status;
1407 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001408}
1409
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001410status_t Parcel::validateReadData(size_t upperBound) const
1411{
1412 // Don't allow non-object reads on object data
1413 if (mObjectsSorted || mObjectsSize <= 1) {
1414data_sorted:
1415 // Expect to check only against the next object
1416 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1417 // For some reason the current read position is greater than the next object
1418 // hint. Iterate until we find the right object
1419 size_t nextObject = mNextObjectHint;
1420 do {
1421 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1422 // Requested info overlaps with an object
1423 ALOGE("Attempt to read from protected data in Parcel %p", this);
1424 return PERMISSION_DENIED;
1425 }
1426 nextObject++;
1427 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1428 mNextObjectHint = nextObject;
1429 }
1430 return NO_ERROR;
1431 }
1432 // Quickly determine if mObjects is sorted.
1433 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1434 binder_size_t* prevObj = currObj;
1435 while (currObj > mObjects) {
1436 prevObj--;
1437 if(*prevObj > *currObj) {
1438 goto data_unsorted;
1439 }
1440 currObj--;
1441 }
1442 mObjectsSorted = true;
1443 goto data_sorted;
1444
1445data_unsorted:
1446 // Insertion Sort mObjects
1447 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1448 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1449 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1450 binder_size_t temp = *iter0;
1451 binder_size_t* iter1 = iter0 - 1;
1452 while (iter1 >= mObjects && *iter1 > temp) {
1453 *(iter1 + 1) = *iter1;
1454 iter1--;
1455 }
1456 *(iter1 + 1) = temp;
1457 }
1458 mNextObjectHint = 0;
1459 mObjectsSorted = true;
1460 goto data_sorted;
1461}
1462
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001463status_t Parcel::read(void* outData, size_t len) const
1464{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001465 if (len > INT32_MAX) {
1466 // don't accept size_t values which may have come from an
1467 // inadvertent conversion from a negative int.
1468 return BAD_VALUE;
1469 }
1470
1471 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1472 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001473 if (mObjectsSize > 0) {
1474 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001475 if(err != NO_ERROR) {
1476 // Still increment the data position by the expected length
1477 mDataPos += pad_size(len);
1478 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1479 return err;
1480 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001481 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001482 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001483 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001484 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001485 return NO_ERROR;
1486 }
1487 return NOT_ENOUGH_DATA;
1488}
1489
1490const void* Parcel::readInplace(size_t len) const
1491{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001492 if (len > INT32_MAX) {
1493 // don't accept size_t values which may have come from an
1494 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001495 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001496 }
1497
1498 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1499 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001500 if (mObjectsSize > 0) {
1501 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001502 if(err != NO_ERROR) {
1503 // Still increment the data position by the expected length
1504 mDataPos += pad_size(len);
1505 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001506 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001507 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001508 }
1509
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001510 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001511 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001512 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001513 return data;
1514 }
Yi Kong91635562018-06-07 14:38:36 -07001515 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001516}
1517
Andreas Huber84a6d042009-08-17 13:33:27 -07001518template<class T>
1519status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001520 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001521
1522 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001523 if (mObjectsSize > 0) {
1524 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001525 if(err != NO_ERROR) {
1526 // Still increment the data position by the expected length
1527 mDataPos += sizeof(T);
1528 return err;
1529 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001530 }
1531
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001532 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001533 mDataPos += sizeof(T);
1534 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001535 return NO_ERROR;
1536 } else {
1537 return NOT_ENOUGH_DATA;
1538 }
1539}
1540
Andreas Huber84a6d042009-08-17 13:33:27 -07001541template<class T>
1542T Parcel::readAligned() const {
1543 T result;
1544 if (readAligned(&result) != NO_ERROR) {
1545 result = 0;
1546 }
1547
1548 return result;
1549}
1550
1551template<class T>
1552status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001553 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001554
1555 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1556restart_write:
1557 *reinterpret_cast<T*>(mData+mDataPos) = val;
1558 return finishWrite(sizeof(val));
1559 }
1560
1561 status_t err = growData(sizeof(val));
1562 if (err == NO_ERROR) goto restart_write;
1563 return err;
1564}
1565
Casey Dahlin185d3442016-02-09 11:08:35 -08001566namespace {
1567
1568template<typename T>
1569status_t readByteVectorInternal(const Parcel* parcel,
1570 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001571 val->clear();
1572
1573 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001574 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001575
1576 if (status != OK) {
1577 return status;
1578 }
1579
Christopher Wiley4db672d2015-11-10 09:44:30 -08001580 if (size < 0) {
1581 status = UNEXPECTED_NULL;
1582 return status;
1583 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001584 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001585 status = BAD_VALUE;
1586 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001587 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001588
Paul Lietar433e87b2016-09-16 10:39:32 -07001589 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001590 if (!data) {
1591 status = BAD_VALUE;
1592 return status;
1593 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001594 val->reserve(size);
1595 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001596
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001597 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001598}
1599
Casey Dahlin185d3442016-02-09 11:08:35 -08001600template<typename T>
1601status_t readByteVectorInternalPtr(
1602 const Parcel* parcel,
1603 std::unique_ptr<std::vector<T>>* val) {
1604 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001605 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001606 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001607 val->reset();
1608
1609 if (status != OK || size < 0) {
1610 return status;
1611 }
1612
Casey Dahlin185d3442016-02-09 11:08:35 -08001613 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001614 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001615
Casey Dahlin185d3442016-02-09 11:08:35 -08001616 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001617
1618 if (status != OK) {
1619 val->reset();
1620 }
1621
1622 return status;
1623}
1624
Casey Dahlin185d3442016-02-09 11:08:35 -08001625} // namespace
1626
1627status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1628 return readByteVectorInternal(this, val);
1629}
1630
1631status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1632 return readByteVectorInternal(this, val);
1633}
1634
1635status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1636 return readByteVectorInternalPtr(this, val);
1637}
1638
1639status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1640 return readByteVectorInternalPtr(this, val);
1641}
1642
Casey Dahlinb9872622015-11-25 15:09:45 -08001643status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1644 return readNullableTypedVector(val, &Parcel::readInt32);
1645}
1646
Casey Dahlin451ff582015-10-19 18:12:18 -07001647status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001648 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001649}
1650
Casey Dahlinb9872622015-11-25 15:09:45 -08001651status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1652 return readNullableTypedVector(val, &Parcel::readInt64);
1653}
1654
Casey Dahlin451ff582015-10-19 18:12:18 -07001655status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001656 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001657}
1658
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001659status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1660 return readNullableTypedVector(val, &Parcel::readUint64);
1661}
1662
1663status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1664 return readTypedVector(val, &Parcel::readUint64);
1665}
1666
Casey Dahlinb9872622015-11-25 15:09:45 -08001667status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1668 return readNullableTypedVector(val, &Parcel::readFloat);
1669}
1670
Casey Dahlin451ff582015-10-19 18:12:18 -07001671status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001672 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001673}
1674
Casey Dahlinb9872622015-11-25 15:09:45 -08001675status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1676 return readNullableTypedVector(val, &Parcel::readDouble);
1677}
1678
Casey Dahlin451ff582015-10-19 18:12:18 -07001679status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001680 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001681}
1682
Casey Dahlinb9872622015-11-25 15:09:45 -08001683status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1684 const int32_t start = dataPosition();
1685 int32_t size;
1686 status_t status = readInt32(&size);
1687 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001688
Casey Dahlinb9872622015-11-25 15:09:45 -08001689 if (status != OK || size < 0) {
1690 return status;
1691 }
1692
1693 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001694 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001695
1696 status = readBoolVector(val->get());
1697
1698 if (status != OK) {
1699 val->reset();
1700 }
1701
1702 return status;
1703}
1704
1705status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001706 int32_t size;
1707 status_t status = readInt32(&size);
1708
1709 if (status != OK) {
1710 return status;
1711 }
1712
1713 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001714 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001715 }
1716
1717 val->resize(size);
1718
1719 /* C++ bool handling means a vector of bools isn't necessarily addressable
1720 * (we might use individual bits)
1721 */
Christopher Wiley97887982015-10-27 16:33:47 -07001722 bool data;
1723 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001724 status = readBool(&data);
1725 (*val)[i] = data;
1726
1727 if (status != OK) {
1728 return status;
1729 }
1730 }
1731
1732 return OK;
1733}
1734
Casey Dahlinb9872622015-11-25 15:09:45 -08001735status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1736 return readNullableTypedVector(val, &Parcel::readChar);
1737}
1738
Casey Dahlin451ff582015-10-19 18:12:18 -07001739status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001740 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001741}
1742
Casey Dahlinb9872622015-11-25 15:09:45 -08001743status_t Parcel::readString16Vector(
1744 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1745 return readNullableTypedVector(val, &Parcel::readString16);
1746}
1747
Casey Dahlin451ff582015-10-19 18:12:18 -07001748status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001749 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001750}
1751
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001752status_t Parcel::readUtf8VectorFromUtf16Vector(
1753 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1754 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1755}
1756
1757status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1758 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1759}
Casey Dahlin451ff582015-10-19 18:12:18 -07001760
Andreas Huber84a6d042009-08-17 13:33:27 -07001761status_t Parcel::readInt32(int32_t *pArg) const
1762{
1763 return readAligned(pArg);
1764}
1765
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001766int32_t Parcel::readInt32() const
1767{
Andreas Huber84a6d042009-08-17 13:33:27 -07001768 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001769}
1770
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001771status_t Parcel::readUint32(uint32_t *pArg) const
1772{
1773 return readAligned(pArg);
1774}
1775
1776uint32_t Parcel::readUint32() const
1777{
1778 return readAligned<uint32_t>();
1779}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001780
1781status_t Parcel::readInt64(int64_t *pArg) const
1782{
Andreas Huber84a6d042009-08-17 13:33:27 -07001783 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001784}
1785
1786
1787int64_t Parcel::readInt64() const
1788{
Andreas Huber84a6d042009-08-17 13:33:27 -07001789 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001790}
1791
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001792status_t Parcel::readUint64(uint64_t *pArg) const
1793{
1794 return readAligned(pArg);
1795}
1796
1797uint64_t Parcel::readUint64() const
1798{
1799 return readAligned<uint64_t>();
1800}
1801
Serban Constantinescuf683e012013-11-05 16:53:55 +00001802status_t Parcel::readPointer(uintptr_t *pArg) const
1803{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001804 status_t ret;
1805 binder_uintptr_t ptr;
1806 ret = readAligned(&ptr);
1807 if (!ret)
1808 *pArg = ptr;
1809 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001810}
1811
1812uintptr_t Parcel::readPointer() const
1813{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001814 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001815}
1816
1817
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001818status_t Parcel::readFloat(float *pArg) const
1819{
Andreas Huber84a6d042009-08-17 13:33:27 -07001820 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001821}
1822
1823
1824float Parcel::readFloat() const
1825{
Andreas Huber84a6d042009-08-17 13:33:27 -07001826 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001827}
1828
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001829#if defined(__mips__) && defined(__mips_hard_float)
1830
1831status_t Parcel::readDouble(double *pArg) const
1832{
1833 union {
1834 double d;
1835 unsigned long long ll;
1836 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001837 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001838 status_t status;
1839 status = readAligned(&u.ll);
1840 *pArg = u.d;
1841 return status;
1842}
1843
1844double Parcel::readDouble() const
1845{
1846 union {
1847 double d;
1848 unsigned long long ll;
1849 } u;
1850 u.ll = readAligned<unsigned long long>();
1851 return u.d;
1852}
1853
1854#else
1855
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001856status_t Parcel::readDouble(double *pArg) const
1857{
Andreas Huber84a6d042009-08-17 13:33:27 -07001858 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001859}
1860
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001861double Parcel::readDouble() const
1862{
Andreas Huber84a6d042009-08-17 13:33:27 -07001863 return readAligned<double>();
1864}
1865
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001866#endif
1867
Andreas Huber84a6d042009-08-17 13:33:27 -07001868status_t Parcel::readIntPtr(intptr_t *pArg) const
1869{
1870 return readAligned(pArg);
1871}
1872
1873
1874intptr_t Parcel::readIntPtr() const
1875{
1876 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001877}
1878
Casey Dahlind6848f52015-10-15 15:44:59 -07001879status_t Parcel::readBool(bool *pArg) const
1880{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001881 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001882 status_t ret = readInt32(&tmp);
1883 *pArg = (tmp != 0);
1884 return ret;
1885}
1886
1887bool Parcel::readBool() const
1888{
1889 return readInt32() != 0;
1890}
1891
1892status_t Parcel::readChar(char16_t *pArg) const
1893{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001894 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001895 status_t ret = readInt32(&tmp);
1896 *pArg = char16_t(tmp);
1897 return ret;
1898}
1899
1900char16_t Parcel::readChar() const
1901{
1902 return char16_t(readInt32());
1903}
1904
1905status_t Parcel::readByte(int8_t *pArg) const
1906{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001907 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001908 status_t ret = readInt32(&tmp);
1909 *pArg = int8_t(tmp);
1910 return ret;
1911}
1912
1913int8_t Parcel::readByte() const
1914{
1915 return int8_t(readInt32());
1916}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001917
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001918status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1919 size_t utf16Size = 0;
1920 const char16_t* src = readString16Inplace(&utf16Size);
1921 if (!src) {
1922 return UNEXPECTED_NULL;
1923 }
1924
1925 // Save ourselves the trouble, we're done.
1926 if (utf16Size == 0u) {
1927 str->clear();
1928 return NO_ERROR;
1929 }
1930
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001931 // Allow for closing '\0'
1932 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1933 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001934 return BAD_VALUE;
1935 }
1936 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001937 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001938 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001939 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1940 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001941 return NO_ERROR;
1942}
1943
1944status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1945 const int32_t start = dataPosition();
1946 int32_t size;
1947 status_t status = readInt32(&size);
1948 str->reset();
1949
1950 if (status != OK || size < 0) {
1951 return status;
1952 }
1953
1954 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001955 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001956 return readUtf8FromUtf16(str->get());
1957}
1958
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001959const char* Parcel::readCString() const
1960{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001961 if (mDataPos < mDataSize) {
1962 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001963 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1964 // is the string's trailing NUL within the parcel's valid bounds?
1965 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1966 if (eos) {
1967 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001968 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001969 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001970 return str;
1971 }
1972 }
Yi Kong91635562018-06-07 14:38:36 -07001973 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001974}
1975
1976String8 Parcel::readString8() const
1977{
Roshan Pius87b64d22016-07-18 12:51:02 -07001978 String8 retString;
1979 status_t status = readString8(&retString);
1980 if (status != OK) {
1981 // We don't care about errors here, so just return an empty string.
1982 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001983 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001984 return retString;
1985}
1986
1987status_t Parcel::readString8(String8* pArg) const
1988{
1989 int32_t size;
1990 status_t status = readInt32(&size);
1991 if (status != OK) {
1992 return status;
1993 }
1994 // watch for potential int overflow from size+1
1995 if (size < 0 || size >= INT32_MAX) {
1996 return BAD_VALUE;
1997 }
1998 // |writeString8| writes nothing for empty string.
1999 if (size == 0) {
2000 *pArg = String8();
2001 return OK;
2002 }
2003 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07002004 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002005 return BAD_VALUE;
2006 }
2007 pArg->setTo(str, size);
2008 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002009}
2010
2011String16 Parcel::readString16() const
2012{
2013 size_t len;
2014 const char16_t* str = readString16Inplace(&len);
2015 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002016 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002017 return String16();
2018}
2019
Casey Dahlinb9872622015-11-25 15:09:45 -08002020status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2021{
2022 const int32_t start = dataPosition();
2023 int32_t size;
2024 status_t status = readInt32(&size);
2025 pArg->reset();
2026
2027 if (status != OK || size < 0) {
2028 return status;
2029 }
2030
2031 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002032 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002033
2034 status = readString16(pArg->get());
2035
2036 if (status != OK) {
2037 pArg->reset();
2038 }
2039
2040 return status;
2041}
2042
Casey Dahlin451ff582015-10-19 18:12:18 -07002043status_t Parcel::readString16(String16* pArg) const
2044{
2045 size_t len;
2046 const char16_t* str = readString16Inplace(&len);
2047 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002048 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002049 return 0;
2050 } else {
2051 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002052 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002053 }
2054}
2055
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002056const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2057{
2058 int32_t size = readInt32();
2059 // watch for potential int overflow from size+1
2060 if (size >= 0 && size < INT32_MAX) {
2061 *outLen = size;
2062 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002063 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002064 return str;
2065 }
2066 }
2067 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002068 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002069}
2070
Casey Dahlinf0c13772015-10-27 18:33:56 -07002071status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2072{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002073 status_t status = readNullableStrongBinder(val);
2074 if (status == OK && !val->get()) {
2075 status = UNEXPECTED_NULL;
2076 }
2077 return status;
2078}
2079
2080status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2081{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002082 return unflatten_binder(ProcessState::self(), *this, val);
2083}
2084
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002085sp<IBinder> Parcel::readStrongBinder() const
2086{
2087 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002088 // Note that a lot of code in Android reads binders by hand with this
2089 // method, and that code has historically been ok with getting nullptr
2090 // back (while ignoring error codes).
2091 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002092 return val;
2093}
2094
2095wp<IBinder> Parcel::readWeakBinder() const
2096{
2097 wp<IBinder> val;
2098 unflatten_binder(ProcessState::self(), *this, &val);
2099 return val;
2100}
2101
Christopher Wiley97f048d2015-11-19 06:49:05 -08002102status_t Parcel::readParcelable(Parcelable* parcelable) const {
2103 int32_t have_parcelable = 0;
2104 status_t status = readInt32(&have_parcelable);
2105 if (status != OK) {
2106 return status;
2107 }
2108 if (!have_parcelable) {
2109 return UNEXPECTED_NULL;
2110 }
2111 return parcelable->readFromParcel(this);
2112}
2113
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002114int32_t Parcel::readExceptionCode() const
2115{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002116 binder::Status status;
2117 status.readFromParcel(*this);
2118 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002119}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002120
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002121native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002122{
2123 int numFds, numInts;
2124 status_t err;
2125 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002126 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002127 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002128 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002129
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002130 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002131 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002132 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002133 }
2134
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002135 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002136 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002137 if (h->data[i] < 0) {
2138 for (int j = 0; j < i; j++) {
2139 close(h->data[j]);
2140 }
2141 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002142 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002143 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002144 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002145 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002146 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002147 native_handle_close(h);
2148 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002149 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002150 }
2151 return h;
2152}
2153
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002154int Parcel::readFileDescriptor() const
2155{
2156 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002157
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002158 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002159 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002160 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002161
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002162 return BAD_TYPE;
2163}
2164
Dianne Hackborn1941a402016-08-29 12:30:43 -07002165int Parcel::readParcelFileDescriptor() const
2166{
2167 int32_t hasComm = readInt32();
2168 int fd = readFileDescriptor();
2169 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002170 // detach (owned by the binder driver)
2171 int comm = readFileDescriptor();
2172
2173 // warning: this must be kept in sync with:
2174 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2175 enum ParcelFileDescriptorStatus {
2176 DETACHED = 2,
2177 };
2178
2179#if BYTE_ORDER == BIG_ENDIAN
2180 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2181#endif
2182#if BYTE_ORDER == LITTLE_ENDIAN
2183 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2184#endif
2185
2186 ssize_t written = TEMP_FAILURE_RETRY(
2187 ::write(comm, &message, sizeof(message)));
2188
2189 if (written == -1 || written != sizeof(message)) {
2190 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2191 written, strerror(errno));
2192 return BAD_TYPE;
2193 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002194 }
2195 return fd;
2196}
2197
Christopher Wiley2cf19952016-04-11 11:09:37 -07002198status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002199{
2200 int got = readFileDescriptor();
2201
2202 if (got == BAD_TYPE) {
2203 return BAD_TYPE;
2204 }
2205
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002206 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002207
2208 if (val->get() < 0) {
2209 return BAD_VALUE;
2210 }
2211
2212 return OK;
2213}
2214
Ryo Hashimotobf551892018-05-31 16:58:35 +09002215status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2216{
2217 int got = readParcelFileDescriptor();
2218
2219 if (got == BAD_TYPE) {
2220 return BAD_TYPE;
2221 }
2222
2223 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2224
2225 if (val->get() < 0) {
2226 return BAD_VALUE;
2227 }
2228
2229 return OK;
2230}
Casey Dahlin06673e32015-11-23 13:24:23 -08002231
Christopher Wiley2cf19952016-04-11 11:09:37 -07002232status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002233 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2234}
2235
Christopher Wiley2cf19952016-04-11 11:09:37 -07002236status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002237 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2238}
2239
Jeff Brown5707dbf2011-09-23 21:17:56 -07002240status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2241{
Jeff Brown13b16042014-11-11 16:44:25 -08002242 int32_t blobType;
2243 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002244 if (status) return status;
2245
Jeff Brown13b16042014-11-11 16:44:25 -08002246 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002247 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002248 const void* ptr = readInplace(len);
2249 if (!ptr) return BAD_VALUE;
2250
Jeff Brown13b16042014-11-11 16:44:25 -08002251 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002252 return NO_ERROR;
2253 }
2254
Steve Block6807e592011-10-20 11:56:00 +01002255 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002256 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002257 int fd = readFileDescriptor();
2258 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2259
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002260 if (!ashmem_valid(fd)) {
2261 ALOGE("invalid fd");
2262 return BAD_VALUE;
2263 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002264 int size = ashmem_get_size_region(fd);
2265 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002266 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002267 return BAD_VALUE;
2268 }
Yi Kong91635562018-06-07 14:38:36 -07002269 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002270 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002271 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002272
Jeff Brown13b16042014-11-11 16:44:25 -08002273 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002274 return NO_ERROR;
2275}
2276
Mathias Agopiane1424282013-07-29 21:24:40 -07002277status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002278{
2279 // size
2280 const size_t len = this->readInt32();
2281 const size_t fd_count = this->readInt32();
2282
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002283 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002284 // don't accept size_t values which may have come from an
2285 // inadvertent conversion from a negative int.
2286 return BAD_VALUE;
2287 }
2288
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002289 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002290 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002291 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002292 return BAD_VALUE;
2293
Yi Kong91635562018-06-07 14:38:36 -07002294 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002295 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002296 fds = new (std::nothrow) int[fd_count];
2297 if (fds == nullptr) {
2298 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2299 return BAD_VALUE;
2300 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002301 }
2302
2303 status_t err = NO_ERROR;
2304 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002305 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002306 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002307 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002308 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 -07002309 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2310 // Close all the file descriptors that were dup-ed.
2311 for (size_t j=0; j<i ;j++) {
2312 close(fds[j]);
2313 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002314 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002315 }
2316
2317 if (err == NO_ERROR) {
2318 err = val.unflatten(buf, len, fds, fd_count);
2319 }
2320
2321 if (fd_count) {
2322 delete [] fds;
2323 }
2324
2325 return err;
2326}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002327const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2328{
2329 const size_t DPOS = mDataPos;
2330 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2331 const flat_binder_object* obj
2332 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2333 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002334 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002335 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002336 // the object list, so we don't want to check for it when
2337 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002338 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002339 return obj;
2340 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002341
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002342 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002343 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002344 const size_t N = mObjectsSize;
2345 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002346
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002347 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002348 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002349 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002350
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002351 // Start at the current hint position, looking for an object at
2352 // the current data position.
2353 if (opos < N) {
2354 while (opos < (N-1) && OBJS[opos] < DPOS) {
2355 opos++;
2356 }
2357 } else {
2358 opos = N-1;
2359 }
2360 if (OBJS[opos] == DPOS) {
2361 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002362 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002363 this, DPOS, opos);
2364 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002365 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002366 return obj;
2367 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002368
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002369 // Look backwards for it...
2370 while (opos > 0 && OBJS[opos] > DPOS) {
2371 opos--;
2372 }
2373 if (OBJS[opos] == DPOS) {
2374 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002375 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002376 this, DPOS, opos);
2377 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002378 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002379 return obj;
2380 }
2381 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002382 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 -07002383 this, DPOS);
2384 }
Yi Kong91635562018-06-07 14:38:36 -07002385 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002386}
2387
2388void Parcel::closeFileDescriptors()
2389{
2390 size_t i = mObjectsSize;
2391 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002392 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002393 }
2394 while (i > 0) {
2395 i--;
2396 const flat_binder_object* flat
2397 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002398 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002399 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002400 close(flat->handle);
2401 }
2402 }
2403}
2404
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002405uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002406{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002407 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002408}
2409
2410size_t Parcel::ipcDataSize() const
2411{
2412 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2413}
2414
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002415uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002416{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002417 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002418}
2419
2420size_t Parcel::ipcObjectsCount() const
2421{
2422 return mObjectsSize;
2423}
2424
2425void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002426 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002428 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002429 freeDataNoInit();
2430 mError = NO_ERROR;
2431 mData = const_cast<uint8_t*>(data);
2432 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002433 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002434 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002435 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002436 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002437 mObjectsSize = mObjectsCapacity = objectsCount;
2438 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002439 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002440 mOwner = relFunc;
2441 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002442 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002443 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002444 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002445 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002446 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002447 mObjectsSize = 0;
2448 break;
2449 }
2450 minOffset = offset + sizeof(flat_binder_object);
2451 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002452 scanForFds();
2453}
2454
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002455void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002456{
2457 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002458
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002459 if (errorCheck() != NO_ERROR) {
2460 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002461 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002462 } else if (dataSize() > 0) {
2463 const uint8_t* DATA = data();
2464 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002465 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002466 const size_t N = objectsCount();
2467 for (size_t i=0; i<N; i++) {
2468 const flat_binder_object* flat
2469 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2470 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002471 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002472 << " = " << flat->binder;
2473 }
2474 } else {
2475 to << "NULL";
2476 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002477
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002478 to << ")";
2479}
2480
2481void Parcel::releaseObjects()
2482{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002483 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002484 if (i == 0) {
2485 return;
2486 }
2487 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002488 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002489 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002490 while (i > 0) {
2491 i--;
2492 const flat_binder_object* flat
2493 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002494 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002495 }
2496}
2497
2498void Parcel::acquireObjects()
2499{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002500 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002501 if (i == 0) {
2502 return;
2503 }
2504 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002505 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002506 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002507 while (i > 0) {
2508 i--;
2509 const flat_binder_object* flat
2510 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002511 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002512 }
2513}
2514
2515void Parcel::freeData()
2516{
2517 freeDataNoInit();
2518 initState();
2519}
2520
2521void Parcel::freeDataNoInit()
2522{
2523 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002524 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002525 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002526 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2527 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002528 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002529 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002530 if (mData) {
2531 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002532 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002533 if (mDataCapacity <= gParcelGlobalAllocSize) {
2534 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2535 } else {
2536 gParcelGlobalAllocSize = 0;
2537 }
2538 if (gParcelGlobalAllocCount > 0) {
2539 gParcelGlobalAllocCount--;
2540 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002541 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002542 free(mData);
2543 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544 if (mObjects) free(mObjects);
2545 }
2546}
2547
2548status_t Parcel::growData(size_t len)
2549{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002550 if (len > INT32_MAX) {
2551 // don't accept size_t values which may have come from an
2552 // inadvertent conversion from a negative int.
2553 return BAD_VALUE;
2554 }
2555
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002556 size_t newSize = ((mDataSize+len)*3)/2;
2557 return (newSize <= mDataSize)
2558 ? (status_t) NO_MEMORY
2559 : continueWrite(newSize);
2560}
2561
2562status_t Parcel::restartWrite(size_t desired)
2563{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002564 if (desired > INT32_MAX) {
2565 // don't accept size_t values which may have come from an
2566 // inadvertent conversion from a negative int.
2567 return BAD_VALUE;
2568 }
2569
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002570 if (mOwner) {
2571 freeData();
2572 return continueWrite(desired);
2573 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002574
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002575 uint8_t* data = (uint8_t*)realloc(mData, desired);
2576 if (!data && desired > mDataCapacity) {
2577 mError = NO_MEMORY;
2578 return NO_MEMORY;
2579 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002580
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002581 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002582
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002583 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002584 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002585 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002586 gParcelGlobalAllocSize += desired;
2587 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002588 if (!mData) {
2589 gParcelGlobalAllocCount++;
2590 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002591 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002592 mData = data;
2593 mDataCapacity = desired;
2594 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002595
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002596 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002597 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2598 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2599
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002600 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002601 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002602 mObjectsSize = mObjectsCapacity = 0;
2603 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002604 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002605 mHasFds = false;
2606 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002607 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002608
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002609 return NO_ERROR;
2610}
2611
2612status_t Parcel::continueWrite(size_t desired)
2613{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002614 if (desired > INT32_MAX) {
2615 // don't accept size_t values which may have come from an
2616 // inadvertent conversion from a negative int.
2617 return BAD_VALUE;
2618 }
2619
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002620 // If shrinking, first adjust for any objects that appear
2621 // after the new data size.
2622 size_t objectsSize = mObjectsSize;
2623 if (desired < mDataSize) {
2624 if (desired == 0) {
2625 objectsSize = 0;
2626 } else {
2627 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002628 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002629 break;
2630 objectsSize--;
2631 }
2632 }
2633 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002634
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002635 if (mOwner) {
2636 // If the size is going to zero, just release the owner's data.
2637 if (desired == 0) {
2638 freeData();
2639 return NO_ERROR;
2640 }
2641
2642 // If there is a different owner, we need to take
2643 // posession.
2644 uint8_t* data = (uint8_t*)malloc(desired);
2645 if (!data) {
2646 mError = NO_MEMORY;
2647 return NO_MEMORY;
2648 }
Yi Kong91635562018-06-07 14:38:36 -07002649 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002650
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002651 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002652 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002653 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002654 free(data);
2655
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002656 mError = NO_MEMORY;
2657 return NO_MEMORY;
2658 }
2659
2660 // Little hack to only acquire references on objects
2661 // we will be keeping.
2662 size_t oldObjectsSize = mObjectsSize;
2663 mObjectsSize = objectsSize;
2664 acquireObjects();
2665 mObjectsSize = oldObjectsSize;
2666 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002667
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002668 if (mData) {
2669 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2670 }
2671 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002672 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002673 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002674 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002675 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002676 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002677
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002678 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002679 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002680 gParcelGlobalAllocSize += desired;
2681 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002682 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002683
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002684 mData = data;
2685 mObjects = objects;
2686 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002687 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002688 mDataCapacity = desired;
2689 mObjectsSize = mObjectsCapacity = objectsSize;
2690 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002691 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002692
2693 } else if (mData) {
2694 if (objectsSize < mObjectsSize) {
2695 // Need to release refs on any objects we are dropping.
2696 const sp<ProcessState> proc(ProcessState::self());
2697 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2698 const flat_binder_object* flat
2699 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002700 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002701 // will need to rescan because we may have lopped off the only FDs
2702 mFdsKnown = false;
2703 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002704 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002705 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002706
2707 if (objectsSize == 0) {
2708 free(mObjects);
2709 mObjects = nullptr;
2710 } else {
2711 binder_size_t* objects =
2712 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2713 if (objects) {
2714 mObjects = objects;
2715 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002716 }
2717 mObjectsSize = objectsSize;
2718 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002719 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002720 }
2721
2722 // We own the data, so we can just do a realloc().
2723 if (desired > mDataCapacity) {
2724 uint8_t* data = (uint8_t*)realloc(mData, desired);
2725 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002726 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2727 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002728 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002729 gParcelGlobalAllocSize += desired;
2730 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002731 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002732 mData = data;
2733 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002734 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002735 mError = NO_MEMORY;
2736 return NO_MEMORY;
2737 }
2738 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002739 if (mDataSize > desired) {
2740 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002741 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002742 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002743 if (mDataPos > desired) {
2744 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002745 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002746 }
2747 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002748
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002749 } else {
2750 // This is the first data. Easy!
2751 uint8_t* data = (uint8_t*)malloc(desired);
2752 if (!data) {
2753 mError = NO_MEMORY;
2754 return NO_MEMORY;
2755 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002756
Yi Kong91635562018-06-07 14:38:36 -07002757 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002758 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002759 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002760 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002761
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002762 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002763 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002764 gParcelGlobalAllocSize += desired;
2765 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002766 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002767
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002768 mData = data;
2769 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002770 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2771 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002772 mDataCapacity = desired;
2773 }
2774
2775 return NO_ERROR;
2776}
2777
2778void Parcel::initState()
2779{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002780 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002781 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002782 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002783 mDataSize = 0;
2784 mDataCapacity = 0;
2785 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002786 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2787 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002788 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002789 mObjectsSize = 0;
2790 mObjectsCapacity = 0;
2791 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002792 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002793 mHasFds = false;
2794 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002795 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002796 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002797 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002798 mWorkSourceRequestHeaderPosition = 0;
2799 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002800
2801 // racing multiple init leads only to multiple identical write
2802 if (gMaxFds == 0) {
2803 struct rlimit result;
2804 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2805 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002806 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002807 } else {
2808 ALOGW("Unable to getrlimit: %s", strerror(errno));
2809 gMaxFds = 1024;
2810 }
2811 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002812}
2813
2814void Parcel::scanForFds() const
2815{
2816 bool hasFds = false;
2817 for (size_t i=0; i<mObjectsSize; i++) {
2818 const flat_binder_object* flat
2819 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002820 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002821 hasFds = true;
2822 break;
2823 }
2824 }
2825 mHasFds = hasFds;
2826 mFdsKnown = true;
2827}
2828
Dan Sandleraa5c2342015-04-10 10:08:45 -04002829size_t Parcel::getBlobAshmemSize() const
2830{
Adrian Roos6bb31142015-10-22 16:46:12 -07002831 // This used to return the size of all blobs that were written to ashmem, now we're returning
2832 // the ashmem currently referenced by this Parcel, which should be equivalent.
2833 // TODO: Remove method once ABI can be changed.
2834 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002835}
2836
Adrian Rooscbf37262015-10-22 16:12:53 -07002837size_t Parcel::getOpenAshmemSize() const
2838{
2839 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002840}
2841
2842// --- Parcel::Blob ---
2843
2844Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002845 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002846}
2847
2848Parcel::Blob::~Blob() {
2849 release();
2850}
2851
2852void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002853 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002854 ::munmap(mData, mSize);
2855 }
2856 clear();
2857}
2858
Jeff Brown13b16042014-11-11 16:44:25 -08002859void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2860 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002861 mData = data;
2862 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002863 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002864}
2865
2866void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002867 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002868 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002869 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002870 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002871}
2872
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002873}; // namespace android