blob: 49592a8a6d54b6cb99a03c5fe8ff68a26e04710f [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) {
184 *outAshmemSize -= size;
Adrian Roos6bb31142015-10-22 16:46:12 -0700185 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700186 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800187
188 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700189 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700190 return;
191 }
192 }
193
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700194 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700195}
196
Adrian Roos6bb31142015-10-22 16:46:12 -0700197void release_object(const sp<ProcessState>& proc,
198 const flat_binder_object& obj, const void* who)
199{
Yi Kong91635562018-06-07 14:38:36 -0700200 release_object(proc, obj, who, nullptr);
Adrian Roos6bb31142015-10-22 16:46:12 -0700201}
202
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700203inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800204 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205{
206 return out->writeObject(flat, false);
207}
208
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800209status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700210 const sp<IBinder>& binder, Parcel* out)
211{
212 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700213
Martijn Coenen2b631742017-05-05 11:16:59 -0700214 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
215 /* minimum priority for all nodes is nice 0 */
216 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
217 } else {
218 /* minimum priority for all nodes is MAX_NICE(19) */
219 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
220 }
221
Yi Kong91635562018-06-07 14:38:36 -0700222 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800223 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700224 if (!local) {
225 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700226 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000227 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700228 }
229 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700230 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800231 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700232 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800233 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700234 } else {
Steven Morelandf0212002018-12-26 13:59:23 -0800235 if (local->isRequestingSid()) {
236 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
237 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700238 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800239 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
240 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700241 }
242 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700243 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800244 obj.binder = 0;
245 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700246 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700247
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 return finish_flatten_binder(binder, obj, out);
249}
250
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800251status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700252 const wp<IBinder>& binder, Parcel* out)
253{
254 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700255
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700256 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Yi Kong91635562018-06-07 14:38:36 -0700257 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258 sp<IBinder> real = binder.promote();
Yi Kong91635562018-06-07 14:38:36 -0700259 if (real != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700260 IBinder *local = real->localBinder();
261 if (!local) {
262 BpBinder *proxy = real->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700263 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000264 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700265 }
266 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700267 obj.hdr.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800268 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700269 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800270 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700271 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700272 obj.hdr.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800273 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
274 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700275 }
276 return finish_flatten_binder(real, obj, out);
277 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700278
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700279 // XXX How to deal? In order to flatten the given binder,
280 // we need to probe it for information, which requires a primary
281 // reference... but we don't have one.
282 //
283 // The OpenBinder implementation uses a dynamic_cast<> here,
284 // but we can't do that with the different reference counting
285 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000286 ALOGE("Unable to unflatten Binder weak reference!");
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700287 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800288 obj.binder = 0;
289 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700290 return finish_flatten_binder(nullptr, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700291
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700292 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700293 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800294 obj.binder = 0;
295 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700296 return finish_flatten_binder(nullptr, obj, out);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700297 }
298}
299
300inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800301 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
302 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700303{
304 return NO_ERROR;
305}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700306
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700307status_t unflatten_binder(const sp<ProcessState>& proc,
308 const Parcel& in, sp<IBinder>* out)
309{
310 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700311
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700312 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700313 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700314 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800315 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700316 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700317 case BINDER_TYPE_HANDLE:
318 *out = proc->getStrongProxyForHandle(flat->handle);
319 return finish_unflatten_binder(
320 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700321 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700322 }
323 return BAD_TYPE;
324}
325
326status_t unflatten_binder(const sp<ProcessState>& proc,
327 const Parcel& in, wp<IBinder>* out)
328{
329 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700330
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700331 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700332 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700333 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800334 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700335 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700336 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800337 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700338 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800339 reinterpret_cast<IBinder*>(flat->cookie),
340 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700341 } else {
Yi Kong91635562018-06-07 14:38:36 -0700342 *out = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700343 }
Yi Kong91635562018-06-07 14:38:36 -0700344 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700345 case BINDER_TYPE_HANDLE:
346 case BINDER_TYPE_WEAK_HANDLE:
347 *out = proc->getWeakProxyForHandle(flat->handle);
348 return finish_unflatten_binder(
349 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
350 }
351 }
352 return BAD_TYPE;
353}
354
355// ---------------------------------------------------------------------------
356
357Parcel::Parcel()
358{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800359 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700360 initState();
361}
362
363Parcel::~Parcel()
364{
365 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800366 LOG_ALLOC("Parcel %p: destroyed", this);
367}
368
369size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800370 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
371 size_t size = gParcelGlobalAllocSize;
372 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
373 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800374}
375
376size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800377 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
378 size_t count = gParcelGlobalAllocCount;
379 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
380 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700381}
382
383const uint8_t* Parcel::data() const
384{
385 return mData;
386}
387
388size_t Parcel::dataSize() const
389{
390 return (mDataSize > mDataPos ? mDataSize : mDataPos);
391}
392
393size_t Parcel::dataAvail() const
394{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700395 size_t result = dataSize() - dataPosition();
396 if (result > INT32_MAX) {
397 abort();
398 }
399 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700400}
401
402size_t Parcel::dataPosition() const
403{
404 return mDataPos;
405}
406
407size_t Parcel::dataCapacity() const
408{
409 return mDataCapacity;
410}
411
412status_t Parcel::setDataSize(size_t size)
413{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700414 if (size > INT32_MAX) {
415 // don't accept size_t values which may have come from an
416 // inadvertent conversion from a negative int.
417 return BAD_VALUE;
418 }
419
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700420 status_t err;
421 err = continueWrite(size);
422 if (err == NO_ERROR) {
423 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700424 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700425 }
426 return err;
427}
428
429void Parcel::setDataPosition(size_t pos) const
430{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700431 if (pos > INT32_MAX) {
432 // don't accept size_t values which may have come from an
433 // inadvertent conversion from a negative int.
434 abort();
435 }
436
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437 mDataPos = pos;
438 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800439 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700440}
441
442status_t Parcel::setDataCapacity(size_t size)
443{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700444 if (size > INT32_MAX) {
445 // don't accept size_t values which may have come from an
446 // inadvertent conversion from a negative int.
447 return BAD_VALUE;
448 }
449
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700450 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700451 return NO_ERROR;
452}
453
454status_t Parcel::setData(const uint8_t* buffer, size_t len)
455{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700456 if (len > INT32_MAX) {
457 // don't accept size_t values which may have come from an
458 // inadvertent conversion from a negative int.
459 return BAD_VALUE;
460 }
461
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700462 status_t err = restartWrite(len);
463 if (err == NO_ERROR) {
464 memcpy(const_cast<uint8_t*>(data()), buffer, len);
465 mDataSize = len;
466 mFdsKnown = false;
467 }
468 return err;
469}
470
Andreas Huber51faf462011-04-13 10:21:56 -0700471status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700472{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700474 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800475 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700476 size_t size = parcel->mObjectsSize;
477 int startPos = mDataPos;
478 int firstIndex = -1, lastIndex = -2;
479
480 if (len == 0) {
481 return NO_ERROR;
482 }
483
Nick Kralevichb6b14232015-04-02 09:36:02 -0700484 if (len > INT32_MAX) {
485 // don't accept size_t values which may have come from an
486 // inadvertent conversion from a negative int.
487 return BAD_VALUE;
488 }
489
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700490 // range checks against the source parcel size
491 if ((offset > parcel->mDataSize)
492 || (len > parcel->mDataSize)
493 || (offset + len > parcel->mDataSize)) {
494 return BAD_VALUE;
495 }
496
497 // Count objects in range
498 for (int i = 0; i < (int) size; i++) {
499 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700500 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700501 if (firstIndex == -1) {
502 firstIndex = i;
503 }
504 lastIndex = i;
505 }
506 }
507 int numObjects = lastIndex - firstIndex + 1;
508
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700509 if ((mDataSize+len) > mDataCapacity) {
510 // grow data
511 err = growData(len);
512 if (err != NO_ERROR) {
513 return err;
514 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700515 }
516
517 // append data
518 memcpy(mData + mDataPos, data + offset, len);
519 mDataPos += len;
520 mDataSize += len;
521
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400522 err = NO_ERROR;
523
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700524 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200525 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700526 // grow objects
527 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700528 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700529 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800530 binder_size_t *objects =
531 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700532 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700533 return NO_MEMORY;
534 }
535 mObjects = objects;
536 mObjectsCapacity = newSize;
537 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700538
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700539 // append and acquire objects
540 int idx = mObjectsSize;
541 for (int i = firstIndex; i <= lastIndex; i++) {
542 size_t off = objects[i] - offset + startPos;
543 mObjects[idx++] = off;
544 mObjectsSize++;
545
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700546 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700547 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700548 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700549
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700550 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700551 // If this is a file descriptor, we need to dup it so the
552 // new Parcel now owns its own fd, and can declare that we
553 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800554 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800555 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700556 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400557 if (!mAllowFds) {
558 err = FDS_NOT_ALLOWED;
559 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700560 }
561 }
562 }
563
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400564 return err;
565}
566
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700567int Parcel::compareData(const Parcel& other) {
568 size_t size = dataSize();
569 if (size != other.dataSize()) {
570 return size < other.dataSize() ? -1 : 1;
571 }
572 return memcmp(data(), other.data(), size);
573}
574
Jeff Brown13b16042014-11-11 16:44:25 -0800575bool Parcel::allowFds() const
576{
577 return mAllowFds;
578}
579
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700580bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400581{
582 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700583 if (!allowFds) {
584 mAllowFds = false;
585 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400586 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700587}
588
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700589void Parcel::restoreAllowFds(bool lastValue)
590{
591 mAllowFds = lastValue;
592}
593
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700594bool Parcel::hasFileDescriptors() const
595{
596 if (!mFdsKnown) {
597 scanForFds();
598 }
599 return mHasFds;
600}
601
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000602void Parcel::updateWorkSourceRequestHeaderPosition() const {
603 // Only update the request headers once. We only want to point
604 // to the first headers read/written.
605 if (!mRequestHeaderPresent) {
606 mWorkSourceRequestHeaderPosition = dataPosition();
607 mRequestHeaderPresent = true;
608 }
609}
610
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700611// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700612status_t Parcel::writeInterfaceToken(const String16& interface)
613{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000614 const IPCThreadState* threadState = IPCThreadState::self();
615 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000616 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000617 writeInt32(threadState->shouldPropagateWorkSource() ?
618 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700619 // currently the interface identification token is just its name as a string
620 return writeString16(interface);
621}
622
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000623bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
624{
625 if (!mRequestHeaderPresent) {
626 return false;
627 }
628
629 const size_t initialPosition = dataPosition();
630 setDataPosition(mWorkSourceRequestHeaderPosition);
631 status_t err = writeInt32(uid);
632 setDataPosition(initialPosition);
633 return err == NO_ERROR;
634}
635
636uid_t Parcel::readCallingWorkSourceUid()
637{
638 if (!mRequestHeaderPresent) {
639 return IPCThreadState::kUnsetWorkSource;
640 }
641
642 const size_t initialPosition = dataPosition();
643 setDataPosition(mWorkSourceRequestHeaderPosition);
644 uid_t uid = readInt32();
645 setDataPosition(initialPosition);
646 return uid;
647}
648
Mathias Agopian83c04462009-05-22 19:00:22 -0700649bool Parcel::checkInterface(IBinder* binder) const
650{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700651 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700652}
653
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700654bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700655 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700656{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100657 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700658 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700659 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700660 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700661 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700662 if ((threadState->getLastTransactionBinderFlags() &
663 IBinder::FLAG_ONEWAY) != 0) {
664 // For one-way calls, the callee is running entirely
665 // disconnected from the caller, so disable StrictMode entirely.
666 // Not only does disk/network usage not impact the caller, but
667 // there's no way to commuicate back any violations anyway.
668 threadState->setStrictModePolicy(0);
669 } else {
670 threadState->setStrictModePolicy(strictPolicy);
671 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100672 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000673 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100674 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000675 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100676 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700677 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700678 if (str == interface) {
679 return true;
680 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700681 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700682 String8(interface).string(), String8(str).string());
683 return false;
684 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700685}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700686
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800687const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700688{
689 return mObjects;
690}
691
692size_t Parcel::objectsCount() const
693{
694 return mObjectsSize;
695}
696
697status_t Parcel::errorCheck() const
698{
699 return mError;
700}
701
702void Parcel::setError(status_t err)
703{
704 mError = err;
705}
706
707status_t Parcel::finishWrite(size_t len)
708{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700709 if (len > INT32_MAX) {
710 // don't accept size_t values which may have come from an
711 // inadvertent conversion from a negative int.
712 return BAD_VALUE;
713 }
714
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700715 //printf("Finish write of %d\n", len);
716 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700717 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700718 if (mDataPos > mDataSize) {
719 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700720 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700721 }
722 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
723 return NO_ERROR;
724}
725
726status_t Parcel::writeUnpadded(const void* data, size_t len)
727{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700728 if (len > INT32_MAX) {
729 // don't accept size_t values which may have come from an
730 // inadvertent conversion from a negative int.
731 return BAD_VALUE;
732 }
733
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700734 size_t end = mDataPos + len;
735 if (end < mDataPos) {
736 // integer overflow
737 return BAD_VALUE;
738 }
739
740 if (end <= mDataCapacity) {
741restart_write:
742 memcpy(mData+mDataPos, data, len);
743 return finishWrite(len);
744 }
745
746 status_t err = growData(len);
747 if (err == NO_ERROR) goto restart_write;
748 return err;
749}
750
751status_t Parcel::write(const void* data, size_t len)
752{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700753 if (len > INT32_MAX) {
754 // don't accept size_t values which may have come from an
755 // inadvertent conversion from a negative int.
756 return BAD_VALUE;
757 }
758
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700759 void* const d = writeInplace(len);
760 if (d) {
761 memcpy(d, data, len);
762 return NO_ERROR;
763 }
764 return mError;
765}
766
767void* Parcel::writeInplace(size_t len)
768{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700769 if (len > INT32_MAX) {
770 // don't accept size_t values which may have come from an
771 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700772 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700773 }
774
775 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700776
777 // sanity check for integer overflow
778 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700779 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700780 }
781
782 if ((mDataPos+padded) <= mDataCapacity) {
783restart_write:
784 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
785 uint8_t* const data = mData+mDataPos;
786
787 // Need to pad at end?
788 if (padded != len) {
789#if BYTE_ORDER == BIG_ENDIAN
790 static const uint32_t mask[4] = {
791 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
792 };
793#endif
794#if BYTE_ORDER == LITTLE_ENDIAN
795 static const uint32_t mask[4] = {
796 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
797 };
798#endif
799 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
800 // *reinterpret_cast<void**>(data+padded-4));
801 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
802 }
803
804 finishWrite(padded);
805 return data;
806 }
807
808 status_t err = growData(padded);
809 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700810 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700811}
812
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800813status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
814 const uint8_t* strData = (uint8_t*)str.data();
815 const size_t strLen= str.length();
816 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100817 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800818 return BAD_VALUE;
819 }
820
821 status_t err = writeInt32(utf16Len);
822 if (err) {
823 return err;
824 }
825
826 // Allocate enough bytes to hold our converted string and its terminating NULL.
827 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
828 if (!dst) {
829 return NO_MEMORY;
830 }
831
Sergio Girof4607432016-07-21 14:46:35 +0100832 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800833
834 return NO_ERROR;
835}
836
837status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
838 if (!str) {
839 return writeInt32(-1);
840 }
841 return writeUtf8AsUtf16(*str);
842}
843
Casey Dahlin185d3442016-02-09 11:08:35 -0800844namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800845
Casey Dahlin185d3442016-02-09 11:08:35 -0800846template<typename T>
847status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700848{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700849 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700850 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700851 status = BAD_VALUE;
852 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700853 }
854
Casey Dahlin185d3442016-02-09 11:08:35 -0800855 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700856 if (status != OK) {
857 return status;
858 }
859
Casey Dahlin185d3442016-02-09 11:08:35 -0800860 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700861 if (!data) {
862 status = BAD_VALUE;
863 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700864 }
865
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700866 memcpy(data, val.data(), val.size());
867 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700868}
869
Casey Dahlin185d3442016-02-09 11:08:35 -0800870template<typename T>
871status_t writeByteVectorInternalPtr(Parcel* parcel,
872 const std::unique_ptr<std::vector<T>>& val)
873{
874 if (!val) {
875 return parcel->writeInt32(-1);
876 }
877
878 return writeByteVectorInternal(parcel, *val);
879}
880
881} // namespace
882
883status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
884 return writeByteVectorInternal(this, val);
885}
886
887status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
888{
889 return writeByteVectorInternalPtr(this, val);
890}
891
892status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
893 return writeByteVectorInternal(this, val);
894}
895
896status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
897{
898 return writeByteVectorInternalPtr(this, val);
899}
900
Casey Dahlin451ff582015-10-19 18:12:18 -0700901status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
902{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800903 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700904}
905
Casey Dahlinb9872622015-11-25 15:09:45 -0800906status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
907{
908 return writeNullableTypedVector(val, &Parcel::writeInt32);
909}
910
Casey Dahlin451ff582015-10-19 18:12:18 -0700911status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
912{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800913 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700914}
915
Casey Dahlinb9872622015-11-25 15:09:45 -0800916status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
917{
918 return writeNullableTypedVector(val, &Parcel::writeInt64);
919}
920
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800921status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
922{
923 return writeTypedVector(val, &Parcel::writeUint64);
924}
925
926status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
927{
928 return writeNullableTypedVector(val, &Parcel::writeUint64);
929}
930
Casey Dahlin451ff582015-10-19 18:12:18 -0700931status_t Parcel::writeFloatVector(const std::vector<float>& val)
932{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800933 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700934}
935
Casey Dahlinb9872622015-11-25 15:09:45 -0800936status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
937{
938 return writeNullableTypedVector(val, &Parcel::writeFloat);
939}
940
Casey Dahlin451ff582015-10-19 18:12:18 -0700941status_t Parcel::writeDoubleVector(const std::vector<double>& val)
942{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800943 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700944}
945
Casey Dahlinb9872622015-11-25 15:09:45 -0800946status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
947{
948 return writeNullableTypedVector(val, &Parcel::writeDouble);
949}
950
Casey Dahlin451ff582015-10-19 18:12:18 -0700951status_t Parcel::writeBoolVector(const std::vector<bool>& val)
952{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800953 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700954}
955
Casey Dahlinb9872622015-11-25 15:09:45 -0800956status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
957{
958 return writeNullableTypedVector(val, &Parcel::writeBool);
959}
960
Casey Dahlin451ff582015-10-19 18:12:18 -0700961status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
962{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800963 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700964}
965
Casey Dahlinb9872622015-11-25 15:09:45 -0800966status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
967{
968 return writeNullableTypedVector(val, &Parcel::writeChar);
969}
970
Casey Dahlin451ff582015-10-19 18:12:18 -0700971status_t Parcel::writeString16Vector(const std::vector<String16>& val)
972{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800973 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700974}
975
Casey Dahlinb9872622015-11-25 15:09:45 -0800976status_t Parcel::writeString16Vector(
977 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
978{
979 return writeNullableTypedVector(val, &Parcel::writeString16);
980}
981
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800982status_t Parcel::writeUtf8VectorAsUtf16Vector(
983 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
984 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
985}
986
987status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
988 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
989}
990
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700991status_t Parcel::writeInt32(int32_t val)
992{
Andreas Huber84a6d042009-08-17 13:33:27 -0700993 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700994}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800995
996status_t Parcel::writeUint32(uint32_t val)
997{
998 return writeAligned(val);
999}
1000
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001001status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001002 if (len > INT32_MAX) {
1003 // don't accept size_t values which may have come from an
1004 // inadvertent conversion from a negative int.
1005 return BAD_VALUE;
1006 }
1007
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001008 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001009 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001010 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001011 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001012 if (ret == NO_ERROR) {
1013 ret = write(val, len * sizeof(*val));
1014 }
1015 return ret;
1016}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001017status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001018 if (len > INT32_MAX) {
1019 // don't accept size_t values which may have come from an
1020 // inadvertent conversion from a negative int.
1021 return BAD_VALUE;
1022 }
1023
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001024 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001025 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001026 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001027 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001028 if (ret == NO_ERROR) {
1029 ret = write(val, len * sizeof(*val));
1030 }
1031 return ret;
1032}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001033
Casey Dahlind6848f52015-10-15 15:44:59 -07001034status_t Parcel::writeBool(bool val)
1035{
1036 return writeInt32(int32_t(val));
1037}
1038
1039status_t Parcel::writeChar(char16_t val)
1040{
1041 return writeInt32(int32_t(val));
1042}
1043
1044status_t Parcel::writeByte(int8_t val)
1045{
1046 return writeInt32(int32_t(val));
1047}
1048
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001049status_t Parcel::writeInt64(int64_t val)
1050{
Andreas Huber84a6d042009-08-17 13:33:27 -07001051 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001052}
1053
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001054status_t Parcel::writeUint64(uint64_t val)
1055{
1056 return writeAligned(val);
1057}
1058
Serban Constantinescuf683e012013-11-05 16:53:55 +00001059status_t Parcel::writePointer(uintptr_t val)
1060{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001061 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001062}
1063
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001064status_t Parcel::writeFloat(float val)
1065{
Andreas Huber84a6d042009-08-17 13:33:27 -07001066 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001067}
1068
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001069#if defined(__mips__) && defined(__mips_hard_float)
1070
1071status_t Parcel::writeDouble(double val)
1072{
1073 union {
1074 double d;
1075 unsigned long long ll;
1076 } u;
1077 u.d = val;
1078 return writeAligned(u.ll);
1079}
1080
1081#else
1082
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001083status_t Parcel::writeDouble(double val)
1084{
Andreas Huber84a6d042009-08-17 13:33:27 -07001085 return writeAligned(val);
1086}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001087
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001088#endif
1089
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001090status_t Parcel::writeCString(const char* str)
1091{
1092 return write(str, strlen(str)+1);
1093}
1094
1095status_t Parcel::writeString8(const String8& str)
1096{
1097 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001098 // only write string if its length is more than zero characters,
1099 // as readString8 will only read if the length field is non-zero.
1100 // this is slightly different from how writeString16 works.
1101 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001102 err = write(str.string(), str.bytes()+1);
1103 }
1104 return err;
1105}
1106
Casey Dahlinb9872622015-11-25 15:09:45 -08001107status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1108{
1109 if (!str) {
1110 return writeInt32(-1);
1111 }
1112
1113 return writeString16(*str);
1114}
1115
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001116status_t Parcel::writeString16(const String16& str)
1117{
1118 return writeString16(str.string(), str.size());
1119}
1120
1121status_t Parcel::writeString16(const char16_t* str, size_t len)
1122{
Yi Kong91635562018-06-07 14:38:36 -07001123 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001124
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001125 status_t err = writeInt32(len);
1126 if (err == NO_ERROR) {
1127 len *= sizeof(char16_t);
1128 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1129 if (data) {
1130 memcpy(data, str, len);
1131 *reinterpret_cast<char16_t*>(data+len) = 0;
1132 return NO_ERROR;
1133 }
1134 err = mError;
1135 }
1136 return err;
1137}
1138
1139status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1140{
1141 return flatten_binder(ProcessState::self(), val, this);
1142}
1143
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001144status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1145{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001146 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001147}
1148
Casey Dahlinb9872622015-11-25 15:09:45 -08001149status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1150{
1151 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1152}
1153
1154status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001155 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001156}
1157
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001158status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001159 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001160}
1161
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001162status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1163{
1164 return flatten_binder(ProcessState::self(), val, this);
1165}
1166
Casey Dahlinb9872622015-11-25 15:09:45 -08001167status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1168 if (!parcelable) {
1169 return writeInt32(0);
1170 }
1171
1172 return writeParcelable(*parcelable);
1173}
1174
Christopher Wiley97f048d2015-11-19 06:49:05 -08001175status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1176 status_t status = writeInt32(1); // parcelable is not null.
1177 if (status != OK) {
1178 return status;
1179 }
1180 return parcelable.writeToParcel(this);
1181}
1182
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001183status_t Parcel::writeValue(const binder::Value& value) {
1184 return value.writeToParcel(this);
1185}
1186
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001187status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001188{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001189 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001190 return BAD_TYPE;
1191
1192 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001193 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001194 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001196 err = writeInt32(handle->numInts);
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 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1200 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201
1202 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001203 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204 return err;
1205 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001206 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001207 return err;
1208}
1209
Jeff Brown93ff1f92011-11-04 19:01:44 -07001210status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001211{
1212 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001213 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001214 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001215 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001216 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001217 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001218 return writeObject(obj, true);
1219}
1220
1221status_t Parcel::writeDupFileDescriptor(int fd)
1222{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001223 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001224 if (dupFd < 0) {
1225 return -errno;
1226 }
1227 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001228 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001229 close(dupFd);
1230 }
1231 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001232}
1233
Dianne Hackborn1941a402016-08-29 12:30:43 -07001234status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1235{
1236 writeInt32(0);
1237 return writeFileDescriptor(fd, takeOwnership);
1238}
1239
Ryo Hashimotobf551892018-05-31 16:58:35 +09001240status_t Parcel::writeDupParcelFileDescriptor(int fd)
1241{
1242 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1243 if (dupFd < 0) {
1244 return -errno;
1245 }
1246 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1247 if (err != OK) {
1248 close(dupFd);
1249 }
1250 return err;
1251}
1252
Christopher Wiley2cf19952016-04-11 11:09:37 -07001253status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001254 return writeDupFileDescriptor(fd.get());
1255}
1256
Christopher Wiley2cf19952016-04-11 11:09:37 -07001257status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001258 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1259}
1260
Christopher Wiley2cf19952016-04-11 11:09:37 -07001261status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001262 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1263}
1264
Jeff Brown13b16042014-11-11 16:44:25 -08001265status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001266{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001267 if (len > INT32_MAX) {
1268 // don't accept size_t values which may have come from an
1269 // inadvertent conversion from a negative int.
1270 return BAD_VALUE;
1271 }
1272
Jeff Brown13b16042014-11-11 16:44:25 -08001273 status_t status;
1274 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001275 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001276 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001277 if (status) return status;
1278
1279 void* ptr = writeInplace(len);
1280 if (!ptr) return NO_MEMORY;
1281
Jeff Brown13b16042014-11-11 16:44:25 -08001282 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001283 return NO_ERROR;
1284 }
1285
Steve Block6807e592011-10-20 11:56:00 +01001286 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001287 int fd = ashmem_create_region("Parcel Blob", len);
1288 if (fd < 0) return NO_MEMORY;
1289
1290 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1291 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001292 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001293 } else {
Yi Kong91635562018-06-07 14:38:36 -07001294 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001295 if (ptr == MAP_FAILED) {
1296 status = -errno;
1297 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001298 if (!mutableCopy) {
1299 result = ashmem_set_prot_region(fd, PROT_READ);
1300 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001301 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001302 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001303 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001304 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001305 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001306 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001307 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001308 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001309 return NO_ERROR;
1310 }
1311 }
1312 }
1313 }
1314 ::munmap(ptr, len);
1315 }
1316 ::close(fd);
1317 return status;
1318}
1319
Jeff Brown13b16042014-11-11 16:44:25 -08001320status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1321{
1322 // Must match up with what's done in writeBlob.
1323 if (!mAllowFds) return FDS_NOT_ALLOWED;
1324 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1325 if (status) return status;
1326 return writeDupFileDescriptor(fd);
1327}
1328
Mathias Agopiane1424282013-07-29 21:24:40 -07001329status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001330{
1331 status_t err;
1332
1333 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001334 const size_t len = val.getFlattenedSize();
1335 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001336
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001337 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001338 // don't accept size_t values which may have come from an
1339 // inadvertent conversion from a negative int.
1340 return BAD_VALUE;
1341 }
1342
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001343 err = this->writeInt32(len);
1344 if (err) return err;
1345
1346 err = this->writeInt32(fd_count);
1347 if (err) return err;
1348
1349 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001350 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001351 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001352 return BAD_VALUE;
1353
Yi Kong91635562018-06-07 14:38:36 -07001354 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001355 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001356 fds = new (std::nothrow) int[fd_count];
1357 if (fds == nullptr) {
1358 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1359 return BAD_VALUE;
1360 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001361 }
1362
1363 err = val.flatten(buf, len, fds, fd_count);
1364 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1365 err = this->writeDupFileDescriptor( fds[i] );
1366 }
1367
1368 if (fd_count) {
1369 delete [] fds;
1370 }
1371
1372 return err;
1373}
1374
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001375status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1376{
1377 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1378 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1379 if (enoughData && enoughObjects) {
1380restart_write:
1381 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001382
Christopher Tate98e67d32015-06-03 18:44:15 -07001383 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001384 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001385 if (!mAllowFds) {
1386 // fail before modifying our object index
1387 return FDS_NOT_ALLOWED;
1388 }
1389 mHasFds = mFdsKnown = true;
1390 }
1391
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001392 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001393 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001394 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001395 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001396 mObjectsSize++;
1397 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001398
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001399 return finishWrite(sizeof(flat_binder_object));
1400 }
1401
1402 if (!enoughData) {
1403 const status_t err = growData(sizeof(val));
1404 if (err != NO_ERROR) return err;
1405 }
1406 if (!enoughObjects) {
1407 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001408 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001409 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001410 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001411 mObjects = objects;
1412 mObjectsCapacity = newSize;
1413 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001414
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001415 goto restart_write;
1416}
1417
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001418status_t Parcel::writeNoException()
1419{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001420 binder::Status status;
1421 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001422}
1423
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001424status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1425{
1426 using ::std::map;
1427 using ::android::binder::Value;
1428 using ::android::binder::Map;
1429
1430 Map::const_iterator iter;
1431 status_t ret;
1432
1433 ret = writeInt32(map_in.size());
1434
1435 if (ret != NO_ERROR) {
1436 return ret;
1437 }
1438
1439 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1440 ret = writeValue(Value(iter->first));
1441 if (ret != NO_ERROR) {
1442 return ret;
1443 }
1444
1445 ret = writeValue(iter->second);
1446 if (ret != NO_ERROR) {
1447 return ret;
1448 }
1449 }
1450
1451 return ret;
1452}
1453
1454status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1455{
Yi Kong91635562018-06-07 14:38:36 -07001456 if (map == nullptr) {
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001457 return writeInt32(-1);
1458 }
1459
1460 return writeMap(*map.get());
1461}
1462
1463status_t Parcel::readMap(::android::binder::Map* map_out)const
1464{
1465 using ::std::map;
1466 using ::android::String16;
1467 using ::android::String8;
1468 using ::android::binder::Value;
1469 using ::android::binder::Map;
1470
1471 status_t ret = NO_ERROR;
1472 int32_t count;
1473
1474 ret = readInt32(&count);
1475 if (ret != NO_ERROR) {
1476 return ret;
1477 }
1478
1479 if (count < 0) {
1480 ALOGE("readMap: Unexpected count: %d", count);
1481 return (count == -1)
1482 ? UNEXPECTED_NULL
1483 : BAD_VALUE;
1484 }
1485
1486 map_out->clear();
1487
1488 while (count--) {
1489 Map::key_type key;
1490 Value value;
1491
1492 ret = readValue(&value);
1493 if (ret != NO_ERROR) {
1494 return ret;
1495 }
1496
1497 if (!value.getString(&key)) {
1498 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1499 return BAD_VALUE;
1500 }
1501
1502 ret = readValue(&value);
1503 if (ret != NO_ERROR) {
1504 return ret;
1505 }
1506
1507 (*map_out)[key] = value;
1508 }
1509
1510 return ret;
1511}
1512
1513status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1514{
1515 const size_t start = dataPosition();
1516 int32_t count;
1517 status_t status = readInt32(&count);
1518 map->reset();
1519
1520 if (status != OK || count == -1) {
1521 return status;
1522 }
1523
1524 setDataPosition(start);
1525 map->reset(new binder::Map());
1526
1527 status = readMap(map->get());
1528
1529 if (status != OK) {
1530 map->reset();
1531 }
1532
1533 return status;
1534}
1535
1536
1537
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001538void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001539{
1540 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1541}
1542
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001543status_t Parcel::validateReadData(size_t upperBound) const
1544{
1545 // Don't allow non-object reads on object data
1546 if (mObjectsSorted || mObjectsSize <= 1) {
1547data_sorted:
1548 // Expect to check only against the next object
1549 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1550 // For some reason the current read position is greater than the next object
1551 // hint. Iterate until we find the right object
1552 size_t nextObject = mNextObjectHint;
1553 do {
1554 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1555 // Requested info overlaps with an object
1556 ALOGE("Attempt to read from protected data in Parcel %p", this);
1557 return PERMISSION_DENIED;
1558 }
1559 nextObject++;
1560 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1561 mNextObjectHint = nextObject;
1562 }
1563 return NO_ERROR;
1564 }
1565 // Quickly determine if mObjects is sorted.
1566 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1567 binder_size_t* prevObj = currObj;
1568 while (currObj > mObjects) {
1569 prevObj--;
1570 if(*prevObj > *currObj) {
1571 goto data_unsorted;
1572 }
1573 currObj--;
1574 }
1575 mObjectsSorted = true;
1576 goto data_sorted;
1577
1578data_unsorted:
1579 // Insertion Sort mObjects
1580 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1581 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1582 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1583 binder_size_t temp = *iter0;
1584 binder_size_t* iter1 = iter0 - 1;
1585 while (iter1 >= mObjects && *iter1 > temp) {
1586 *(iter1 + 1) = *iter1;
1587 iter1--;
1588 }
1589 *(iter1 + 1) = temp;
1590 }
1591 mNextObjectHint = 0;
1592 mObjectsSorted = true;
1593 goto data_sorted;
1594}
1595
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001596status_t Parcel::read(void* outData, size_t len) const
1597{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001598 if (len > INT32_MAX) {
1599 // don't accept size_t values which may have come from an
1600 // inadvertent conversion from a negative int.
1601 return BAD_VALUE;
1602 }
1603
1604 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1605 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001606 if (mObjectsSize > 0) {
1607 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001608 if(err != NO_ERROR) {
1609 // Still increment the data position by the expected length
1610 mDataPos += pad_size(len);
1611 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1612 return err;
1613 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001614 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001615 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001616 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001617 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001618 return NO_ERROR;
1619 }
1620 return NOT_ENOUGH_DATA;
1621}
1622
1623const void* Parcel::readInplace(size_t len) const
1624{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001625 if (len > INT32_MAX) {
1626 // don't accept size_t values which may have come from an
1627 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001628 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001629 }
1630
1631 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1632 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001633 if (mObjectsSize > 0) {
1634 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001635 if(err != NO_ERROR) {
1636 // Still increment the data position by the expected length
1637 mDataPos += pad_size(len);
1638 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001639 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001640 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001641 }
1642
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001643 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001644 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001645 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001646 return data;
1647 }
Yi Kong91635562018-06-07 14:38:36 -07001648 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001649}
1650
Andreas Huber84a6d042009-08-17 13:33:27 -07001651template<class T>
1652status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001653 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001654
1655 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001656 if (mObjectsSize > 0) {
1657 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001658 if(err != NO_ERROR) {
1659 // Still increment the data position by the expected length
1660 mDataPos += sizeof(T);
1661 return err;
1662 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001663 }
1664
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001665 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001666 mDataPos += sizeof(T);
1667 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001668 return NO_ERROR;
1669 } else {
1670 return NOT_ENOUGH_DATA;
1671 }
1672}
1673
Andreas Huber84a6d042009-08-17 13:33:27 -07001674template<class T>
1675T Parcel::readAligned() const {
1676 T result;
1677 if (readAligned(&result) != NO_ERROR) {
1678 result = 0;
1679 }
1680
1681 return result;
1682}
1683
1684template<class T>
1685status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001686 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001687
1688 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1689restart_write:
1690 *reinterpret_cast<T*>(mData+mDataPos) = val;
1691 return finishWrite(sizeof(val));
1692 }
1693
1694 status_t err = growData(sizeof(val));
1695 if (err == NO_ERROR) goto restart_write;
1696 return err;
1697}
1698
Casey Dahlin185d3442016-02-09 11:08:35 -08001699namespace {
1700
1701template<typename T>
1702status_t readByteVectorInternal(const Parcel* parcel,
1703 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001704 val->clear();
1705
1706 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001707 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001708
1709 if (status != OK) {
1710 return status;
1711 }
1712
Christopher Wiley4db672d2015-11-10 09:44:30 -08001713 if (size < 0) {
1714 status = UNEXPECTED_NULL;
1715 return status;
1716 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001717 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001718 status = BAD_VALUE;
1719 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001720 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001721
Paul Lietar433e87b2016-09-16 10:39:32 -07001722 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001723 if (!data) {
1724 status = BAD_VALUE;
1725 return status;
1726 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001727 val->reserve(size);
1728 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001729
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001730 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001731}
1732
Casey Dahlin185d3442016-02-09 11:08:35 -08001733template<typename T>
1734status_t readByteVectorInternalPtr(
1735 const Parcel* parcel,
1736 std::unique_ptr<std::vector<T>>* val) {
1737 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001738 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001739 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001740 val->reset();
1741
1742 if (status != OK || size < 0) {
1743 return status;
1744 }
1745
Casey Dahlin185d3442016-02-09 11:08:35 -08001746 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001747 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001748
Casey Dahlin185d3442016-02-09 11:08:35 -08001749 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001750
1751 if (status != OK) {
1752 val->reset();
1753 }
1754
1755 return status;
1756}
1757
Casey Dahlin185d3442016-02-09 11:08:35 -08001758} // namespace
1759
1760status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1761 return readByteVectorInternal(this, val);
1762}
1763
1764status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1765 return readByteVectorInternal(this, val);
1766}
1767
1768status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1769 return readByteVectorInternalPtr(this, val);
1770}
1771
1772status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1773 return readByteVectorInternalPtr(this, val);
1774}
1775
Casey Dahlinb9872622015-11-25 15:09:45 -08001776status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1777 return readNullableTypedVector(val, &Parcel::readInt32);
1778}
1779
Casey Dahlin451ff582015-10-19 18:12:18 -07001780status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001781 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001782}
1783
Casey Dahlinb9872622015-11-25 15:09:45 -08001784status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1785 return readNullableTypedVector(val, &Parcel::readInt64);
1786}
1787
Casey Dahlin451ff582015-10-19 18:12:18 -07001788status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001789 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001790}
1791
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001792status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1793 return readNullableTypedVector(val, &Parcel::readUint64);
1794}
1795
1796status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1797 return readTypedVector(val, &Parcel::readUint64);
1798}
1799
Casey Dahlinb9872622015-11-25 15:09:45 -08001800status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1801 return readNullableTypedVector(val, &Parcel::readFloat);
1802}
1803
Casey Dahlin451ff582015-10-19 18:12:18 -07001804status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001805 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001806}
1807
Casey Dahlinb9872622015-11-25 15:09:45 -08001808status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1809 return readNullableTypedVector(val, &Parcel::readDouble);
1810}
1811
Casey Dahlin451ff582015-10-19 18:12:18 -07001812status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001813 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001814}
1815
Casey Dahlinb9872622015-11-25 15:09:45 -08001816status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1817 const int32_t start = dataPosition();
1818 int32_t size;
1819 status_t status = readInt32(&size);
1820 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001821
Casey Dahlinb9872622015-11-25 15:09:45 -08001822 if (status != OK || size < 0) {
1823 return status;
1824 }
1825
1826 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001827 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001828
1829 status = readBoolVector(val->get());
1830
1831 if (status != OK) {
1832 val->reset();
1833 }
1834
1835 return status;
1836}
1837
1838status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001839 int32_t size;
1840 status_t status = readInt32(&size);
1841
1842 if (status != OK) {
1843 return status;
1844 }
1845
1846 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001847 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001848 }
1849
1850 val->resize(size);
1851
1852 /* C++ bool handling means a vector of bools isn't necessarily addressable
1853 * (we might use individual bits)
1854 */
Christopher Wiley97887982015-10-27 16:33:47 -07001855 bool data;
1856 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001857 status = readBool(&data);
1858 (*val)[i] = data;
1859
1860 if (status != OK) {
1861 return status;
1862 }
1863 }
1864
1865 return OK;
1866}
1867
Casey Dahlinb9872622015-11-25 15:09:45 -08001868status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1869 return readNullableTypedVector(val, &Parcel::readChar);
1870}
1871
Casey Dahlin451ff582015-10-19 18:12:18 -07001872status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001873 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001874}
1875
Casey Dahlinb9872622015-11-25 15:09:45 -08001876status_t Parcel::readString16Vector(
1877 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1878 return readNullableTypedVector(val, &Parcel::readString16);
1879}
1880
Casey Dahlin451ff582015-10-19 18:12:18 -07001881status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001882 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001883}
1884
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001885status_t Parcel::readUtf8VectorFromUtf16Vector(
1886 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1887 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1888}
1889
1890status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1891 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1892}
Casey Dahlin451ff582015-10-19 18:12:18 -07001893
Andreas Huber84a6d042009-08-17 13:33:27 -07001894status_t Parcel::readInt32(int32_t *pArg) const
1895{
1896 return readAligned(pArg);
1897}
1898
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001899int32_t Parcel::readInt32() const
1900{
Andreas Huber84a6d042009-08-17 13:33:27 -07001901 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001902}
1903
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001904status_t Parcel::readUint32(uint32_t *pArg) const
1905{
1906 return readAligned(pArg);
1907}
1908
1909uint32_t Parcel::readUint32() const
1910{
1911 return readAligned<uint32_t>();
1912}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001913
1914status_t Parcel::readInt64(int64_t *pArg) const
1915{
Andreas Huber84a6d042009-08-17 13:33:27 -07001916 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001917}
1918
1919
1920int64_t Parcel::readInt64() const
1921{
Andreas Huber84a6d042009-08-17 13:33:27 -07001922 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001923}
1924
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001925status_t Parcel::readUint64(uint64_t *pArg) const
1926{
1927 return readAligned(pArg);
1928}
1929
1930uint64_t Parcel::readUint64() const
1931{
1932 return readAligned<uint64_t>();
1933}
1934
Serban Constantinescuf683e012013-11-05 16:53:55 +00001935status_t Parcel::readPointer(uintptr_t *pArg) const
1936{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001937 status_t ret;
1938 binder_uintptr_t ptr;
1939 ret = readAligned(&ptr);
1940 if (!ret)
1941 *pArg = ptr;
1942 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001943}
1944
1945uintptr_t Parcel::readPointer() const
1946{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001947 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001948}
1949
1950
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001951status_t Parcel::readFloat(float *pArg) const
1952{
Andreas Huber84a6d042009-08-17 13:33:27 -07001953 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001954}
1955
1956
1957float Parcel::readFloat() const
1958{
Andreas Huber84a6d042009-08-17 13:33:27 -07001959 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001960}
1961
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001962#if defined(__mips__) && defined(__mips_hard_float)
1963
1964status_t Parcel::readDouble(double *pArg) const
1965{
1966 union {
1967 double d;
1968 unsigned long long ll;
1969 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001970 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001971 status_t status;
1972 status = readAligned(&u.ll);
1973 *pArg = u.d;
1974 return status;
1975}
1976
1977double Parcel::readDouble() const
1978{
1979 union {
1980 double d;
1981 unsigned long long ll;
1982 } u;
1983 u.ll = readAligned<unsigned long long>();
1984 return u.d;
1985}
1986
1987#else
1988
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001989status_t Parcel::readDouble(double *pArg) const
1990{
Andreas Huber84a6d042009-08-17 13:33:27 -07001991 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001992}
1993
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001994double Parcel::readDouble() const
1995{
Andreas Huber84a6d042009-08-17 13:33:27 -07001996 return readAligned<double>();
1997}
1998
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001999#endif
2000
Andreas Huber84a6d042009-08-17 13:33:27 -07002001status_t Parcel::readIntPtr(intptr_t *pArg) const
2002{
2003 return readAligned(pArg);
2004}
2005
2006
2007intptr_t Parcel::readIntPtr() const
2008{
2009 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002010}
2011
Casey Dahlind6848f52015-10-15 15:44:59 -07002012status_t Parcel::readBool(bool *pArg) const
2013{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002014 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002015 status_t ret = readInt32(&tmp);
2016 *pArg = (tmp != 0);
2017 return ret;
2018}
2019
2020bool Parcel::readBool() const
2021{
2022 return readInt32() != 0;
2023}
2024
2025status_t Parcel::readChar(char16_t *pArg) const
2026{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002027 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002028 status_t ret = readInt32(&tmp);
2029 *pArg = char16_t(tmp);
2030 return ret;
2031}
2032
2033char16_t Parcel::readChar() const
2034{
2035 return char16_t(readInt32());
2036}
2037
2038status_t Parcel::readByte(int8_t *pArg) const
2039{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002040 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002041 status_t ret = readInt32(&tmp);
2042 *pArg = int8_t(tmp);
2043 return ret;
2044}
2045
2046int8_t Parcel::readByte() const
2047{
2048 return int8_t(readInt32());
2049}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002050
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002051status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2052 size_t utf16Size = 0;
2053 const char16_t* src = readString16Inplace(&utf16Size);
2054 if (!src) {
2055 return UNEXPECTED_NULL;
2056 }
2057
2058 // Save ourselves the trouble, we're done.
2059 if (utf16Size == 0u) {
2060 str->clear();
2061 return NO_ERROR;
2062 }
2063
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002064 // Allow for closing '\0'
2065 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2066 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002067 return BAD_VALUE;
2068 }
2069 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002070 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002071 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002072 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2073 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002074 return NO_ERROR;
2075}
2076
2077status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2078 const int32_t start = dataPosition();
2079 int32_t size;
2080 status_t status = readInt32(&size);
2081 str->reset();
2082
2083 if (status != OK || size < 0) {
2084 return status;
2085 }
2086
2087 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002088 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002089 return readUtf8FromUtf16(str->get());
2090}
2091
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002092const char* Parcel::readCString() const
2093{
2094 const size_t avail = mDataSize-mDataPos;
2095 if (avail > 0) {
2096 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2097 // is the string's trailing NUL within the parcel's valid bounds?
2098 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2099 if (eos) {
2100 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002101 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002102 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002103 return str;
2104 }
2105 }
Yi Kong91635562018-06-07 14:38:36 -07002106 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002107}
2108
2109String8 Parcel::readString8() const
2110{
Roshan Pius87b64d22016-07-18 12:51:02 -07002111 String8 retString;
2112 status_t status = readString8(&retString);
2113 if (status != OK) {
2114 // We don't care about errors here, so just return an empty string.
2115 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002116 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002117 return retString;
2118}
2119
2120status_t Parcel::readString8(String8* pArg) const
2121{
2122 int32_t size;
2123 status_t status = readInt32(&size);
2124 if (status != OK) {
2125 return status;
2126 }
2127 // watch for potential int overflow from size+1
2128 if (size < 0 || size >= INT32_MAX) {
2129 return BAD_VALUE;
2130 }
2131 // |writeString8| writes nothing for empty string.
2132 if (size == 0) {
2133 *pArg = String8();
2134 return OK;
2135 }
2136 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07002137 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002138 return BAD_VALUE;
2139 }
2140 pArg->setTo(str, size);
2141 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002142}
2143
2144String16 Parcel::readString16() const
2145{
2146 size_t len;
2147 const char16_t* str = readString16Inplace(&len);
2148 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002149 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002150 return String16();
2151}
2152
Casey Dahlinb9872622015-11-25 15:09:45 -08002153status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2154{
2155 const int32_t start = dataPosition();
2156 int32_t size;
2157 status_t status = readInt32(&size);
2158 pArg->reset();
2159
2160 if (status != OK || size < 0) {
2161 return status;
2162 }
2163
2164 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002165 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002166
2167 status = readString16(pArg->get());
2168
2169 if (status != OK) {
2170 pArg->reset();
2171 }
2172
2173 return status;
2174}
2175
Casey Dahlin451ff582015-10-19 18:12:18 -07002176status_t Parcel::readString16(String16* pArg) const
2177{
2178 size_t len;
2179 const char16_t* str = readString16Inplace(&len);
2180 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002181 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002182 return 0;
2183 } else {
2184 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002185 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002186 }
2187}
2188
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002189const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2190{
2191 int32_t size = readInt32();
2192 // watch for potential int overflow from size+1
2193 if (size >= 0 && size < INT32_MAX) {
2194 *outLen = size;
2195 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002196 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002197 return str;
2198 }
2199 }
2200 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002201 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002202}
2203
Casey Dahlinf0c13772015-10-27 18:33:56 -07002204status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2205{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002206 status_t status = readNullableStrongBinder(val);
2207 if (status == OK && !val->get()) {
2208 status = UNEXPECTED_NULL;
2209 }
2210 return status;
2211}
2212
2213status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2214{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002215 return unflatten_binder(ProcessState::self(), *this, val);
2216}
2217
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002218sp<IBinder> Parcel::readStrongBinder() const
2219{
2220 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002221 // Note that a lot of code in Android reads binders by hand with this
2222 // method, and that code has historically been ok with getting nullptr
2223 // back (while ignoring error codes).
2224 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002225 return val;
2226}
2227
2228wp<IBinder> Parcel::readWeakBinder() const
2229{
2230 wp<IBinder> val;
2231 unflatten_binder(ProcessState::self(), *this, &val);
2232 return val;
2233}
2234
Christopher Wiley97f048d2015-11-19 06:49:05 -08002235status_t Parcel::readParcelable(Parcelable* parcelable) const {
2236 int32_t have_parcelable = 0;
2237 status_t status = readInt32(&have_parcelable);
2238 if (status != OK) {
2239 return status;
2240 }
2241 if (!have_parcelable) {
2242 return UNEXPECTED_NULL;
2243 }
2244 return parcelable->readFromParcel(this);
2245}
2246
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002247status_t Parcel::readValue(binder::Value* value) const {
2248 return value->readFromParcel(this);
2249}
2250
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002251int32_t Parcel::readExceptionCode() const
2252{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002253 binder::Status status;
2254 status.readFromParcel(*this);
2255 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002256}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002257
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002258native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002259{
2260 int numFds, numInts;
2261 status_t err;
2262 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002263 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002264 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002265 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002266
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002267 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002268 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002269 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002270 }
2271
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002272 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002273 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002274 if (h->data[i] < 0) {
2275 for (int j = 0; j < i; j++) {
2276 close(h->data[j]);
2277 }
2278 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002279 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002280 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002281 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002282 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002283 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002284 native_handle_close(h);
2285 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002286 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002287 }
2288 return h;
2289}
2290
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002291int Parcel::readFileDescriptor() const
2292{
2293 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002294
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002295 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002296 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002297 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002298
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002299 return BAD_TYPE;
2300}
2301
Dianne Hackborn1941a402016-08-29 12:30:43 -07002302int Parcel::readParcelFileDescriptor() const
2303{
2304 int32_t hasComm = readInt32();
2305 int fd = readFileDescriptor();
2306 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002307 // detach (owned by the binder driver)
2308 int comm = readFileDescriptor();
2309
2310 // warning: this must be kept in sync with:
2311 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2312 enum ParcelFileDescriptorStatus {
2313 DETACHED = 2,
2314 };
2315
2316#if BYTE_ORDER == BIG_ENDIAN
2317 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2318#endif
2319#if BYTE_ORDER == LITTLE_ENDIAN
2320 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2321#endif
2322
2323 ssize_t written = TEMP_FAILURE_RETRY(
2324 ::write(comm, &message, sizeof(message)));
2325
2326 if (written == -1 || written != sizeof(message)) {
2327 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2328 written, strerror(errno));
2329 return BAD_TYPE;
2330 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002331 }
2332 return fd;
2333}
2334
Christopher Wiley2cf19952016-04-11 11:09:37 -07002335status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002336{
2337 int got = readFileDescriptor();
2338
2339 if (got == BAD_TYPE) {
2340 return BAD_TYPE;
2341 }
2342
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002343 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002344
2345 if (val->get() < 0) {
2346 return BAD_VALUE;
2347 }
2348
2349 return OK;
2350}
2351
Ryo Hashimotobf551892018-05-31 16:58:35 +09002352status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2353{
2354 int got = readParcelFileDescriptor();
2355
2356 if (got == BAD_TYPE) {
2357 return BAD_TYPE;
2358 }
2359
2360 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2361
2362 if (val->get() < 0) {
2363 return BAD_VALUE;
2364 }
2365
2366 return OK;
2367}
Casey Dahlin06673e32015-11-23 13:24:23 -08002368
Christopher Wiley2cf19952016-04-11 11:09:37 -07002369status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002370 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2371}
2372
Christopher Wiley2cf19952016-04-11 11:09:37 -07002373status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002374 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2375}
2376
Jeff Brown5707dbf2011-09-23 21:17:56 -07002377status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2378{
Jeff Brown13b16042014-11-11 16:44:25 -08002379 int32_t blobType;
2380 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002381 if (status) return status;
2382
Jeff Brown13b16042014-11-11 16:44:25 -08002383 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002384 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002385 const void* ptr = readInplace(len);
2386 if (!ptr) return BAD_VALUE;
2387
Jeff Brown13b16042014-11-11 16:44:25 -08002388 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002389 return NO_ERROR;
2390 }
2391
Steve Block6807e592011-10-20 11:56:00 +01002392 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002393 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002394 int fd = readFileDescriptor();
2395 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2396
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002397 if (!ashmem_valid(fd)) {
2398 ALOGE("invalid fd");
2399 return BAD_VALUE;
2400 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002401 int size = ashmem_get_size_region(fd);
2402 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002403 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002404 return BAD_VALUE;
2405 }
Yi Kong91635562018-06-07 14:38:36 -07002406 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002407 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002408 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002409
Jeff Brown13b16042014-11-11 16:44:25 -08002410 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002411 return NO_ERROR;
2412}
2413
Mathias Agopiane1424282013-07-29 21:24:40 -07002414status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002415{
2416 // size
2417 const size_t len = this->readInt32();
2418 const size_t fd_count = this->readInt32();
2419
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002420 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002421 // don't accept size_t values which may have come from an
2422 // inadvertent conversion from a negative int.
2423 return BAD_VALUE;
2424 }
2425
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002426 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002427 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002428 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002429 return BAD_VALUE;
2430
Yi Kong91635562018-06-07 14:38:36 -07002431 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002432 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002433 fds = new (std::nothrow) int[fd_count];
2434 if (fds == nullptr) {
2435 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2436 return BAD_VALUE;
2437 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002438 }
2439
2440 status_t err = NO_ERROR;
2441 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002442 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002443 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002444 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002445 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 -07002446 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2447 // Close all the file descriptors that were dup-ed.
2448 for (size_t j=0; j<i ;j++) {
2449 close(fds[j]);
2450 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002451 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002452 }
2453
2454 if (err == NO_ERROR) {
2455 err = val.unflatten(buf, len, fds, fd_count);
2456 }
2457
2458 if (fd_count) {
2459 delete [] fds;
2460 }
2461
2462 return err;
2463}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002464const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2465{
2466 const size_t DPOS = mDataPos;
2467 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2468 const flat_binder_object* obj
2469 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2470 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002471 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002472 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002473 // the object list, so we don't want to check for it when
2474 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002475 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002476 return obj;
2477 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002478
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002480 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002481 const size_t N = mObjectsSize;
2482 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002483
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002484 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002485 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002486 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002487
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002488 // Start at the current hint position, looking for an object at
2489 // the current data position.
2490 if (opos < N) {
2491 while (opos < (N-1) && OBJS[opos] < DPOS) {
2492 opos++;
2493 }
2494 } else {
2495 opos = N-1;
2496 }
2497 if (OBJS[opos] == DPOS) {
2498 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002499 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002500 this, DPOS, opos);
2501 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002502 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002503 return obj;
2504 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002505
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002506 // Look backwards for it...
2507 while (opos > 0 && OBJS[opos] > DPOS) {
2508 opos--;
2509 }
2510 if (OBJS[opos] == DPOS) {
2511 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002512 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513 this, DPOS, opos);
2514 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002515 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516 return obj;
2517 }
2518 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002519 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 -07002520 this, DPOS);
2521 }
Yi Kong91635562018-06-07 14:38:36 -07002522 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002523}
2524
2525void Parcel::closeFileDescriptors()
2526{
2527 size_t i = mObjectsSize;
2528 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002529 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002530 }
2531 while (i > 0) {
2532 i--;
2533 const flat_binder_object* flat
2534 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002535 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002536 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002537 close(flat->handle);
2538 }
2539 }
2540}
2541
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002542uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002543{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002544 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002545}
2546
2547size_t Parcel::ipcDataSize() const
2548{
2549 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2550}
2551
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002552uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002553{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002554 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002555}
2556
2557size_t Parcel::ipcObjectsCount() const
2558{
2559 return mObjectsSize;
2560}
2561
2562void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002563 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002564{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002565 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002566 freeDataNoInit();
2567 mError = NO_ERROR;
2568 mData = const_cast<uint8_t*>(data);
2569 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002570 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002571 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002572 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002573 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002574 mObjectsSize = mObjectsCapacity = objectsCount;
2575 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002576 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002577 mOwner = relFunc;
2578 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002579 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002580 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002581 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002582 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002583 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002584 mObjectsSize = 0;
2585 break;
2586 }
2587 minOffset = offset + sizeof(flat_binder_object);
2588 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002589 scanForFds();
2590}
2591
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002592void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002593{
2594 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002595
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002596 if (errorCheck() != NO_ERROR) {
2597 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002598 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002599 } else if (dataSize() > 0) {
2600 const uint8_t* DATA = data();
2601 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002602 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002603 const size_t N = objectsCount();
2604 for (size_t i=0; i<N; i++) {
2605 const flat_binder_object* flat
2606 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2607 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002608 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002609 << " = " << flat->binder;
2610 }
2611 } else {
2612 to << "NULL";
2613 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002614
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002615 to << ")";
2616}
2617
2618void Parcel::releaseObjects()
2619{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002620 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002621 if (i == 0) {
2622 return;
2623 }
2624 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002625 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002626 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002627 while (i > 0) {
2628 i--;
2629 const flat_binder_object* flat
2630 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002631 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002632 }
2633}
2634
2635void Parcel::acquireObjects()
2636{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002637 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002638 if (i == 0) {
2639 return;
2640 }
2641 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002642 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002643 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002644 while (i > 0) {
2645 i--;
2646 const flat_binder_object* flat
2647 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002648 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002649 }
2650}
2651
2652void Parcel::freeData()
2653{
2654 freeDataNoInit();
2655 initState();
2656}
2657
2658void Parcel::freeDataNoInit()
2659{
2660 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002661 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002662 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002663 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2664 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002665 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002666 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002667 if (mData) {
2668 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002669 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002670 if (mDataCapacity <= gParcelGlobalAllocSize) {
2671 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2672 } else {
2673 gParcelGlobalAllocSize = 0;
2674 }
2675 if (gParcelGlobalAllocCount > 0) {
2676 gParcelGlobalAllocCount--;
2677 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002678 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002679 free(mData);
2680 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002681 if (mObjects) free(mObjects);
2682 }
2683}
2684
2685status_t Parcel::growData(size_t len)
2686{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002687 if (len > INT32_MAX) {
2688 // don't accept size_t values which may have come from an
2689 // inadvertent conversion from a negative int.
2690 return BAD_VALUE;
2691 }
2692
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002693 size_t newSize = ((mDataSize+len)*3)/2;
2694 return (newSize <= mDataSize)
2695 ? (status_t) NO_MEMORY
2696 : continueWrite(newSize);
2697}
2698
2699status_t Parcel::restartWrite(size_t desired)
2700{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002701 if (desired > INT32_MAX) {
2702 // don't accept size_t values which may have come from an
2703 // inadvertent conversion from a negative int.
2704 return BAD_VALUE;
2705 }
2706
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002707 if (mOwner) {
2708 freeData();
2709 return continueWrite(desired);
2710 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002711
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002712 uint8_t* data = (uint8_t*)realloc(mData, desired);
2713 if (!data && desired > mDataCapacity) {
2714 mError = NO_MEMORY;
2715 return NO_MEMORY;
2716 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002717
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002718 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002719
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002720 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002721 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002722 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002723 gParcelGlobalAllocSize += desired;
2724 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002725 if (!mData) {
2726 gParcelGlobalAllocCount++;
2727 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002728 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002729 mData = data;
2730 mDataCapacity = desired;
2731 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002732
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002733 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002734 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2735 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2736
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002737 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002738 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002739 mObjectsSize = mObjectsCapacity = 0;
2740 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002741 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002742 mHasFds = false;
2743 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002744 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002745
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002746 return NO_ERROR;
2747}
2748
2749status_t Parcel::continueWrite(size_t desired)
2750{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002751 if (desired > INT32_MAX) {
2752 // don't accept size_t values which may have come from an
2753 // inadvertent conversion from a negative int.
2754 return BAD_VALUE;
2755 }
2756
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002757 // If shrinking, first adjust for any objects that appear
2758 // after the new data size.
2759 size_t objectsSize = mObjectsSize;
2760 if (desired < mDataSize) {
2761 if (desired == 0) {
2762 objectsSize = 0;
2763 } else {
2764 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002765 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002766 break;
2767 objectsSize--;
2768 }
2769 }
2770 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002771
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002772 if (mOwner) {
2773 // If the size is going to zero, just release the owner's data.
2774 if (desired == 0) {
2775 freeData();
2776 return NO_ERROR;
2777 }
2778
2779 // If there is a different owner, we need to take
2780 // posession.
2781 uint8_t* data = (uint8_t*)malloc(desired);
2782 if (!data) {
2783 mError = NO_MEMORY;
2784 return NO_MEMORY;
2785 }
Yi Kong91635562018-06-07 14:38:36 -07002786 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002787
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002788 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002789 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002790 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002791 free(data);
2792
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002793 mError = NO_MEMORY;
2794 return NO_MEMORY;
2795 }
2796
2797 // Little hack to only acquire references on objects
2798 // we will be keeping.
2799 size_t oldObjectsSize = mObjectsSize;
2800 mObjectsSize = objectsSize;
2801 acquireObjects();
2802 mObjectsSize = oldObjectsSize;
2803 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002804
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002805 if (mData) {
2806 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2807 }
2808 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002809 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002810 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002811 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002812 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002813 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002814
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002815 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002816 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002817 gParcelGlobalAllocSize += desired;
2818 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002819 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002820
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002821 mData = data;
2822 mObjects = objects;
2823 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002824 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002825 mDataCapacity = desired;
2826 mObjectsSize = mObjectsCapacity = objectsSize;
2827 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002828 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002829
2830 } else if (mData) {
2831 if (objectsSize < mObjectsSize) {
2832 // Need to release refs on any objects we are dropping.
2833 const sp<ProcessState> proc(ProcessState::self());
2834 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2835 const flat_binder_object* flat
2836 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002837 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002838 // will need to rescan because we may have lopped off the only FDs
2839 mFdsKnown = false;
2840 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002841 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002842 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002843 binder_size_t* objects =
2844 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002845 if (objects) {
2846 mObjects = objects;
2847 }
2848 mObjectsSize = objectsSize;
2849 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002850 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002851 }
2852
2853 // We own the data, so we can just do a realloc().
2854 if (desired > mDataCapacity) {
2855 uint8_t* data = (uint8_t*)realloc(mData, desired);
2856 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002857 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2858 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002859 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002860 gParcelGlobalAllocSize += desired;
2861 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002862 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002863 mData = data;
2864 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002865 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002866 mError = NO_MEMORY;
2867 return NO_MEMORY;
2868 }
2869 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002870 if (mDataSize > desired) {
2871 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002872 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002873 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002874 if (mDataPos > desired) {
2875 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002876 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002877 }
2878 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002879
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002880 } else {
2881 // This is the first data. Easy!
2882 uint8_t* data = (uint8_t*)malloc(desired);
2883 if (!data) {
2884 mError = NO_MEMORY;
2885 return NO_MEMORY;
2886 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002887
Yi Kong91635562018-06-07 14:38:36 -07002888 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002889 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002890 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002891 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002892
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002893 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002894 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002895 gParcelGlobalAllocSize += desired;
2896 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002897 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002898
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002899 mData = data;
2900 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002901 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2902 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002903 mDataCapacity = desired;
2904 }
2905
2906 return NO_ERROR;
2907}
2908
2909void Parcel::initState()
2910{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002911 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002912 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002913 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002914 mDataSize = 0;
2915 mDataCapacity = 0;
2916 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002917 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2918 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002919 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002920 mObjectsSize = 0;
2921 mObjectsCapacity = 0;
2922 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002923 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002924 mHasFds = false;
2925 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002926 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002927 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002928 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002929 mWorkSourceRequestHeaderPosition = 0;
2930 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002931
2932 // racing multiple init leads only to multiple identical write
2933 if (gMaxFds == 0) {
2934 struct rlimit result;
2935 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2936 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002937 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002938 } else {
2939 ALOGW("Unable to getrlimit: %s", strerror(errno));
2940 gMaxFds = 1024;
2941 }
2942 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002943}
2944
2945void Parcel::scanForFds() const
2946{
2947 bool hasFds = false;
2948 for (size_t i=0; i<mObjectsSize; i++) {
2949 const flat_binder_object* flat
2950 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002951 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002952 hasFds = true;
2953 break;
2954 }
2955 }
2956 mHasFds = hasFds;
2957 mFdsKnown = true;
2958}
2959
Dan Sandleraa5c2342015-04-10 10:08:45 -04002960size_t Parcel::getBlobAshmemSize() const
2961{
Adrian Roos6bb31142015-10-22 16:46:12 -07002962 // This used to return the size of all blobs that were written to ashmem, now we're returning
2963 // the ashmem currently referenced by this Parcel, which should be equivalent.
2964 // TODO: Remove method once ABI can be changed.
2965 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002966}
2967
Adrian Rooscbf37262015-10-22 16:12:53 -07002968size_t Parcel::getOpenAshmemSize() const
2969{
2970 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002971}
2972
2973// --- Parcel::Blob ---
2974
2975Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002976 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002977}
2978
2979Parcel::Blob::~Blob() {
2980 release();
2981}
2982
2983void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002984 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002985 ::munmap(mData, mSize);
2986 }
2987 clear();
2988}
2989
Jeff Brown13b16042014-11-11 16:44:25 -08002990void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2991 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002992 mData = data;
2993 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002994 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002995}
2996
2997void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002998 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002999 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003000 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08003001 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003002}
3003
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003004}; // namespace android