blob: f779d6e446e12b1e57b077a772bdc37e388bb49e [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080023#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080028#include <sys/stat.h>
29#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070030#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080031#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070032
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/Binder.h>
34#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080035#include <binder/IPCThreadState.h>
36#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070037#include <binder/ProcessState.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080038#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070039#include <binder/TextOutput.h>
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -080040#include <binder/Value.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041
Mark Salyzynabed7f72016-01-27 08:02:48 -080042#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070043#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080044#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080046#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String8.h>
48#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049
Mathias Agopian208059f2009-05-18 15:08:03 -070050#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080051#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053#ifndef INT32_MAX
54#define INT32_MAX ((int32_t)(2147483647))
55#endif
56
57#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080058//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080059#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080060//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070061
62// ---------------------------------------------------------------------------
63
Nick Kralevichb6b14232015-04-02 09:36:02 -070064// This macro should never be used at runtime, as a too large value
65// of s could cause an integer overflow. Instead, you should always
66// use the wrapper function pad_size()
67#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
68
69static size_t pad_size(size_t s) {
70 if (s > (SIZE_T_MAX - 3)) {
71 abort();
72 }
73 return PAD_SIZE_UNSAFE(s);
74}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070076// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060077#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070078
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070079// XXX This can be made public if we want to provide
80// support for typed data.
81struct small_flat_data
82{
83 uint32_t type;
84 uint32_t data;
85};
86
87namespace android {
88
Dianne Hackborna4cff882014-11-13 17:07:40 -080089static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
90static size_t gParcelGlobalAllocSize = 0;
91static size_t gParcelGlobalAllocCount = 0;
92
Christopher Tatee4e0ae82016-03-24 16:03:44 -070093static size_t gMaxFds = 0;
94
Jeff Brown13b16042014-11-11 16:44:25 -080095// Maximum size of a blob to transfer in-place.
96static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
97
98enum {
99 BLOB_INPLACE = 0,
100 BLOB_ASHMEM_IMMUTABLE = 1,
101 BLOB_ASHMEM_MUTABLE = 2,
102};
103
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700105 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700107 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108 case BINDER_TYPE_BINDER:
109 if (obj.binder) {
110 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800111 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700112 }
113 return;
114 case BINDER_TYPE_WEAK_BINDER:
115 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800116 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700117 return;
118 case BINDER_TYPE_HANDLE: {
119 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700120 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
122 b->incStrong(who);
123 }
124 return;
125 }
126 case BINDER_TYPE_WEAK_HANDLE: {
127 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700128 if (b != nullptr) b.get_refs()->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700129 return;
130 }
131 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000132 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700133 // If we own an ashmem fd, keep track of how much memory it refers to.
134 int size = ashmem_get_size_region(obj.handle);
135 if (size > 0) {
136 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700137 }
138 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 return;
140 }
141 }
142
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700143 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144}
145
Adrian Roos6bb31142015-10-22 16:46:12 -0700146void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 const flat_binder_object& obj, const void* who)
148{
Yi Kong91635562018-06-07 14:38:36 -0700149 acquire_object(proc, obj, who, nullptr);
Adrian Roos6bb31142015-10-22 16:46:12 -0700150}
151
152static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700153 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700154{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700155 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700156 case BINDER_TYPE_BINDER:
157 if (obj.binder) {
158 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800159 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 }
161 return;
162 case BINDER_TYPE_WEAK_BINDER:
163 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800164 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165 return;
166 case BINDER_TYPE_HANDLE: {
167 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700168 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
170 b->decStrong(who);
171 }
172 return;
173 }
174 case BINDER_TYPE_WEAK_HANDLE: {
175 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700176 if (b != nullptr) b.get_refs()->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700177 return;
178 }
179 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800180 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000181 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700182 int size = ashmem_get_size_region(obj.handle);
183 if (size > 0) {
184 *outAshmemSize -= size;
Adrian Roos6bb31142015-10-22 16:46:12 -0700185 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700186 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800187
188 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700189 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700190 return;
191 }
192 }
193
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700194 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700195}
196
Adrian Roos6bb31142015-10-22 16:46:12 -0700197void release_object(const sp<ProcessState>& proc,
198 const flat_binder_object& obj, const void* who)
199{
Yi Kong91635562018-06-07 14:38:36 -0700200 release_object(proc, obj, who, nullptr);
Adrian Roos6bb31142015-10-22 16:46:12 -0700201}
202
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700203inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800204 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205{
206 return out->writeObject(flat, false);
207}
208
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800209status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700210 const sp<IBinder>& binder, Parcel* out)
211{
212 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700213
Martijn Coenen2b631742017-05-05 11:16:59 -0700214 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
215 /* minimum priority for all nodes is nice 0 */
216 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
217 } else {
218 /* minimum priority for all nodes is MAX_NICE(19) */
219 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
220 }
221
Yi Kong91635562018-06-07 14:38:36 -0700222 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800223 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700224 if (!local) {
225 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700226 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000227 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700228 }
229 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700230 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800231 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700232 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800233 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700234 } else {
Steven Morelandf0212002018-12-26 13:59:23 -0800235 if (local->isRequestingSid()) {
236 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
237 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700238 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800239 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
240 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700241 }
242 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700243 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800244 obj.binder = 0;
245 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700246 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700247
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 return finish_flatten_binder(binder, obj, out);
249}
250
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800251status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700252 const wp<IBinder>& binder, Parcel* out)
253{
254 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700255
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700256 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Yi Kong91635562018-06-07 14:38:36 -0700257 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258 sp<IBinder> real = binder.promote();
Yi Kong91635562018-06-07 14:38:36 -0700259 if (real != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700260 IBinder *local = real->localBinder();
261 if (!local) {
262 BpBinder *proxy = real->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700263 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000264 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700265 }
266 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700267 obj.hdr.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800268 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700269 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800270 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700271 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700272 obj.hdr.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800273 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
274 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700275 }
276 return finish_flatten_binder(real, obj, out);
277 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700278
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700279 // XXX How to deal? In order to flatten the given binder,
280 // we need to probe it for information, which requires a primary
281 // reference... but we don't have one.
282 //
283 // The OpenBinder implementation uses a dynamic_cast<> here,
284 // but we can't do that with the different reference counting
285 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000286 ALOGE("Unable to unflatten Binder weak reference!");
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700287 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800288 obj.binder = 0;
289 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700290 return finish_flatten_binder(nullptr, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700291
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700292 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700293 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800294 obj.binder = 0;
295 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700296 return finish_flatten_binder(nullptr, obj, out);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700297 }
298}
299
300inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800301 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
302 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700303{
304 return NO_ERROR;
305}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700306
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700307status_t unflatten_binder(const sp<ProcessState>& proc,
308 const Parcel& in, sp<IBinder>* out)
309{
310 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700311
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700312 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700313 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700314 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800315 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700316 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700317 case BINDER_TYPE_HANDLE:
318 *out = proc->getStrongProxyForHandle(flat->handle);
319 return finish_unflatten_binder(
320 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700321 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700322 }
323 return BAD_TYPE;
324}
325
326status_t unflatten_binder(const sp<ProcessState>& proc,
327 const Parcel& in, wp<IBinder>* out)
328{
329 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700330
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700331 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700332 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700333 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800334 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700335 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700336 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800337 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700338 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800339 reinterpret_cast<IBinder*>(flat->cookie),
340 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700341 } else {
Yi Kong91635562018-06-07 14:38:36 -0700342 *out = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700343 }
Yi Kong91635562018-06-07 14:38:36 -0700344 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700345 case BINDER_TYPE_HANDLE:
346 case BINDER_TYPE_WEAK_HANDLE:
347 *out = proc->getWeakProxyForHandle(flat->handle);
348 return finish_unflatten_binder(
349 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
350 }
351 }
352 return BAD_TYPE;
353}
354
355// ---------------------------------------------------------------------------
356
357Parcel::Parcel()
358{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800359 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700360 initState();
361}
362
363Parcel::~Parcel()
364{
365 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800366 LOG_ALLOC("Parcel %p: destroyed", this);
367}
368
369size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800370 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
371 size_t size = gParcelGlobalAllocSize;
372 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
373 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800374}
375
376size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800377 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
378 size_t count = gParcelGlobalAllocCount;
379 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
380 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700381}
382
383const uint8_t* Parcel::data() const
384{
385 return mData;
386}
387
388size_t Parcel::dataSize() const
389{
390 return (mDataSize > mDataPos ? mDataSize : mDataPos);
391}
392
393size_t Parcel::dataAvail() const
394{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700395 size_t result = dataSize() - dataPosition();
396 if (result > INT32_MAX) {
397 abort();
398 }
399 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700400}
401
402size_t Parcel::dataPosition() const
403{
404 return mDataPos;
405}
406
407size_t Parcel::dataCapacity() const
408{
409 return mDataCapacity;
410}
411
412status_t Parcel::setDataSize(size_t size)
413{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700414 if (size > INT32_MAX) {
415 // don't accept size_t values which may have come from an
416 // inadvertent conversion from a negative int.
417 return BAD_VALUE;
418 }
419
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700420 status_t err;
421 err = continueWrite(size);
422 if (err == NO_ERROR) {
423 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700424 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700425 }
426 return err;
427}
428
429void Parcel::setDataPosition(size_t pos) const
430{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700431 if (pos > INT32_MAX) {
432 // don't accept size_t values which may have come from an
433 // inadvertent conversion from a negative int.
434 abort();
435 }
436
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437 mDataPos = pos;
438 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800439 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700440}
441
442status_t Parcel::setDataCapacity(size_t size)
443{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700444 if (size > INT32_MAX) {
445 // don't accept size_t values which may have come from an
446 // inadvertent conversion from a negative int.
447 return BAD_VALUE;
448 }
449
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700450 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700451 return NO_ERROR;
452}
453
454status_t Parcel::setData(const uint8_t* buffer, size_t len)
455{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700456 if (len > INT32_MAX) {
457 // don't accept size_t values which may have come from an
458 // inadvertent conversion from a negative int.
459 return BAD_VALUE;
460 }
461
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700462 status_t err = restartWrite(len);
463 if (err == NO_ERROR) {
464 memcpy(const_cast<uint8_t*>(data()), buffer, len);
465 mDataSize = len;
466 mFdsKnown = false;
467 }
468 return err;
469}
470
Andreas Huber51faf462011-04-13 10:21:56 -0700471status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700472{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700474 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800475 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700476 size_t size = parcel->mObjectsSize;
477 int startPos = mDataPos;
478 int firstIndex = -1, lastIndex = -2;
479
480 if (len == 0) {
481 return NO_ERROR;
482 }
483
Nick Kralevichb6b14232015-04-02 09:36:02 -0700484 if (len > INT32_MAX) {
485 // don't accept size_t values which may have come from an
486 // inadvertent conversion from a negative int.
487 return BAD_VALUE;
488 }
489
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700490 // range checks against the source parcel size
491 if ((offset > parcel->mDataSize)
492 || (len > parcel->mDataSize)
493 || (offset + len > parcel->mDataSize)) {
494 return BAD_VALUE;
495 }
496
497 // Count objects in range
498 for (int i = 0; i < (int) size; i++) {
499 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700500 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700501 if (firstIndex == -1) {
502 firstIndex = i;
503 }
504 lastIndex = i;
505 }
506 }
507 int numObjects = lastIndex - firstIndex + 1;
508
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700509 if ((mDataSize+len) > mDataCapacity) {
510 // grow data
511 err = growData(len);
512 if (err != NO_ERROR) {
513 return err;
514 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700515 }
516
517 // append data
518 memcpy(mData + mDataPos, data + offset, len);
519 mDataPos += len;
520 mDataSize += len;
521
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400522 err = NO_ERROR;
523
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700524 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200525 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700526 // grow objects
527 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700528 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700529 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800530 binder_size_t *objects =
531 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700532 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700533 return NO_MEMORY;
534 }
535 mObjects = objects;
536 mObjectsCapacity = newSize;
537 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700538
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700539 // append and acquire objects
540 int idx = mObjectsSize;
541 for (int i = firstIndex; i <= lastIndex; i++) {
542 size_t off = objects[i] - offset + startPos;
543 mObjects[idx++] = off;
544 mObjectsSize++;
545
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700546 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700547 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700548 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700549
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700550 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700551 // If this is a file descriptor, we need to dup it so the
552 // new Parcel now owns its own fd, and can declare that we
553 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800554 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800555 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700556 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400557 if (!mAllowFds) {
558 err = FDS_NOT_ALLOWED;
559 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700560 }
561 }
562 }
563
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400564 return err;
565}
566
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700567int Parcel::compareData(const Parcel& other) {
568 size_t size = dataSize();
569 if (size != other.dataSize()) {
570 return size < other.dataSize() ? -1 : 1;
571 }
572 return memcmp(data(), other.data(), size);
573}
574
Jeff Brown13b16042014-11-11 16:44:25 -0800575bool Parcel::allowFds() const
576{
577 return mAllowFds;
578}
579
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700580bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400581{
582 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700583 if (!allowFds) {
584 mAllowFds = false;
585 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400586 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700587}
588
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700589void Parcel::restoreAllowFds(bool lastValue)
590{
591 mAllowFds = lastValue;
592}
593
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700594bool Parcel::hasFileDescriptors() const
595{
596 if (!mFdsKnown) {
597 scanForFds();
598 }
599 return mHasFds;
600}
601
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700602// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700603status_t Parcel::writeInterfaceToken(const String16& interface)
604{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000605 const IPCThreadState* threadState = IPCThreadState::self();
606 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
607 writeInt32(threadState->shouldPropagateWorkSource() ?
608 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700609 // currently the interface identification token is just its name as a string
610 return writeString16(interface);
611}
612
Mathias Agopian83c04462009-05-22 19:00:22 -0700613bool Parcel::checkInterface(IBinder* binder) const
614{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700615 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700616}
617
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700618bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700619 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100621 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700622 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700623 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700624 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700625 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700626 if ((threadState->getLastTransactionBinderFlags() &
627 IBinder::FLAG_ONEWAY) != 0) {
628 // For one-way calls, the callee is running entirely
629 // disconnected from the caller, so disable StrictMode entirely.
630 // Not only does disk/network usage not impact the caller, but
631 // there's no way to commuicate back any violations anyway.
632 threadState->setStrictModePolicy(0);
633 } else {
634 threadState->setStrictModePolicy(strictPolicy);
635 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100636 // WorkSource.
637 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000638 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100639 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700640 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700641 if (str == interface) {
642 return true;
643 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700644 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700645 String8(interface).string(), String8(str).string());
646 return false;
647 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700648}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700649
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800650const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700651{
652 return mObjects;
653}
654
655size_t Parcel::objectsCount() const
656{
657 return mObjectsSize;
658}
659
660status_t Parcel::errorCheck() const
661{
662 return mError;
663}
664
665void Parcel::setError(status_t err)
666{
667 mError = err;
668}
669
670status_t Parcel::finishWrite(size_t len)
671{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700672 if (len > INT32_MAX) {
673 // don't accept size_t values which may have come from an
674 // inadvertent conversion from a negative int.
675 return BAD_VALUE;
676 }
677
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700678 //printf("Finish write of %d\n", len);
679 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700680 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700681 if (mDataPos > mDataSize) {
682 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700683 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700684 }
685 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
686 return NO_ERROR;
687}
688
689status_t Parcel::writeUnpadded(const void* data, size_t len)
690{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700691 if (len > INT32_MAX) {
692 // don't accept size_t values which may have come from an
693 // inadvertent conversion from a negative int.
694 return BAD_VALUE;
695 }
696
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700697 size_t end = mDataPos + len;
698 if (end < mDataPos) {
699 // integer overflow
700 return BAD_VALUE;
701 }
702
703 if (end <= mDataCapacity) {
704restart_write:
705 memcpy(mData+mDataPos, data, len);
706 return finishWrite(len);
707 }
708
709 status_t err = growData(len);
710 if (err == NO_ERROR) goto restart_write;
711 return err;
712}
713
714status_t Parcel::write(const void* data, size_t len)
715{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700716 if (len > INT32_MAX) {
717 // don't accept size_t values which may have come from an
718 // inadvertent conversion from a negative int.
719 return BAD_VALUE;
720 }
721
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700722 void* const d = writeInplace(len);
723 if (d) {
724 memcpy(d, data, len);
725 return NO_ERROR;
726 }
727 return mError;
728}
729
730void* Parcel::writeInplace(size_t len)
731{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700732 if (len > INT32_MAX) {
733 // don't accept size_t values which may have come from an
734 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700735 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700736 }
737
738 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700739
740 // sanity check for integer overflow
741 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700742 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700743 }
744
745 if ((mDataPos+padded) <= mDataCapacity) {
746restart_write:
747 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
748 uint8_t* const data = mData+mDataPos;
749
750 // Need to pad at end?
751 if (padded != len) {
752#if BYTE_ORDER == BIG_ENDIAN
753 static const uint32_t mask[4] = {
754 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
755 };
756#endif
757#if BYTE_ORDER == LITTLE_ENDIAN
758 static const uint32_t mask[4] = {
759 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
760 };
761#endif
762 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
763 // *reinterpret_cast<void**>(data+padded-4));
764 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
765 }
766
767 finishWrite(padded);
768 return data;
769 }
770
771 status_t err = growData(padded);
772 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700773 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700774}
775
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800776status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
777 const uint8_t* strData = (uint8_t*)str.data();
778 const size_t strLen= str.length();
779 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100780 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800781 return BAD_VALUE;
782 }
783
784 status_t err = writeInt32(utf16Len);
785 if (err) {
786 return err;
787 }
788
789 // Allocate enough bytes to hold our converted string and its terminating NULL.
790 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
791 if (!dst) {
792 return NO_MEMORY;
793 }
794
Sergio Girof4607432016-07-21 14:46:35 +0100795 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800796
797 return NO_ERROR;
798}
799
800status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
801 if (!str) {
802 return writeInt32(-1);
803 }
804 return writeUtf8AsUtf16(*str);
805}
806
Casey Dahlin185d3442016-02-09 11:08:35 -0800807namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800808
Casey Dahlin185d3442016-02-09 11:08:35 -0800809template<typename T>
810status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700811{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700812 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700813 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700814 status = BAD_VALUE;
815 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700816 }
817
Casey Dahlin185d3442016-02-09 11:08:35 -0800818 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700819 if (status != OK) {
820 return status;
821 }
822
Casey Dahlin185d3442016-02-09 11:08:35 -0800823 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700824 if (!data) {
825 status = BAD_VALUE;
826 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700827 }
828
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700829 memcpy(data, val.data(), val.size());
830 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700831}
832
Casey Dahlin185d3442016-02-09 11:08:35 -0800833template<typename T>
834status_t writeByteVectorInternalPtr(Parcel* parcel,
835 const std::unique_ptr<std::vector<T>>& val)
836{
837 if (!val) {
838 return parcel->writeInt32(-1);
839 }
840
841 return writeByteVectorInternal(parcel, *val);
842}
843
844} // namespace
845
846status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
847 return writeByteVectorInternal(this, val);
848}
849
850status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
851{
852 return writeByteVectorInternalPtr(this, val);
853}
854
855status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
856 return writeByteVectorInternal(this, val);
857}
858
859status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
860{
861 return writeByteVectorInternalPtr(this, val);
862}
863
Casey Dahlin451ff582015-10-19 18:12:18 -0700864status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
865{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800866 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700867}
868
Casey Dahlinb9872622015-11-25 15:09:45 -0800869status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
870{
871 return writeNullableTypedVector(val, &Parcel::writeInt32);
872}
873
Casey Dahlin451ff582015-10-19 18:12:18 -0700874status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
875{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800876 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700877}
878
Casey Dahlinb9872622015-11-25 15:09:45 -0800879status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
880{
881 return writeNullableTypedVector(val, &Parcel::writeInt64);
882}
883
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800884status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
885{
886 return writeTypedVector(val, &Parcel::writeUint64);
887}
888
889status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
890{
891 return writeNullableTypedVector(val, &Parcel::writeUint64);
892}
893
Casey Dahlin451ff582015-10-19 18:12:18 -0700894status_t Parcel::writeFloatVector(const std::vector<float>& val)
895{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800896 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700897}
898
Casey Dahlinb9872622015-11-25 15:09:45 -0800899status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
900{
901 return writeNullableTypedVector(val, &Parcel::writeFloat);
902}
903
Casey Dahlin451ff582015-10-19 18:12:18 -0700904status_t Parcel::writeDoubleVector(const std::vector<double>& val)
905{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800906 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700907}
908
Casey Dahlinb9872622015-11-25 15:09:45 -0800909status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
910{
911 return writeNullableTypedVector(val, &Parcel::writeDouble);
912}
913
Casey Dahlin451ff582015-10-19 18:12:18 -0700914status_t Parcel::writeBoolVector(const std::vector<bool>& val)
915{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800916 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700917}
918
Casey Dahlinb9872622015-11-25 15:09:45 -0800919status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
920{
921 return writeNullableTypedVector(val, &Parcel::writeBool);
922}
923
Casey Dahlin451ff582015-10-19 18:12:18 -0700924status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
925{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800926 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700927}
928
Casey Dahlinb9872622015-11-25 15:09:45 -0800929status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
930{
931 return writeNullableTypedVector(val, &Parcel::writeChar);
932}
933
Casey Dahlin451ff582015-10-19 18:12:18 -0700934status_t Parcel::writeString16Vector(const std::vector<String16>& val)
935{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800936 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700937}
938
Casey Dahlinb9872622015-11-25 15:09:45 -0800939status_t Parcel::writeString16Vector(
940 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
941{
942 return writeNullableTypedVector(val, &Parcel::writeString16);
943}
944
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800945status_t Parcel::writeUtf8VectorAsUtf16Vector(
946 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
947 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
948}
949
950status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
951 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
952}
953
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700954status_t Parcel::writeInt32(int32_t val)
955{
Andreas Huber84a6d042009-08-17 13:33:27 -0700956 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700957}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800958
959status_t Parcel::writeUint32(uint32_t val)
960{
961 return writeAligned(val);
962}
963
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700964status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700965 if (len > INT32_MAX) {
966 // don't accept size_t values which may have come from an
967 // inadvertent conversion from a negative int.
968 return BAD_VALUE;
969 }
970
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700971 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700972 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700973 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700974 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700975 if (ret == NO_ERROR) {
976 ret = write(val, len * sizeof(*val));
977 }
978 return ret;
979}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700980status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700981 if (len > INT32_MAX) {
982 // don't accept size_t values which may have come from an
983 // inadvertent conversion from a negative int.
984 return BAD_VALUE;
985 }
986
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700987 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700988 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700989 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700990 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700991 if (ret == NO_ERROR) {
992 ret = write(val, len * sizeof(*val));
993 }
994 return ret;
995}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700996
Casey Dahlind6848f52015-10-15 15:44:59 -0700997status_t Parcel::writeBool(bool val)
998{
999 return writeInt32(int32_t(val));
1000}
1001
1002status_t Parcel::writeChar(char16_t val)
1003{
1004 return writeInt32(int32_t(val));
1005}
1006
1007status_t Parcel::writeByte(int8_t val)
1008{
1009 return writeInt32(int32_t(val));
1010}
1011
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001012status_t Parcel::writeInt64(int64_t val)
1013{
Andreas Huber84a6d042009-08-17 13:33:27 -07001014 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001015}
1016
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001017status_t Parcel::writeUint64(uint64_t val)
1018{
1019 return writeAligned(val);
1020}
1021
Serban Constantinescuf683e012013-11-05 16:53:55 +00001022status_t Parcel::writePointer(uintptr_t val)
1023{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001024 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001025}
1026
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001027status_t Parcel::writeFloat(float val)
1028{
Andreas Huber84a6d042009-08-17 13:33:27 -07001029 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001030}
1031
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001032#if defined(__mips__) && defined(__mips_hard_float)
1033
1034status_t Parcel::writeDouble(double val)
1035{
1036 union {
1037 double d;
1038 unsigned long long ll;
1039 } u;
1040 u.d = val;
1041 return writeAligned(u.ll);
1042}
1043
1044#else
1045
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001046status_t Parcel::writeDouble(double val)
1047{
Andreas Huber84a6d042009-08-17 13:33:27 -07001048 return writeAligned(val);
1049}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001050
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001051#endif
1052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001053status_t Parcel::writeCString(const char* str)
1054{
1055 return write(str, strlen(str)+1);
1056}
1057
1058status_t Parcel::writeString8(const String8& str)
1059{
1060 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001061 // only write string if its length is more than zero characters,
1062 // as readString8 will only read if the length field is non-zero.
1063 // this is slightly different from how writeString16 works.
1064 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001065 err = write(str.string(), str.bytes()+1);
1066 }
1067 return err;
1068}
1069
Casey Dahlinb9872622015-11-25 15:09:45 -08001070status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1071{
1072 if (!str) {
1073 return writeInt32(-1);
1074 }
1075
1076 return writeString16(*str);
1077}
1078
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001079status_t Parcel::writeString16(const String16& str)
1080{
1081 return writeString16(str.string(), str.size());
1082}
1083
1084status_t Parcel::writeString16(const char16_t* str, size_t len)
1085{
Yi Kong91635562018-06-07 14:38:36 -07001086 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001087
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001088 status_t err = writeInt32(len);
1089 if (err == NO_ERROR) {
1090 len *= sizeof(char16_t);
1091 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1092 if (data) {
1093 memcpy(data, str, len);
1094 *reinterpret_cast<char16_t*>(data+len) = 0;
1095 return NO_ERROR;
1096 }
1097 err = mError;
1098 }
1099 return err;
1100}
1101
1102status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1103{
1104 return flatten_binder(ProcessState::self(), val, this);
1105}
1106
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001107status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1108{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001109 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001110}
1111
Casey Dahlinb9872622015-11-25 15:09:45 -08001112status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1113{
1114 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1115}
1116
1117status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001118 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001119}
1120
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001121status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001122 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001123}
1124
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001125status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1126{
1127 return flatten_binder(ProcessState::self(), val, this);
1128}
1129
Casey Dahlinb9872622015-11-25 15:09:45 -08001130status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1131 if (!parcelable) {
1132 return writeInt32(0);
1133 }
1134
1135 return writeParcelable(*parcelable);
1136}
1137
Christopher Wiley97f048d2015-11-19 06:49:05 -08001138status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1139 status_t status = writeInt32(1); // parcelable is not null.
1140 if (status != OK) {
1141 return status;
1142 }
1143 return parcelable.writeToParcel(this);
1144}
1145
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001146status_t Parcel::writeValue(const binder::Value& value) {
1147 return value.writeToParcel(this);
1148}
1149
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001150status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001151{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001152 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001153 return BAD_TYPE;
1154
1155 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001156 err = writeInt32(handle->numFds);
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 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001160 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001162 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1163 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164
1165 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001166 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167 return err;
1168 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001169 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001170 return err;
1171}
1172
Jeff Brown93ff1f92011-11-04 19:01:44 -07001173status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001174{
1175 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001176 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001177 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001178 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001179 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001180 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001181 return writeObject(obj, true);
1182}
1183
1184status_t Parcel::writeDupFileDescriptor(int fd)
1185{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001186 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001187 if (dupFd < 0) {
1188 return -errno;
1189 }
1190 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001191 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001192 close(dupFd);
1193 }
1194 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001195}
1196
Dianne Hackborn1941a402016-08-29 12:30:43 -07001197status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1198{
1199 writeInt32(0);
1200 return writeFileDescriptor(fd, takeOwnership);
1201}
1202
Ryo Hashimotobf551892018-05-31 16:58:35 +09001203status_t Parcel::writeDupParcelFileDescriptor(int fd)
1204{
1205 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1206 if (dupFd < 0) {
1207 return -errno;
1208 }
1209 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1210 if (err != OK) {
1211 close(dupFd);
1212 }
1213 return err;
1214}
1215
Christopher Wiley2cf19952016-04-11 11:09:37 -07001216status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001217 return writeDupFileDescriptor(fd.get());
1218}
1219
Christopher Wiley2cf19952016-04-11 11:09:37 -07001220status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001221 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1222}
1223
Christopher Wiley2cf19952016-04-11 11:09:37 -07001224status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001225 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1226}
1227
Jeff Brown13b16042014-11-11 16:44:25 -08001228status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001229{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001230 if (len > INT32_MAX) {
1231 // don't accept size_t values which may have come from an
1232 // inadvertent conversion from a negative int.
1233 return BAD_VALUE;
1234 }
1235
Jeff Brown13b16042014-11-11 16:44:25 -08001236 status_t status;
1237 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001238 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001239 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001240 if (status) return status;
1241
1242 void* ptr = writeInplace(len);
1243 if (!ptr) return NO_MEMORY;
1244
Jeff Brown13b16042014-11-11 16:44:25 -08001245 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001246 return NO_ERROR;
1247 }
1248
Steve Block6807e592011-10-20 11:56:00 +01001249 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001250 int fd = ashmem_create_region("Parcel Blob", len);
1251 if (fd < 0) return NO_MEMORY;
1252
1253 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1254 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001255 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001256 } else {
Yi Kong91635562018-06-07 14:38:36 -07001257 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001258 if (ptr == MAP_FAILED) {
1259 status = -errno;
1260 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001261 if (!mutableCopy) {
1262 result = ashmem_set_prot_region(fd, PROT_READ);
1263 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001264 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001265 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001266 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001267 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001268 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001269 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001270 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001271 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001272 return NO_ERROR;
1273 }
1274 }
1275 }
1276 }
1277 ::munmap(ptr, len);
1278 }
1279 ::close(fd);
1280 return status;
1281}
1282
Jeff Brown13b16042014-11-11 16:44:25 -08001283status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1284{
1285 // Must match up with what's done in writeBlob.
1286 if (!mAllowFds) return FDS_NOT_ALLOWED;
1287 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1288 if (status) return status;
1289 return writeDupFileDescriptor(fd);
1290}
1291
Mathias Agopiane1424282013-07-29 21:24:40 -07001292status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001293{
1294 status_t err;
1295
1296 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001297 const size_t len = val.getFlattenedSize();
1298 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001299
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001300 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001301 // don't accept size_t values which may have come from an
1302 // inadvertent conversion from a negative int.
1303 return BAD_VALUE;
1304 }
1305
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001306 err = this->writeInt32(len);
1307 if (err) return err;
1308
1309 err = this->writeInt32(fd_count);
1310 if (err) return err;
1311
1312 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001313 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001314 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001315 return BAD_VALUE;
1316
Yi Kong91635562018-06-07 14:38:36 -07001317 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001318 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001319 fds = new (std::nothrow) int[fd_count];
1320 if (fds == nullptr) {
1321 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1322 return BAD_VALUE;
1323 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001324 }
1325
1326 err = val.flatten(buf, len, fds, fd_count);
1327 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1328 err = this->writeDupFileDescriptor( fds[i] );
1329 }
1330
1331 if (fd_count) {
1332 delete [] fds;
1333 }
1334
1335 return err;
1336}
1337
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001338status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1339{
1340 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1341 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1342 if (enoughData && enoughObjects) {
1343restart_write:
1344 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001345
Christopher Tate98e67d32015-06-03 18:44:15 -07001346 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001347 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001348 if (!mAllowFds) {
1349 // fail before modifying our object index
1350 return FDS_NOT_ALLOWED;
1351 }
1352 mHasFds = mFdsKnown = true;
1353 }
1354
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001355 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001356 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001357 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001358 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001359 mObjectsSize++;
1360 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001361
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001362 return finishWrite(sizeof(flat_binder_object));
1363 }
1364
1365 if (!enoughData) {
1366 const status_t err = growData(sizeof(val));
1367 if (err != NO_ERROR) return err;
1368 }
1369 if (!enoughObjects) {
1370 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001371 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001372 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001373 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001374 mObjects = objects;
1375 mObjectsCapacity = newSize;
1376 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001377
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001378 goto restart_write;
1379}
1380
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001381status_t Parcel::writeNoException()
1382{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001383 binder::Status status;
1384 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001385}
1386
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001387status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1388{
1389 using ::std::map;
1390 using ::android::binder::Value;
1391 using ::android::binder::Map;
1392
1393 Map::const_iterator iter;
1394 status_t ret;
1395
1396 ret = writeInt32(map_in.size());
1397
1398 if (ret != NO_ERROR) {
1399 return ret;
1400 }
1401
1402 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1403 ret = writeValue(Value(iter->first));
1404 if (ret != NO_ERROR) {
1405 return ret;
1406 }
1407
1408 ret = writeValue(iter->second);
1409 if (ret != NO_ERROR) {
1410 return ret;
1411 }
1412 }
1413
1414 return ret;
1415}
1416
1417status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1418{
Yi Kong91635562018-06-07 14:38:36 -07001419 if (map == nullptr) {
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001420 return writeInt32(-1);
1421 }
1422
1423 return writeMap(*map.get());
1424}
1425
1426status_t Parcel::readMap(::android::binder::Map* map_out)const
1427{
1428 using ::std::map;
1429 using ::android::String16;
1430 using ::android::String8;
1431 using ::android::binder::Value;
1432 using ::android::binder::Map;
1433
1434 status_t ret = NO_ERROR;
1435 int32_t count;
1436
1437 ret = readInt32(&count);
1438 if (ret != NO_ERROR) {
1439 return ret;
1440 }
1441
1442 if (count < 0) {
1443 ALOGE("readMap: Unexpected count: %d", count);
1444 return (count == -1)
1445 ? UNEXPECTED_NULL
1446 : BAD_VALUE;
1447 }
1448
1449 map_out->clear();
1450
1451 while (count--) {
1452 Map::key_type key;
1453 Value value;
1454
1455 ret = readValue(&value);
1456 if (ret != NO_ERROR) {
1457 return ret;
1458 }
1459
1460 if (!value.getString(&key)) {
1461 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1462 return BAD_VALUE;
1463 }
1464
1465 ret = readValue(&value);
1466 if (ret != NO_ERROR) {
1467 return ret;
1468 }
1469
1470 (*map_out)[key] = value;
1471 }
1472
1473 return ret;
1474}
1475
1476status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1477{
1478 const size_t start = dataPosition();
1479 int32_t count;
1480 status_t status = readInt32(&count);
1481 map->reset();
1482
1483 if (status != OK || count == -1) {
1484 return status;
1485 }
1486
1487 setDataPosition(start);
1488 map->reset(new binder::Map());
1489
1490 status = readMap(map->get());
1491
1492 if (status != OK) {
1493 map->reset();
1494 }
1495
1496 return status;
1497}
1498
1499
1500
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001501void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001502{
1503 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1504}
1505
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001506status_t Parcel::validateReadData(size_t upperBound) const
1507{
1508 // Don't allow non-object reads on object data
1509 if (mObjectsSorted || mObjectsSize <= 1) {
1510data_sorted:
1511 // Expect to check only against the next object
1512 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1513 // For some reason the current read position is greater than the next object
1514 // hint. Iterate until we find the right object
1515 size_t nextObject = mNextObjectHint;
1516 do {
1517 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1518 // Requested info overlaps with an object
1519 ALOGE("Attempt to read from protected data in Parcel %p", this);
1520 return PERMISSION_DENIED;
1521 }
1522 nextObject++;
1523 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1524 mNextObjectHint = nextObject;
1525 }
1526 return NO_ERROR;
1527 }
1528 // Quickly determine if mObjects is sorted.
1529 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1530 binder_size_t* prevObj = currObj;
1531 while (currObj > mObjects) {
1532 prevObj--;
1533 if(*prevObj > *currObj) {
1534 goto data_unsorted;
1535 }
1536 currObj--;
1537 }
1538 mObjectsSorted = true;
1539 goto data_sorted;
1540
1541data_unsorted:
1542 // Insertion Sort mObjects
1543 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1544 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1545 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1546 binder_size_t temp = *iter0;
1547 binder_size_t* iter1 = iter0 - 1;
1548 while (iter1 >= mObjects && *iter1 > temp) {
1549 *(iter1 + 1) = *iter1;
1550 iter1--;
1551 }
1552 *(iter1 + 1) = temp;
1553 }
1554 mNextObjectHint = 0;
1555 mObjectsSorted = true;
1556 goto data_sorted;
1557}
1558
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001559status_t Parcel::read(void* outData, size_t len) const
1560{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001561 if (len > INT32_MAX) {
1562 // don't accept size_t values which may have come from an
1563 // inadvertent conversion from a negative int.
1564 return BAD_VALUE;
1565 }
1566
1567 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1568 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001569 if (mObjectsSize > 0) {
1570 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001571 if(err != NO_ERROR) {
1572 // Still increment the data position by the expected length
1573 mDataPos += pad_size(len);
1574 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1575 return err;
1576 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001577 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001578 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001579 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001580 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001581 return NO_ERROR;
1582 }
1583 return NOT_ENOUGH_DATA;
1584}
1585
1586const void* Parcel::readInplace(size_t len) const
1587{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001588 if (len > INT32_MAX) {
1589 // don't accept size_t values which may have come from an
1590 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001591 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001592 }
1593
1594 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1595 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001596 if (mObjectsSize > 0) {
1597 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001598 if(err != NO_ERROR) {
1599 // Still increment the data position by the expected length
1600 mDataPos += pad_size(len);
1601 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001602 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001603 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001604 }
1605
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001606 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001607 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001608 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001609 return data;
1610 }
Yi Kong91635562018-06-07 14:38:36 -07001611 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001612}
1613
Andreas Huber84a6d042009-08-17 13:33:27 -07001614template<class T>
1615status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001616 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001617
1618 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001619 if (mObjectsSize > 0) {
1620 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001621 if(err != NO_ERROR) {
1622 // Still increment the data position by the expected length
1623 mDataPos += sizeof(T);
1624 return err;
1625 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001626 }
1627
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001628 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001629 mDataPos += sizeof(T);
1630 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001631 return NO_ERROR;
1632 } else {
1633 return NOT_ENOUGH_DATA;
1634 }
1635}
1636
Andreas Huber84a6d042009-08-17 13:33:27 -07001637template<class T>
1638T Parcel::readAligned() const {
1639 T result;
1640 if (readAligned(&result) != NO_ERROR) {
1641 result = 0;
1642 }
1643
1644 return result;
1645}
1646
1647template<class T>
1648status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001649 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001650
1651 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1652restart_write:
1653 *reinterpret_cast<T*>(mData+mDataPos) = val;
1654 return finishWrite(sizeof(val));
1655 }
1656
1657 status_t err = growData(sizeof(val));
1658 if (err == NO_ERROR) goto restart_write;
1659 return err;
1660}
1661
Casey Dahlin185d3442016-02-09 11:08:35 -08001662namespace {
1663
1664template<typename T>
1665status_t readByteVectorInternal(const Parcel* parcel,
1666 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001667 val->clear();
1668
1669 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001670 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001671
1672 if (status != OK) {
1673 return status;
1674 }
1675
Christopher Wiley4db672d2015-11-10 09:44:30 -08001676 if (size < 0) {
1677 status = UNEXPECTED_NULL;
1678 return status;
1679 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001680 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001681 status = BAD_VALUE;
1682 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001683 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001684
Paul Lietar433e87b2016-09-16 10:39:32 -07001685 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001686 if (!data) {
1687 status = BAD_VALUE;
1688 return status;
1689 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001690 val->reserve(size);
1691 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001692
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001693 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001694}
1695
Casey Dahlin185d3442016-02-09 11:08:35 -08001696template<typename T>
1697status_t readByteVectorInternalPtr(
1698 const Parcel* parcel,
1699 std::unique_ptr<std::vector<T>>* val) {
1700 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001701 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001702 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001703 val->reset();
1704
1705 if (status != OK || size < 0) {
1706 return status;
1707 }
1708
Casey Dahlin185d3442016-02-09 11:08:35 -08001709 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001710 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001711
Casey Dahlin185d3442016-02-09 11:08:35 -08001712 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001713
1714 if (status != OK) {
1715 val->reset();
1716 }
1717
1718 return status;
1719}
1720
Casey Dahlin185d3442016-02-09 11:08:35 -08001721} // namespace
1722
1723status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1724 return readByteVectorInternal(this, val);
1725}
1726
1727status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1728 return readByteVectorInternal(this, val);
1729}
1730
1731status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1732 return readByteVectorInternalPtr(this, val);
1733}
1734
1735status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1736 return readByteVectorInternalPtr(this, val);
1737}
1738
Casey Dahlinb9872622015-11-25 15:09:45 -08001739status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1740 return readNullableTypedVector(val, &Parcel::readInt32);
1741}
1742
Casey Dahlin451ff582015-10-19 18:12:18 -07001743status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001744 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001745}
1746
Casey Dahlinb9872622015-11-25 15:09:45 -08001747status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1748 return readNullableTypedVector(val, &Parcel::readInt64);
1749}
1750
Casey Dahlin451ff582015-10-19 18:12:18 -07001751status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001752 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001753}
1754
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001755status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1756 return readNullableTypedVector(val, &Parcel::readUint64);
1757}
1758
1759status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1760 return readTypedVector(val, &Parcel::readUint64);
1761}
1762
Casey Dahlinb9872622015-11-25 15:09:45 -08001763status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1764 return readNullableTypedVector(val, &Parcel::readFloat);
1765}
1766
Casey Dahlin451ff582015-10-19 18:12:18 -07001767status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001768 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001769}
1770
Casey Dahlinb9872622015-11-25 15:09:45 -08001771status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1772 return readNullableTypedVector(val, &Parcel::readDouble);
1773}
1774
Casey Dahlin451ff582015-10-19 18:12:18 -07001775status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001776 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001777}
1778
Casey Dahlinb9872622015-11-25 15:09:45 -08001779status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1780 const int32_t start = dataPosition();
1781 int32_t size;
1782 status_t status = readInt32(&size);
1783 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001784
Casey Dahlinb9872622015-11-25 15:09:45 -08001785 if (status != OK || size < 0) {
1786 return status;
1787 }
1788
1789 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001790 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001791
1792 status = readBoolVector(val->get());
1793
1794 if (status != OK) {
1795 val->reset();
1796 }
1797
1798 return status;
1799}
1800
1801status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001802 int32_t size;
1803 status_t status = readInt32(&size);
1804
1805 if (status != OK) {
1806 return status;
1807 }
1808
1809 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001810 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001811 }
1812
1813 val->resize(size);
1814
1815 /* C++ bool handling means a vector of bools isn't necessarily addressable
1816 * (we might use individual bits)
1817 */
Christopher Wiley97887982015-10-27 16:33:47 -07001818 bool data;
1819 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001820 status = readBool(&data);
1821 (*val)[i] = data;
1822
1823 if (status != OK) {
1824 return status;
1825 }
1826 }
1827
1828 return OK;
1829}
1830
Casey Dahlinb9872622015-11-25 15:09:45 -08001831status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1832 return readNullableTypedVector(val, &Parcel::readChar);
1833}
1834
Casey Dahlin451ff582015-10-19 18:12:18 -07001835status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001836 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001837}
1838
Casey Dahlinb9872622015-11-25 15:09:45 -08001839status_t Parcel::readString16Vector(
1840 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1841 return readNullableTypedVector(val, &Parcel::readString16);
1842}
1843
Casey Dahlin451ff582015-10-19 18:12:18 -07001844status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001845 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001846}
1847
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001848status_t Parcel::readUtf8VectorFromUtf16Vector(
1849 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1850 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1851}
1852
1853status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1854 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1855}
Casey Dahlin451ff582015-10-19 18:12:18 -07001856
Andreas Huber84a6d042009-08-17 13:33:27 -07001857status_t Parcel::readInt32(int32_t *pArg) const
1858{
1859 return readAligned(pArg);
1860}
1861
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001862int32_t Parcel::readInt32() const
1863{
Andreas Huber84a6d042009-08-17 13:33:27 -07001864 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001865}
1866
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001867status_t Parcel::readUint32(uint32_t *pArg) const
1868{
1869 return readAligned(pArg);
1870}
1871
1872uint32_t Parcel::readUint32() const
1873{
1874 return readAligned<uint32_t>();
1875}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001876
1877status_t Parcel::readInt64(int64_t *pArg) const
1878{
Andreas Huber84a6d042009-08-17 13:33:27 -07001879 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001880}
1881
1882
1883int64_t Parcel::readInt64() const
1884{
Andreas Huber84a6d042009-08-17 13:33:27 -07001885 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001886}
1887
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001888status_t Parcel::readUint64(uint64_t *pArg) const
1889{
1890 return readAligned(pArg);
1891}
1892
1893uint64_t Parcel::readUint64() const
1894{
1895 return readAligned<uint64_t>();
1896}
1897
Serban Constantinescuf683e012013-11-05 16:53:55 +00001898status_t Parcel::readPointer(uintptr_t *pArg) const
1899{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001900 status_t ret;
1901 binder_uintptr_t ptr;
1902 ret = readAligned(&ptr);
1903 if (!ret)
1904 *pArg = ptr;
1905 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001906}
1907
1908uintptr_t Parcel::readPointer() const
1909{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001910 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001911}
1912
1913
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001914status_t Parcel::readFloat(float *pArg) const
1915{
Andreas Huber84a6d042009-08-17 13:33:27 -07001916 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001917}
1918
1919
1920float Parcel::readFloat() const
1921{
Andreas Huber84a6d042009-08-17 13:33:27 -07001922 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001923}
1924
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001925#if defined(__mips__) && defined(__mips_hard_float)
1926
1927status_t Parcel::readDouble(double *pArg) const
1928{
1929 union {
1930 double d;
1931 unsigned long long ll;
1932 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001933 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001934 status_t status;
1935 status = readAligned(&u.ll);
1936 *pArg = u.d;
1937 return status;
1938}
1939
1940double Parcel::readDouble() const
1941{
1942 union {
1943 double d;
1944 unsigned long long ll;
1945 } u;
1946 u.ll = readAligned<unsigned long long>();
1947 return u.d;
1948}
1949
1950#else
1951
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001952status_t Parcel::readDouble(double *pArg) const
1953{
Andreas Huber84a6d042009-08-17 13:33:27 -07001954 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001955}
1956
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001957double Parcel::readDouble() const
1958{
Andreas Huber84a6d042009-08-17 13:33:27 -07001959 return readAligned<double>();
1960}
1961
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001962#endif
1963
Andreas Huber84a6d042009-08-17 13:33:27 -07001964status_t Parcel::readIntPtr(intptr_t *pArg) const
1965{
1966 return readAligned(pArg);
1967}
1968
1969
1970intptr_t Parcel::readIntPtr() const
1971{
1972 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001973}
1974
Casey Dahlind6848f52015-10-15 15:44:59 -07001975status_t Parcel::readBool(bool *pArg) const
1976{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001977 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001978 status_t ret = readInt32(&tmp);
1979 *pArg = (tmp != 0);
1980 return ret;
1981}
1982
1983bool Parcel::readBool() const
1984{
1985 return readInt32() != 0;
1986}
1987
1988status_t Parcel::readChar(char16_t *pArg) const
1989{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001990 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001991 status_t ret = readInt32(&tmp);
1992 *pArg = char16_t(tmp);
1993 return ret;
1994}
1995
1996char16_t Parcel::readChar() const
1997{
1998 return char16_t(readInt32());
1999}
2000
2001status_t Parcel::readByte(int8_t *pArg) const
2002{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002003 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002004 status_t ret = readInt32(&tmp);
2005 *pArg = int8_t(tmp);
2006 return ret;
2007}
2008
2009int8_t Parcel::readByte() const
2010{
2011 return int8_t(readInt32());
2012}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002013
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002014status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2015 size_t utf16Size = 0;
2016 const char16_t* src = readString16Inplace(&utf16Size);
2017 if (!src) {
2018 return UNEXPECTED_NULL;
2019 }
2020
2021 // Save ourselves the trouble, we're done.
2022 if (utf16Size == 0u) {
2023 str->clear();
2024 return NO_ERROR;
2025 }
2026
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002027 // Allow for closing '\0'
2028 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2029 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002030 return BAD_VALUE;
2031 }
2032 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002033 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002034 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002035 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2036 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002037 return NO_ERROR;
2038}
2039
2040status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2041 const int32_t start = dataPosition();
2042 int32_t size;
2043 status_t status = readInt32(&size);
2044 str->reset();
2045
2046 if (status != OK || size < 0) {
2047 return status;
2048 }
2049
2050 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002051 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002052 return readUtf8FromUtf16(str->get());
2053}
2054
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002055const char* Parcel::readCString() const
2056{
2057 const size_t avail = mDataSize-mDataPos;
2058 if (avail > 0) {
2059 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2060 // is the string's trailing NUL within the parcel's valid bounds?
2061 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2062 if (eos) {
2063 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002064 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002065 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002066 return str;
2067 }
2068 }
Yi Kong91635562018-06-07 14:38:36 -07002069 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002070}
2071
2072String8 Parcel::readString8() const
2073{
Roshan Pius87b64d22016-07-18 12:51:02 -07002074 String8 retString;
2075 status_t status = readString8(&retString);
2076 if (status != OK) {
2077 // We don't care about errors here, so just return an empty string.
2078 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002079 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002080 return retString;
2081}
2082
2083status_t Parcel::readString8(String8* pArg) const
2084{
2085 int32_t size;
2086 status_t status = readInt32(&size);
2087 if (status != OK) {
2088 return status;
2089 }
2090 // watch for potential int overflow from size+1
2091 if (size < 0 || size >= INT32_MAX) {
2092 return BAD_VALUE;
2093 }
2094 // |writeString8| writes nothing for empty string.
2095 if (size == 0) {
2096 *pArg = String8();
2097 return OK;
2098 }
2099 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07002100 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002101 return BAD_VALUE;
2102 }
2103 pArg->setTo(str, size);
2104 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002105}
2106
2107String16 Parcel::readString16() const
2108{
2109 size_t len;
2110 const char16_t* str = readString16Inplace(&len);
2111 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002112 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002113 return String16();
2114}
2115
Casey Dahlinb9872622015-11-25 15:09:45 -08002116status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2117{
2118 const int32_t start = dataPosition();
2119 int32_t size;
2120 status_t status = readInt32(&size);
2121 pArg->reset();
2122
2123 if (status != OK || size < 0) {
2124 return status;
2125 }
2126
2127 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002128 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002129
2130 status = readString16(pArg->get());
2131
2132 if (status != OK) {
2133 pArg->reset();
2134 }
2135
2136 return status;
2137}
2138
Casey Dahlin451ff582015-10-19 18:12:18 -07002139status_t Parcel::readString16(String16* pArg) const
2140{
2141 size_t len;
2142 const char16_t* str = readString16Inplace(&len);
2143 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002144 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002145 return 0;
2146 } else {
2147 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002148 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002149 }
2150}
2151
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002152const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2153{
2154 int32_t size = readInt32();
2155 // watch for potential int overflow from size+1
2156 if (size >= 0 && size < INT32_MAX) {
2157 *outLen = size;
2158 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002159 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002160 return str;
2161 }
2162 }
2163 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002164 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002165}
2166
Casey Dahlinf0c13772015-10-27 18:33:56 -07002167status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2168{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002169 status_t status = readNullableStrongBinder(val);
2170 if (status == OK && !val->get()) {
2171 status = UNEXPECTED_NULL;
2172 }
2173 return status;
2174}
2175
2176status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2177{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002178 return unflatten_binder(ProcessState::self(), *this, val);
2179}
2180
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002181sp<IBinder> Parcel::readStrongBinder() const
2182{
2183 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002184 // Note that a lot of code in Android reads binders by hand with this
2185 // method, and that code has historically been ok with getting nullptr
2186 // back (while ignoring error codes).
2187 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002188 return val;
2189}
2190
2191wp<IBinder> Parcel::readWeakBinder() const
2192{
2193 wp<IBinder> val;
2194 unflatten_binder(ProcessState::self(), *this, &val);
2195 return val;
2196}
2197
Christopher Wiley97f048d2015-11-19 06:49:05 -08002198status_t Parcel::readParcelable(Parcelable* parcelable) const {
2199 int32_t have_parcelable = 0;
2200 status_t status = readInt32(&have_parcelable);
2201 if (status != OK) {
2202 return status;
2203 }
2204 if (!have_parcelable) {
2205 return UNEXPECTED_NULL;
2206 }
2207 return parcelable->readFromParcel(this);
2208}
2209
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002210status_t Parcel::readValue(binder::Value* value) const {
2211 return value->readFromParcel(this);
2212}
2213
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002214int32_t Parcel::readExceptionCode() const
2215{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002216 binder::Status status;
2217 status.readFromParcel(*this);
2218 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002219}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002220
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002221native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002222{
2223 int numFds, numInts;
2224 status_t err;
2225 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002226 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002227 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002228 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002229
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002230 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002231 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002232 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002233 }
2234
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002235 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002236 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002237 if (h->data[i] < 0) {
2238 for (int j = 0; j < i; j++) {
2239 close(h->data[j]);
2240 }
2241 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002242 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002243 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002244 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002245 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002246 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002247 native_handle_close(h);
2248 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002249 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002250 }
2251 return h;
2252}
2253
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002254int Parcel::readFileDescriptor() const
2255{
2256 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002257
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002258 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002259 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002260 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002261
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002262 return BAD_TYPE;
2263}
2264
Dianne Hackborn1941a402016-08-29 12:30:43 -07002265int Parcel::readParcelFileDescriptor() const
2266{
2267 int32_t hasComm = readInt32();
2268 int fd = readFileDescriptor();
2269 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002270 // detach (owned by the binder driver)
2271 int comm = readFileDescriptor();
2272
2273 // warning: this must be kept in sync with:
2274 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2275 enum ParcelFileDescriptorStatus {
2276 DETACHED = 2,
2277 };
2278
2279#if BYTE_ORDER == BIG_ENDIAN
2280 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2281#endif
2282#if BYTE_ORDER == LITTLE_ENDIAN
2283 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2284#endif
2285
2286 ssize_t written = TEMP_FAILURE_RETRY(
2287 ::write(comm, &message, sizeof(message)));
2288
2289 if (written == -1 || written != sizeof(message)) {
2290 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2291 written, strerror(errno));
2292 return BAD_TYPE;
2293 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002294 }
2295 return fd;
2296}
2297
Christopher Wiley2cf19952016-04-11 11:09:37 -07002298status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002299{
2300 int got = readFileDescriptor();
2301
2302 if (got == BAD_TYPE) {
2303 return BAD_TYPE;
2304 }
2305
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002306 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002307
2308 if (val->get() < 0) {
2309 return BAD_VALUE;
2310 }
2311
2312 return OK;
2313}
2314
Ryo Hashimotobf551892018-05-31 16:58:35 +09002315status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2316{
2317 int got = readParcelFileDescriptor();
2318
2319 if (got == BAD_TYPE) {
2320 return BAD_TYPE;
2321 }
2322
2323 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2324
2325 if (val->get() < 0) {
2326 return BAD_VALUE;
2327 }
2328
2329 return OK;
2330}
Casey Dahlin06673e32015-11-23 13:24:23 -08002331
Christopher Wiley2cf19952016-04-11 11:09:37 -07002332status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002333 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2334}
2335
Christopher Wiley2cf19952016-04-11 11:09:37 -07002336status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002337 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2338}
2339
Jeff Brown5707dbf2011-09-23 21:17:56 -07002340status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2341{
Jeff Brown13b16042014-11-11 16:44:25 -08002342 int32_t blobType;
2343 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002344 if (status) return status;
2345
Jeff Brown13b16042014-11-11 16:44:25 -08002346 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002347 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002348 const void* ptr = readInplace(len);
2349 if (!ptr) return BAD_VALUE;
2350
Jeff Brown13b16042014-11-11 16:44:25 -08002351 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002352 return NO_ERROR;
2353 }
2354
Steve Block6807e592011-10-20 11:56:00 +01002355 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002356 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002357 int fd = readFileDescriptor();
2358 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2359
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002360 if (!ashmem_valid(fd)) {
2361 ALOGE("invalid fd");
2362 return BAD_VALUE;
2363 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002364 int size = ashmem_get_size_region(fd);
2365 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002366 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002367 return BAD_VALUE;
2368 }
Yi Kong91635562018-06-07 14:38:36 -07002369 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002370 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002371 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002372
Jeff Brown13b16042014-11-11 16:44:25 -08002373 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002374 return NO_ERROR;
2375}
2376
Mathias Agopiane1424282013-07-29 21:24:40 -07002377status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002378{
2379 // size
2380 const size_t len = this->readInt32();
2381 const size_t fd_count = this->readInt32();
2382
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002383 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002384 // don't accept size_t values which may have come from an
2385 // inadvertent conversion from a negative int.
2386 return BAD_VALUE;
2387 }
2388
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002389 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002390 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002391 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002392 return BAD_VALUE;
2393
Yi Kong91635562018-06-07 14:38:36 -07002394 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002395 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002396 fds = new (std::nothrow) int[fd_count];
2397 if (fds == nullptr) {
2398 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2399 return BAD_VALUE;
2400 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002401 }
2402
2403 status_t err = NO_ERROR;
2404 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002405 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002406 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002407 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002408 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 -07002409 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2410 // Close all the file descriptors that were dup-ed.
2411 for (size_t j=0; j<i ;j++) {
2412 close(fds[j]);
2413 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002414 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002415 }
2416
2417 if (err == NO_ERROR) {
2418 err = val.unflatten(buf, len, fds, fd_count);
2419 }
2420
2421 if (fd_count) {
2422 delete [] fds;
2423 }
2424
2425 return err;
2426}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2428{
2429 const size_t DPOS = mDataPos;
2430 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2431 const flat_binder_object* obj
2432 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2433 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002434 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002435 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002436 // the object list, so we don't want to check for it when
2437 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002438 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002439 return obj;
2440 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002441
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002442 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002443 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002444 const size_t N = mObjectsSize;
2445 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002446
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002447 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002448 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002449 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002450
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002451 // Start at the current hint position, looking for an object at
2452 // the current data position.
2453 if (opos < N) {
2454 while (opos < (N-1) && OBJS[opos] < DPOS) {
2455 opos++;
2456 }
2457 } else {
2458 opos = N-1;
2459 }
2460 if (OBJS[opos] == DPOS) {
2461 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002462 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002463 this, DPOS, opos);
2464 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002465 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002466 return obj;
2467 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002468
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002469 // Look backwards for it...
2470 while (opos > 0 && OBJS[opos] > DPOS) {
2471 opos--;
2472 }
2473 if (OBJS[opos] == DPOS) {
2474 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002475 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002476 this, DPOS, opos);
2477 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002478 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479 return obj;
2480 }
2481 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002482 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 -07002483 this, DPOS);
2484 }
Yi Kong91635562018-06-07 14:38:36 -07002485 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002486}
2487
2488void Parcel::closeFileDescriptors()
2489{
2490 size_t i = mObjectsSize;
2491 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002492 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002493 }
2494 while (i > 0) {
2495 i--;
2496 const flat_binder_object* flat
2497 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002498 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002499 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002500 close(flat->handle);
2501 }
2502 }
2503}
2504
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002505uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002506{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002507 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508}
2509
2510size_t Parcel::ipcDataSize() const
2511{
2512 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2513}
2514
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002515uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002517 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002518}
2519
2520size_t Parcel::ipcObjectsCount() const
2521{
2522 return mObjectsSize;
2523}
2524
2525void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002526 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002527{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002528 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002529 freeDataNoInit();
2530 mError = NO_ERROR;
2531 mData = const_cast<uint8_t*>(data);
2532 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002533 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002534 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002535 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002536 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002537 mObjectsSize = mObjectsCapacity = objectsCount;
2538 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002539 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002540 mOwner = relFunc;
2541 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002542 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002543 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002544 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002545 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002546 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002547 mObjectsSize = 0;
2548 break;
2549 }
2550 minOffset = offset + sizeof(flat_binder_object);
2551 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002552 scanForFds();
2553}
2554
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002555void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002556{
2557 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002558
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002559 if (errorCheck() != NO_ERROR) {
2560 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002561 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002562 } else if (dataSize() > 0) {
2563 const uint8_t* DATA = data();
2564 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002565 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002566 const size_t N = objectsCount();
2567 for (size_t i=0; i<N; i++) {
2568 const flat_binder_object* flat
2569 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2570 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002571 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002572 << " = " << flat->binder;
2573 }
2574 } else {
2575 to << "NULL";
2576 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002577
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002578 to << ")";
2579}
2580
2581void Parcel::releaseObjects()
2582{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002583 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002584 if (i == 0) {
2585 return;
2586 }
2587 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002588 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002589 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002590 while (i > 0) {
2591 i--;
2592 const flat_binder_object* flat
2593 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002594 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002595 }
2596}
2597
2598void Parcel::acquireObjects()
2599{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002600 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002601 if (i == 0) {
2602 return;
2603 }
2604 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002605 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002606 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002607 while (i > 0) {
2608 i--;
2609 const flat_binder_object* flat
2610 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002611 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002612 }
2613}
2614
2615void Parcel::freeData()
2616{
2617 freeDataNoInit();
2618 initState();
2619}
2620
2621void Parcel::freeDataNoInit()
2622{
2623 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002624 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002625 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002626 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2627 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002628 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002629 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002630 if (mData) {
2631 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002632 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002633 if (mDataCapacity <= gParcelGlobalAllocSize) {
2634 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2635 } else {
2636 gParcelGlobalAllocSize = 0;
2637 }
2638 if (gParcelGlobalAllocCount > 0) {
2639 gParcelGlobalAllocCount--;
2640 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002641 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002642 free(mData);
2643 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002644 if (mObjects) free(mObjects);
2645 }
2646}
2647
2648status_t Parcel::growData(size_t len)
2649{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002650 if (len > INT32_MAX) {
2651 // don't accept size_t values which may have come from an
2652 // inadvertent conversion from a negative int.
2653 return BAD_VALUE;
2654 }
2655
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002656 size_t newSize = ((mDataSize+len)*3)/2;
2657 return (newSize <= mDataSize)
2658 ? (status_t) NO_MEMORY
2659 : continueWrite(newSize);
2660}
2661
2662status_t Parcel::restartWrite(size_t desired)
2663{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002664 if (desired > INT32_MAX) {
2665 // don't accept size_t values which may have come from an
2666 // inadvertent conversion from a negative int.
2667 return BAD_VALUE;
2668 }
2669
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002670 if (mOwner) {
2671 freeData();
2672 return continueWrite(desired);
2673 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002674
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002675 uint8_t* data = (uint8_t*)realloc(mData, desired);
2676 if (!data && desired > mDataCapacity) {
2677 mError = NO_MEMORY;
2678 return NO_MEMORY;
2679 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002680
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002681 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002682
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002683 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002684 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002685 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002686 gParcelGlobalAllocSize += desired;
2687 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002688 if (!mData) {
2689 gParcelGlobalAllocCount++;
2690 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002691 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002692 mData = data;
2693 mDataCapacity = desired;
2694 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002695
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002696 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002697 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2698 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2699
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002700 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002701 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002702 mObjectsSize = mObjectsCapacity = 0;
2703 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002704 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002705 mHasFds = false;
2706 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002707 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002708
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002709 return NO_ERROR;
2710}
2711
2712status_t Parcel::continueWrite(size_t desired)
2713{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002714 if (desired > INT32_MAX) {
2715 // don't accept size_t values which may have come from an
2716 // inadvertent conversion from a negative int.
2717 return BAD_VALUE;
2718 }
2719
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002720 // If shrinking, first adjust for any objects that appear
2721 // after the new data size.
2722 size_t objectsSize = mObjectsSize;
2723 if (desired < mDataSize) {
2724 if (desired == 0) {
2725 objectsSize = 0;
2726 } else {
2727 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002728 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002729 break;
2730 objectsSize--;
2731 }
2732 }
2733 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002734
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002735 if (mOwner) {
2736 // If the size is going to zero, just release the owner's data.
2737 if (desired == 0) {
2738 freeData();
2739 return NO_ERROR;
2740 }
2741
2742 // If there is a different owner, we need to take
2743 // posession.
2744 uint8_t* data = (uint8_t*)malloc(desired);
2745 if (!data) {
2746 mError = NO_MEMORY;
2747 return NO_MEMORY;
2748 }
Yi Kong91635562018-06-07 14:38:36 -07002749 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002750
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002751 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002752 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002753 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002754 free(data);
2755
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002756 mError = NO_MEMORY;
2757 return NO_MEMORY;
2758 }
2759
2760 // Little hack to only acquire references on objects
2761 // we will be keeping.
2762 size_t oldObjectsSize = mObjectsSize;
2763 mObjectsSize = objectsSize;
2764 acquireObjects();
2765 mObjectsSize = oldObjectsSize;
2766 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002767
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002768 if (mData) {
2769 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2770 }
2771 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002772 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002773 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002774 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002775 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002776 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002777
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002778 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002779 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002780 gParcelGlobalAllocSize += desired;
2781 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002782 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002783
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002784 mData = data;
2785 mObjects = objects;
2786 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002787 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002788 mDataCapacity = desired;
2789 mObjectsSize = mObjectsCapacity = objectsSize;
2790 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002791 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002792
2793 } else if (mData) {
2794 if (objectsSize < mObjectsSize) {
2795 // Need to release refs on any objects we are dropping.
2796 const sp<ProcessState> proc(ProcessState::self());
2797 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2798 const flat_binder_object* flat
2799 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002800 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002801 // will need to rescan because we may have lopped off the only FDs
2802 mFdsKnown = false;
2803 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002804 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002805 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002806 binder_size_t* objects =
2807 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002808 if (objects) {
2809 mObjects = objects;
2810 }
2811 mObjectsSize = objectsSize;
2812 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002813 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002814 }
2815
2816 // We own the data, so we can just do a realloc().
2817 if (desired > mDataCapacity) {
2818 uint8_t* data = (uint8_t*)realloc(mData, desired);
2819 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002820 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2821 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002822 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002823 gParcelGlobalAllocSize += desired;
2824 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002825 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002826 mData = data;
2827 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002828 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002829 mError = NO_MEMORY;
2830 return NO_MEMORY;
2831 }
2832 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002833 if (mDataSize > desired) {
2834 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002835 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002836 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002837 if (mDataPos > desired) {
2838 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002839 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002840 }
2841 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002842
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002843 } else {
2844 // This is the first data. Easy!
2845 uint8_t* data = (uint8_t*)malloc(desired);
2846 if (!data) {
2847 mError = NO_MEMORY;
2848 return NO_MEMORY;
2849 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002850
Yi Kong91635562018-06-07 14:38:36 -07002851 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002852 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002853 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002854 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002855
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002856 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002857 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002858 gParcelGlobalAllocSize += desired;
2859 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002860 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002861
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002862 mData = data;
2863 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002864 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2865 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002866 mDataCapacity = desired;
2867 }
2868
2869 return NO_ERROR;
2870}
2871
2872void Parcel::initState()
2873{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002874 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002875 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002876 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002877 mDataSize = 0;
2878 mDataCapacity = 0;
2879 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002880 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2881 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002882 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002883 mObjectsSize = 0;
2884 mObjectsCapacity = 0;
2885 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002886 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002887 mHasFds = false;
2888 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002889 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002890 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002891 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002892
2893 // racing multiple init leads only to multiple identical write
2894 if (gMaxFds == 0) {
2895 struct rlimit result;
2896 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2897 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002898 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002899 } else {
2900 ALOGW("Unable to getrlimit: %s", strerror(errno));
2901 gMaxFds = 1024;
2902 }
2903 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002904}
2905
2906void Parcel::scanForFds() const
2907{
2908 bool hasFds = false;
2909 for (size_t i=0; i<mObjectsSize; i++) {
2910 const flat_binder_object* flat
2911 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002912 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002913 hasFds = true;
2914 break;
2915 }
2916 }
2917 mHasFds = hasFds;
2918 mFdsKnown = true;
2919}
2920
Dan Sandleraa5c2342015-04-10 10:08:45 -04002921size_t Parcel::getBlobAshmemSize() const
2922{
Adrian Roos6bb31142015-10-22 16:46:12 -07002923 // This used to return the size of all blobs that were written to ashmem, now we're returning
2924 // the ashmem currently referenced by this Parcel, which should be equivalent.
2925 // TODO: Remove method once ABI can be changed.
2926 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002927}
2928
Adrian Rooscbf37262015-10-22 16:12:53 -07002929size_t Parcel::getOpenAshmemSize() const
2930{
2931 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002932}
2933
2934// --- Parcel::Blob ---
2935
2936Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002937 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002938}
2939
2940Parcel::Blob::~Blob() {
2941 release();
2942}
2943
2944void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002945 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002946 ::munmap(mData, mSize);
2947 }
2948 clear();
2949}
2950
Jeff Brown13b16042014-11-11 16:44:25 -08002951void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2952 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002953 mData = data;
2954 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002955 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002956}
2957
2958void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002959 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002960 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002961 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002962 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002963}
2964
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002965}; // namespace android