blob: a595c013ca4a9759fec216a9e9c560f3d54f75d7 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080023#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080028#include <sys/stat.h>
29#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070030#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080031#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070032
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/Binder.h>
34#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080035#include <binder/IPCThreadState.h>
36#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070037#include <binder/ProcessState.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080038#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070039#include <binder/TextOutput.h>
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -080040#include <binder/Value.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041
Mark Salyzynabed7f72016-01-27 08:02:48 -080042#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070043#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080044#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080046#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String8.h>
48#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049
Mathias Agopian208059f2009-05-18 15:08:03 -070050#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080051#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053#ifndef INT32_MAX
54#define INT32_MAX ((int32_t)(2147483647))
55#endif
56
57#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080058//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080059#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080060//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070061
62// ---------------------------------------------------------------------------
63
Nick Kralevichb6b14232015-04-02 09:36:02 -070064// This macro should never be used at runtime, as a too large value
65// of s could cause an integer overflow. Instead, you should always
66// use the wrapper function pad_size()
67#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
68
69static size_t pad_size(size_t s) {
70 if (s > (SIZE_T_MAX - 3)) {
71 abort();
72 }
73 return PAD_SIZE_UNSAFE(s);
74}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070076// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060077#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070078
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070079// XXX This can be made public if we want to provide
80// support for typed data.
81struct small_flat_data
82{
83 uint32_t type;
84 uint32_t data;
85};
86
87namespace android {
88
Dianne Hackborna4cff882014-11-13 17:07:40 -080089static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
90static size_t gParcelGlobalAllocSize = 0;
91static size_t gParcelGlobalAllocCount = 0;
92
Christopher Tatee4e0ae82016-03-24 16:03:44 -070093static size_t gMaxFds = 0;
94
Jeff Brown13b16042014-11-11 16:44:25 -080095// Maximum size of a blob to transfer in-place.
96static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
97
98enum {
99 BLOB_INPLACE = 0,
100 BLOB_ASHMEM_IMMUTABLE = 1,
101 BLOB_ASHMEM_MUTABLE = 2,
102};
103
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700105 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700107 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108 case BINDER_TYPE_BINDER:
109 if (obj.binder) {
110 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800111 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700112 }
113 return;
114 case BINDER_TYPE_WEAK_BINDER:
115 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800116 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700117 return;
118 case BINDER_TYPE_HANDLE: {
119 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700120 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
122 b->incStrong(who);
123 }
124 return;
125 }
126 case BINDER_TYPE_WEAK_HANDLE: {
127 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700128 if (b != nullptr) b.get_refs()->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700129 return;
130 }
131 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000132 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700133 // If we own an ashmem fd, keep track of how much memory it refers to.
134 int size = ashmem_get_size_region(obj.handle);
135 if (size > 0) {
136 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700137 }
138 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 return;
140 }
141 }
142
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700143 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144}
145
Adrian Roos6bb31142015-10-22 16:46:12 -0700146void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 const flat_binder_object& obj, const void* who)
148{
Yi Kong91635562018-06-07 14:38:36 -0700149 acquire_object(proc, obj, who, nullptr);
Adrian Roos6bb31142015-10-22 16:46:12 -0700150}
151
152static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700153 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700154{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700155 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700156 case BINDER_TYPE_BINDER:
157 if (obj.binder) {
158 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800159 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 }
161 return;
162 case BINDER_TYPE_WEAK_BINDER:
163 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800164 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165 return;
166 case BINDER_TYPE_HANDLE: {
167 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700168 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
170 b->decStrong(who);
171 }
172 return;
173 }
174 case BINDER_TYPE_WEAK_HANDLE: {
175 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700176 if (b != nullptr) b.get_refs()->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700177 return;
178 }
179 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800180 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000181 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700182 int size = ashmem_get_size_region(obj.handle);
183 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800184 // ashmem size might have changed since last time it was accounted for, e.g.
185 // in acquire_object(). Value of *outAshmemSize is not critical since we are
186 // releasing the object anyway. Check for integer overflow condition.
187 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700188 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700189 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800190
191 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700192 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700193 return;
194 }
195 }
196
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700197 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700198}
199
Adrian Roos6bb31142015-10-22 16:46:12 -0700200void release_object(const sp<ProcessState>& proc,
201 const flat_binder_object& obj, const void* who)
202{
Yi Kong91635562018-06-07 14:38:36 -0700203 release_object(proc, obj, who, nullptr);
Adrian Roos6bb31142015-10-22 16:46:12 -0700204}
205
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700206inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800207 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700208{
209 return out->writeObject(flat, false);
210}
211
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800212status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700213 const sp<IBinder>& binder, Parcel* out)
214{
215 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700216
Martijn Coenen2b631742017-05-05 11:16:59 -0700217 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
218 /* minimum priority for all nodes is nice 0 */
219 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
220 } else {
221 /* minimum priority for all nodes is MAX_NICE(19) */
222 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
223 }
224
Yi Kong91635562018-06-07 14:38:36 -0700225 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800226 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 if (!local) {
228 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700229 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000230 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 }
232 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700233 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800234 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800236 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700237 } else {
Steven Morelandf0212002018-12-26 13:59:23 -0800238 if (local->isRequestingSid()) {
239 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
240 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700241 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800242 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
243 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700244 }
245 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700246 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800247 obj.binder = 0;
248 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700249 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700250
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700251 return finish_flatten_binder(binder, obj, out);
252}
253
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800254status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255 const wp<IBinder>& binder, Parcel* out)
256{
257 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700258
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700259 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Yi Kong91635562018-06-07 14:38:36 -0700260 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261 sp<IBinder> real = binder.promote();
Yi Kong91635562018-06-07 14:38:36 -0700262 if (real != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700263 IBinder *local = real->localBinder();
264 if (!local) {
265 BpBinder *proxy = real->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700266 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000267 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700268 }
269 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700270 obj.hdr.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800271 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700272 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800273 obj.cookie = 0;
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_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800276 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
277 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700278 }
279 return finish_flatten_binder(real, obj, out);
280 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700281
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700282 // XXX How to deal? In order to flatten the given binder,
283 // we need to probe it for information, which requires a primary
284 // reference... but we don't have one.
285 //
286 // The OpenBinder implementation uses a dynamic_cast<> here,
287 // but we can't do that with the different reference counting
288 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000289 ALOGE("Unable to unflatten Binder weak reference!");
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700290 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800291 obj.binder = 0;
292 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700293 return finish_flatten_binder(nullptr, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700294
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700295 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700296 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800297 obj.binder = 0;
298 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700299 return finish_flatten_binder(nullptr, obj, out);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700300 }
301}
302
303inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800304 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
305 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700306{
307 return NO_ERROR;
308}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700309
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700310status_t unflatten_binder(const sp<ProcessState>& proc,
311 const Parcel& in, sp<IBinder>* out)
312{
313 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700314
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700315 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700316 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700317 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800318 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700319 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700320 case BINDER_TYPE_HANDLE:
321 *out = proc->getStrongProxyForHandle(flat->handle);
322 return finish_unflatten_binder(
323 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700324 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700325 }
326 return BAD_TYPE;
327}
328
329status_t unflatten_binder(const sp<ProcessState>& proc,
330 const Parcel& in, wp<IBinder>* out)
331{
332 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700333
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700334 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700335 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700336 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800337 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700338 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700339 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800340 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700341 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800342 reinterpret_cast<IBinder*>(flat->cookie),
343 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700344 } else {
Yi Kong91635562018-06-07 14:38:36 -0700345 *out = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700346 }
Yi Kong91635562018-06-07 14:38:36 -0700347 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700348 case BINDER_TYPE_HANDLE:
349 case BINDER_TYPE_WEAK_HANDLE:
350 *out = proc->getWeakProxyForHandle(flat->handle);
351 return finish_unflatten_binder(
352 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
353 }
354 }
355 return BAD_TYPE;
356}
357
358// ---------------------------------------------------------------------------
359
360Parcel::Parcel()
361{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800362 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700363 initState();
364}
365
366Parcel::~Parcel()
367{
368 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800369 LOG_ALLOC("Parcel %p: destroyed", this);
370}
371
372size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800373 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
374 size_t size = gParcelGlobalAllocSize;
375 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
376 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800377}
378
379size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800380 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
381 size_t count = gParcelGlobalAllocCount;
382 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
383 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700384}
385
386const uint8_t* Parcel::data() const
387{
388 return mData;
389}
390
391size_t Parcel::dataSize() const
392{
393 return (mDataSize > mDataPos ? mDataSize : mDataPos);
394}
395
396size_t Parcel::dataAvail() const
397{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700398 size_t result = dataSize() - dataPosition();
399 if (result > INT32_MAX) {
400 abort();
401 }
402 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700403}
404
405size_t Parcel::dataPosition() const
406{
407 return mDataPos;
408}
409
410size_t Parcel::dataCapacity() const
411{
412 return mDataCapacity;
413}
414
415status_t Parcel::setDataSize(size_t size)
416{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700417 if (size > INT32_MAX) {
418 // don't accept size_t values which may have come from an
419 // inadvertent conversion from a negative int.
420 return BAD_VALUE;
421 }
422
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700423 status_t err;
424 err = continueWrite(size);
425 if (err == NO_ERROR) {
426 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700427 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700428 }
429 return err;
430}
431
432void Parcel::setDataPosition(size_t pos) const
433{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700434 if (pos > INT32_MAX) {
435 // don't accept size_t values which may have come from an
436 // inadvertent conversion from a negative int.
437 abort();
438 }
439
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700440 mDataPos = pos;
441 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800442 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700443}
444
445status_t Parcel::setDataCapacity(size_t size)
446{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700447 if (size > INT32_MAX) {
448 // don't accept size_t values which may have come from an
449 // inadvertent conversion from a negative int.
450 return BAD_VALUE;
451 }
452
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700453 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700454 return NO_ERROR;
455}
456
457status_t Parcel::setData(const uint8_t* buffer, size_t len)
458{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700459 if (len > INT32_MAX) {
460 // don't accept size_t values which may have come from an
461 // inadvertent conversion from a negative int.
462 return BAD_VALUE;
463 }
464
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700465 status_t err = restartWrite(len);
466 if (err == NO_ERROR) {
467 memcpy(const_cast<uint8_t*>(data()), buffer, len);
468 mDataSize = len;
469 mFdsKnown = false;
470 }
471 return err;
472}
473
Andreas Huber51faf462011-04-13 10:21:56 -0700474status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700475{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700476 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700477 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800478 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700479 size_t size = parcel->mObjectsSize;
480 int startPos = mDataPos;
481 int firstIndex = -1, lastIndex = -2;
482
483 if (len == 0) {
484 return NO_ERROR;
485 }
486
Nick Kralevichb6b14232015-04-02 09:36:02 -0700487 if (len > INT32_MAX) {
488 // don't accept size_t values which may have come from an
489 // inadvertent conversion from a negative int.
490 return BAD_VALUE;
491 }
492
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700493 // range checks against the source parcel size
494 if ((offset > parcel->mDataSize)
495 || (len > parcel->mDataSize)
496 || (offset + len > parcel->mDataSize)) {
497 return BAD_VALUE;
498 }
499
500 // Count objects in range
501 for (int i = 0; i < (int) size; i++) {
502 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700503 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700504 if (firstIndex == -1) {
505 firstIndex = i;
506 }
507 lastIndex = i;
508 }
509 }
510 int numObjects = lastIndex - firstIndex + 1;
511
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700512 if ((mDataSize+len) > mDataCapacity) {
513 // grow data
514 err = growData(len);
515 if (err != NO_ERROR) {
516 return err;
517 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700518 }
519
520 // append data
521 memcpy(mData + mDataPos, data + offset, len);
522 mDataPos += len;
523 mDataSize += len;
524
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400525 err = NO_ERROR;
526
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700527 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200528 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700529 // grow objects
530 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700531 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700532 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800533 binder_size_t *objects =
534 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700535 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700536 return NO_MEMORY;
537 }
538 mObjects = objects;
539 mObjectsCapacity = newSize;
540 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700541
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700542 // append and acquire objects
543 int idx = mObjectsSize;
544 for (int i = firstIndex; i <= lastIndex; i++) {
545 size_t off = objects[i] - offset + startPos;
546 mObjects[idx++] = off;
547 mObjectsSize++;
548
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700549 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700550 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700551 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700552
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700553 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700554 // If this is a file descriptor, we need to dup it so the
555 // new Parcel now owns its own fd, and can declare that we
556 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800557 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800558 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700559 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400560 if (!mAllowFds) {
561 err = FDS_NOT_ALLOWED;
562 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700563 }
564 }
565 }
566
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400567 return err;
568}
569
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700570int Parcel::compareData(const Parcel& other) {
571 size_t size = dataSize();
572 if (size != other.dataSize()) {
573 return size < other.dataSize() ? -1 : 1;
574 }
575 return memcmp(data(), other.data(), size);
576}
577
Jeff Brown13b16042014-11-11 16:44:25 -0800578bool Parcel::allowFds() const
579{
580 return mAllowFds;
581}
582
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700583bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400584{
585 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700586 if (!allowFds) {
587 mAllowFds = false;
588 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400589 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700590}
591
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700592void Parcel::restoreAllowFds(bool lastValue)
593{
594 mAllowFds = lastValue;
595}
596
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700597bool Parcel::hasFileDescriptors() const
598{
599 if (!mFdsKnown) {
600 scanForFds();
601 }
602 return mHasFds;
603}
604
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000605void Parcel::updateWorkSourceRequestHeaderPosition() const {
606 // Only update the request headers once. We only want to point
607 // to the first headers read/written.
608 if (!mRequestHeaderPresent) {
609 mWorkSourceRequestHeaderPosition = dataPosition();
610 mRequestHeaderPresent = true;
611 }
612}
613
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700614// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700615status_t Parcel::writeInterfaceToken(const String16& interface)
616{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000617 const IPCThreadState* threadState = IPCThreadState::self();
618 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000619 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000620 writeInt32(threadState->shouldPropagateWorkSource() ?
621 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700622 // currently the interface identification token is just its name as a string
623 return writeString16(interface);
624}
625
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000626bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
627{
628 if (!mRequestHeaderPresent) {
629 return false;
630 }
631
632 const size_t initialPosition = dataPosition();
633 setDataPosition(mWorkSourceRequestHeaderPosition);
634 status_t err = writeInt32(uid);
635 setDataPosition(initialPosition);
636 return err == NO_ERROR;
637}
638
639uid_t Parcel::readCallingWorkSourceUid()
640{
641 if (!mRequestHeaderPresent) {
642 return IPCThreadState::kUnsetWorkSource;
643 }
644
645 const size_t initialPosition = dataPosition();
646 setDataPosition(mWorkSourceRequestHeaderPosition);
647 uid_t uid = readInt32();
648 setDataPosition(initialPosition);
649 return uid;
650}
651
Mathias Agopian83c04462009-05-22 19:00:22 -0700652bool Parcel::checkInterface(IBinder* binder) const
653{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700654 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700655}
656
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700657bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700658 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700659{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100660 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700661 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700662 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700663 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700664 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700665 if ((threadState->getLastTransactionBinderFlags() &
666 IBinder::FLAG_ONEWAY) != 0) {
667 // For one-way calls, the callee is running entirely
668 // disconnected from the caller, so disable StrictMode entirely.
669 // Not only does disk/network usage not impact the caller, but
670 // there's no way to commuicate back any violations anyway.
671 threadState->setStrictModePolicy(0);
672 } else {
673 threadState->setStrictModePolicy(strictPolicy);
674 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100675 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000676 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100677 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000678 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100679 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700680 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700681 if (str == interface) {
682 return true;
683 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700684 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700685 String8(interface).string(), String8(str).string());
686 return false;
687 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700688}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700689
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800690const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700691{
692 return mObjects;
693}
694
695size_t Parcel::objectsCount() const
696{
697 return mObjectsSize;
698}
699
700status_t Parcel::errorCheck() const
701{
702 return mError;
703}
704
705void Parcel::setError(status_t err)
706{
707 mError = err;
708}
709
710status_t Parcel::finishWrite(size_t len)
711{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700712 if (len > INT32_MAX) {
713 // don't accept size_t values which may have come from an
714 // inadvertent conversion from a negative int.
715 return BAD_VALUE;
716 }
717
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700718 //printf("Finish write of %d\n", len);
719 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700720 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700721 if (mDataPos > mDataSize) {
722 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700723 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700724 }
725 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
726 return NO_ERROR;
727}
728
729status_t Parcel::writeUnpadded(const void* data, size_t len)
730{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700731 if (len > INT32_MAX) {
732 // don't accept size_t values which may have come from an
733 // inadvertent conversion from a negative int.
734 return BAD_VALUE;
735 }
736
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700737 size_t end = mDataPos + len;
738 if (end < mDataPos) {
739 // integer overflow
740 return BAD_VALUE;
741 }
742
743 if (end <= mDataCapacity) {
744restart_write:
745 memcpy(mData+mDataPos, data, len);
746 return finishWrite(len);
747 }
748
749 status_t err = growData(len);
750 if (err == NO_ERROR) goto restart_write;
751 return err;
752}
753
754status_t Parcel::write(const void* data, size_t len)
755{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700756 if (len > INT32_MAX) {
757 // don't accept size_t values which may have come from an
758 // inadvertent conversion from a negative int.
759 return BAD_VALUE;
760 }
761
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700762 void* const d = writeInplace(len);
763 if (d) {
764 memcpy(d, data, len);
765 return NO_ERROR;
766 }
767 return mError;
768}
769
770void* Parcel::writeInplace(size_t len)
771{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700772 if (len > INT32_MAX) {
773 // don't accept size_t values which may have come from an
774 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700775 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700776 }
777
778 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700779
780 // sanity check for integer overflow
781 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700782 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700783 }
784
785 if ((mDataPos+padded) <= mDataCapacity) {
786restart_write:
787 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
788 uint8_t* const data = mData+mDataPos;
789
790 // Need to pad at end?
791 if (padded != len) {
792#if BYTE_ORDER == BIG_ENDIAN
793 static const uint32_t mask[4] = {
794 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
795 };
796#endif
797#if BYTE_ORDER == LITTLE_ENDIAN
798 static const uint32_t mask[4] = {
799 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
800 };
801#endif
802 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
803 // *reinterpret_cast<void**>(data+padded-4));
804 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
805 }
806
807 finishWrite(padded);
808 return data;
809 }
810
811 status_t err = growData(padded);
812 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700813 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700814}
815
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800816status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
817 const uint8_t* strData = (uint8_t*)str.data();
818 const size_t strLen= str.length();
819 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100820 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800821 return BAD_VALUE;
822 }
823
824 status_t err = writeInt32(utf16Len);
825 if (err) {
826 return err;
827 }
828
829 // Allocate enough bytes to hold our converted string and its terminating NULL.
830 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
831 if (!dst) {
832 return NO_MEMORY;
833 }
834
Sergio Girof4607432016-07-21 14:46:35 +0100835 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800836
837 return NO_ERROR;
838}
839
840status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
841 if (!str) {
842 return writeInt32(-1);
843 }
844 return writeUtf8AsUtf16(*str);
845}
846
Casey Dahlin185d3442016-02-09 11:08:35 -0800847namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800848
Casey Dahlin185d3442016-02-09 11:08:35 -0800849template<typename T>
850status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700851{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700852 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700853 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700854 status = BAD_VALUE;
855 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700856 }
857
Casey Dahlin185d3442016-02-09 11:08:35 -0800858 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700859 if (status != OK) {
860 return status;
861 }
862
Casey Dahlin185d3442016-02-09 11:08:35 -0800863 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700864 if (!data) {
865 status = BAD_VALUE;
866 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700867 }
868
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700869 memcpy(data, val.data(), val.size());
870 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700871}
872
Casey Dahlin185d3442016-02-09 11:08:35 -0800873template<typename T>
874status_t writeByteVectorInternalPtr(Parcel* parcel,
875 const std::unique_ptr<std::vector<T>>& val)
876{
877 if (!val) {
878 return parcel->writeInt32(-1);
879 }
880
881 return writeByteVectorInternal(parcel, *val);
882}
883
884} // namespace
885
886status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
887 return writeByteVectorInternal(this, val);
888}
889
890status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
891{
892 return writeByteVectorInternalPtr(this, val);
893}
894
895status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
896 return writeByteVectorInternal(this, val);
897}
898
899status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
900{
901 return writeByteVectorInternalPtr(this, val);
902}
903
Casey Dahlin451ff582015-10-19 18:12:18 -0700904status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
905{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800906 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700907}
908
Casey Dahlinb9872622015-11-25 15:09:45 -0800909status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
910{
911 return writeNullableTypedVector(val, &Parcel::writeInt32);
912}
913
Casey Dahlin451ff582015-10-19 18:12:18 -0700914status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
915{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800916 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700917}
918
Casey Dahlinb9872622015-11-25 15:09:45 -0800919status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
920{
921 return writeNullableTypedVector(val, &Parcel::writeInt64);
922}
923
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800924status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
925{
926 return writeTypedVector(val, &Parcel::writeUint64);
927}
928
929status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
930{
931 return writeNullableTypedVector(val, &Parcel::writeUint64);
932}
933
Casey Dahlin451ff582015-10-19 18:12:18 -0700934status_t Parcel::writeFloatVector(const std::vector<float>& val)
935{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800936 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700937}
938
Casey Dahlinb9872622015-11-25 15:09:45 -0800939status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
940{
941 return writeNullableTypedVector(val, &Parcel::writeFloat);
942}
943
Casey Dahlin451ff582015-10-19 18:12:18 -0700944status_t Parcel::writeDoubleVector(const std::vector<double>& val)
945{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800946 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700947}
948
Casey Dahlinb9872622015-11-25 15:09:45 -0800949status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
950{
951 return writeNullableTypedVector(val, &Parcel::writeDouble);
952}
953
Casey Dahlin451ff582015-10-19 18:12:18 -0700954status_t Parcel::writeBoolVector(const std::vector<bool>& val)
955{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800956 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700957}
958
Casey Dahlinb9872622015-11-25 15:09:45 -0800959status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
960{
961 return writeNullableTypedVector(val, &Parcel::writeBool);
962}
963
Casey Dahlin451ff582015-10-19 18:12:18 -0700964status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
965{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800966 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700967}
968
Casey Dahlinb9872622015-11-25 15:09:45 -0800969status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
970{
971 return writeNullableTypedVector(val, &Parcel::writeChar);
972}
973
Casey Dahlin451ff582015-10-19 18:12:18 -0700974status_t Parcel::writeString16Vector(const std::vector<String16>& val)
975{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800976 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700977}
978
Casey Dahlinb9872622015-11-25 15:09:45 -0800979status_t Parcel::writeString16Vector(
980 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
981{
982 return writeNullableTypedVector(val, &Parcel::writeString16);
983}
984
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800985status_t Parcel::writeUtf8VectorAsUtf16Vector(
986 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
987 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
988}
989
990status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
991 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
992}
993
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700994status_t Parcel::writeInt32(int32_t val)
995{
Andreas Huber84a6d042009-08-17 13:33:27 -0700996 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700997}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800998
999status_t Parcel::writeUint32(uint32_t val)
1000{
1001 return writeAligned(val);
1002}
1003
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001004status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001005 if (len > INT32_MAX) {
1006 // don't accept size_t values which may have come from an
1007 // inadvertent conversion from a negative int.
1008 return BAD_VALUE;
1009 }
1010
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001011 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001012 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001013 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001014 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001015 if (ret == NO_ERROR) {
1016 ret = write(val, len * sizeof(*val));
1017 }
1018 return ret;
1019}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001020status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001021 if (len > INT32_MAX) {
1022 // don't accept size_t values which may have come from an
1023 // inadvertent conversion from a negative int.
1024 return BAD_VALUE;
1025 }
1026
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001027 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001028 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001029 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001030 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001031 if (ret == NO_ERROR) {
1032 ret = write(val, len * sizeof(*val));
1033 }
1034 return ret;
1035}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001036
Casey Dahlind6848f52015-10-15 15:44:59 -07001037status_t Parcel::writeBool(bool val)
1038{
1039 return writeInt32(int32_t(val));
1040}
1041
1042status_t Parcel::writeChar(char16_t val)
1043{
1044 return writeInt32(int32_t(val));
1045}
1046
1047status_t Parcel::writeByte(int8_t val)
1048{
1049 return writeInt32(int32_t(val));
1050}
1051
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001052status_t Parcel::writeInt64(int64_t val)
1053{
Andreas Huber84a6d042009-08-17 13:33:27 -07001054 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001055}
1056
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001057status_t Parcel::writeUint64(uint64_t val)
1058{
1059 return writeAligned(val);
1060}
1061
Serban Constantinescuf683e012013-11-05 16:53:55 +00001062status_t Parcel::writePointer(uintptr_t val)
1063{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001064 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001065}
1066
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001067status_t Parcel::writeFloat(float val)
1068{
Andreas Huber84a6d042009-08-17 13:33:27 -07001069 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001070}
1071
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001072#if defined(__mips__) && defined(__mips_hard_float)
1073
1074status_t Parcel::writeDouble(double val)
1075{
1076 union {
1077 double d;
1078 unsigned long long ll;
1079 } u;
1080 u.d = val;
1081 return writeAligned(u.ll);
1082}
1083
1084#else
1085
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001086status_t Parcel::writeDouble(double val)
1087{
Andreas Huber84a6d042009-08-17 13:33:27 -07001088 return writeAligned(val);
1089}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001090
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001091#endif
1092
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001093status_t Parcel::writeCString(const char* str)
1094{
1095 return write(str, strlen(str)+1);
1096}
1097
1098status_t Parcel::writeString8(const String8& str)
1099{
1100 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001101 // only write string if its length is more than zero characters,
1102 // as readString8 will only read if the length field is non-zero.
1103 // this is slightly different from how writeString16 works.
1104 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001105 err = write(str.string(), str.bytes()+1);
1106 }
1107 return err;
1108}
1109
Casey Dahlinb9872622015-11-25 15:09:45 -08001110status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1111{
1112 if (!str) {
1113 return writeInt32(-1);
1114 }
1115
1116 return writeString16(*str);
1117}
1118
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001119status_t Parcel::writeString16(const String16& str)
1120{
1121 return writeString16(str.string(), str.size());
1122}
1123
1124status_t Parcel::writeString16(const char16_t* str, size_t len)
1125{
Yi Kong91635562018-06-07 14:38:36 -07001126 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001127
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001128 status_t err = writeInt32(len);
1129 if (err == NO_ERROR) {
1130 len *= sizeof(char16_t);
1131 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1132 if (data) {
1133 memcpy(data, str, len);
1134 *reinterpret_cast<char16_t*>(data+len) = 0;
1135 return NO_ERROR;
1136 }
1137 err = mError;
1138 }
1139 return err;
1140}
1141
1142status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1143{
1144 return flatten_binder(ProcessState::self(), val, this);
1145}
1146
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001147status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1148{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001149 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001150}
1151
Casey Dahlinb9872622015-11-25 15:09:45 -08001152status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1153{
1154 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1155}
1156
1157status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001158 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001159}
1160
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001161status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001162 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001163}
1164
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001165status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1166{
1167 return flatten_binder(ProcessState::self(), val, this);
1168}
1169
Casey Dahlinb9872622015-11-25 15:09:45 -08001170status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1171 if (!parcelable) {
1172 return writeInt32(0);
1173 }
1174
1175 return writeParcelable(*parcelable);
1176}
1177
Christopher Wiley97f048d2015-11-19 06:49:05 -08001178status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1179 status_t status = writeInt32(1); // parcelable is not null.
1180 if (status != OK) {
1181 return status;
1182 }
1183 return parcelable.writeToParcel(this);
1184}
1185
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001186status_t Parcel::writeValue(const binder::Value& value) {
1187 return value.writeToParcel(this);
1188}
1189
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001190status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001191{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001192 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001193 return BAD_TYPE;
1194
1195 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001196 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001197 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001199 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001200 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001202 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1203 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204
1205 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001206 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001207 return err;
1208 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001209 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001210 return err;
1211}
1212
Jeff Brown93ff1f92011-11-04 19:01:44 -07001213status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001214{
1215 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001216 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001217 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001218 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001219 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001220 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001221 return writeObject(obj, true);
1222}
1223
1224status_t Parcel::writeDupFileDescriptor(int fd)
1225{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001226 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001227 if (dupFd < 0) {
1228 return -errno;
1229 }
1230 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001231 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001232 close(dupFd);
1233 }
1234 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001235}
1236
Dianne Hackborn1941a402016-08-29 12:30:43 -07001237status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1238{
1239 writeInt32(0);
1240 return writeFileDescriptor(fd, takeOwnership);
1241}
1242
Ryo Hashimotobf551892018-05-31 16:58:35 +09001243status_t Parcel::writeDupParcelFileDescriptor(int fd)
1244{
1245 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1246 if (dupFd < 0) {
1247 return -errno;
1248 }
1249 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1250 if (err != OK) {
1251 close(dupFd);
1252 }
1253 return err;
1254}
1255
Christopher Wiley2cf19952016-04-11 11:09:37 -07001256status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001257 return writeDupFileDescriptor(fd.get());
1258}
1259
Christopher Wiley2cf19952016-04-11 11:09:37 -07001260status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001261 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1262}
1263
Christopher Wiley2cf19952016-04-11 11:09:37 -07001264status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001265 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1266}
1267
Jeff Brown13b16042014-11-11 16:44:25 -08001268status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001269{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001270 if (len > INT32_MAX) {
1271 // don't accept size_t values which may have come from an
1272 // inadvertent conversion from a negative int.
1273 return BAD_VALUE;
1274 }
1275
Jeff Brown13b16042014-11-11 16:44:25 -08001276 status_t status;
1277 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001278 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001279 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001280 if (status) return status;
1281
1282 void* ptr = writeInplace(len);
1283 if (!ptr) return NO_MEMORY;
1284
Jeff Brown13b16042014-11-11 16:44:25 -08001285 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001286 return NO_ERROR;
1287 }
1288
Steve Block6807e592011-10-20 11:56:00 +01001289 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001290 int fd = ashmem_create_region("Parcel Blob", len);
1291 if (fd < 0) return NO_MEMORY;
1292
1293 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1294 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001295 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001296 } else {
Yi Kong91635562018-06-07 14:38:36 -07001297 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001298 if (ptr == MAP_FAILED) {
1299 status = -errno;
1300 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001301 if (!mutableCopy) {
1302 result = ashmem_set_prot_region(fd, PROT_READ);
1303 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001304 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001305 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001306 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001307 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001308 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001309 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001310 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001311 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001312 return NO_ERROR;
1313 }
1314 }
1315 }
1316 }
1317 ::munmap(ptr, len);
1318 }
1319 ::close(fd);
1320 return status;
1321}
1322
Jeff Brown13b16042014-11-11 16:44:25 -08001323status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1324{
1325 // Must match up with what's done in writeBlob.
1326 if (!mAllowFds) return FDS_NOT_ALLOWED;
1327 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1328 if (status) return status;
1329 return writeDupFileDescriptor(fd);
1330}
1331
Mathias Agopiane1424282013-07-29 21:24:40 -07001332status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001333{
1334 status_t err;
1335
1336 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001337 const size_t len = val.getFlattenedSize();
1338 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001339
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001340 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001341 // don't accept size_t values which may have come from an
1342 // inadvertent conversion from a negative int.
1343 return BAD_VALUE;
1344 }
1345
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001346 err = this->writeInt32(len);
1347 if (err) return err;
1348
1349 err = this->writeInt32(fd_count);
1350 if (err) return err;
1351
1352 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001353 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001354 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001355 return BAD_VALUE;
1356
Yi Kong91635562018-06-07 14:38:36 -07001357 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001358 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001359 fds = new (std::nothrow) int[fd_count];
1360 if (fds == nullptr) {
1361 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1362 return BAD_VALUE;
1363 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001364 }
1365
1366 err = val.flatten(buf, len, fds, fd_count);
1367 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1368 err = this->writeDupFileDescriptor( fds[i] );
1369 }
1370
1371 if (fd_count) {
1372 delete [] fds;
1373 }
1374
1375 return err;
1376}
1377
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001378status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1379{
1380 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1381 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1382 if (enoughData && enoughObjects) {
1383restart_write:
1384 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001385
Christopher Tate98e67d32015-06-03 18:44:15 -07001386 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001387 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001388 if (!mAllowFds) {
1389 // fail before modifying our object index
1390 return FDS_NOT_ALLOWED;
1391 }
1392 mHasFds = mFdsKnown = true;
1393 }
1394
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001395 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001396 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001397 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001398 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001399 mObjectsSize++;
1400 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001401
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001402 return finishWrite(sizeof(flat_binder_object));
1403 }
1404
1405 if (!enoughData) {
1406 const status_t err = growData(sizeof(val));
1407 if (err != NO_ERROR) return err;
1408 }
1409 if (!enoughObjects) {
1410 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001411 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001412 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001413 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001414 mObjects = objects;
1415 mObjectsCapacity = newSize;
1416 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001417
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001418 goto restart_write;
1419}
1420
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001421status_t Parcel::writeNoException()
1422{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001423 binder::Status status;
1424 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001425}
1426
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001427status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1428{
1429 using ::std::map;
1430 using ::android::binder::Value;
1431 using ::android::binder::Map;
1432
1433 Map::const_iterator iter;
1434 status_t ret;
1435
1436 ret = writeInt32(map_in.size());
1437
1438 if (ret != NO_ERROR) {
1439 return ret;
1440 }
1441
1442 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1443 ret = writeValue(Value(iter->first));
1444 if (ret != NO_ERROR) {
1445 return ret;
1446 }
1447
1448 ret = writeValue(iter->second);
1449 if (ret != NO_ERROR) {
1450 return ret;
1451 }
1452 }
1453
1454 return ret;
1455}
1456
1457status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1458{
Yi Kong91635562018-06-07 14:38:36 -07001459 if (map == nullptr) {
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001460 return writeInt32(-1);
1461 }
1462
1463 return writeMap(*map.get());
1464}
1465
1466status_t Parcel::readMap(::android::binder::Map* map_out)const
1467{
1468 using ::std::map;
1469 using ::android::String16;
1470 using ::android::String8;
1471 using ::android::binder::Value;
1472 using ::android::binder::Map;
1473
1474 status_t ret = NO_ERROR;
1475 int32_t count;
1476
1477 ret = readInt32(&count);
1478 if (ret != NO_ERROR) {
1479 return ret;
1480 }
1481
1482 if (count < 0) {
1483 ALOGE("readMap: Unexpected count: %d", count);
1484 return (count == -1)
1485 ? UNEXPECTED_NULL
1486 : BAD_VALUE;
1487 }
1488
1489 map_out->clear();
1490
1491 while (count--) {
1492 Map::key_type key;
1493 Value value;
1494
1495 ret = readValue(&value);
1496 if (ret != NO_ERROR) {
1497 return ret;
1498 }
1499
1500 if (!value.getString(&key)) {
1501 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1502 return BAD_VALUE;
1503 }
1504
1505 ret = readValue(&value);
1506 if (ret != NO_ERROR) {
1507 return ret;
1508 }
1509
1510 (*map_out)[key] = value;
1511 }
1512
1513 return ret;
1514}
1515
1516status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1517{
1518 const size_t start = dataPosition();
1519 int32_t count;
1520 status_t status = readInt32(&count);
1521 map->reset();
1522
1523 if (status != OK || count == -1) {
1524 return status;
1525 }
1526
1527 setDataPosition(start);
1528 map->reset(new binder::Map());
1529
1530 status = readMap(map->get());
1531
1532 if (status != OK) {
1533 map->reset();
1534 }
1535
1536 return status;
1537}
1538
1539
1540
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001541void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001542{
1543 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1544}
1545
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001546status_t Parcel::validateReadData(size_t upperBound) const
1547{
1548 // Don't allow non-object reads on object data
1549 if (mObjectsSorted || mObjectsSize <= 1) {
1550data_sorted:
1551 // Expect to check only against the next object
1552 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1553 // For some reason the current read position is greater than the next object
1554 // hint. Iterate until we find the right object
1555 size_t nextObject = mNextObjectHint;
1556 do {
1557 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1558 // Requested info overlaps with an object
1559 ALOGE("Attempt to read from protected data in Parcel %p", this);
1560 return PERMISSION_DENIED;
1561 }
1562 nextObject++;
1563 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1564 mNextObjectHint = nextObject;
1565 }
1566 return NO_ERROR;
1567 }
1568 // Quickly determine if mObjects is sorted.
1569 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1570 binder_size_t* prevObj = currObj;
1571 while (currObj > mObjects) {
1572 prevObj--;
1573 if(*prevObj > *currObj) {
1574 goto data_unsorted;
1575 }
1576 currObj--;
1577 }
1578 mObjectsSorted = true;
1579 goto data_sorted;
1580
1581data_unsorted:
1582 // Insertion Sort mObjects
1583 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1584 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1585 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1586 binder_size_t temp = *iter0;
1587 binder_size_t* iter1 = iter0 - 1;
1588 while (iter1 >= mObjects && *iter1 > temp) {
1589 *(iter1 + 1) = *iter1;
1590 iter1--;
1591 }
1592 *(iter1 + 1) = temp;
1593 }
1594 mNextObjectHint = 0;
1595 mObjectsSorted = true;
1596 goto data_sorted;
1597}
1598
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001599status_t Parcel::read(void* outData, size_t len) const
1600{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001601 if (len > INT32_MAX) {
1602 // don't accept size_t values which may have come from an
1603 // inadvertent conversion from a negative int.
1604 return BAD_VALUE;
1605 }
1606
1607 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1608 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001609 if (mObjectsSize > 0) {
1610 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001611 if(err != NO_ERROR) {
1612 // Still increment the data position by the expected length
1613 mDataPos += pad_size(len);
1614 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1615 return err;
1616 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001617 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001618 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001619 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001620 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001621 return NO_ERROR;
1622 }
1623 return NOT_ENOUGH_DATA;
1624}
1625
1626const void* Parcel::readInplace(size_t len) const
1627{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001628 if (len > INT32_MAX) {
1629 // don't accept size_t values which may have come from an
1630 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001631 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001632 }
1633
1634 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1635 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001636 if (mObjectsSize > 0) {
1637 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001638 if(err != NO_ERROR) {
1639 // Still increment the data position by the expected length
1640 mDataPos += pad_size(len);
1641 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001642 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001643 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001644 }
1645
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001646 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001647 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001648 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001649 return data;
1650 }
Yi Kong91635562018-06-07 14:38:36 -07001651 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001652}
1653
Andreas Huber84a6d042009-08-17 13:33:27 -07001654template<class T>
1655status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001656 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001657
1658 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001659 if (mObjectsSize > 0) {
1660 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001661 if(err != NO_ERROR) {
1662 // Still increment the data position by the expected length
1663 mDataPos += sizeof(T);
1664 return err;
1665 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001666 }
1667
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001668 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001669 mDataPos += sizeof(T);
1670 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001671 return NO_ERROR;
1672 } else {
1673 return NOT_ENOUGH_DATA;
1674 }
1675}
1676
Andreas Huber84a6d042009-08-17 13:33:27 -07001677template<class T>
1678T Parcel::readAligned() const {
1679 T result;
1680 if (readAligned(&result) != NO_ERROR) {
1681 result = 0;
1682 }
1683
1684 return result;
1685}
1686
1687template<class T>
1688status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001689 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001690
1691 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1692restart_write:
1693 *reinterpret_cast<T*>(mData+mDataPos) = val;
1694 return finishWrite(sizeof(val));
1695 }
1696
1697 status_t err = growData(sizeof(val));
1698 if (err == NO_ERROR) goto restart_write;
1699 return err;
1700}
1701
Casey Dahlin185d3442016-02-09 11:08:35 -08001702namespace {
1703
1704template<typename T>
1705status_t readByteVectorInternal(const Parcel* parcel,
1706 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001707 val->clear();
1708
1709 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001710 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001711
1712 if (status != OK) {
1713 return status;
1714 }
1715
Christopher Wiley4db672d2015-11-10 09:44:30 -08001716 if (size < 0) {
1717 status = UNEXPECTED_NULL;
1718 return status;
1719 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001720 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001721 status = BAD_VALUE;
1722 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001723 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001724
Paul Lietar433e87b2016-09-16 10:39:32 -07001725 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001726 if (!data) {
1727 status = BAD_VALUE;
1728 return status;
1729 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001730 val->reserve(size);
1731 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001732
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001733 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001734}
1735
Casey Dahlin185d3442016-02-09 11:08:35 -08001736template<typename T>
1737status_t readByteVectorInternalPtr(
1738 const Parcel* parcel,
1739 std::unique_ptr<std::vector<T>>* val) {
1740 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001741 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001742 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001743 val->reset();
1744
1745 if (status != OK || size < 0) {
1746 return status;
1747 }
1748
Casey Dahlin185d3442016-02-09 11:08:35 -08001749 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001750 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001751
Casey Dahlin185d3442016-02-09 11:08:35 -08001752 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001753
1754 if (status != OK) {
1755 val->reset();
1756 }
1757
1758 return status;
1759}
1760
Casey Dahlin185d3442016-02-09 11:08:35 -08001761} // namespace
1762
1763status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1764 return readByteVectorInternal(this, val);
1765}
1766
1767status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1768 return readByteVectorInternal(this, val);
1769}
1770
1771status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1772 return readByteVectorInternalPtr(this, val);
1773}
1774
1775status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1776 return readByteVectorInternalPtr(this, val);
1777}
1778
Casey Dahlinb9872622015-11-25 15:09:45 -08001779status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1780 return readNullableTypedVector(val, &Parcel::readInt32);
1781}
1782
Casey Dahlin451ff582015-10-19 18:12:18 -07001783status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001784 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001785}
1786
Casey Dahlinb9872622015-11-25 15:09:45 -08001787status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1788 return readNullableTypedVector(val, &Parcel::readInt64);
1789}
1790
Casey Dahlin451ff582015-10-19 18:12:18 -07001791status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001792 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001793}
1794
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001795status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1796 return readNullableTypedVector(val, &Parcel::readUint64);
1797}
1798
1799status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1800 return readTypedVector(val, &Parcel::readUint64);
1801}
1802
Casey Dahlinb9872622015-11-25 15:09:45 -08001803status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1804 return readNullableTypedVector(val, &Parcel::readFloat);
1805}
1806
Casey Dahlin451ff582015-10-19 18:12:18 -07001807status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001808 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001809}
1810
Casey Dahlinb9872622015-11-25 15:09:45 -08001811status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1812 return readNullableTypedVector(val, &Parcel::readDouble);
1813}
1814
Casey Dahlin451ff582015-10-19 18:12:18 -07001815status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001816 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001817}
1818
Casey Dahlinb9872622015-11-25 15:09:45 -08001819status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1820 const int32_t start = dataPosition();
1821 int32_t size;
1822 status_t status = readInt32(&size);
1823 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001824
Casey Dahlinb9872622015-11-25 15:09:45 -08001825 if (status != OK || size < 0) {
1826 return status;
1827 }
1828
1829 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001830 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001831
1832 status = readBoolVector(val->get());
1833
1834 if (status != OK) {
1835 val->reset();
1836 }
1837
1838 return status;
1839}
1840
1841status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001842 int32_t size;
1843 status_t status = readInt32(&size);
1844
1845 if (status != OK) {
1846 return status;
1847 }
1848
1849 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001850 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001851 }
1852
1853 val->resize(size);
1854
1855 /* C++ bool handling means a vector of bools isn't necessarily addressable
1856 * (we might use individual bits)
1857 */
Christopher Wiley97887982015-10-27 16:33:47 -07001858 bool data;
1859 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001860 status = readBool(&data);
1861 (*val)[i] = data;
1862
1863 if (status != OK) {
1864 return status;
1865 }
1866 }
1867
1868 return OK;
1869}
1870
Casey Dahlinb9872622015-11-25 15:09:45 -08001871status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1872 return readNullableTypedVector(val, &Parcel::readChar);
1873}
1874
Casey Dahlin451ff582015-10-19 18:12:18 -07001875status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001876 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001877}
1878
Casey Dahlinb9872622015-11-25 15:09:45 -08001879status_t Parcel::readString16Vector(
1880 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1881 return readNullableTypedVector(val, &Parcel::readString16);
1882}
1883
Casey Dahlin451ff582015-10-19 18:12:18 -07001884status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001885 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001886}
1887
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001888status_t Parcel::readUtf8VectorFromUtf16Vector(
1889 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1890 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1891}
1892
1893status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1894 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1895}
Casey Dahlin451ff582015-10-19 18:12:18 -07001896
Andreas Huber84a6d042009-08-17 13:33:27 -07001897status_t Parcel::readInt32(int32_t *pArg) const
1898{
1899 return readAligned(pArg);
1900}
1901
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001902int32_t Parcel::readInt32() const
1903{
Andreas Huber84a6d042009-08-17 13:33:27 -07001904 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001905}
1906
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001907status_t Parcel::readUint32(uint32_t *pArg) const
1908{
1909 return readAligned(pArg);
1910}
1911
1912uint32_t Parcel::readUint32() const
1913{
1914 return readAligned<uint32_t>();
1915}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001916
1917status_t Parcel::readInt64(int64_t *pArg) const
1918{
Andreas Huber84a6d042009-08-17 13:33:27 -07001919 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001920}
1921
1922
1923int64_t Parcel::readInt64() const
1924{
Andreas Huber84a6d042009-08-17 13:33:27 -07001925 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001926}
1927
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001928status_t Parcel::readUint64(uint64_t *pArg) const
1929{
1930 return readAligned(pArg);
1931}
1932
1933uint64_t Parcel::readUint64() const
1934{
1935 return readAligned<uint64_t>();
1936}
1937
Serban Constantinescuf683e012013-11-05 16:53:55 +00001938status_t Parcel::readPointer(uintptr_t *pArg) const
1939{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001940 status_t ret;
1941 binder_uintptr_t ptr;
1942 ret = readAligned(&ptr);
1943 if (!ret)
1944 *pArg = ptr;
1945 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001946}
1947
1948uintptr_t Parcel::readPointer() const
1949{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001950 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001951}
1952
1953
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001954status_t Parcel::readFloat(float *pArg) const
1955{
Andreas Huber84a6d042009-08-17 13:33:27 -07001956 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001957}
1958
1959
1960float Parcel::readFloat() const
1961{
Andreas Huber84a6d042009-08-17 13:33:27 -07001962 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001963}
1964
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001965#if defined(__mips__) && defined(__mips_hard_float)
1966
1967status_t Parcel::readDouble(double *pArg) const
1968{
1969 union {
1970 double d;
1971 unsigned long long ll;
1972 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001973 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001974 status_t status;
1975 status = readAligned(&u.ll);
1976 *pArg = u.d;
1977 return status;
1978}
1979
1980double Parcel::readDouble() const
1981{
1982 union {
1983 double d;
1984 unsigned long long ll;
1985 } u;
1986 u.ll = readAligned<unsigned long long>();
1987 return u.d;
1988}
1989
1990#else
1991
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001992status_t Parcel::readDouble(double *pArg) const
1993{
Andreas Huber84a6d042009-08-17 13:33:27 -07001994 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001995}
1996
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001997double Parcel::readDouble() const
1998{
Andreas Huber84a6d042009-08-17 13:33:27 -07001999 return readAligned<double>();
2000}
2001
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002002#endif
2003
Andreas Huber84a6d042009-08-17 13:33:27 -07002004status_t Parcel::readIntPtr(intptr_t *pArg) const
2005{
2006 return readAligned(pArg);
2007}
2008
2009
2010intptr_t Parcel::readIntPtr() const
2011{
2012 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002013}
2014
Casey Dahlind6848f52015-10-15 15:44:59 -07002015status_t Parcel::readBool(bool *pArg) const
2016{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002017 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002018 status_t ret = readInt32(&tmp);
2019 *pArg = (tmp != 0);
2020 return ret;
2021}
2022
2023bool Parcel::readBool() const
2024{
2025 return readInt32() != 0;
2026}
2027
2028status_t Parcel::readChar(char16_t *pArg) const
2029{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002030 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002031 status_t ret = readInt32(&tmp);
2032 *pArg = char16_t(tmp);
2033 return ret;
2034}
2035
2036char16_t Parcel::readChar() const
2037{
2038 return char16_t(readInt32());
2039}
2040
2041status_t Parcel::readByte(int8_t *pArg) const
2042{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002043 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002044 status_t ret = readInt32(&tmp);
2045 *pArg = int8_t(tmp);
2046 return ret;
2047}
2048
2049int8_t Parcel::readByte() const
2050{
2051 return int8_t(readInt32());
2052}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002053
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002054status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2055 size_t utf16Size = 0;
2056 const char16_t* src = readString16Inplace(&utf16Size);
2057 if (!src) {
2058 return UNEXPECTED_NULL;
2059 }
2060
2061 // Save ourselves the trouble, we're done.
2062 if (utf16Size == 0u) {
2063 str->clear();
2064 return NO_ERROR;
2065 }
2066
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002067 // Allow for closing '\0'
2068 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2069 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002070 return BAD_VALUE;
2071 }
2072 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002073 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002074 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002075 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2076 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002077 return NO_ERROR;
2078}
2079
2080status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2081 const int32_t start = dataPosition();
2082 int32_t size;
2083 status_t status = readInt32(&size);
2084 str->reset();
2085
2086 if (status != OK || size < 0) {
2087 return status;
2088 }
2089
2090 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002091 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002092 return readUtf8FromUtf16(str->get());
2093}
2094
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002095const char* Parcel::readCString() const
2096{
2097 const size_t avail = mDataSize-mDataPos;
2098 if (avail > 0) {
2099 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2100 // is the string's trailing NUL within the parcel's valid bounds?
2101 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2102 if (eos) {
2103 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002104 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002105 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002106 return str;
2107 }
2108 }
Yi Kong91635562018-06-07 14:38:36 -07002109 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002110}
2111
2112String8 Parcel::readString8() const
2113{
Roshan Pius87b64d22016-07-18 12:51:02 -07002114 String8 retString;
2115 status_t status = readString8(&retString);
2116 if (status != OK) {
2117 // We don't care about errors here, so just return an empty string.
2118 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002119 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002120 return retString;
2121}
2122
2123status_t Parcel::readString8(String8* pArg) const
2124{
2125 int32_t size;
2126 status_t status = readInt32(&size);
2127 if (status != OK) {
2128 return status;
2129 }
2130 // watch for potential int overflow from size+1
2131 if (size < 0 || size >= INT32_MAX) {
2132 return BAD_VALUE;
2133 }
2134 // |writeString8| writes nothing for empty string.
2135 if (size == 0) {
2136 *pArg = String8();
2137 return OK;
2138 }
2139 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07002140 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002141 return BAD_VALUE;
2142 }
2143 pArg->setTo(str, size);
2144 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002145}
2146
2147String16 Parcel::readString16() const
2148{
2149 size_t len;
2150 const char16_t* str = readString16Inplace(&len);
2151 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002152 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002153 return String16();
2154}
2155
Casey Dahlinb9872622015-11-25 15:09:45 -08002156status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2157{
2158 const int32_t start = dataPosition();
2159 int32_t size;
2160 status_t status = readInt32(&size);
2161 pArg->reset();
2162
2163 if (status != OK || size < 0) {
2164 return status;
2165 }
2166
2167 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002168 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002169
2170 status = readString16(pArg->get());
2171
2172 if (status != OK) {
2173 pArg->reset();
2174 }
2175
2176 return status;
2177}
2178
Casey Dahlin451ff582015-10-19 18:12:18 -07002179status_t Parcel::readString16(String16* pArg) const
2180{
2181 size_t len;
2182 const char16_t* str = readString16Inplace(&len);
2183 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002184 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002185 return 0;
2186 } else {
2187 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002188 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002189 }
2190}
2191
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002192const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2193{
2194 int32_t size = readInt32();
2195 // watch for potential int overflow from size+1
2196 if (size >= 0 && size < INT32_MAX) {
2197 *outLen = size;
2198 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002199 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002200 return str;
2201 }
2202 }
2203 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002204 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002205}
2206
Casey Dahlinf0c13772015-10-27 18:33:56 -07002207status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2208{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002209 status_t status = readNullableStrongBinder(val);
2210 if (status == OK && !val->get()) {
2211 status = UNEXPECTED_NULL;
2212 }
2213 return status;
2214}
2215
2216status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2217{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002218 return unflatten_binder(ProcessState::self(), *this, val);
2219}
2220
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002221sp<IBinder> Parcel::readStrongBinder() const
2222{
2223 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002224 // Note that a lot of code in Android reads binders by hand with this
2225 // method, and that code has historically been ok with getting nullptr
2226 // back (while ignoring error codes).
2227 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002228 return val;
2229}
2230
2231wp<IBinder> Parcel::readWeakBinder() const
2232{
2233 wp<IBinder> val;
2234 unflatten_binder(ProcessState::self(), *this, &val);
2235 return val;
2236}
2237
Christopher Wiley97f048d2015-11-19 06:49:05 -08002238status_t Parcel::readParcelable(Parcelable* parcelable) const {
2239 int32_t have_parcelable = 0;
2240 status_t status = readInt32(&have_parcelable);
2241 if (status != OK) {
2242 return status;
2243 }
2244 if (!have_parcelable) {
2245 return UNEXPECTED_NULL;
2246 }
2247 return parcelable->readFromParcel(this);
2248}
2249
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002250status_t Parcel::readValue(binder::Value* value) const {
2251 return value->readFromParcel(this);
2252}
2253
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002254int32_t Parcel::readExceptionCode() const
2255{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002256 binder::Status status;
2257 status.readFromParcel(*this);
2258 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002259}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002260
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002261native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002262{
2263 int numFds, numInts;
2264 status_t err;
2265 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002266 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002267 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002268 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002269
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002270 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002271 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002272 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002273 }
2274
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002275 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002276 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002277 if (h->data[i] < 0) {
2278 for (int j = 0; j < i; j++) {
2279 close(h->data[j]);
2280 }
2281 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002282 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002283 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002284 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002285 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002286 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002287 native_handle_close(h);
2288 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002289 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002290 }
2291 return h;
2292}
2293
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002294int Parcel::readFileDescriptor() const
2295{
2296 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002297
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002298 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002299 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002300 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002301
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002302 return BAD_TYPE;
2303}
2304
Dianne Hackborn1941a402016-08-29 12:30:43 -07002305int Parcel::readParcelFileDescriptor() const
2306{
2307 int32_t hasComm = readInt32();
2308 int fd = readFileDescriptor();
2309 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002310 // detach (owned by the binder driver)
2311 int comm = readFileDescriptor();
2312
2313 // warning: this must be kept in sync with:
2314 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2315 enum ParcelFileDescriptorStatus {
2316 DETACHED = 2,
2317 };
2318
2319#if BYTE_ORDER == BIG_ENDIAN
2320 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2321#endif
2322#if BYTE_ORDER == LITTLE_ENDIAN
2323 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2324#endif
2325
2326 ssize_t written = TEMP_FAILURE_RETRY(
2327 ::write(comm, &message, sizeof(message)));
2328
2329 if (written == -1 || written != sizeof(message)) {
2330 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2331 written, strerror(errno));
2332 return BAD_TYPE;
2333 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002334 }
2335 return fd;
2336}
2337
Christopher Wiley2cf19952016-04-11 11:09:37 -07002338status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002339{
2340 int got = readFileDescriptor();
2341
2342 if (got == BAD_TYPE) {
2343 return BAD_TYPE;
2344 }
2345
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002346 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002347
2348 if (val->get() < 0) {
2349 return BAD_VALUE;
2350 }
2351
2352 return OK;
2353}
2354
Ryo Hashimotobf551892018-05-31 16:58:35 +09002355status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2356{
2357 int got = readParcelFileDescriptor();
2358
2359 if (got == BAD_TYPE) {
2360 return BAD_TYPE;
2361 }
2362
2363 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2364
2365 if (val->get() < 0) {
2366 return BAD_VALUE;
2367 }
2368
2369 return OK;
2370}
Casey Dahlin06673e32015-11-23 13:24:23 -08002371
Christopher Wiley2cf19952016-04-11 11:09:37 -07002372status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002373 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2374}
2375
Christopher Wiley2cf19952016-04-11 11:09:37 -07002376status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002377 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2378}
2379
Jeff Brown5707dbf2011-09-23 21:17:56 -07002380status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2381{
Jeff Brown13b16042014-11-11 16:44:25 -08002382 int32_t blobType;
2383 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002384 if (status) return status;
2385
Jeff Brown13b16042014-11-11 16:44:25 -08002386 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002387 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002388 const void* ptr = readInplace(len);
2389 if (!ptr) return BAD_VALUE;
2390
Jeff Brown13b16042014-11-11 16:44:25 -08002391 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002392 return NO_ERROR;
2393 }
2394
Steve Block6807e592011-10-20 11:56:00 +01002395 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002396 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002397 int fd = readFileDescriptor();
2398 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2399
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002400 if (!ashmem_valid(fd)) {
2401 ALOGE("invalid fd");
2402 return BAD_VALUE;
2403 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002404 int size = ashmem_get_size_region(fd);
2405 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002406 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002407 return BAD_VALUE;
2408 }
Yi Kong91635562018-06-07 14:38:36 -07002409 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002410 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002411 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002412
Jeff Brown13b16042014-11-11 16:44:25 -08002413 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002414 return NO_ERROR;
2415}
2416
Mathias Agopiane1424282013-07-29 21:24:40 -07002417status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002418{
2419 // size
2420 const size_t len = this->readInt32();
2421 const size_t fd_count = this->readInt32();
2422
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002423 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002424 // don't accept size_t values which may have come from an
2425 // inadvertent conversion from a negative int.
2426 return BAD_VALUE;
2427 }
2428
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002429 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002430 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002431 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002432 return BAD_VALUE;
2433
Yi Kong91635562018-06-07 14:38:36 -07002434 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002435 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002436 fds = new (std::nothrow) int[fd_count];
2437 if (fds == nullptr) {
2438 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2439 return BAD_VALUE;
2440 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002441 }
2442
2443 status_t err = NO_ERROR;
2444 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002445 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002446 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002447 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002448 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 -07002449 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2450 // Close all the file descriptors that were dup-ed.
2451 for (size_t j=0; j<i ;j++) {
2452 close(fds[j]);
2453 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002454 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002455 }
2456
2457 if (err == NO_ERROR) {
2458 err = val.unflatten(buf, len, fds, fd_count);
2459 }
2460
2461 if (fd_count) {
2462 delete [] fds;
2463 }
2464
2465 return err;
2466}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002467const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2468{
2469 const size_t DPOS = mDataPos;
2470 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2471 const flat_binder_object* obj
2472 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2473 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002474 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002475 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002476 // the object list, so we don't want to check for it when
2477 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002478 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479 return obj;
2480 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002481
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002482 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002483 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002484 const size_t N = mObjectsSize;
2485 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002486
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002487 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002488 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002489 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002490
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002491 // Start at the current hint position, looking for an object at
2492 // the current data position.
2493 if (opos < N) {
2494 while (opos < (N-1) && OBJS[opos] < DPOS) {
2495 opos++;
2496 }
2497 } else {
2498 opos = N-1;
2499 }
2500 if (OBJS[opos] == DPOS) {
2501 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002502 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002503 this, DPOS, opos);
2504 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002505 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002506 return obj;
2507 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002508
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002509 // Look backwards for it...
2510 while (opos > 0 && OBJS[opos] > DPOS) {
2511 opos--;
2512 }
2513 if (OBJS[opos] == DPOS) {
2514 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002515 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516 this, DPOS, opos);
2517 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002518 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002519 return obj;
2520 }
2521 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002522 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 -07002523 this, DPOS);
2524 }
Yi Kong91635562018-06-07 14:38:36 -07002525 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002526}
2527
2528void Parcel::closeFileDescriptors()
2529{
2530 size_t i = mObjectsSize;
2531 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002532 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002533 }
2534 while (i > 0) {
2535 i--;
2536 const flat_binder_object* flat
2537 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002538 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002539 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002540 close(flat->handle);
2541 }
2542 }
2543}
2544
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002545uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002546{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002547 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002548}
2549
2550size_t Parcel::ipcDataSize() const
2551{
2552 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2553}
2554
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002555uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002556{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002557 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002558}
2559
2560size_t Parcel::ipcObjectsCount() const
2561{
2562 return mObjectsSize;
2563}
2564
2565void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002566 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002567{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002568 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002569 freeDataNoInit();
2570 mError = NO_ERROR;
2571 mData = const_cast<uint8_t*>(data);
2572 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002573 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002574 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002575 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002576 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002577 mObjectsSize = mObjectsCapacity = objectsCount;
2578 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002579 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002580 mOwner = relFunc;
2581 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002582 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002583 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002584 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002585 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002586 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002587 mObjectsSize = 0;
2588 break;
2589 }
2590 minOffset = offset + sizeof(flat_binder_object);
2591 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002592 scanForFds();
2593}
2594
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002595void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002596{
2597 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002598
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002599 if (errorCheck() != NO_ERROR) {
2600 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002601 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002602 } else if (dataSize() > 0) {
2603 const uint8_t* DATA = data();
2604 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002605 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002606 const size_t N = objectsCount();
2607 for (size_t i=0; i<N; i++) {
2608 const flat_binder_object* flat
2609 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2610 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002611 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002612 << " = " << flat->binder;
2613 }
2614 } else {
2615 to << "NULL";
2616 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002617
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002618 to << ")";
2619}
2620
2621void Parcel::releaseObjects()
2622{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002623 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002624 if (i == 0) {
2625 return;
2626 }
2627 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002628 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002629 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002630 while (i > 0) {
2631 i--;
2632 const flat_binder_object* flat
2633 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002634 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002635 }
2636}
2637
2638void Parcel::acquireObjects()
2639{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002640 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002641 if (i == 0) {
2642 return;
2643 }
2644 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002645 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002646 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002647 while (i > 0) {
2648 i--;
2649 const flat_binder_object* flat
2650 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002651 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002652 }
2653}
2654
2655void Parcel::freeData()
2656{
2657 freeDataNoInit();
2658 initState();
2659}
2660
2661void Parcel::freeDataNoInit()
2662{
2663 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002664 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002665 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002666 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2667 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002668 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002669 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002670 if (mData) {
2671 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002672 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002673 if (mDataCapacity <= gParcelGlobalAllocSize) {
2674 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2675 } else {
2676 gParcelGlobalAllocSize = 0;
2677 }
2678 if (gParcelGlobalAllocCount > 0) {
2679 gParcelGlobalAllocCount--;
2680 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002681 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002682 free(mData);
2683 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002684 if (mObjects) free(mObjects);
2685 }
2686}
2687
2688status_t Parcel::growData(size_t len)
2689{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002690 if (len > INT32_MAX) {
2691 // don't accept size_t values which may have come from an
2692 // inadvertent conversion from a negative int.
2693 return BAD_VALUE;
2694 }
2695
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002696 size_t newSize = ((mDataSize+len)*3)/2;
2697 return (newSize <= mDataSize)
2698 ? (status_t) NO_MEMORY
2699 : continueWrite(newSize);
2700}
2701
2702status_t Parcel::restartWrite(size_t desired)
2703{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002704 if (desired > INT32_MAX) {
2705 // don't accept size_t values which may have come from an
2706 // inadvertent conversion from a negative int.
2707 return BAD_VALUE;
2708 }
2709
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002710 if (mOwner) {
2711 freeData();
2712 return continueWrite(desired);
2713 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002714
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002715 uint8_t* data = (uint8_t*)realloc(mData, desired);
2716 if (!data && desired > mDataCapacity) {
2717 mError = NO_MEMORY;
2718 return NO_MEMORY;
2719 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002720
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002721 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002722
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002723 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002724 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002725 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002726 gParcelGlobalAllocSize += desired;
2727 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002728 if (!mData) {
2729 gParcelGlobalAllocCount++;
2730 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002731 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002732 mData = data;
2733 mDataCapacity = desired;
2734 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002735
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002736 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002737 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2738 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2739
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002740 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002741 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002742 mObjectsSize = mObjectsCapacity = 0;
2743 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002744 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002745 mHasFds = false;
2746 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002747 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002748
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002749 return NO_ERROR;
2750}
2751
2752status_t Parcel::continueWrite(size_t desired)
2753{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002754 if (desired > INT32_MAX) {
2755 // don't accept size_t values which may have come from an
2756 // inadvertent conversion from a negative int.
2757 return BAD_VALUE;
2758 }
2759
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002760 // If shrinking, first adjust for any objects that appear
2761 // after the new data size.
2762 size_t objectsSize = mObjectsSize;
2763 if (desired < mDataSize) {
2764 if (desired == 0) {
2765 objectsSize = 0;
2766 } else {
2767 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002768 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002769 break;
2770 objectsSize--;
2771 }
2772 }
2773 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002774
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002775 if (mOwner) {
2776 // If the size is going to zero, just release the owner's data.
2777 if (desired == 0) {
2778 freeData();
2779 return NO_ERROR;
2780 }
2781
2782 // If there is a different owner, we need to take
2783 // posession.
2784 uint8_t* data = (uint8_t*)malloc(desired);
2785 if (!data) {
2786 mError = NO_MEMORY;
2787 return NO_MEMORY;
2788 }
Yi Kong91635562018-06-07 14:38:36 -07002789 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002790
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002791 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002792 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002793 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002794 free(data);
2795
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002796 mError = NO_MEMORY;
2797 return NO_MEMORY;
2798 }
2799
2800 // Little hack to only acquire references on objects
2801 // we will be keeping.
2802 size_t oldObjectsSize = mObjectsSize;
2803 mObjectsSize = objectsSize;
2804 acquireObjects();
2805 mObjectsSize = oldObjectsSize;
2806 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002807
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002808 if (mData) {
2809 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2810 }
2811 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002812 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002813 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002814 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002815 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002816 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002817
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002818 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002819 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002820 gParcelGlobalAllocSize += desired;
2821 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002822 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002823
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002824 mData = data;
2825 mObjects = objects;
2826 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002827 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002828 mDataCapacity = desired;
2829 mObjectsSize = mObjectsCapacity = objectsSize;
2830 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002831 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002832
2833 } else if (mData) {
2834 if (objectsSize < mObjectsSize) {
2835 // Need to release refs on any objects we are dropping.
2836 const sp<ProcessState> proc(ProcessState::self());
2837 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2838 const flat_binder_object* flat
2839 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002840 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002841 // will need to rescan because we may have lopped off the only FDs
2842 mFdsKnown = false;
2843 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002844 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002845 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002846 binder_size_t* objects =
2847 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002848 if (objects) {
2849 mObjects = objects;
2850 }
2851 mObjectsSize = objectsSize;
2852 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002853 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002854 }
2855
2856 // We own the data, so we can just do a realloc().
2857 if (desired > mDataCapacity) {
2858 uint8_t* data = (uint8_t*)realloc(mData, desired);
2859 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002860 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2861 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002862 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002863 gParcelGlobalAllocSize += desired;
2864 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002865 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002866 mData = data;
2867 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002868 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002869 mError = NO_MEMORY;
2870 return NO_MEMORY;
2871 }
2872 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002873 if (mDataSize > desired) {
2874 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002875 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002876 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002877 if (mDataPos > desired) {
2878 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002879 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002880 }
2881 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002882
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002883 } else {
2884 // This is the first data. Easy!
2885 uint8_t* data = (uint8_t*)malloc(desired);
2886 if (!data) {
2887 mError = NO_MEMORY;
2888 return NO_MEMORY;
2889 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002890
Yi Kong91635562018-06-07 14:38:36 -07002891 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002892 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002893 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002894 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002895
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002896 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002897 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002898 gParcelGlobalAllocSize += desired;
2899 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002900 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002901
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002902 mData = data;
2903 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002904 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2905 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002906 mDataCapacity = desired;
2907 }
2908
2909 return NO_ERROR;
2910}
2911
2912void Parcel::initState()
2913{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002914 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002915 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002916 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002917 mDataSize = 0;
2918 mDataCapacity = 0;
2919 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002920 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2921 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002922 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002923 mObjectsSize = 0;
2924 mObjectsCapacity = 0;
2925 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002926 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002927 mHasFds = false;
2928 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002929 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002930 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002931 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002932 mWorkSourceRequestHeaderPosition = 0;
2933 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002934
2935 // racing multiple init leads only to multiple identical write
2936 if (gMaxFds == 0) {
2937 struct rlimit result;
2938 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2939 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002940 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002941 } else {
2942 ALOGW("Unable to getrlimit: %s", strerror(errno));
2943 gMaxFds = 1024;
2944 }
2945 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002946}
2947
2948void Parcel::scanForFds() const
2949{
2950 bool hasFds = false;
2951 for (size_t i=0; i<mObjectsSize; i++) {
2952 const flat_binder_object* flat
2953 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002954 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002955 hasFds = true;
2956 break;
2957 }
2958 }
2959 mHasFds = hasFds;
2960 mFdsKnown = true;
2961}
2962
Dan Sandleraa5c2342015-04-10 10:08:45 -04002963size_t Parcel::getBlobAshmemSize() const
2964{
Adrian Roos6bb31142015-10-22 16:46:12 -07002965 // This used to return the size of all blobs that were written to ashmem, now we're returning
2966 // the ashmem currently referenced by this Parcel, which should be equivalent.
2967 // TODO: Remove method once ABI can be changed.
2968 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002969}
2970
Adrian Rooscbf37262015-10-22 16:12:53 -07002971size_t Parcel::getOpenAshmemSize() const
2972{
2973 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002974}
2975
2976// --- Parcel::Blob ---
2977
2978Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002979 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002980}
2981
2982Parcel::Blob::~Blob() {
2983 release();
2984}
2985
2986void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002987 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002988 ::munmap(mData, mSize);
2989 }
2990 clear();
2991}
2992
Jeff Brown13b16042014-11-11 16:44:25 -08002993void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2994 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002995 mData = data;
2996 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002997 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002998}
2999
3000void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08003001 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07003002 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003003 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08003004 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003005}
3006
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003007}; // namespace android