blob: 85822efa09f70ef1340bf384d49bf5166ec9329d [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
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700593// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700594status_t Parcel::writeInterfaceToken(const String16& interface)
595{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000596 const IPCThreadState* threadState = IPCThreadState::self();
597 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000598 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000599 writeInt32(threadState->shouldPropagateWorkSource() ?
600 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700601 // currently the interface identification token is just its name as a string
602 return writeString16(interface);
603}
604
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000605bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
606{
607 if (!mRequestHeaderPresent) {
608 return false;
609 }
610
611 const size_t initialPosition = dataPosition();
612 setDataPosition(mWorkSourceRequestHeaderPosition);
613 status_t err = writeInt32(uid);
614 setDataPosition(initialPosition);
615 return err == NO_ERROR;
616}
617
618uid_t Parcel::readCallingWorkSourceUid()
619{
620 if (!mRequestHeaderPresent) {
621 return IPCThreadState::kUnsetWorkSource;
622 }
623
624 const size_t initialPosition = dataPosition();
625 setDataPosition(mWorkSourceRequestHeaderPosition);
626 uid_t uid = readInt32();
627 setDataPosition(initialPosition);
628 return uid;
629}
630
Mathias Agopian83c04462009-05-22 19:00:22 -0700631bool Parcel::checkInterface(IBinder* binder) const
632{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700633 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700634}
635
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700636bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700637 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700638{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100639 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700640 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700641 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700642 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700643 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700644 if ((threadState->getLastTransactionBinderFlags() &
645 IBinder::FLAG_ONEWAY) != 0) {
646 // For one-way calls, the callee is running entirely
647 // disconnected from the caller, so disable StrictMode entirely.
648 // Not only does disk/network usage not impact the caller, but
649 // there's no way to commuicate back any violations anyway.
650 threadState->setStrictModePolicy(0);
651 } else {
652 threadState->setStrictModePolicy(strictPolicy);
653 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100654 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000655 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100656 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000657 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100658 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700659 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700660 if (str == interface) {
661 return true;
662 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700663 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700664 String8(interface).string(), String8(str).string());
665 return false;
666 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700667}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700668
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700669size_t Parcel::objectsCount() const
670{
671 return mObjectsSize;
672}
673
674status_t Parcel::errorCheck() const
675{
676 return mError;
677}
678
679void Parcel::setError(status_t err)
680{
681 mError = err;
682}
683
684status_t Parcel::finishWrite(size_t len)
685{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700686 if (len > INT32_MAX) {
687 // don't accept size_t values which may have come from an
688 // inadvertent conversion from a negative int.
689 return BAD_VALUE;
690 }
691
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700692 //printf("Finish write of %d\n", len);
693 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700694 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700695 if (mDataPos > mDataSize) {
696 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700697 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700698 }
699 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
700 return NO_ERROR;
701}
702
703status_t Parcel::writeUnpadded(const void* data, size_t len)
704{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700705 if (len > INT32_MAX) {
706 // don't accept size_t values which may have come from an
707 // inadvertent conversion from a negative int.
708 return BAD_VALUE;
709 }
710
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700711 size_t end = mDataPos + len;
712 if (end < mDataPos) {
713 // integer overflow
714 return BAD_VALUE;
715 }
716
717 if (end <= mDataCapacity) {
718restart_write:
719 memcpy(mData+mDataPos, data, len);
720 return finishWrite(len);
721 }
722
723 status_t err = growData(len);
724 if (err == NO_ERROR) goto restart_write;
725 return err;
726}
727
728status_t Parcel::write(const void* data, size_t len)
729{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700730 if (len > INT32_MAX) {
731 // don't accept size_t values which may have come from an
732 // inadvertent conversion from a negative int.
733 return BAD_VALUE;
734 }
735
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700736 void* const d = writeInplace(len);
737 if (d) {
738 memcpy(d, data, len);
739 return NO_ERROR;
740 }
741 return mError;
742}
743
744void* Parcel::writeInplace(size_t len)
745{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700746 if (len > INT32_MAX) {
747 // don't accept size_t values which may have come from an
748 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700749 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700750 }
751
752 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700753
754 // sanity check for integer overflow
755 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700756 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700757 }
758
759 if ((mDataPos+padded) <= mDataCapacity) {
760restart_write:
761 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
762 uint8_t* const data = mData+mDataPos;
763
764 // Need to pad at end?
765 if (padded != len) {
766#if BYTE_ORDER == BIG_ENDIAN
767 static const uint32_t mask[4] = {
768 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
769 };
770#endif
771#if BYTE_ORDER == LITTLE_ENDIAN
772 static const uint32_t mask[4] = {
773 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
774 };
775#endif
776 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
777 // *reinterpret_cast<void**>(data+padded-4));
778 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
779 }
780
781 finishWrite(padded);
782 return data;
783 }
784
785 status_t err = growData(padded);
786 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700787 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700788}
789
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800790status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
791 const uint8_t* strData = (uint8_t*)str.data();
792 const size_t strLen= str.length();
793 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100794 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800795 return BAD_VALUE;
796 }
797
798 status_t err = writeInt32(utf16Len);
799 if (err) {
800 return err;
801 }
802
803 // Allocate enough bytes to hold our converted string and its terminating NULL.
804 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
805 if (!dst) {
806 return NO_MEMORY;
807 }
808
Sergio Girof4607432016-07-21 14:46:35 +0100809 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800810
811 return NO_ERROR;
812}
813
814status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
815 if (!str) {
816 return writeInt32(-1);
817 }
818 return writeUtf8AsUtf16(*str);
819}
820
Casey Dahlin185d3442016-02-09 11:08:35 -0800821namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800822
Casey Dahlin185d3442016-02-09 11:08:35 -0800823template<typename T>
824status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700825{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700826 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700827 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700828 status = BAD_VALUE;
829 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700830 }
831
Casey Dahlin185d3442016-02-09 11:08:35 -0800832 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700833 if (status != OK) {
834 return status;
835 }
836
Casey Dahlin185d3442016-02-09 11:08:35 -0800837 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700838 if (!data) {
839 status = BAD_VALUE;
840 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700841 }
842
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700843 memcpy(data, val.data(), val.size());
844 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700845}
846
Casey Dahlin185d3442016-02-09 11:08:35 -0800847template<typename T>
848status_t writeByteVectorInternalPtr(Parcel* parcel,
849 const std::unique_ptr<std::vector<T>>& val)
850{
851 if (!val) {
852 return parcel->writeInt32(-1);
853 }
854
855 return writeByteVectorInternal(parcel, *val);
856}
857
858} // namespace
859
860status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
861 return writeByteVectorInternal(this, val);
862}
863
864status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
865{
866 return writeByteVectorInternalPtr(this, val);
867}
868
869status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
870 return writeByteVectorInternal(this, val);
871}
872
873status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
874{
875 return writeByteVectorInternalPtr(this, val);
876}
877
Casey Dahlin451ff582015-10-19 18:12:18 -0700878status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
879{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800880 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700881}
882
Casey Dahlinb9872622015-11-25 15:09:45 -0800883status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
884{
885 return writeNullableTypedVector(val, &Parcel::writeInt32);
886}
887
Casey Dahlin451ff582015-10-19 18:12:18 -0700888status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
889{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800890 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700891}
892
Casey Dahlinb9872622015-11-25 15:09:45 -0800893status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
894{
895 return writeNullableTypedVector(val, &Parcel::writeInt64);
896}
897
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800898status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
899{
900 return writeTypedVector(val, &Parcel::writeUint64);
901}
902
903status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
904{
905 return writeNullableTypedVector(val, &Parcel::writeUint64);
906}
907
Casey Dahlin451ff582015-10-19 18:12:18 -0700908status_t Parcel::writeFloatVector(const std::vector<float>& val)
909{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800910 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700911}
912
Casey Dahlinb9872622015-11-25 15:09:45 -0800913status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
914{
915 return writeNullableTypedVector(val, &Parcel::writeFloat);
916}
917
Casey Dahlin451ff582015-10-19 18:12:18 -0700918status_t Parcel::writeDoubleVector(const std::vector<double>& val)
919{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800920 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700921}
922
Casey Dahlinb9872622015-11-25 15:09:45 -0800923status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
924{
925 return writeNullableTypedVector(val, &Parcel::writeDouble);
926}
927
Casey Dahlin451ff582015-10-19 18:12:18 -0700928status_t Parcel::writeBoolVector(const std::vector<bool>& val)
929{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800930 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700931}
932
Casey Dahlinb9872622015-11-25 15:09:45 -0800933status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
934{
935 return writeNullableTypedVector(val, &Parcel::writeBool);
936}
937
Casey Dahlin451ff582015-10-19 18:12:18 -0700938status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
939{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800940 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700941}
942
Casey Dahlinb9872622015-11-25 15:09:45 -0800943status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
944{
945 return writeNullableTypedVector(val, &Parcel::writeChar);
946}
947
Casey Dahlin451ff582015-10-19 18:12:18 -0700948status_t Parcel::writeString16Vector(const std::vector<String16>& val)
949{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800950 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700951}
952
Casey Dahlinb9872622015-11-25 15:09:45 -0800953status_t Parcel::writeString16Vector(
954 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
955{
956 return writeNullableTypedVector(val, &Parcel::writeString16);
957}
958
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800959status_t Parcel::writeUtf8VectorAsUtf16Vector(
960 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
961 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
962}
963
964status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
965 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
966}
967
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700968status_t Parcel::writeInt32(int32_t val)
969{
Andreas Huber84a6d042009-08-17 13:33:27 -0700970 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700971}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800972
973status_t Parcel::writeUint32(uint32_t val)
974{
975 return writeAligned(val);
976}
977
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700978status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700979 if (len > INT32_MAX) {
980 // don't accept size_t values which may have come from an
981 // inadvertent conversion from a negative int.
982 return BAD_VALUE;
983 }
984
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700985 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700986 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700987 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700988 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700989 if (ret == NO_ERROR) {
990 ret = write(val, len * sizeof(*val));
991 }
992 return ret;
993}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700994status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700995 if (len > INT32_MAX) {
996 // don't accept size_t values which may have come from an
997 // inadvertent conversion from a negative int.
998 return BAD_VALUE;
999 }
1000
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001001 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001002 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001003 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001004 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001005 if (ret == NO_ERROR) {
1006 ret = write(val, len * sizeof(*val));
1007 }
1008 return ret;
1009}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001010
Casey Dahlind6848f52015-10-15 15:44:59 -07001011status_t Parcel::writeBool(bool val)
1012{
1013 return writeInt32(int32_t(val));
1014}
1015
1016status_t Parcel::writeChar(char16_t val)
1017{
1018 return writeInt32(int32_t(val));
1019}
1020
1021status_t Parcel::writeByte(int8_t val)
1022{
1023 return writeInt32(int32_t(val));
1024}
1025
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001026status_t Parcel::writeInt64(int64_t val)
1027{
Andreas Huber84a6d042009-08-17 13:33:27 -07001028 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001029}
1030
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001031status_t Parcel::writeUint64(uint64_t val)
1032{
1033 return writeAligned(val);
1034}
1035
Serban Constantinescuf683e012013-11-05 16:53:55 +00001036status_t Parcel::writePointer(uintptr_t val)
1037{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001038 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001039}
1040
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001041status_t Parcel::writeFloat(float val)
1042{
Andreas Huber84a6d042009-08-17 13:33:27 -07001043 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001044}
1045
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001046#if defined(__mips__) && defined(__mips_hard_float)
1047
1048status_t Parcel::writeDouble(double val)
1049{
1050 union {
1051 double d;
1052 unsigned long long ll;
1053 } u;
1054 u.d = val;
1055 return writeAligned(u.ll);
1056}
1057
1058#else
1059
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001060status_t Parcel::writeDouble(double val)
1061{
Andreas Huber84a6d042009-08-17 13:33:27 -07001062 return writeAligned(val);
1063}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001064
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001065#endif
1066
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001067status_t Parcel::writeCString(const char* str)
1068{
1069 return write(str, strlen(str)+1);
1070}
1071
1072status_t Parcel::writeString8(const String8& str)
1073{
1074 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001075 // only write string if its length is more than zero characters,
1076 // as readString8 will only read if the length field is non-zero.
1077 // this is slightly different from how writeString16 works.
1078 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001079 err = write(str.string(), str.bytes()+1);
1080 }
1081 return err;
1082}
1083
Casey Dahlinb9872622015-11-25 15:09:45 -08001084status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1085{
1086 if (!str) {
1087 return writeInt32(-1);
1088 }
1089
1090 return writeString16(*str);
1091}
1092
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001093status_t Parcel::writeString16(const String16& str)
1094{
1095 return writeString16(str.string(), str.size());
1096}
1097
1098status_t Parcel::writeString16(const char16_t* str, size_t len)
1099{
Yi Kong91635562018-06-07 14:38:36 -07001100 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001101
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001102 status_t err = writeInt32(len);
1103 if (err == NO_ERROR) {
1104 len *= sizeof(char16_t);
1105 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1106 if (data) {
1107 memcpy(data, str, len);
1108 *reinterpret_cast<char16_t*>(data+len) = 0;
1109 return NO_ERROR;
1110 }
1111 err = mError;
1112 }
1113 return err;
1114}
1115
1116status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1117{
1118 return flatten_binder(ProcessState::self(), val, this);
1119}
1120
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001121status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1122{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001123 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001124}
1125
Casey Dahlinb9872622015-11-25 15:09:45 -08001126status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1127{
1128 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1129}
1130
1131status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001132 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001133}
1134
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001135status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001136 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001137}
1138
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001139status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1140{
1141 return flatten_binder(ProcessState::self(), val, this);
1142}
1143
Casey Dahlinb9872622015-11-25 15:09:45 -08001144status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1145 if (!parcelable) {
1146 return writeInt32(0);
1147 }
1148
1149 return writeParcelable(*parcelable);
1150}
1151
Christopher Wiley97f048d2015-11-19 06:49:05 -08001152status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1153 status_t status = writeInt32(1); // parcelable is not null.
1154 if (status != OK) {
1155 return status;
1156 }
1157 return parcelable.writeToParcel(this);
1158}
1159
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001160status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001161{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001162 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001163 return BAD_TYPE;
1164
1165 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001166 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001167 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001168
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001169 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001170 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001172 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1173 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001174
1175 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001176 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177 return err;
1178 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001179 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001180 return err;
1181}
1182
Jeff Brown93ff1f92011-11-04 19:01:44 -07001183status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001184{
1185 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001186 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001187 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001188 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001189 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001190 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001191 return writeObject(obj, true);
1192}
1193
1194status_t Parcel::writeDupFileDescriptor(int fd)
1195{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001196 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001197 if (dupFd < 0) {
1198 return -errno;
1199 }
1200 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001201 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001202 close(dupFd);
1203 }
1204 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001205}
1206
Dianne Hackborn1941a402016-08-29 12:30:43 -07001207status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1208{
1209 writeInt32(0);
1210 return writeFileDescriptor(fd, takeOwnership);
1211}
1212
Ryo Hashimotobf551892018-05-31 16:58:35 +09001213status_t Parcel::writeDupParcelFileDescriptor(int fd)
1214{
1215 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1216 if (dupFd < 0) {
1217 return -errno;
1218 }
1219 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1220 if (err != OK) {
1221 close(dupFd);
1222 }
1223 return err;
1224}
1225
Christopher Wiley2cf19952016-04-11 11:09:37 -07001226status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001227 return writeDupFileDescriptor(fd.get());
1228}
1229
Christopher Wiley2cf19952016-04-11 11:09:37 -07001230status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001231 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1232}
1233
Christopher Wiley2cf19952016-04-11 11:09:37 -07001234status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001235 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1236}
1237
Jeff Brown13b16042014-11-11 16:44:25 -08001238status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001239{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001240 if (len > INT32_MAX) {
1241 // don't accept size_t values which may have come from an
1242 // inadvertent conversion from a negative int.
1243 return BAD_VALUE;
1244 }
1245
Jeff Brown13b16042014-11-11 16:44:25 -08001246 status_t status;
1247 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001248 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001249 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001250 if (status) return status;
1251
1252 void* ptr = writeInplace(len);
1253 if (!ptr) return NO_MEMORY;
1254
Jeff Brown13b16042014-11-11 16:44:25 -08001255 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001256 return NO_ERROR;
1257 }
1258
Steve Block6807e592011-10-20 11:56:00 +01001259 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001260 int fd = ashmem_create_region("Parcel Blob", len);
1261 if (fd < 0) return NO_MEMORY;
1262
1263 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1264 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001265 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001266 } else {
Yi Kong91635562018-06-07 14:38:36 -07001267 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001268 if (ptr == MAP_FAILED) {
1269 status = -errno;
1270 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001271 if (!mutableCopy) {
1272 result = ashmem_set_prot_region(fd, PROT_READ);
1273 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001274 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001275 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001276 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001277 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001278 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001279 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001280 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001281 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001282 return NO_ERROR;
1283 }
1284 }
1285 }
1286 }
1287 ::munmap(ptr, len);
1288 }
1289 ::close(fd);
1290 return status;
1291}
1292
Jeff Brown13b16042014-11-11 16:44:25 -08001293status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1294{
1295 // Must match up with what's done in writeBlob.
1296 if (!mAllowFds) return FDS_NOT_ALLOWED;
1297 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1298 if (status) return status;
1299 return writeDupFileDescriptor(fd);
1300}
1301
Mathias Agopiane1424282013-07-29 21:24:40 -07001302status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001303{
1304 status_t err;
1305
1306 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001307 const size_t len = val.getFlattenedSize();
1308 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001309
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001310 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001311 // don't accept size_t values which may have come from an
1312 // inadvertent conversion from a negative int.
1313 return BAD_VALUE;
1314 }
1315
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001316 err = this->writeInt32(len);
1317 if (err) return err;
1318
1319 err = this->writeInt32(fd_count);
1320 if (err) return err;
1321
1322 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001323 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001324 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001325 return BAD_VALUE;
1326
Yi Kong91635562018-06-07 14:38:36 -07001327 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001328 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001329 fds = new (std::nothrow) int[fd_count];
1330 if (fds == nullptr) {
1331 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1332 return BAD_VALUE;
1333 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001334 }
1335
1336 err = val.flatten(buf, len, fds, fd_count);
1337 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1338 err = this->writeDupFileDescriptor( fds[i] );
1339 }
1340
1341 if (fd_count) {
1342 delete [] fds;
1343 }
1344
1345 return err;
1346}
1347
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001348status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1349{
1350 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1351 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1352 if (enoughData && enoughObjects) {
1353restart_write:
1354 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001355
Christopher Tate98e67d32015-06-03 18:44:15 -07001356 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001357 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001358 if (!mAllowFds) {
1359 // fail before modifying our object index
1360 return FDS_NOT_ALLOWED;
1361 }
1362 mHasFds = mFdsKnown = true;
1363 }
1364
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001365 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001366 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001367 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001368 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001369 mObjectsSize++;
1370 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001371
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001372 return finishWrite(sizeof(flat_binder_object));
1373 }
1374
1375 if (!enoughData) {
1376 const status_t err = growData(sizeof(val));
1377 if (err != NO_ERROR) return err;
1378 }
1379 if (!enoughObjects) {
1380 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001381 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001382 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001383 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001384 mObjects = objects;
1385 mObjectsCapacity = newSize;
1386 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001387
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001388 goto restart_write;
1389}
1390
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001391status_t Parcel::writeNoException()
1392{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001393 binder::Status status;
1394 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001395}
1396
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001397status_t Parcel::validateReadData(size_t upperBound) const
1398{
1399 // Don't allow non-object reads on object data
1400 if (mObjectsSorted || mObjectsSize <= 1) {
1401data_sorted:
1402 // Expect to check only against the next object
1403 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1404 // For some reason the current read position is greater than the next object
1405 // hint. Iterate until we find the right object
1406 size_t nextObject = mNextObjectHint;
1407 do {
1408 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1409 // Requested info overlaps with an object
1410 ALOGE("Attempt to read from protected data in Parcel %p", this);
1411 return PERMISSION_DENIED;
1412 }
1413 nextObject++;
1414 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1415 mNextObjectHint = nextObject;
1416 }
1417 return NO_ERROR;
1418 }
1419 // Quickly determine if mObjects is sorted.
1420 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1421 binder_size_t* prevObj = currObj;
1422 while (currObj > mObjects) {
1423 prevObj--;
1424 if(*prevObj > *currObj) {
1425 goto data_unsorted;
1426 }
1427 currObj--;
1428 }
1429 mObjectsSorted = true;
1430 goto data_sorted;
1431
1432data_unsorted:
1433 // Insertion Sort mObjects
1434 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1435 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1436 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1437 binder_size_t temp = *iter0;
1438 binder_size_t* iter1 = iter0 - 1;
1439 while (iter1 >= mObjects && *iter1 > temp) {
1440 *(iter1 + 1) = *iter1;
1441 iter1--;
1442 }
1443 *(iter1 + 1) = temp;
1444 }
1445 mNextObjectHint = 0;
1446 mObjectsSorted = true;
1447 goto data_sorted;
1448}
1449
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001450status_t Parcel::read(void* outData, size_t len) const
1451{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001452 if (len > INT32_MAX) {
1453 // don't accept size_t values which may have come from an
1454 // inadvertent conversion from a negative int.
1455 return BAD_VALUE;
1456 }
1457
1458 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1459 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001460 if (mObjectsSize > 0) {
1461 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001462 if(err != NO_ERROR) {
1463 // Still increment the data position by the expected length
1464 mDataPos += pad_size(len);
1465 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1466 return err;
1467 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001468 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001469 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001470 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001471 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001472 return NO_ERROR;
1473 }
1474 return NOT_ENOUGH_DATA;
1475}
1476
1477const void* Parcel::readInplace(size_t len) const
1478{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001479 if (len > INT32_MAX) {
1480 // don't accept size_t values which may have come from an
1481 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001482 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001483 }
1484
1485 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1486 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001487 if (mObjectsSize > 0) {
1488 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001489 if(err != NO_ERROR) {
1490 // Still increment the data position by the expected length
1491 mDataPos += pad_size(len);
1492 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001493 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001494 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001495 }
1496
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001497 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001498 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001499 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001500 return data;
1501 }
Yi Kong91635562018-06-07 14:38:36 -07001502 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001503}
1504
Andreas Huber84a6d042009-08-17 13:33:27 -07001505template<class T>
1506status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001507 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001508
1509 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001510 if (mObjectsSize > 0) {
1511 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001512 if(err != NO_ERROR) {
1513 // Still increment the data position by the expected length
1514 mDataPos += sizeof(T);
1515 return err;
1516 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001517 }
1518
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001519 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001520 mDataPos += sizeof(T);
1521 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001522 return NO_ERROR;
1523 } else {
1524 return NOT_ENOUGH_DATA;
1525 }
1526}
1527
Andreas Huber84a6d042009-08-17 13:33:27 -07001528template<class T>
1529T Parcel::readAligned() const {
1530 T result;
1531 if (readAligned(&result) != NO_ERROR) {
1532 result = 0;
1533 }
1534
1535 return result;
1536}
1537
1538template<class T>
1539status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001540 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001541
1542 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1543restart_write:
1544 *reinterpret_cast<T*>(mData+mDataPos) = val;
1545 return finishWrite(sizeof(val));
1546 }
1547
1548 status_t err = growData(sizeof(val));
1549 if (err == NO_ERROR) goto restart_write;
1550 return err;
1551}
1552
Casey Dahlin185d3442016-02-09 11:08:35 -08001553namespace {
1554
1555template<typename T>
1556status_t readByteVectorInternal(const Parcel* parcel,
1557 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001558 val->clear();
1559
1560 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001561 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001562
1563 if (status != OK) {
1564 return status;
1565 }
1566
Christopher Wiley4db672d2015-11-10 09:44:30 -08001567 if (size < 0) {
1568 status = UNEXPECTED_NULL;
1569 return status;
1570 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001571 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001572 status = BAD_VALUE;
1573 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001574 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001575
Paul Lietar433e87b2016-09-16 10:39:32 -07001576 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001577 if (!data) {
1578 status = BAD_VALUE;
1579 return status;
1580 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001581 val->reserve(size);
1582 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001583
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001584 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001585}
1586
Casey Dahlin185d3442016-02-09 11:08:35 -08001587template<typename T>
1588status_t readByteVectorInternalPtr(
1589 const Parcel* parcel,
1590 std::unique_ptr<std::vector<T>>* val) {
1591 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001592 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001593 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001594 val->reset();
1595
1596 if (status != OK || size < 0) {
1597 return status;
1598 }
1599
Casey Dahlin185d3442016-02-09 11:08:35 -08001600 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001601 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001602
Casey Dahlin185d3442016-02-09 11:08:35 -08001603 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001604
1605 if (status != OK) {
1606 val->reset();
1607 }
1608
1609 return status;
1610}
1611
Casey Dahlin185d3442016-02-09 11:08:35 -08001612} // namespace
1613
1614status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1615 return readByteVectorInternal(this, val);
1616}
1617
1618status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1619 return readByteVectorInternal(this, val);
1620}
1621
1622status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1623 return readByteVectorInternalPtr(this, val);
1624}
1625
1626status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1627 return readByteVectorInternalPtr(this, val);
1628}
1629
Casey Dahlinb9872622015-11-25 15:09:45 -08001630status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1631 return readNullableTypedVector(val, &Parcel::readInt32);
1632}
1633
Casey Dahlin451ff582015-10-19 18:12:18 -07001634status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001635 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001636}
1637
Casey Dahlinb9872622015-11-25 15:09:45 -08001638status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1639 return readNullableTypedVector(val, &Parcel::readInt64);
1640}
1641
Casey Dahlin451ff582015-10-19 18:12:18 -07001642status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001643 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001644}
1645
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001646status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1647 return readNullableTypedVector(val, &Parcel::readUint64);
1648}
1649
1650status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1651 return readTypedVector(val, &Parcel::readUint64);
1652}
1653
Casey Dahlinb9872622015-11-25 15:09:45 -08001654status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1655 return readNullableTypedVector(val, &Parcel::readFloat);
1656}
1657
Casey Dahlin451ff582015-10-19 18:12:18 -07001658status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001659 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001660}
1661
Casey Dahlinb9872622015-11-25 15:09:45 -08001662status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1663 return readNullableTypedVector(val, &Parcel::readDouble);
1664}
1665
Casey Dahlin451ff582015-10-19 18:12:18 -07001666status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001667 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001668}
1669
Casey Dahlinb9872622015-11-25 15:09:45 -08001670status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1671 const int32_t start = dataPosition();
1672 int32_t size;
1673 status_t status = readInt32(&size);
1674 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001675
Casey Dahlinb9872622015-11-25 15:09:45 -08001676 if (status != OK || size < 0) {
1677 return status;
1678 }
1679
1680 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001681 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001682
1683 status = readBoolVector(val->get());
1684
1685 if (status != OK) {
1686 val->reset();
1687 }
1688
1689 return status;
1690}
1691
1692status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001693 int32_t size;
1694 status_t status = readInt32(&size);
1695
1696 if (status != OK) {
1697 return status;
1698 }
1699
1700 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001701 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001702 }
1703
1704 val->resize(size);
1705
1706 /* C++ bool handling means a vector of bools isn't necessarily addressable
1707 * (we might use individual bits)
1708 */
Christopher Wiley97887982015-10-27 16:33:47 -07001709 bool data;
1710 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001711 status = readBool(&data);
1712 (*val)[i] = data;
1713
1714 if (status != OK) {
1715 return status;
1716 }
1717 }
1718
1719 return OK;
1720}
1721
Casey Dahlinb9872622015-11-25 15:09:45 -08001722status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1723 return readNullableTypedVector(val, &Parcel::readChar);
1724}
1725
Casey Dahlin451ff582015-10-19 18:12:18 -07001726status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001727 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001728}
1729
Casey Dahlinb9872622015-11-25 15:09:45 -08001730status_t Parcel::readString16Vector(
1731 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1732 return readNullableTypedVector(val, &Parcel::readString16);
1733}
1734
Casey Dahlin451ff582015-10-19 18:12:18 -07001735status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001736 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001737}
1738
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001739status_t Parcel::readUtf8VectorFromUtf16Vector(
1740 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1741 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1742}
1743
1744status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1745 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1746}
Casey Dahlin451ff582015-10-19 18:12:18 -07001747
Andreas Huber84a6d042009-08-17 13:33:27 -07001748status_t Parcel::readInt32(int32_t *pArg) const
1749{
1750 return readAligned(pArg);
1751}
1752
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001753int32_t Parcel::readInt32() const
1754{
Andreas Huber84a6d042009-08-17 13:33:27 -07001755 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001756}
1757
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001758status_t Parcel::readUint32(uint32_t *pArg) const
1759{
1760 return readAligned(pArg);
1761}
1762
1763uint32_t Parcel::readUint32() const
1764{
1765 return readAligned<uint32_t>();
1766}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001767
1768status_t Parcel::readInt64(int64_t *pArg) const
1769{
Andreas Huber84a6d042009-08-17 13:33:27 -07001770 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001771}
1772
1773
1774int64_t Parcel::readInt64() const
1775{
Andreas Huber84a6d042009-08-17 13:33:27 -07001776 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001777}
1778
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001779status_t Parcel::readUint64(uint64_t *pArg) const
1780{
1781 return readAligned(pArg);
1782}
1783
1784uint64_t Parcel::readUint64() const
1785{
1786 return readAligned<uint64_t>();
1787}
1788
Serban Constantinescuf683e012013-11-05 16:53:55 +00001789status_t Parcel::readPointer(uintptr_t *pArg) const
1790{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001791 status_t ret;
1792 binder_uintptr_t ptr;
1793 ret = readAligned(&ptr);
1794 if (!ret)
1795 *pArg = ptr;
1796 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001797}
1798
1799uintptr_t Parcel::readPointer() const
1800{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001801 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001802}
1803
1804
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001805status_t Parcel::readFloat(float *pArg) const
1806{
Andreas Huber84a6d042009-08-17 13:33:27 -07001807 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001808}
1809
1810
1811float Parcel::readFloat() const
1812{
Andreas Huber84a6d042009-08-17 13:33:27 -07001813 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001814}
1815
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001816#if defined(__mips__) && defined(__mips_hard_float)
1817
1818status_t Parcel::readDouble(double *pArg) const
1819{
1820 union {
1821 double d;
1822 unsigned long long ll;
1823 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001824 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001825 status_t status;
1826 status = readAligned(&u.ll);
1827 *pArg = u.d;
1828 return status;
1829}
1830
1831double Parcel::readDouble() const
1832{
1833 union {
1834 double d;
1835 unsigned long long ll;
1836 } u;
1837 u.ll = readAligned<unsigned long long>();
1838 return u.d;
1839}
1840
1841#else
1842
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001843status_t Parcel::readDouble(double *pArg) const
1844{
Andreas Huber84a6d042009-08-17 13:33:27 -07001845 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001846}
1847
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001848double Parcel::readDouble() const
1849{
Andreas Huber84a6d042009-08-17 13:33:27 -07001850 return readAligned<double>();
1851}
1852
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001853#endif
1854
Andreas Huber84a6d042009-08-17 13:33:27 -07001855status_t Parcel::readIntPtr(intptr_t *pArg) const
1856{
1857 return readAligned(pArg);
1858}
1859
1860
1861intptr_t Parcel::readIntPtr() const
1862{
1863 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001864}
1865
Casey Dahlind6848f52015-10-15 15:44:59 -07001866status_t Parcel::readBool(bool *pArg) const
1867{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001868 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001869 status_t ret = readInt32(&tmp);
1870 *pArg = (tmp != 0);
1871 return ret;
1872}
1873
1874bool Parcel::readBool() const
1875{
1876 return readInt32() != 0;
1877}
1878
1879status_t Parcel::readChar(char16_t *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 = char16_t(tmp);
1884 return ret;
1885}
1886
1887char16_t Parcel::readChar() const
1888{
1889 return char16_t(readInt32());
1890}
1891
1892status_t Parcel::readByte(int8_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 = int8_t(tmp);
1897 return ret;
1898}
1899
1900int8_t Parcel::readByte() const
1901{
1902 return int8_t(readInt32());
1903}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001904
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001905status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1906 size_t utf16Size = 0;
1907 const char16_t* src = readString16Inplace(&utf16Size);
1908 if (!src) {
1909 return UNEXPECTED_NULL;
1910 }
1911
1912 // Save ourselves the trouble, we're done.
1913 if (utf16Size == 0u) {
1914 str->clear();
1915 return NO_ERROR;
1916 }
1917
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001918 // Allow for closing '\0'
1919 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1920 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001921 return BAD_VALUE;
1922 }
1923 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001924 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001925 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001926 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1927 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001928 return NO_ERROR;
1929}
1930
1931status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1932 const int32_t start = dataPosition();
1933 int32_t size;
1934 status_t status = readInt32(&size);
1935 str->reset();
1936
1937 if (status != OK || size < 0) {
1938 return status;
1939 }
1940
1941 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001942 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001943 return readUtf8FromUtf16(str->get());
1944}
1945
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001946const char* Parcel::readCString() const
1947{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001948 if (mDataPos < mDataSize) {
1949 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001950 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1951 // is the string's trailing NUL within the parcel's valid bounds?
1952 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1953 if (eos) {
1954 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001955 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001956 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001957 return str;
1958 }
1959 }
Yi Kong91635562018-06-07 14:38:36 -07001960 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001961}
1962
1963String8 Parcel::readString8() const
1964{
Roshan Pius87b64d22016-07-18 12:51:02 -07001965 String8 retString;
1966 status_t status = readString8(&retString);
1967 if (status != OK) {
1968 // We don't care about errors here, so just return an empty string.
1969 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001970 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001971 return retString;
1972}
1973
1974status_t Parcel::readString8(String8* pArg) const
1975{
1976 int32_t size;
1977 status_t status = readInt32(&size);
1978 if (status != OK) {
1979 return status;
1980 }
1981 // watch for potential int overflow from size+1
1982 if (size < 0 || size >= INT32_MAX) {
1983 return BAD_VALUE;
1984 }
1985 // |writeString8| writes nothing for empty string.
1986 if (size == 0) {
1987 *pArg = String8();
1988 return OK;
1989 }
1990 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07001991 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07001992 return BAD_VALUE;
1993 }
1994 pArg->setTo(str, size);
1995 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001996}
1997
1998String16 Parcel::readString16() const
1999{
2000 size_t len;
2001 const char16_t* str = readString16Inplace(&len);
2002 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002003 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002004 return String16();
2005}
2006
Casey Dahlinb9872622015-11-25 15:09:45 -08002007status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2008{
2009 const int32_t start = dataPosition();
2010 int32_t size;
2011 status_t status = readInt32(&size);
2012 pArg->reset();
2013
2014 if (status != OK || size < 0) {
2015 return status;
2016 }
2017
2018 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002019 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002020
2021 status = readString16(pArg->get());
2022
2023 if (status != OK) {
2024 pArg->reset();
2025 }
2026
2027 return status;
2028}
2029
Casey Dahlin451ff582015-10-19 18:12:18 -07002030status_t Parcel::readString16(String16* pArg) const
2031{
2032 size_t len;
2033 const char16_t* str = readString16Inplace(&len);
2034 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002035 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002036 return 0;
2037 } else {
2038 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002039 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002040 }
2041}
2042
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002043const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2044{
2045 int32_t size = readInt32();
2046 // watch for potential int overflow from size+1
2047 if (size >= 0 && size < INT32_MAX) {
2048 *outLen = size;
2049 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002050 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002051 return str;
2052 }
2053 }
2054 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002055 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002056}
2057
Casey Dahlinf0c13772015-10-27 18:33:56 -07002058status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2059{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002060 status_t status = readNullableStrongBinder(val);
2061 if (status == OK && !val->get()) {
2062 status = UNEXPECTED_NULL;
2063 }
2064 return status;
2065}
2066
2067status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2068{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002069 return unflatten_binder(ProcessState::self(), *this, val);
2070}
2071
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002072sp<IBinder> Parcel::readStrongBinder() const
2073{
2074 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002075 // Note that a lot of code in Android reads binders by hand with this
2076 // method, and that code has historically been ok with getting nullptr
2077 // back (while ignoring error codes).
2078 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002079 return val;
2080}
2081
2082wp<IBinder> Parcel::readWeakBinder() const
2083{
2084 wp<IBinder> val;
2085 unflatten_binder(ProcessState::self(), *this, &val);
2086 return val;
2087}
2088
Christopher Wiley97f048d2015-11-19 06:49:05 -08002089status_t Parcel::readParcelable(Parcelable* parcelable) const {
2090 int32_t have_parcelable = 0;
2091 status_t status = readInt32(&have_parcelable);
2092 if (status != OK) {
2093 return status;
2094 }
2095 if (!have_parcelable) {
2096 return UNEXPECTED_NULL;
2097 }
2098 return parcelable->readFromParcel(this);
2099}
2100
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002101int32_t Parcel::readExceptionCode() const
2102{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002103 binder::Status status;
2104 status.readFromParcel(*this);
2105 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002106}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002107
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002108native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002109{
2110 int numFds, numInts;
2111 status_t err;
2112 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002113 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002114 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002115 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002116
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002117 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002118 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002119 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002120 }
2121
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002122 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002123 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002124 if (h->data[i] < 0) {
2125 for (int j = 0; j < i; j++) {
2126 close(h->data[j]);
2127 }
2128 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002129 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002130 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002131 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002132 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002133 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002134 native_handle_close(h);
2135 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002136 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002137 }
2138 return h;
2139}
2140
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141int Parcel::readFileDescriptor() const
2142{
2143 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002144
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002145 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002146 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002147 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002148
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002149 return BAD_TYPE;
2150}
2151
Dianne Hackborn1941a402016-08-29 12:30:43 -07002152int Parcel::readParcelFileDescriptor() const
2153{
2154 int32_t hasComm = readInt32();
2155 int fd = readFileDescriptor();
2156 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002157 // detach (owned by the binder driver)
2158 int comm = readFileDescriptor();
2159
2160 // warning: this must be kept in sync with:
2161 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2162 enum ParcelFileDescriptorStatus {
2163 DETACHED = 2,
2164 };
2165
2166#if BYTE_ORDER == BIG_ENDIAN
2167 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2168#endif
2169#if BYTE_ORDER == LITTLE_ENDIAN
2170 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2171#endif
2172
2173 ssize_t written = TEMP_FAILURE_RETRY(
2174 ::write(comm, &message, sizeof(message)));
2175
2176 if (written == -1 || written != sizeof(message)) {
2177 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2178 written, strerror(errno));
2179 return BAD_TYPE;
2180 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002181 }
2182 return fd;
2183}
2184
Christopher Wiley2cf19952016-04-11 11:09:37 -07002185status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002186{
2187 int got = readFileDescriptor();
2188
2189 if (got == BAD_TYPE) {
2190 return BAD_TYPE;
2191 }
2192
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002193 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002194
2195 if (val->get() < 0) {
2196 return BAD_VALUE;
2197 }
2198
2199 return OK;
2200}
2201
Ryo Hashimotobf551892018-05-31 16:58:35 +09002202status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2203{
2204 int got = readParcelFileDescriptor();
2205
2206 if (got == BAD_TYPE) {
2207 return BAD_TYPE;
2208 }
2209
2210 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2211
2212 if (val->get() < 0) {
2213 return BAD_VALUE;
2214 }
2215
2216 return OK;
2217}
Casey Dahlin06673e32015-11-23 13:24:23 -08002218
Christopher Wiley2cf19952016-04-11 11:09:37 -07002219status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002220 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2221}
2222
Christopher Wiley2cf19952016-04-11 11:09:37 -07002223status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002224 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2225}
2226
Jeff Brown5707dbf2011-09-23 21:17:56 -07002227status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2228{
Jeff Brown13b16042014-11-11 16:44:25 -08002229 int32_t blobType;
2230 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002231 if (status) return status;
2232
Jeff Brown13b16042014-11-11 16:44:25 -08002233 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002234 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002235 const void* ptr = readInplace(len);
2236 if (!ptr) return BAD_VALUE;
2237
Jeff Brown13b16042014-11-11 16:44:25 -08002238 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002239 return NO_ERROR;
2240 }
2241
Steve Block6807e592011-10-20 11:56:00 +01002242 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002243 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002244 int fd = readFileDescriptor();
2245 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2246
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002247 if (!ashmem_valid(fd)) {
2248 ALOGE("invalid fd");
2249 return BAD_VALUE;
2250 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002251 int size = ashmem_get_size_region(fd);
2252 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002253 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002254 return BAD_VALUE;
2255 }
Yi Kong91635562018-06-07 14:38:36 -07002256 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002257 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002258 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002259
Jeff Brown13b16042014-11-11 16:44:25 -08002260 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002261 return NO_ERROR;
2262}
2263
Mathias Agopiane1424282013-07-29 21:24:40 -07002264status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002265{
2266 // size
2267 const size_t len = this->readInt32();
2268 const size_t fd_count = this->readInt32();
2269
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002270 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002271 // don't accept size_t values which may have come from an
2272 // inadvertent conversion from a negative int.
2273 return BAD_VALUE;
2274 }
2275
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002276 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002277 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002278 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002279 return BAD_VALUE;
2280
Yi Kong91635562018-06-07 14:38:36 -07002281 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002282 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002283 fds = new (std::nothrow) int[fd_count];
2284 if (fds == nullptr) {
2285 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2286 return BAD_VALUE;
2287 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002288 }
2289
2290 status_t err = NO_ERROR;
2291 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002292 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002293 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002294 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002295 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 -07002296 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2297 // Close all the file descriptors that were dup-ed.
2298 for (size_t j=0; j<i ;j++) {
2299 close(fds[j]);
2300 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002301 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002302 }
2303
2304 if (err == NO_ERROR) {
2305 err = val.unflatten(buf, len, fds, fd_count);
2306 }
2307
2308 if (fd_count) {
2309 delete [] fds;
2310 }
2311
2312 return err;
2313}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002314const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2315{
2316 const size_t DPOS = mDataPos;
2317 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2318 const flat_binder_object* obj
2319 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2320 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002321 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002322 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002323 // the object list, so we don't want to check for it when
2324 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002325 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002326 return obj;
2327 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002328
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002329 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002330 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002331 const size_t N = mObjectsSize;
2332 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002333
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002334 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002335 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002336 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002337
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002338 // Start at the current hint position, looking for an object at
2339 // the current data position.
2340 if (opos < N) {
2341 while (opos < (N-1) && OBJS[opos] < DPOS) {
2342 opos++;
2343 }
2344 } else {
2345 opos = N-1;
2346 }
2347 if (OBJS[opos] == DPOS) {
2348 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002349 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002350 this, DPOS, opos);
2351 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002352 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002353 return obj;
2354 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002355
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002356 // Look backwards for it...
2357 while (opos > 0 && OBJS[opos] > DPOS) {
2358 opos--;
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 backward 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 }
2368 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002369 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 -07002370 this, DPOS);
2371 }
Yi Kong91635562018-06-07 14:38:36 -07002372 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002373}
2374
2375void Parcel::closeFileDescriptors()
2376{
2377 size_t i = mObjectsSize;
2378 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002379 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002380 }
2381 while (i > 0) {
2382 i--;
2383 const flat_binder_object* flat
2384 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002385 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002386 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002387 close(flat->handle);
2388 }
2389 }
2390}
2391
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002392uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002393{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002394 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002395}
2396
2397size_t Parcel::ipcDataSize() const
2398{
2399 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2400}
2401
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002402uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002403{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002404 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002405}
2406
2407size_t Parcel::ipcObjectsCount() const
2408{
2409 return mObjectsSize;
2410}
2411
2412void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002413 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002414{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002415 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002416 freeDataNoInit();
2417 mError = NO_ERROR;
2418 mData = const_cast<uint8_t*>(data);
2419 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002420 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002422 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002423 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002424 mObjectsSize = mObjectsCapacity = objectsCount;
2425 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002426 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427 mOwner = relFunc;
2428 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002429 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002430 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002431 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002432 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002433 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002434 mObjectsSize = 0;
2435 break;
2436 }
2437 minOffset = offset + sizeof(flat_binder_object);
2438 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002439 scanForFds();
2440}
2441
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002442void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002443{
2444 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002445
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002446 if (errorCheck() != NO_ERROR) {
2447 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002448 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002449 } else if (dataSize() > 0) {
2450 const uint8_t* DATA = data();
2451 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002452 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002453 const size_t N = objectsCount();
2454 for (size_t i=0; i<N; i++) {
2455 const flat_binder_object* flat
2456 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2457 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002458 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002459 << " = " << flat->binder;
2460 }
2461 } else {
2462 to << "NULL";
2463 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002464
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002465 to << ")";
2466}
2467
2468void Parcel::releaseObjects()
2469{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002470 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002471 if (i == 0) {
2472 return;
2473 }
2474 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002475 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002476 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002477 while (i > 0) {
2478 i--;
2479 const flat_binder_object* flat
2480 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002481 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002482 }
2483}
2484
2485void Parcel::acquireObjects()
2486{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002487 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002488 if (i == 0) {
2489 return;
2490 }
2491 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002492 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002493 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002494 while (i > 0) {
2495 i--;
2496 const flat_binder_object* flat
2497 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002498 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002499 }
2500}
2501
2502void Parcel::freeData()
2503{
2504 freeDataNoInit();
2505 initState();
2506}
2507
2508void Parcel::freeDataNoInit()
2509{
2510 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002511 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002512 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2514 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002515 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002517 if (mData) {
2518 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002519 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002520 if (mDataCapacity <= gParcelGlobalAllocSize) {
2521 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2522 } else {
2523 gParcelGlobalAllocSize = 0;
2524 }
2525 if (gParcelGlobalAllocCount > 0) {
2526 gParcelGlobalAllocCount--;
2527 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002528 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002529 free(mData);
2530 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002531 if (mObjects) free(mObjects);
2532 }
2533}
2534
2535status_t Parcel::growData(size_t len)
2536{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002537 if (len > INT32_MAX) {
2538 // don't accept size_t values which may have come from an
2539 // inadvertent conversion from a negative int.
2540 return BAD_VALUE;
2541 }
2542
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002543 size_t newSize = ((mDataSize+len)*3)/2;
2544 return (newSize <= mDataSize)
2545 ? (status_t) NO_MEMORY
2546 : continueWrite(newSize);
2547}
2548
2549status_t Parcel::restartWrite(size_t desired)
2550{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002551 if (desired > INT32_MAX) {
2552 // don't accept size_t values which may have come from an
2553 // inadvertent conversion from a negative int.
2554 return BAD_VALUE;
2555 }
2556
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002557 if (mOwner) {
2558 freeData();
2559 return continueWrite(desired);
2560 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002561
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002562 uint8_t* data = (uint8_t*)realloc(mData, desired);
2563 if (!data && desired > mDataCapacity) {
2564 mError = NO_MEMORY;
2565 return NO_MEMORY;
2566 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002567
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002568 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002569
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002570 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002571 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002572 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002573 gParcelGlobalAllocSize += desired;
2574 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002575 if (!mData) {
2576 gParcelGlobalAllocCount++;
2577 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002578 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002579 mData = data;
2580 mDataCapacity = desired;
2581 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002582
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002583 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002584 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2585 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2586
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002587 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002588 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002589 mObjectsSize = mObjectsCapacity = 0;
2590 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002591 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002592 mHasFds = false;
2593 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002594 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002595
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002596 return NO_ERROR;
2597}
2598
2599status_t Parcel::continueWrite(size_t desired)
2600{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002601 if (desired > INT32_MAX) {
2602 // don't accept size_t values which may have come from an
2603 // inadvertent conversion from a negative int.
2604 return BAD_VALUE;
2605 }
2606
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002607 // If shrinking, first adjust for any objects that appear
2608 // after the new data size.
2609 size_t objectsSize = mObjectsSize;
2610 if (desired < mDataSize) {
2611 if (desired == 0) {
2612 objectsSize = 0;
2613 } else {
2614 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002615 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002616 break;
2617 objectsSize--;
2618 }
2619 }
2620 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002621
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002622 if (mOwner) {
2623 // If the size is going to zero, just release the owner's data.
2624 if (desired == 0) {
2625 freeData();
2626 return NO_ERROR;
2627 }
2628
2629 // If there is a different owner, we need to take
2630 // posession.
2631 uint8_t* data = (uint8_t*)malloc(desired);
2632 if (!data) {
2633 mError = NO_MEMORY;
2634 return NO_MEMORY;
2635 }
Yi Kong91635562018-06-07 14:38:36 -07002636 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002637
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002638 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002639 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002640 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002641 free(data);
2642
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002643 mError = NO_MEMORY;
2644 return NO_MEMORY;
2645 }
2646
2647 // Little hack to only acquire references on objects
2648 // we will be keeping.
2649 size_t oldObjectsSize = mObjectsSize;
2650 mObjectsSize = objectsSize;
2651 acquireObjects();
2652 mObjectsSize = oldObjectsSize;
2653 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002654
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002655 if (mData) {
2656 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2657 }
2658 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002659 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002660 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002661 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002662 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002663 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002664
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002665 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002666 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002667 gParcelGlobalAllocSize += desired;
2668 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002669 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002670
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002671 mData = data;
2672 mObjects = objects;
2673 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002674 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002675 mDataCapacity = desired;
2676 mObjectsSize = mObjectsCapacity = objectsSize;
2677 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002678 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002679
2680 } else if (mData) {
2681 if (objectsSize < mObjectsSize) {
2682 // Need to release refs on any objects we are dropping.
2683 const sp<ProcessState> proc(ProcessState::self());
2684 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2685 const flat_binder_object* flat
2686 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002687 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002688 // will need to rescan because we may have lopped off the only FDs
2689 mFdsKnown = false;
2690 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002691 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002692 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002693
2694 if (objectsSize == 0) {
2695 free(mObjects);
2696 mObjects = nullptr;
2697 } else {
2698 binder_size_t* objects =
2699 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2700 if (objects) {
2701 mObjects = objects;
2702 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002703 }
2704 mObjectsSize = objectsSize;
2705 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002706 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002707 }
2708
2709 // We own the data, so we can just do a realloc().
2710 if (desired > mDataCapacity) {
2711 uint8_t* data = (uint8_t*)realloc(mData, desired);
2712 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002713 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2714 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002715 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002716 gParcelGlobalAllocSize += desired;
2717 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002718 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002719 mData = data;
2720 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002721 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002722 mError = NO_MEMORY;
2723 return NO_MEMORY;
2724 }
2725 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002726 if (mDataSize > desired) {
2727 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002728 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002729 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002730 if (mDataPos > desired) {
2731 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002732 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002733 }
2734 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002735
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002736 } else {
2737 // This is the first data. Easy!
2738 uint8_t* data = (uint8_t*)malloc(desired);
2739 if (!data) {
2740 mError = NO_MEMORY;
2741 return NO_MEMORY;
2742 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002743
Yi Kong91635562018-06-07 14:38:36 -07002744 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002745 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002746 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002747 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002748
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002749 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002750 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002751 gParcelGlobalAllocSize += desired;
2752 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002753 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002754
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002755 mData = data;
2756 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002757 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2758 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002759 mDataCapacity = desired;
2760 }
2761
2762 return NO_ERROR;
2763}
2764
2765void Parcel::initState()
2766{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002767 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002768 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002769 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002770 mDataSize = 0;
2771 mDataCapacity = 0;
2772 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002773 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2774 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002775 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002776 mObjectsSize = 0;
2777 mObjectsCapacity = 0;
2778 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002779 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002780 mHasFds = false;
2781 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002782 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002783 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002784 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002785 mWorkSourceRequestHeaderPosition = 0;
2786 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002787
2788 // racing multiple init leads only to multiple identical write
2789 if (gMaxFds == 0) {
2790 struct rlimit result;
2791 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2792 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002793 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002794 } else {
2795 ALOGW("Unable to getrlimit: %s", strerror(errno));
2796 gMaxFds = 1024;
2797 }
2798 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002799}
2800
2801void Parcel::scanForFds() const
2802{
2803 bool hasFds = false;
2804 for (size_t i=0; i<mObjectsSize; i++) {
2805 const flat_binder_object* flat
2806 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002807 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002808 hasFds = true;
2809 break;
2810 }
2811 }
2812 mHasFds = hasFds;
2813 mFdsKnown = true;
2814}
2815
Dan Sandleraa5c2342015-04-10 10:08:45 -04002816size_t Parcel::getBlobAshmemSize() const
2817{
Adrian Roos6bb31142015-10-22 16:46:12 -07002818 // This used to return the size of all blobs that were written to ashmem, now we're returning
2819 // the ashmem currently referenced by this Parcel, which should be equivalent.
2820 // TODO: Remove method once ABI can be changed.
2821 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002822}
2823
Adrian Rooscbf37262015-10-22 16:12:53 -07002824size_t Parcel::getOpenAshmemSize() const
2825{
2826 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002827}
2828
2829// --- Parcel::Blob ---
2830
2831Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002832 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002833}
2834
2835Parcel::Blob::~Blob() {
2836 release();
2837}
2838
2839void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002840 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002841 ::munmap(mData, mSize);
2842 }
2843 clear();
2844}
2845
Jeff Brown13b16042014-11-11 16:44:25 -08002846void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2847 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002848 mData = data;
2849 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002850 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002851}
2852
2853void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002854 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002855 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002856 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002857 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002858}
2859
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002860}; // namespace android