blob: d285030c1b9bdc178cdc47a205462410e560b0fe [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) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 IBinder *local = binder->localBinder();
224 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 {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700235 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800236 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
237 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238 }
239 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700240 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800241 obj.binder = 0;
242 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700244
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700245 return finish_flatten_binder(binder, obj, out);
246}
247
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800248status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700249 const wp<IBinder>& binder, Parcel* out)
250{
251 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700252
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700253 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Yi Kong91635562018-06-07 14:38:36 -0700254 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255 sp<IBinder> real = binder.promote();
Yi Kong91635562018-06-07 14:38:36 -0700256 if (real != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 IBinder *local = real->localBinder();
258 if (!local) {
259 BpBinder *proxy = real->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700260 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000261 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700262 }
263 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700264 obj.hdr.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800265 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700266 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800267 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700268 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700269 obj.hdr.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800270 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
271 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700272 }
273 return finish_flatten_binder(real, obj, out);
274 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700275
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700276 // XXX How to deal? In order to flatten the given binder,
277 // we need to probe it for information, which requires a primary
278 // reference... but we don't have one.
279 //
280 // The OpenBinder implementation uses a dynamic_cast<> here,
281 // but we can't do that with the different reference counting
282 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000283 ALOGE("Unable to unflatten Binder weak reference!");
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700284 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800285 obj.binder = 0;
286 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700287 return finish_flatten_binder(nullptr, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700288
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700289 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700290 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800291 obj.binder = 0;
292 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700293 return finish_flatten_binder(nullptr, obj, out);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700294 }
295}
296
297inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800298 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
299 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700300{
301 return NO_ERROR;
302}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700303
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700304status_t unflatten_binder(const sp<ProcessState>& proc,
305 const Parcel& in, sp<IBinder>* out)
306{
307 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700308
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700309 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700310 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700311 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800312 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700313 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700314 case BINDER_TYPE_HANDLE:
315 *out = proc->getStrongProxyForHandle(flat->handle);
316 return finish_unflatten_binder(
317 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700318 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700319 }
320 return BAD_TYPE;
321}
322
323status_t unflatten_binder(const sp<ProcessState>& proc,
324 const Parcel& in, wp<IBinder>* out)
325{
326 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700327
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700328 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700329 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700330 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800331 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700332 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700333 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800334 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700335 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800336 reinterpret_cast<IBinder*>(flat->cookie),
337 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700338 } else {
Yi Kong91635562018-06-07 14:38:36 -0700339 *out = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700340 }
Yi Kong91635562018-06-07 14:38:36 -0700341 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700342 case BINDER_TYPE_HANDLE:
343 case BINDER_TYPE_WEAK_HANDLE:
344 *out = proc->getWeakProxyForHandle(flat->handle);
345 return finish_unflatten_binder(
346 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
347 }
348 }
349 return BAD_TYPE;
350}
351
352// ---------------------------------------------------------------------------
353
354Parcel::Parcel()
355{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800356 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700357 initState();
358}
359
360Parcel::~Parcel()
361{
362 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800363 LOG_ALLOC("Parcel %p: destroyed", this);
364}
365
366size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800367 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
368 size_t size = gParcelGlobalAllocSize;
369 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
370 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800371}
372
373size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800374 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
375 size_t count = gParcelGlobalAllocCount;
376 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
377 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700378}
379
380const uint8_t* Parcel::data() const
381{
382 return mData;
383}
384
385size_t Parcel::dataSize() const
386{
387 return (mDataSize > mDataPos ? mDataSize : mDataPos);
388}
389
390size_t Parcel::dataAvail() const
391{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700392 size_t result = dataSize() - dataPosition();
393 if (result > INT32_MAX) {
394 abort();
395 }
396 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700397}
398
399size_t Parcel::dataPosition() const
400{
401 return mDataPos;
402}
403
404size_t Parcel::dataCapacity() const
405{
406 return mDataCapacity;
407}
408
409status_t Parcel::setDataSize(size_t size)
410{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700411 if (size > INT32_MAX) {
412 // don't accept size_t values which may have come from an
413 // inadvertent conversion from a negative int.
414 return BAD_VALUE;
415 }
416
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700417 status_t err;
418 err = continueWrite(size);
419 if (err == NO_ERROR) {
420 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700421 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700422 }
423 return err;
424}
425
426void Parcel::setDataPosition(size_t pos) const
427{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700428 if (pos > INT32_MAX) {
429 // don't accept size_t values which may have come from an
430 // inadvertent conversion from a negative int.
431 abort();
432 }
433
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 mDataPos = pos;
435 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800436 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437}
438
439status_t Parcel::setDataCapacity(size_t size)
440{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700441 if (size > INT32_MAX) {
442 // don't accept size_t values which may have come from an
443 // inadvertent conversion from a negative int.
444 return BAD_VALUE;
445 }
446
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700447 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 return NO_ERROR;
449}
450
451status_t Parcel::setData(const uint8_t* buffer, size_t len)
452{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700453 if (len > INT32_MAX) {
454 // don't accept size_t values which may have come from an
455 // inadvertent conversion from a negative int.
456 return BAD_VALUE;
457 }
458
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 status_t err = restartWrite(len);
460 if (err == NO_ERROR) {
461 memcpy(const_cast<uint8_t*>(data()), buffer, len);
462 mDataSize = len;
463 mFdsKnown = false;
464 }
465 return err;
466}
467
Andreas Huber51faf462011-04-13 10:21:56 -0700468status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700469{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700470 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700471 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800472 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473 size_t size = parcel->mObjectsSize;
474 int startPos = mDataPos;
475 int firstIndex = -1, lastIndex = -2;
476
477 if (len == 0) {
478 return NO_ERROR;
479 }
480
Nick Kralevichb6b14232015-04-02 09:36:02 -0700481 if (len > INT32_MAX) {
482 // don't accept size_t values which may have come from an
483 // inadvertent conversion from a negative int.
484 return BAD_VALUE;
485 }
486
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700487 // range checks against the source parcel size
488 if ((offset > parcel->mDataSize)
489 || (len > parcel->mDataSize)
490 || (offset + len > parcel->mDataSize)) {
491 return BAD_VALUE;
492 }
493
494 // Count objects in range
495 for (int i = 0; i < (int) size; i++) {
496 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700497 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700498 if (firstIndex == -1) {
499 firstIndex = i;
500 }
501 lastIndex = i;
502 }
503 }
504 int numObjects = lastIndex - firstIndex + 1;
505
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700506 if ((mDataSize+len) > mDataCapacity) {
507 // grow data
508 err = growData(len);
509 if (err != NO_ERROR) {
510 return err;
511 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700512 }
513
514 // append data
515 memcpy(mData + mDataPos, data + offset, len);
516 mDataPos += len;
517 mDataSize += len;
518
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400519 err = NO_ERROR;
520
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200522 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700523 // grow objects
524 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700525 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700526 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800527 binder_size_t *objects =
528 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700529 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700530 return NO_MEMORY;
531 }
532 mObjects = objects;
533 mObjectsCapacity = newSize;
534 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700535
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700536 // append and acquire objects
537 int idx = mObjectsSize;
538 for (int i = firstIndex; i <= lastIndex; i++) {
539 size_t off = objects[i] - offset + startPos;
540 mObjects[idx++] = off;
541 mObjectsSize++;
542
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700543 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700544 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700545 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700546
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700547 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700548 // If this is a file descriptor, we need to dup it so the
549 // new Parcel now owns its own fd, and can declare that we
550 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800551 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800552 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700553 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400554 if (!mAllowFds) {
555 err = FDS_NOT_ALLOWED;
556 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700557 }
558 }
559 }
560
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400561 return err;
562}
563
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700564int Parcel::compareData(const Parcel& other) {
565 size_t size = dataSize();
566 if (size != other.dataSize()) {
567 return size < other.dataSize() ? -1 : 1;
568 }
569 return memcmp(data(), other.data(), size);
570}
571
Jeff Brown13b16042014-11-11 16:44:25 -0800572bool Parcel::allowFds() const
573{
574 return mAllowFds;
575}
576
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700577bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400578{
579 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700580 if (!allowFds) {
581 mAllowFds = false;
582 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400583 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700584}
585
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700586void Parcel::restoreAllowFds(bool lastValue)
587{
588 mAllowFds = lastValue;
589}
590
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700591bool Parcel::hasFileDescriptors() const
592{
593 if (!mFdsKnown) {
594 scanForFds();
595 }
596 return mHasFds;
597}
598
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700599// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700600status_t Parcel::writeInterfaceToken(const String16& interface)
601{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000602 const IPCThreadState* threadState = IPCThreadState::self();
603 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
604 writeInt32(threadState->shouldPropagateWorkSource() ?
605 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700606 // currently the interface identification token is just its name as a string
607 return writeString16(interface);
608}
609
Mathias Agopian83c04462009-05-22 19:00:22 -0700610bool Parcel::checkInterface(IBinder* binder) const
611{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700612 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700613}
614
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700615bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700616 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700617{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100618 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700619 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700620 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700621 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700622 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700623 if ((threadState->getLastTransactionBinderFlags() &
624 IBinder::FLAG_ONEWAY) != 0) {
625 // For one-way calls, the callee is running entirely
626 // disconnected from the caller, so disable StrictMode entirely.
627 // Not only does disk/network usage not impact the caller, but
628 // there's no way to commuicate back any violations anyway.
629 threadState->setStrictModePolicy(0);
630 } else {
631 threadState->setStrictModePolicy(strictPolicy);
632 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100633 // WorkSource.
634 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000635 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100636 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700637 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700638 if (str == interface) {
639 return true;
640 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700641 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700642 String8(interface).string(), String8(str).string());
643 return false;
644 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700645}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700646
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800647const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700648{
649 return mObjects;
650}
651
652size_t Parcel::objectsCount() const
653{
654 return mObjectsSize;
655}
656
657status_t Parcel::errorCheck() const
658{
659 return mError;
660}
661
662void Parcel::setError(status_t err)
663{
664 mError = err;
665}
666
667status_t Parcel::finishWrite(size_t len)
668{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700669 if (len > INT32_MAX) {
670 // don't accept size_t values which may have come from an
671 // inadvertent conversion from a negative int.
672 return BAD_VALUE;
673 }
674
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700675 //printf("Finish write of %d\n", len);
676 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700677 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700678 if (mDataPos > mDataSize) {
679 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700680 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700681 }
682 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
683 return NO_ERROR;
684}
685
686status_t Parcel::writeUnpadded(const void* data, size_t len)
687{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700688 if (len > INT32_MAX) {
689 // don't accept size_t values which may have come from an
690 // inadvertent conversion from a negative int.
691 return BAD_VALUE;
692 }
693
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700694 size_t end = mDataPos + len;
695 if (end < mDataPos) {
696 // integer overflow
697 return BAD_VALUE;
698 }
699
700 if (end <= mDataCapacity) {
701restart_write:
702 memcpy(mData+mDataPos, data, len);
703 return finishWrite(len);
704 }
705
706 status_t err = growData(len);
707 if (err == NO_ERROR) goto restart_write;
708 return err;
709}
710
711status_t Parcel::write(const void* data, size_t len)
712{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700713 if (len > INT32_MAX) {
714 // don't accept size_t values which may have come from an
715 // inadvertent conversion from a negative int.
716 return BAD_VALUE;
717 }
718
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700719 void* const d = writeInplace(len);
720 if (d) {
721 memcpy(d, data, len);
722 return NO_ERROR;
723 }
724 return mError;
725}
726
727void* Parcel::writeInplace(size_t len)
728{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700729 if (len > INT32_MAX) {
730 // don't accept size_t values which may have come from an
731 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700732 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700733 }
734
735 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700736
737 // sanity check for integer overflow
738 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700739 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700740 }
741
742 if ((mDataPos+padded) <= mDataCapacity) {
743restart_write:
744 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
745 uint8_t* const data = mData+mDataPos;
746
747 // Need to pad at end?
748 if (padded != len) {
749#if BYTE_ORDER == BIG_ENDIAN
750 static const uint32_t mask[4] = {
751 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
752 };
753#endif
754#if BYTE_ORDER == LITTLE_ENDIAN
755 static const uint32_t mask[4] = {
756 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
757 };
758#endif
759 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
760 // *reinterpret_cast<void**>(data+padded-4));
761 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
762 }
763
764 finishWrite(padded);
765 return data;
766 }
767
768 status_t err = growData(padded);
769 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700770 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700771}
772
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800773status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
774 const uint8_t* strData = (uint8_t*)str.data();
775 const size_t strLen= str.length();
776 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100777 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800778 return BAD_VALUE;
779 }
780
781 status_t err = writeInt32(utf16Len);
782 if (err) {
783 return err;
784 }
785
786 // Allocate enough bytes to hold our converted string and its terminating NULL.
787 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
788 if (!dst) {
789 return NO_MEMORY;
790 }
791
Sergio Girof4607432016-07-21 14:46:35 +0100792 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800793
794 return NO_ERROR;
795}
796
797status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
798 if (!str) {
799 return writeInt32(-1);
800 }
801 return writeUtf8AsUtf16(*str);
802}
803
Casey Dahlin185d3442016-02-09 11:08:35 -0800804namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800805
Casey Dahlin185d3442016-02-09 11:08:35 -0800806template<typename T>
807status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700808{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700809 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700810 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700811 status = BAD_VALUE;
812 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700813 }
814
Casey Dahlin185d3442016-02-09 11:08:35 -0800815 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700816 if (status != OK) {
817 return status;
818 }
819
Casey Dahlin185d3442016-02-09 11:08:35 -0800820 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700821 if (!data) {
822 status = BAD_VALUE;
823 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700824 }
825
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700826 memcpy(data, val.data(), val.size());
827 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700828}
829
Casey Dahlin185d3442016-02-09 11:08:35 -0800830template<typename T>
831status_t writeByteVectorInternalPtr(Parcel* parcel,
832 const std::unique_ptr<std::vector<T>>& val)
833{
834 if (!val) {
835 return parcel->writeInt32(-1);
836 }
837
838 return writeByteVectorInternal(parcel, *val);
839}
840
841} // namespace
842
843status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
844 return writeByteVectorInternal(this, val);
845}
846
847status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
848{
849 return writeByteVectorInternalPtr(this, val);
850}
851
852status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
853 return writeByteVectorInternal(this, val);
854}
855
856status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
857{
858 return writeByteVectorInternalPtr(this, val);
859}
860
Casey Dahlin451ff582015-10-19 18:12:18 -0700861status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
862{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800863 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700864}
865
Casey Dahlinb9872622015-11-25 15:09:45 -0800866status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
867{
868 return writeNullableTypedVector(val, &Parcel::writeInt32);
869}
870
Casey Dahlin451ff582015-10-19 18:12:18 -0700871status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
872{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800873 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700874}
875
Casey Dahlinb9872622015-11-25 15:09:45 -0800876status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
877{
878 return writeNullableTypedVector(val, &Parcel::writeInt64);
879}
880
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800881status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
882{
883 return writeTypedVector(val, &Parcel::writeUint64);
884}
885
886status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
887{
888 return writeNullableTypedVector(val, &Parcel::writeUint64);
889}
890
Casey Dahlin451ff582015-10-19 18:12:18 -0700891status_t Parcel::writeFloatVector(const std::vector<float>& val)
892{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800893 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700894}
895
Casey Dahlinb9872622015-11-25 15:09:45 -0800896status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
897{
898 return writeNullableTypedVector(val, &Parcel::writeFloat);
899}
900
Casey Dahlin451ff582015-10-19 18:12:18 -0700901status_t Parcel::writeDoubleVector(const std::vector<double>& val)
902{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800903 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700904}
905
Casey Dahlinb9872622015-11-25 15:09:45 -0800906status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
907{
908 return writeNullableTypedVector(val, &Parcel::writeDouble);
909}
910
Casey Dahlin451ff582015-10-19 18:12:18 -0700911status_t Parcel::writeBoolVector(const std::vector<bool>& val)
912{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800913 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700914}
915
Casey Dahlinb9872622015-11-25 15:09:45 -0800916status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
917{
918 return writeNullableTypedVector(val, &Parcel::writeBool);
919}
920
Casey Dahlin451ff582015-10-19 18:12:18 -0700921status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
922{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800923 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700924}
925
Casey Dahlinb9872622015-11-25 15:09:45 -0800926status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
927{
928 return writeNullableTypedVector(val, &Parcel::writeChar);
929}
930
Casey Dahlin451ff582015-10-19 18:12:18 -0700931status_t Parcel::writeString16Vector(const std::vector<String16>& val)
932{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800933 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700934}
935
Casey Dahlinb9872622015-11-25 15:09:45 -0800936status_t Parcel::writeString16Vector(
937 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
938{
939 return writeNullableTypedVector(val, &Parcel::writeString16);
940}
941
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800942status_t Parcel::writeUtf8VectorAsUtf16Vector(
943 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
944 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
945}
946
947status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
948 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
949}
950
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700951status_t Parcel::writeInt32(int32_t val)
952{
Andreas Huber84a6d042009-08-17 13:33:27 -0700953 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700954}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800955
956status_t Parcel::writeUint32(uint32_t val)
957{
958 return writeAligned(val);
959}
960
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700961status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700962 if (len > INT32_MAX) {
963 // don't accept size_t values which may have come from an
964 // inadvertent conversion from a negative int.
965 return BAD_VALUE;
966 }
967
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700968 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700969 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700970 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700971 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700972 if (ret == NO_ERROR) {
973 ret = write(val, len * sizeof(*val));
974 }
975 return ret;
976}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700977status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700978 if (len > INT32_MAX) {
979 // don't accept size_t values which may have come from an
980 // inadvertent conversion from a negative int.
981 return BAD_VALUE;
982 }
983
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700984 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700985 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700986 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700987 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700988 if (ret == NO_ERROR) {
989 ret = write(val, len * sizeof(*val));
990 }
991 return ret;
992}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700993
Casey Dahlind6848f52015-10-15 15:44:59 -0700994status_t Parcel::writeBool(bool val)
995{
996 return writeInt32(int32_t(val));
997}
998
999status_t Parcel::writeChar(char16_t val)
1000{
1001 return writeInt32(int32_t(val));
1002}
1003
1004status_t Parcel::writeByte(int8_t val)
1005{
1006 return writeInt32(int32_t(val));
1007}
1008
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001009status_t Parcel::writeInt64(int64_t val)
1010{
Andreas Huber84a6d042009-08-17 13:33:27 -07001011 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001012}
1013
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001014status_t Parcel::writeUint64(uint64_t val)
1015{
1016 return writeAligned(val);
1017}
1018
Serban Constantinescuf683e012013-11-05 16:53:55 +00001019status_t Parcel::writePointer(uintptr_t val)
1020{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001021 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001022}
1023
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001024status_t Parcel::writeFloat(float val)
1025{
Andreas Huber84a6d042009-08-17 13:33:27 -07001026 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001027}
1028
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001029#if defined(__mips__) && defined(__mips_hard_float)
1030
1031status_t Parcel::writeDouble(double val)
1032{
1033 union {
1034 double d;
1035 unsigned long long ll;
1036 } u;
1037 u.d = val;
1038 return writeAligned(u.ll);
1039}
1040
1041#else
1042
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001043status_t Parcel::writeDouble(double val)
1044{
Andreas Huber84a6d042009-08-17 13:33:27 -07001045 return writeAligned(val);
1046}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001047
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001048#endif
1049
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001050status_t Parcel::writeCString(const char* str)
1051{
1052 return write(str, strlen(str)+1);
1053}
1054
1055status_t Parcel::writeString8(const String8& str)
1056{
1057 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001058 // only write string if its length is more than zero characters,
1059 // as readString8 will only read if the length field is non-zero.
1060 // this is slightly different from how writeString16 works.
1061 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001062 err = write(str.string(), str.bytes()+1);
1063 }
1064 return err;
1065}
1066
Casey Dahlinb9872622015-11-25 15:09:45 -08001067status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1068{
1069 if (!str) {
1070 return writeInt32(-1);
1071 }
1072
1073 return writeString16(*str);
1074}
1075
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001076status_t Parcel::writeString16(const String16& str)
1077{
1078 return writeString16(str.string(), str.size());
1079}
1080
1081status_t Parcel::writeString16(const char16_t* str, size_t len)
1082{
Yi Kong91635562018-06-07 14:38:36 -07001083 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001084
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001085 status_t err = writeInt32(len);
1086 if (err == NO_ERROR) {
1087 len *= sizeof(char16_t);
1088 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1089 if (data) {
1090 memcpy(data, str, len);
1091 *reinterpret_cast<char16_t*>(data+len) = 0;
1092 return NO_ERROR;
1093 }
1094 err = mError;
1095 }
1096 return err;
1097}
1098
1099status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1100{
1101 return flatten_binder(ProcessState::self(), val, this);
1102}
1103
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001104status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1105{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001106 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001107}
1108
Casey Dahlinb9872622015-11-25 15:09:45 -08001109status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1110{
1111 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1112}
1113
1114status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001115 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001116}
1117
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001118status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001119 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001120}
1121
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001122status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1123{
1124 return flatten_binder(ProcessState::self(), val, this);
1125}
1126
Casey Dahlinb9872622015-11-25 15:09:45 -08001127status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1128 if (!parcelable) {
1129 return writeInt32(0);
1130 }
1131
1132 return writeParcelable(*parcelable);
1133}
1134
Christopher Wiley97f048d2015-11-19 06:49:05 -08001135status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1136 status_t status = writeInt32(1); // parcelable is not null.
1137 if (status != OK) {
1138 return status;
1139 }
1140 return parcelable.writeToParcel(this);
1141}
1142
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001143status_t Parcel::writeValue(const binder::Value& value) {
1144 return value.writeToParcel(this);
1145}
1146
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001147status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001148{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001149 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001150 return BAD_TYPE;
1151
1152 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001153 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001154 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001155
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001156 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001157 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001158
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001159 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1160 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161
1162 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001163 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164 return err;
1165 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001166 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001167 return err;
1168}
1169
Jeff Brown93ff1f92011-11-04 19:01:44 -07001170status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001171{
1172 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001173 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001174 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001175 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001176 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001177 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001178 return writeObject(obj, true);
1179}
1180
1181status_t Parcel::writeDupFileDescriptor(int fd)
1182{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001183 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001184 if (dupFd < 0) {
1185 return -errno;
1186 }
1187 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001188 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001189 close(dupFd);
1190 }
1191 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001192}
1193
Dianne Hackborn1941a402016-08-29 12:30:43 -07001194status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1195{
1196 writeInt32(0);
1197 return writeFileDescriptor(fd, takeOwnership);
1198}
1199
Ryo Hashimotobf551892018-05-31 16:58:35 +09001200status_t Parcel::writeDupParcelFileDescriptor(int fd)
1201{
1202 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1203 if (dupFd < 0) {
1204 return -errno;
1205 }
1206 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1207 if (err != OK) {
1208 close(dupFd);
1209 }
1210 return err;
1211}
1212
Christopher Wiley2cf19952016-04-11 11:09:37 -07001213status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001214 return writeDupFileDescriptor(fd.get());
1215}
1216
Christopher Wiley2cf19952016-04-11 11:09:37 -07001217status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001218 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1219}
1220
Christopher Wiley2cf19952016-04-11 11:09:37 -07001221status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001222 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1223}
1224
Jeff Brown13b16042014-11-11 16:44:25 -08001225status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001226{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001227 if (len > INT32_MAX) {
1228 // don't accept size_t values which may have come from an
1229 // inadvertent conversion from a negative int.
1230 return BAD_VALUE;
1231 }
1232
Jeff Brown13b16042014-11-11 16:44:25 -08001233 status_t status;
1234 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001235 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001236 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001237 if (status) return status;
1238
1239 void* ptr = writeInplace(len);
1240 if (!ptr) return NO_MEMORY;
1241
Jeff Brown13b16042014-11-11 16:44:25 -08001242 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001243 return NO_ERROR;
1244 }
1245
Steve Block6807e592011-10-20 11:56:00 +01001246 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001247 int fd = ashmem_create_region("Parcel Blob", len);
1248 if (fd < 0) return NO_MEMORY;
1249
1250 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1251 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001252 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001253 } else {
Yi Kong91635562018-06-07 14:38:36 -07001254 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001255 if (ptr == MAP_FAILED) {
1256 status = -errno;
1257 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001258 if (!mutableCopy) {
1259 result = ashmem_set_prot_region(fd, PROT_READ);
1260 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001261 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001262 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001263 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001264 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001265 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001266 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001267 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001268 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001269 return NO_ERROR;
1270 }
1271 }
1272 }
1273 }
1274 ::munmap(ptr, len);
1275 }
1276 ::close(fd);
1277 return status;
1278}
1279
Jeff Brown13b16042014-11-11 16:44:25 -08001280status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1281{
1282 // Must match up with what's done in writeBlob.
1283 if (!mAllowFds) return FDS_NOT_ALLOWED;
1284 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1285 if (status) return status;
1286 return writeDupFileDescriptor(fd);
1287}
1288
Mathias Agopiane1424282013-07-29 21:24:40 -07001289status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001290{
1291 status_t err;
1292
1293 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001294 const size_t len = val.getFlattenedSize();
1295 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001296
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001297 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001298 // don't accept size_t values which may have come from an
1299 // inadvertent conversion from a negative int.
1300 return BAD_VALUE;
1301 }
1302
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001303 err = this->writeInt32(len);
1304 if (err) return err;
1305
1306 err = this->writeInt32(fd_count);
1307 if (err) return err;
1308
1309 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001310 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001311 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001312 return BAD_VALUE;
1313
Yi Kong91635562018-06-07 14:38:36 -07001314 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001315 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001316 fds = new (std::nothrow) int[fd_count];
1317 if (fds == nullptr) {
1318 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1319 return BAD_VALUE;
1320 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001321 }
1322
1323 err = val.flatten(buf, len, fds, fd_count);
1324 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1325 err = this->writeDupFileDescriptor( fds[i] );
1326 }
1327
1328 if (fd_count) {
1329 delete [] fds;
1330 }
1331
1332 return err;
1333}
1334
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001335status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1336{
1337 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1338 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1339 if (enoughData && enoughObjects) {
1340restart_write:
1341 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001342
Christopher Tate98e67d32015-06-03 18:44:15 -07001343 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001344 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001345 if (!mAllowFds) {
1346 // fail before modifying our object index
1347 return FDS_NOT_ALLOWED;
1348 }
1349 mHasFds = mFdsKnown = true;
1350 }
1351
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001352 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001353 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001354 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001355 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001356 mObjectsSize++;
1357 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001358
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001359 return finishWrite(sizeof(flat_binder_object));
1360 }
1361
1362 if (!enoughData) {
1363 const status_t err = growData(sizeof(val));
1364 if (err != NO_ERROR) return err;
1365 }
1366 if (!enoughObjects) {
1367 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001368 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001369 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001370 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001371 mObjects = objects;
1372 mObjectsCapacity = newSize;
1373 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001374
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001375 goto restart_write;
1376}
1377
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001378status_t Parcel::writeNoException()
1379{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001380 binder::Status status;
1381 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001382}
1383
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001384status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1385{
1386 using ::std::map;
1387 using ::android::binder::Value;
1388 using ::android::binder::Map;
1389
1390 Map::const_iterator iter;
1391 status_t ret;
1392
1393 ret = writeInt32(map_in.size());
1394
1395 if (ret != NO_ERROR) {
1396 return ret;
1397 }
1398
1399 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1400 ret = writeValue(Value(iter->first));
1401 if (ret != NO_ERROR) {
1402 return ret;
1403 }
1404
1405 ret = writeValue(iter->second);
1406 if (ret != NO_ERROR) {
1407 return ret;
1408 }
1409 }
1410
1411 return ret;
1412}
1413
1414status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1415{
Yi Kong91635562018-06-07 14:38:36 -07001416 if (map == nullptr) {
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001417 return writeInt32(-1);
1418 }
1419
1420 return writeMap(*map.get());
1421}
1422
1423status_t Parcel::readMap(::android::binder::Map* map_out)const
1424{
1425 using ::std::map;
1426 using ::android::String16;
1427 using ::android::String8;
1428 using ::android::binder::Value;
1429 using ::android::binder::Map;
1430
1431 status_t ret = NO_ERROR;
1432 int32_t count;
1433
1434 ret = readInt32(&count);
1435 if (ret != NO_ERROR) {
1436 return ret;
1437 }
1438
1439 if (count < 0) {
1440 ALOGE("readMap: Unexpected count: %d", count);
1441 return (count == -1)
1442 ? UNEXPECTED_NULL
1443 : BAD_VALUE;
1444 }
1445
1446 map_out->clear();
1447
1448 while (count--) {
1449 Map::key_type key;
1450 Value value;
1451
1452 ret = readValue(&value);
1453 if (ret != NO_ERROR) {
1454 return ret;
1455 }
1456
1457 if (!value.getString(&key)) {
1458 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1459 return BAD_VALUE;
1460 }
1461
1462 ret = readValue(&value);
1463 if (ret != NO_ERROR) {
1464 return ret;
1465 }
1466
1467 (*map_out)[key] = value;
1468 }
1469
1470 return ret;
1471}
1472
1473status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1474{
1475 const size_t start = dataPosition();
1476 int32_t count;
1477 status_t status = readInt32(&count);
1478 map->reset();
1479
1480 if (status != OK || count == -1) {
1481 return status;
1482 }
1483
1484 setDataPosition(start);
1485 map->reset(new binder::Map());
1486
1487 status = readMap(map->get());
1488
1489 if (status != OK) {
1490 map->reset();
1491 }
1492
1493 return status;
1494}
1495
1496
1497
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001498void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001499{
1500 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1501}
1502
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001503status_t Parcel::validateReadData(size_t upperBound) const
1504{
1505 // Don't allow non-object reads on object data
1506 if (mObjectsSorted || mObjectsSize <= 1) {
1507data_sorted:
1508 // Expect to check only against the next object
1509 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1510 // For some reason the current read position is greater than the next object
1511 // hint. Iterate until we find the right object
1512 size_t nextObject = mNextObjectHint;
1513 do {
1514 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1515 // Requested info overlaps with an object
1516 ALOGE("Attempt to read from protected data in Parcel %p", this);
1517 return PERMISSION_DENIED;
1518 }
1519 nextObject++;
1520 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1521 mNextObjectHint = nextObject;
1522 }
1523 return NO_ERROR;
1524 }
1525 // Quickly determine if mObjects is sorted.
1526 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1527 binder_size_t* prevObj = currObj;
1528 while (currObj > mObjects) {
1529 prevObj--;
1530 if(*prevObj > *currObj) {
1531 goto data_unsorted;
1532 }
1533 currObj--;
1534 }
1535 mObjectsSorted = true;
1536 goto data_sorted;
1537
1538data_unsorted:
1539 // Insertion Sort mObjects
1540 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1541 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1542 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1543 binder_size_t temp = *iter0;
1544 binder_size_t* iter1 = iter0 - 1;
1545 while (iter1 >= mObjects && *iter1 > temp) {
1546 *(iter1 + 1) = *iter1;
1547 iter1--;
1548 }
1549 *(iter1 + 1) = temp;
1550 }
1551 mNextObjectHint = 0;
1552 mObjectsSorted = true;
1553 goto data_sorted;
1554}
1555
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001556status_t Parcel::read(void* outData, size_t len) const
1557{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001558 if (len > INT32_MAX) {
1559 // don't accept size_t values which may have come from an
1560 // inadvertent conversion from a negative int.
1561 return BAD_VALUE;
1562 }
1563
1564 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1565 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001566 if (mObjectsSize > 0) {
1567 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001568 if(err != NO_ERROR) {
1569 // Still increment the data position by the expected length
1570 mDataPos += pad_size(len);
1571 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1572 return err;
1573 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001574 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001575 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001576 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001577 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001578 return NO_ERROR;
1579 }
1580 return NOT_ENOUGH_DATA;
1581}
1582
1583const void* Parcel::readInplace(size_t len) const
1584{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001585 if (len > INT32_MAX) {
1586 // don't accept size_t values which may have come from an
1587 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001588 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001589 }
1590
1591 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1592 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001593 if (mObjectsSize > 0) {
1594 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001595 if(err != NO_ERROR) {
1596 // Still increment the data position by the expected length
1597 mDataPos += pad_size(len);
1598 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001599 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001600 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001601 }
1602
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001603 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001604 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001605 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001606 return data;
1607 }
Yi Kong91635562018-06-07 14:38:36 -07001608 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001609}
1610
Andreas Huber84a6d042009-08-17 13:33:27 -07001611template<class T>
1612status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001613 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001614
1615 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001616 if (mObjectsSize > 0) {
1617 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001618 if(err != NO_ERROR) {
1619 // Still increment the data position by the expected length
1620 mDataPos += sizeof(T);
1621 return err;
1622 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001623 }
1624
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001625 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001626 mDataPos += sizeof(T);
1627 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001628 return NO_ERROR;
1629 } else {
1630 return NOT_ENOUGH_DATA;
1631 }
1632}
1633
Andreas Huber84a6d042009-08-17 13:33:27 -07001634template<class T>
1635T Parcel::readAligned() const {
1636 T result;
1637 if (readAligned(&result) != NO_ERROR) {
1638 result = 0;
1639 }
1640
1641 return result;
1642}
1643
1644template<class T>
1645status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001646 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001647
1648 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1649restart_write:
1650 *reinterpret_cast<T*>(mData+mDataPos) = val;
1651 return finishWrite(sizeof(val));
1652 }
1653
1654 status_t err = growData(sizeof(val));
1655 if (err == NO_ERROR) goto restart_write;
1656 return err;
1657}
1658
Casey Dahlin185d3442016-02-09 11:08:35 -08001659namespace {
1660
1661template<typename T>
1662status_t readByteVectorInternal(const Parcel* parcel,
1663 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001664 val->clear();
1665
1666 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001667 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001668
1669 if (status != OK) {
1670 return status;
1671 }
1672
Christopher Wiley4db672d2015-11-10 09:44:30 -08001673 if (size < 0) {
1674 status = UNEXPECTED_NULL;
1675 return status;
1676 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001677 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001678 status = BAD_VALUE;
1679 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001680 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001681
Paul Lietar433e87b2016-09-16 10:39:32 -07001682 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001683 if (!data) {
1684 status = BAD_VALUE;
1685 return status;
1686 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001687 val->reserve(size);
1688 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001689
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001690 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001691}
1692
Casey Dahlin185d3442016-02-09 11:08:35 -08001693template<typename T>
1694status_t readByteVectorInternalPtr(
1695 const Parcel* parcel,
1696 std::unique_ptr<std::vector<T>>* val) {
1697 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001698 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001699 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001700 val->reset();
1701
1702 if (status != OK || size < 0) {
1703 return status;
1704 }
1705
Casey Dahlin185d3442016-02-09 11:08:35 -08001706 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001707 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001708
Casey Dahlin185d3442016-02-09 11:08:35 -08001709 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001710
1711 if (status != OK) {
1712 val->reset();
1713 }
1714
1715 return status;
1716}
1717
Casey Dahlin185d3442016-02-09 11:08:35 -08001718} // namespace
1719
1720status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1721 return readByteVectorInternal(this, val);
1722}
1723
1724status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1725 return readByteVectorInternal(this, val);
1726}
1727
1728status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1729 return readByteVectorInternalPtr(this, val);
1730}
1731
1732status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1733 return readByteVectorInternalPtr(this, val);
1734}
1735
Casey Dahlinb9872622015-11-25 15:09:45 -08001736status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1737 return readNullableTypedVector(val, &Parcel::readInt32);
1738}
1739
Casey Dahlin451ff582015-10-19 18:12:18 -07001740status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001741 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001742}
1743
Casey Dahlinb9872622015-11-25 15:09:45 -08001744status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1745 return readNullableTypedVector(val, &Parcel::readInt64);
1746}
1747
Casey Dahlin451ff582015-10-19 18:12:18 -07001748status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001749 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001750}
1751
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001752status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1753 return readNullableTypedVector(val, &Parcel::readUint64);
1754}
1755
1756status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1757 return readTypedVector(val, &Parcel::readUint64);
1758}
1759
Casey Dahlinb9872622015-11-25 15:09:45 -08001760status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1761 return readNullableTypedVector(val, &Parcel::readFloat);
1762}
1763
Casey Dahlin451ff582015-10-19 18:12:18 -07001764status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001765 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001766}
1767
Casey Dahlinb9872622015-11-25 15:09:45 -08001768status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1769 return readNullableTypedVector(val, &Parcel::readDouble);
1770}
1771
Casey Dahlin451ff582015-10-19 18:12:18 -07001772status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001773 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001774}
1775
Casey Dahlinb9872622015-11-25 15:09:45 -08001776status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1777 const int32_t start = dataPosition();
1778 int32_t size;
1779 status_t status = readInt32(&size);
1780 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001781
Casey Dahlinb9872622015-11-25 15:09:45 -08001782 if (status != OK || size < 0) {
1783 return status;
1784 }
1785
1786 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001787 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001788
1789 status = readBoolVector(val->get());
1790
1791 if (status != OK) {
1792 val->reset();
1793 }
1794
1795 return status;
1796}
1797
1798status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001799 int32_t size;
1800 status_t status = readInt32(&size);
1801
1802 if (status != OK) {
1803 return status;
1804 }
1805
1806 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001807 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001808 }
1809
1810 val->resize(size);
1811
1812 /* C++ bool handling means a vector of bools isn't necessarily addressable
1813 * (we might use individual bits)
1814 */
Christopher Wiley97887982015-10-27 16:33:47 -07001815 bool data;
1816 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001817 status = readBool(&data);
1818 (*val)[i] = data;
1819
1820 if (status != OK) {
1821 return status;
1822 }
1823 }
1824
1825 return OK;
1826}
1827
Casey Dahlinb9872622015-11-25 15:09:45 -08001828status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1829 return readNullableTypedVector(val, &Parcel::readChar);
1830}
1831
Casey Dahlin451ff582015-10-19 18:12:18 -07001832status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001833 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001834}
1835
Casey Dahlinb9872622015-11-25 15:09:45 -08001836status_t Parcel::readString16Vector(
1837 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1838 return readNullableTypedVector(val, &Parcel::readString16);
1839}
1840
Casey Dahlin451ff582015-10-19 18:12:18 -07001841status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001842 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001843}
1844
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001845status_t Parcel::readUtf8VectorFromUtf16Vector(
1846 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1847 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1848}
1849
1850status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1851 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1852}
Casey Dahlin451ff582015-10-19 18:12:18 -07001853
Andreas Huber84a6d042009-08-17 13:33:27 -07001854status_t Parcel::readInt32(int32_t *pArg) const
1855{
1856 return readAligned(pArg);
1857}
1858
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001859int32_t Parcel::readInt32() const
1860{
Andreas Huber84a6d042009-08-17 13:33:27 -07001861 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001862}
1863
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001864status_t Parcel::readUint32(uint32_t *pArg) const
1865{
1866 return readAligned(pArg);
1867}
1868
1869uint32_t Parcel::readUint32() const
1870{
1871 return readAligned<uint32_t>();
1872}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001873
1874status_t Parcel::readInt64(int64_t *pArg) const
1875{
Andreas Huber84a6d042009-08-17 13:33:27 -07001876 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001877}
1878
1879
1880int64_t Parcel::readInt64() const
1881{
Andreas Huber84a6d042009-08-17 13:33:27 -07001882 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001883}
1884
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001885status_t Parcel::readUint64(uint64_t *pArg) const
1886{
1887 return readAligned(pArg);
1888}
1889
1890uint64_t Parcel::readUint64() const
1891{
1892 return readAligned<uint64_t>();
1893}
1894
Serban Constantinescuf683e012013-11-05 16:53:55 +00001895status_t Parcel::readPointer(uintptr_t *pArg) const
1896{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001897 status_t ret;
1898 binder_uintptr_t ptr;
1899 ret = readAligned(&ptr);
1900 if (!ret)
1901 *pArg = ptr;
1902 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001903}
1904
1905uintptr_t Parcel::readPointer() const
1906{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001907 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001908}
1909
1910
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001911status_t Parcel::readFloat(float *pArg) const
1912{
Andreas Huber84a6d042009-08-17 13:33:27 -07001913 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001914}
1915
1916
1917float Parcel::readFloat() const
1918{
Andreas Huber84a6d042009-08-17 13:33:27 -07001919 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001920}
1921
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001922#if defined(__mips__) && defined(__mips_hard_float)
1923
1924status_t Parcel::readDouble(double *pArg) const
1925{
1926 union {
1927 double d;
1928 unsigned long long ll;
1929 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001930 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001931 status_t status;
1932 status = readAligned(&u.ll);
1933 *pArg = u.d;
1934 return status;
1935}
1936
1937double Parcel::readDouble() const
1938{
1939 union {
1940 double d;
1941 unsigned long long ll;
1942 } u;
1943 u.ll = readAligned<unsigned long long>();
1944 return u.d;
1945}
1946
1947#else
1948
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001949status_t Parcel::readDouble(double *pArg) const
1950{
Andreas Huber84a6d042009-08-17 13:33:27 -07001951 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001952}
1953
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001954double Parcel::readDouble() const
1955{
Andreas Huber84a6d042009-08-17 13:33:27 -07001956 return readAligned<double>();
1957}
1958
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001959#endif
1960
Andreas Huber84a6d042009-08-17 13:33:27 -07001961status_t Parcel::readIntPtr(intptr_t *pArg) const
1962{
1963 return readAligned(pArg);
1964}
1965
1966
1967intptr_t Parcel::readIntPtr() const
1968{
1969 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001970}
1971
Casey Dahlind6848f52015-10-15 15:44:59 -07001972status_t Parcel::readBool(bool *pArg) const
1973{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001974 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001975 status_t ret = readInt32(&tmp);
1976 *pArg = (tmp != 0);
1977 return ret;
1978}
1979
1980bool Parcel::readBool() const
1981{
1982 return readInt32() != 0;
1983}
1984
1985status_t Parcel::readChar(char16_t *pArg) const
1986{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001987 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001988 status_t ret = readInt32(&tmp);
1989 *pArg = char16_t(tmp);
1990 return ret;
1991}
1992
1993char16_t Parcel::readChar() const
1994{
1995 return char16_t(readInt32());
1996}
1997
1998status_t Parcel::readByte(int8_t *pArg) const
1999{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002000 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002001 status_t ret = readInt32(&tmp);
2002 *pArg = int8_t(tmp);
2003 return ret;
2004}
2005
2006int8_t Parcel::readByte() const
2007{
2008 return int8_t(readInt32());
2009}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002010
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002011status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2012 size_t utf16Size = 0;
2013 const char16_t* src = readString16Inplace(&utf16Size);
2014 if (!src) {
2015 return UNEXPECTED_NULL;
2016 }
2017
2018 // Save ourselves the trouble, we're done.
2019 if (utf16Size == 0u) {
2020 str->clear();
2021 return NO_ERROR;
2022 }
2023
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002024 // Allow for closing '\0'
2025 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2026 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002027 return BAD_VALUE;
2028 }
2029 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002030 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002031 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002032 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2033 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002034 return NO_ERROR;
2035}
2036
2037status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2038 const int32_t start = dataPosition();
2039 int32_t size;
2040 status_t status = readInt32(&size);
2041 str->reset();
2042
2043 if (status != OK || size < 0) {
2044 return status;
2045 }
2046
2047 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002048 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002049 return readUtf8FromUtf16(str->get());
2050}
2051
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002052const char* Parcel::readCString() const
2053{
2054 const size_t avail = mDataSize-mDataPos;
2055 if (avail > 0) {
2056 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2057 // is the string's trailing NUL within the parcel's valid bounds?
2058 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2059 if (eos) {
2060 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002061 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002062 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002063 return str;
2064 }
2065 }
Yi Kong91635562018-06-07 14:38:36 -07002066 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002067}
2068
2069String8 Parcel::readString8() const
2070{
Roshan Pius87b64d22016-07-18 12:51:02 -07002071 String8 retString;
2072 status_t status = readString8(&retString);
2073 if (status != OK) {
2074 // We don't care about errors here, so just return an empty string.
2075 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002076 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002077 return retString;
2078}
2079
2080status_t Parcel::readString8(String8* pArg) const
2081{
2082 int32_t size;
2083 status_t status = readInt32(&size);
2084 if (status != OK) {
2085 return status;
2086 }
2087 // watch for potential int overflow from size+1
2088 if (size < 0 || size >= INT32_MAX) {
2089 return BAD_VALUE;
2090 }
2091 // |writeString8| writes nothing for empty string.
2092 if (size == 0) {
2093 *pArg = String8();
2094 return OK;
2095 }
2096 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07002097 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002098 return BAD_VALUE;
2099 }
2100 pArg->setTo(str, size);
2101 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002102}
2103
2104String16 Parcel::readString16() const
2105{
2106 size_t len;
2107 const char16_t* str = readString16Inplace(&len);
2108 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002109 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002110 return String16();
2111}
2112
Casey Dahlinb9872622015-11-25 15:09:45 -08002113status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2114{
2115 const int32_t start = dataPosition();
2116 int32_t size;
2117 status_t status = readInt32(&size);
2118 pArg->reset();
2119
2120 if (status != OK || size < 0) {
2121 return status;
2122 }
2123
2124 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002125 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002126
2127 status = readString16(pArg->get());
2128
2129 if (status != OK) {
2130 pArg->reset();
2131 }
2132
2133 return status;
2134}
2135
Casey Dahlin451ff582015-10-19 18:12:18 -07002136status_t Parcel::readString16(String16* pArg) const
2137{
2138 size_t len;
2139 const char16_t* str = readString16Inplace(&len);
2140 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002141 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002142 return 0;
2143 } else {
2144 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002145 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002146 }
2147}
2148
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002149const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2150{
2151 int32_t size = readInt32();
2152 // watch for potential int overflow from size+1
2153 if (size >= 0 && size < INT32_MAX) {
2154 *outLen = size;
2155 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002156 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002157 return str;
2158 }
2159 }
2160 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002161 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002162}
2163
Casey Dahlinf0c13772015-10-27 18:33:56 -07002164status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2165{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002166 status_t status = readNullableStrongBinder(val);
2167 if (status == OK && !val->get()) {
2168 status = UNEXPECTED_NULL;
2169 }
2170 return status;
2171}
2172
2173status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2174{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002175 return unflatten_binder(ProcessState::self(), *this, val);
2176}
2177
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002178sp<IBinder> Parcel::readStrongBinder() const
2179{
2180 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002181 // Note that a lot of code in Android reads binders by hand with this
2182 // method, and that code has historically been ok with getting nullptr
2183 // back (while ignoring error codes).
2184 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002185 return val;
2186}
2187
2188wp<IBinder> Parcel::readWeakBinder() const
2189{
2190 wp<IBinder> val;
2191 unflatten_binder(ProcessState::self(), *this, &val);
2192 return val;
2193}
2194
Christopher Wiley97f048d2015-11-19 06:49:05 -08002195status_t Parcel::readParcelable(Parcelable* parcelable) const {
2196 int32_t have_parcelable = 0;
2197 status_t status = readInt32(&have_parcelable);
2198 if (status != OK) {
2199 return status;
2200 }
2201 if (!have_parcelable) {
2202 return UNEXPECTED_NULL;
2203 }
2204 return parcelable->readFromParcel(this);
2205}
2206
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002207status_t Parcel::readValue(binder::Value* value) const {
2208 return value->readFromParcel(this);
2209}
2210
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002211int32_t Parcel::readExceptionCode() const
2212{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002213 binder::Status status;
2214 status.readFromParcel(*this);
2215 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002216}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002217
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002218native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002219{
2220 int numFds, numInts;
2221 status_t err;
2222 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002223 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002224 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002225 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002226
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002227 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002228 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002229 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002230 }
2231
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002232 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002233 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002234 if (h->data[i] < 0) {
2235 for (int j = 0; j < i; j++) {
2236 close(h->data[j]);
2237 }
2238 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002239 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002240 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002241 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002242 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002243 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002244 native_handle_close(h);
2245 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002246 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002247 }
2248 return h;
2249}
2250
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002251int Parcel::readFileDescriptor() const
2252{
2253 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002254
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002255 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002256 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002257 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002258
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002259 return BAD_TYPE;
2260}
2261
Dianne Hackborn1941a402016-08-29 12:30:43 -07002262int Parcel::readParcelFileDescriptor() const
2263{
2264 int32_t hasComm = readInt32();
2265 int fd = readFileDescriptor();
2266 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002267 // detach (owned by the binder driver)
2268 int comm = readFileDescriptor();
2269
2270 // warning: this must be kept in sync with:
2271 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2272 enum ParcelFileDescriptorStatus {
2273 DETACHED = 2,
2274 };
2275
2276#if BYTE_ORDER == BIG_ENDIAN
2277 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2278#endif
2279#if BYTE_ORDER == LITTLE_ENDIAN
2280 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2281#endif
2282
2283 ssize_t written = TEMP_FAILURE_RETRY(
2284 ::write(comm, &message, sizeof(message)));
2285
2286 if (written == -1 || written != sizeof(message)) {
2287 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2288 written, strerror(errno));
2289 return BAD_TYPE;
2290 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002291 }
2292 return fd;
2293}
2294
Christopher Wiley2cf19952016-04-11 11:09:37 -07002295status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002296{
2297 int got = readFileDescriptor();
2298
2299 if (got == BAD_TYPE) {
2300 return BAD_TYPE;
2301 }
2302
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002303 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002304
2305 if (val->get() < 0) {
2306 return BAD_VALUE;
2307 }
2308
2309 return OK;
2310}
2311
Ryo Hashimotobf551892018-05-31 16:58:35 +09002312status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2313{
2314 int got = readParcelFileDescriptor();
2315
2316 if (got == BAD_TYPE) {
2317 return BAD_TYPE;
2318 }
2319
2320 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2321
2322 if (val->get() < 0) {
2323 return BAD_VALUE;
2324 }
2325
2326 return OK;
2327}
Casey Dahlin06673e32015-11-23 13:24:23 -08002328
Christopher Wiley2cf19952016-04-11 11:09:37 -07002329status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002330 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2331}
2332
Christopher Wiley2cf19952016-04-11 11:09:37 -07002333status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002334 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2335}
2336
Jeff Brown5707dbf2011-09-23 21:17:56 -07002337status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2338{
Jeff Brown13b16042014-11-11 16:44:25 -08002339 int32_t blobType;
2340 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002341 if (status) return status;
2342
Jeff Brown13b16042014-11-11 16:44:25 -08002343 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002344 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002345 const void* ptr = readInplace(len);
2346 if (!ptr) return BAD_VALUE;
2347
Jeff Brown13b16042014-11-11 16:44:25 -08002348 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002349 return NO_ERROR;
2350 }
2351
Steve Block6807e592011-10-20 11:56:00 +01002352 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002353 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002354 int fd = readFileDescriptor();
2355 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2356
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002357 if (!ashmem_valid(fd)) {
2358 ALOGE("invalid fd");
2359 return BAD_VALUE;
2360 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002361 int size = ashmem_get_size_region(fd);
2362 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002363 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002364 return BAD_VALUE;
2365 }
Yi Kong91635562018-06-07 14:38:36 -07002366 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002367 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002368 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002369
Jeff Brown13b16042014-11-11 16:44:25 -08002370 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002371 return NO_ERROR;
2372}
2373
Mathias Agopiane1424282013-07-29 21:24:40 -07002374status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002375{
2376 // size
2377 const size_t len = this->readInt32();
2378 const size_t fd_count = this->readInt32();
2379
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002380 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002381 // don't accept size_t values which may have come from an
2382 // inadvertent conversion from a negative int.
2383 return BAD_VALUE;
2384 }
2385
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002386 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002387 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002388 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002389 return BAD_VALUE;
2390
Yi Kong91635562018-06-07 14:38:36 -07002391 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002392 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002393 fds = new (std::nothrow) int[fd_count];
2394 if (fds == nullptr) {
2395 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2396 return BAD_VALUE;
2397 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002398 }
2399
2400 status_t err = NO_ERROR;
2401 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002402 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002403 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002404 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002405 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 -07002406 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2407 // Close all the file descriptors that were dup-ed.
2408 for (size_t j=0; j<i ;j++) {
2409 close(fds[j]);
2410 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002411 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002412 }
2413
2414 if (err == NO_ERROR) {
2415 err = val.unflatten(buf, len, fds, fd_count);
2416 }
2417
2418 if (fd_count) {
2419 delete [] fds;
2420 }
2421
2422 return err;
2423}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002424const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2425{
2426 const size_t DPOS = mDataPos;
2427 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2428 const flat_binder_object* obj
2429 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2430 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002431 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002432 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002433 // the object list, so we don't want to check for it when
2434 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002435 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002436 return obj;
2437 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002438
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002439 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002440 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002441 const size_t N = mObjectsSize;
2442 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002443
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002444 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002445 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002446 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002447
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002448 // Start at the current hint position, looking for an object at
2449 // the current data position.
2450 if (opos < N) {
2451 while (opos < (N-1) && OBJS[opos] < DPOS) {
2452 opos++;
2453 }
2454 } else {
2455 opos = N-1;
2456 }
2457 if (OBJS[opos] == DPOS) {
2458 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002459 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002460 this, DPOS, opos);
2461 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002462 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002463 return obj;
2464 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002465
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002466 // Look backwards for it...
2467 while (opos > 0 && OBJS[opos] > DPOS) {
2468 opos--;
2469 }
2470 if (OBJS[opos] == DPOS) {
2471 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002472 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002473 this, DPOS, opos);
2474 mNextObjectHint = opos+1;
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 }
2478 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002479 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 -07002480 this, DPOS);
2481 }
Yi Kong91635562018-06-07 14:38:36 -07002482 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002483}
2484
2485void Parcel::closeFileDescriptors()
2486{
2487 size_t i = mObjectsSize;
2488 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002489 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002490 }
2491 while (i > 0) {
2492 i--;
2493 const flat_binder_object* flat
2494 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002495 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002496 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002497 close(flat->handle);
2498 }
2499 }
2500}
2501
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002502uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002503{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002504 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002505}
2506
2507size_t Parcel::ipcDataSize() const
2508{
2509 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2510}
2511
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002512uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002514 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002515}
2516
2517size_t Parcel::ipcObjectsCount() const
2518{
2519 return mObjectsSize;
2520}
2521
2522void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002523 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002524{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002525 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002526 freeDataNoInit();
2527 mError = NO_ERROR;
2528 mData = const_cast<uint8_t*>(data);
2529 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002530 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002531 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002532 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002533 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002534 mObjectsSize = mObjectsCapacity = objectsCount;
2535 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002536 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002537 mOwner = relFunc;
2538 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002539 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002540 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002541 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002542 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002543 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002544 mObjectsSize = 0;
2545 break;
2546 }
2547 minOffset = offset + sizeof(flat_binder_object);
2548 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002549 scanForFds();
2550}
2551
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002552void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002553{
2554 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002555
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002556 if (errorCheck() != NO_ERROR) {
2557 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002558 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002559 } else if (dataSize() > 0) {
2560 const uint8_t* DATA = data();
2561 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002562 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002563 const size_t N = objectsCount();
2564 for (size_t i=0; i<N; i++) {
2565 const flat_binder_object* flat
2566 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2567 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002568 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002569 << " = " << flat->binder;
2570 }
2571 } else {
2572 to << "NULL";
2573 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002574
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002575 to << ")";
2576}
2577
2578void Parcel::releaseObjects()
2579{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002580 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002581 if (i == 0) {
2582 return;
2583 }
2584 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002585 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002586 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002587 while (i > 0) {
2588 i--;
2589 const flat_binder_object* flat
2590 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002591 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002592 }
2593}
2594
2595void Parcel::acquireObjects()
2596{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002597 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002598 if (i == 0) {
2599 return;
2600 }
2601 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002602 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002603 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002604 while (i > 0) {
2605 i--;
2606 const flat_binder_object* flat
2607 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002608 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002609 }
2610}
2611
2612void Parcel::freeData()
2613{
2614 freeDataNoInit();
2615 initState();
2616}
2617
2618void Parcel::freeDataNoInit()
2619{
2620 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002621 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002622 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002623 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2624 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002625 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002626 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002627 if (mData) {
2628 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002629 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002630 if (mDataCapacity <= gParcelGlobalAllocSize) {
2631 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2632 } else {
2633 gParcelGlobalAllocSize = 0;
2634 }
2635 if (gParcelGlobalAllocCount > 0) {
2636 gParcelGlobalAllocCount--;
2637 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002638 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002639 free(mData);
2640 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002641 if (mObjects) free(mObjects);
2642 }
2643}
2644
2645status_t Parcel::growData(size_t len)
2646{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002647 if (len > INT32_MAX) {
2648 // don't accept size_t values which may have come from an
2649 // inadvertent conversion from a negative int.
2650 return BAD_VALUE;
2651 }
2652
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002653 size_t newSize = ((mDataSize+len)*3)/2;
2654 return (newSize <= mDataSize)
2655 ? (status_t) NO_MEMORY
2656 : continueWrite(newSize);
2657}
2658
2659status_t Parcel::restartWrite(size_t desired)
2660{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002661 if (desired > INT32_MAX) {
2662 // don't accept size_t values which may have come from an
2663 // inadvertent conversion from a negative int.
2664 return BAD_VALUE;
2665 }
2666
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002667 if (mOwner) {
2668 freeData();
2669 return continueWrite(desired);
2670 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002671
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002672 uint8_t* data = (uint8_t*)realloc(mData, desired);
2673 if (!data && desired > mDataCapacity) {
2674 mError = NO_MEMORY;
2675 return NO_MEMORY;
2676 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002677
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002678 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002679
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002680 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002681 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002682 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002683 gParcelGlobalAllocSize += desired;
2684 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002685 if (!mData) {
2686 gParcelGlobalAllocCount++;
2687 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002688 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002689 mData = data;
2690 mDataCapacity = desired;
2691 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002692
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002693 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002694 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2695 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2696
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002697 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002698 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002699 mObjectsSize = mObjectsCapacity = 0;
2700 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002701 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002702 mHasFds = false;
2703 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002704 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002705
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002706 return NO_ERROR;
2707}
2708
2709status_t Parcel::continueWrite(size_t desired)
2710{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002711 if (desired > INT32_MAX) {
2712 // don't accept size_t values which may have come from an
2713 // inadvertent conversion from a negative int.
2714 return BAD_VALUE;
2715 }
2716
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002717 // If shrinking, first adjust for any objects that appear
2718 // after the new data size.
2719 size_t objectsSize = mObjectsSize;
2720 if (desired < mDataSize) {
2721 if (desired == 0) {
2722 objectsSize = 0;
2723 } else {
2724 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002725 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002726 break;
2727 objectsSize--;
2728 }
2729 }
2730 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002731
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002732 if (mOwner) {
2733 // If the size is going to zero, just release the owner's data.
2734 if (desired == 0) {
2735 freeData();
2736 return NO_ERROR;
2737 }
2738
2739 // If there is a different owner, we need to take
2740 // posession.
2741 uint8_t* data = (uint8_t*)malloc(desired);
2742 if (!data) {
2743 mError = NO_MEMORY;
2744 return NO_MEMORY;
2745 }
Yi Kong91635562018-06-07 14:38:36 -07002746 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002747
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002748 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002749 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002750 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002751 free(data);
2752
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002753 mError = NO_MEMORY;
2754 return NO_MEMORY;
2755 }
2756
2757 // Little hack to only acquire references on objects
2758 // we will be keeping.
2759 size_t oldObjectsSize = mObjectsSize;
2760 mObjectsSize = objectsSize;
2761 acquireObjects();
2762 mObjectsSize = oldObjectsSize;
2763 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002764
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002765 if (mData) {
2766 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2767 }
2768 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002769 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002770 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002771 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002772 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002773 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002774
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002775 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002776 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002777 gParcelGlobalAllocSize += desired;
2778 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002779 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002780
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002781 mData = data;
2782 mObjects = objects;
2783 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002784 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002785 mDataCapacity = desired;
2786 mObjectsSize = mObjectsCapacity = objectsSize;
2787 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002788 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002789
2790 } else if (mData) {
2791 if (objectsSize < mObjectsSize) {
2792 // Need to release refs on any objects we are dropping.
2793 const sp<ProcessState> proc(ProcessState::self());
2794 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2795 const flat_binder_object* flat
2796 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002797 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002798 // will need to rescan because we may have lopped off the only FDs
2799 mFdsKnown = false;
2800 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002801 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002802 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002803 binder_size_t* objects =
2804 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002805 if (objects) {
2806 mObjects = objects;
2807 }
2808 mObjectsSize = objectsSize;
2809 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002810 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002811 }
2812
2813 // We own the data, so we can just do a realloc().
2814 if (desired > mDataCapacity) {
2815 uint8_t* data = (uint8_t*)realloc(mData, desired);
2816 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002817 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2818 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002819 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002820 gParcelGlobalAllocSize += desired;
2821 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002822 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002823 mData = data;
2824 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002825 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002826 mError = NO_MEMORY;
2827 return NO_MEMORY;
2828 }
2829 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002830 if (mDataSize > desired) {
2831 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002832 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002833 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002834 if (mDataPos > desired) {
2835 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002836 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002837 }
2838 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002839
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002840 } else {
2841 // This is the first data. Easy!
2842 uint8_t* data = (uint8_t*)malloc(desired);
2843 if (!data) {
2844 mError = NO_MEMORY;
2845 return NO_MEMORY;
2846 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002847
Yi Kong91635562018-06-07 14:38:36 -07002848 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002849 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002850 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002851 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002852
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002853 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002854 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002855 gParcelGlobalAllocSize += desired;
2856 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002857 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002858
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002859 mData = data;
2860 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002861 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2862 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002863 mDataCapacity = desired;
2864 }
2865
2866 return NO_ERROR;
2867}
2868
2869void Parcel::initState()
2870{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002871 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002872 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002873 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002874 mDataSize = 0;
2875 mDataCapacity = 0;
2876 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002877 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2878 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002879 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002880 mObjectsSize = 0;
2881 mObjectsCapacity = 0;
2882 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002883 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002884 mHasFds = false;
2885 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002886 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002887 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002888 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002889
2890 // racing multiple init leads only to multiple identical write
2891 if (gMaxFds == 0) {
2892 struct rlimit result;
2893 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2894 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002895 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002896 } else {
2897 ALOGW("Unable to getrlimit: %s", strerror(errno));
2898 gMaxFds = 1024;
2899 }
2900 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002901}
2902
2903void Parcel::scanForFds() const
2904{
2905 bool hasFds = false;
2906 for (size_t i=0; i<mObjectsSize; i++) {
2907 const flat_binder_object* flat
2908 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002909 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002910 hasFds = true;
2911 break;
2912 }
2913 }
2914 mHasFds = hasFds;
2915 mFdsKnown = true;
2916}
2917
Dan Sandleraa5c2342015-04-10 10:08:45 -04002918size_t Parcel::getBlobAshmemSize() const
2919{
Adrian Roos6bb31142015-10-22 16:46:12 -07002920 // This used to return the size of all blobs that were written to ashmem, now we're returning
2921 // the ashmem currently referenced by this Parcel, which should be equivalent.
2922 // TODO: Remove method once ABI can be changed.
2923 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002924}
2925
Adrian Rooscbf37262015-10-22 16:12:53 -07002926size_t Parcel::getOpenAshmemSize() const
2927{
2928 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002929}
2930
2931// --- Parcel::Blob ---
2932
2933Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002934 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002935}
2936
2937Parcel::Blob::~Blob() {
2938 release();
2939}
2940
2941void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002942 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002943 ::munmap(mData, mSize);
2944 }
2945 clear();
2946}
2947
Jeff Brown13b16042014-11-11 16:44:25 -08002948void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2949 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002950 mData = data;
2951 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002952 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002953}
2954
2955void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002956 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002957 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002958 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002959 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002960}
2961
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002962}; // namespace android