blob: 0423264135a18def13d7854a161f1d8dda5dbdae [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 Sharkey0c1f5cb2014-12-18 10:26:57 -080077#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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: {
Yi Kongfdd8da92018-06-07 17:52:27 -0700132 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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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
Yi Kongfdd8da92018-06-07 17:52:27 -0700181 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700182 int size = ashmem_get_size_region(obj.handle);
183 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800184 // ashmem size might have changed since last time it was accounted for, e.g.
185 // in acquire_object(). Value of *outAshmemSize is not critical since we are
186 // releasing the object anyway. Check for integer overflow condition.
187 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700188 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700189 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800190
191 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700192 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700193 return;
194 }
195 }
196
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700197 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700198}
199
Adrian Roos6bb31142015-10-22 16:46:12 -0700200void release_object(const sp<ProcessState>& proc,
201 const flat_binder_object& obj, const void* who)
202{
Yi Kongfdd8da92018-06-07 17:52:27 -0700203 release_object(proc, obj, who, nullptr);
Adrian Roos6bb31142015-10-22 16:46:12 -0700204}
205
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700206inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800207 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700208{
209 return out->writeObject(flat, false);
210}
211
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800212status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700213 const sp<IBinder>& binder, Parcel* out)
214{
215 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700216
Martijn Coenen2b631742017-05-05 11:16:59 -0700217 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
218 /* minimum priority for all nodes is nice 0 */
219 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
220 } else {
221 /* minimum priority for all nodes is MAX_NICE(19) */
222 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
223 }
224
Yi Kongfdd8da92018-06-07 17:52:27 -0700225 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700226 IBinder *local = binder->localBinder();
227 if (!local) {
228 BpBinder *proxy = binder->remoteBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700229 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000230 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 }
232 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700233 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800234 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800236 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700237 } else {
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 Kongfdd8da92018-06-07 17:52:27 -0700257 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258 sp<IBinder> real = binder.promote();
Yi Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -0700342 *out = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700343 }
Yi Kongfdd8da92018-06-07 17:52:27 -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 Kongfdd8da92018-06-07 17:52:27 -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{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700605 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
606 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700607 // currently the interface identification token is just its name as a string
608 return writeString16(interface);
609}
610
Mathias Agopian83c04462009-05-22 19:00:22 -0700611bool Parcel::checkInterface(IBinder* binder) const
612{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700613 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700614}
615
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700616bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700617 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700618{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700619 int32_t strictPolicy = readInt32();
Yi Kongfdd8da92018-06-07 17:52:27 -0700620 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700621 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700622 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700623 if ((threadState->getLastTransactionBinderFlags() &
624 IBinder::FLAG_ONEWAY) != 0) {
625 // For one-way calls, the callee is running entirely
626 // disconnected from the caller, so disable StrictMode entirely.
627 // Not only does disk/network usage not impact the caller, but
628 // there's no way to commuicate back any violations anyway.
629 threadState->setStrictModePolicy(0);
630 } else {
631 threadState->setStrictModePolicy(strictPolicy);
632 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700633 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700634 if (str == interface) {
635 return true;
636 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700637 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700638 String8(interface).string(), String8(str).string());
639 return false;
640 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700641}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700642
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800643const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700644{
645 return mObjects;
646}
647
648size_t Parcel::objectsCount() const
649{
650 return mObjectsSize;
651}
652
653status_t Parcel::errorCheck() const
654{
655 return mError;
656}
657
658void Parcel::setError(status_t err)
659{
660 mError = err;
661}
662
663status_t Parcel::finishWrite(size_t len)
664{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700665 if (len > INT32_MAX) {
666 // don't accept size_t values which may have come from an
667 // inadvertent conversion from a negative int.
668 return BAD_VALUE;
669 }
670
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700671 //printf("Finish write of %d\n", len);
672 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700673 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700674 if (mDataPos > mDataSize) {
675 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700676 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700677 }
678 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
679 return NO_ERROR;
680}
681
682status_t Parcel::writeUnpadded(const void* data, size_t len)
683{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700684 if (len > INT32_MAX) {
685 // don't accept size_t values which may have come from an
686 // inadvertent conversion from a negative int.
687 return BAD_VALUE;
688 }
689
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700690 size_t end = mDataPos + len;
691 if (end < mDataPos) {
692 // integer overflow
693 return BAD_VALUE;
694 }
695
696 if (end <= mDataCapacity) {
697restart_write:
698 memcpy(mData+mDataPos, data, len);
699 return finishWrite(len);
700 }
701
702 status_t err = growData(len);
703 if (err == NO_ERROR) goto restart_write;
704 return err;
705}
706
707status_t Parcel::write(const void* data, size_t len)
708{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700709 if (len > INT32_MAX) {
710 // don't accept size_t values which may have come from an
711 // inadvertent conversion from a negative int.
712 return BAD_VALUE;
713 }
714
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700715 void* const d = writeInplace(len);
716 if (d) {
717 memcpy(d, data, len);
718 return NO_ERROR;
719 }
720 return mError;
721}
722
723void* Parcel::writeInplace(size_t len)
724{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700725 if (len > INT32_MAX) {
726 // don't accept size_t values which may have come from an
727 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -0700728 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700729 }
730
731 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700732
733 // sanity check for integer overflow
734 if (mDataPos+padded < mDataPos) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700735 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700736 }
737
738 if ((mDataPos+padded) <= mDataCapacity) {
739restart_write:
740 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
741 uint8_t* const data = mData+mDataPos;
742
743 // Need to pad at end?
744 if (padded != len) {
745#if BYTE_ORDER == BIG_ENDIAN
746 static const uint32_t mask[4] = {
747 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
748 };
749#endif
750#if BYTE_ORDER == LITTLE_ENDIAN
751 static const uint32_t mask[4] = {
752 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
753 };
754#endif
755 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
756 // *reinterpret_cast<void**>(data+padded-4));
757 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
758 }
759
760 finishWrite(padded);
761 return data;
762 }
763
764 status_t err = growData(padded);
765 if (err == NO_ERROR) goto restart_write;
Yi Kongfdd8da92018-06-07 17:52:27 -0700766 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700767}
768
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800769status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
770 const uint8_t* strData = (uint8_t*)str.data();
771 const size_t strLen= str.length();
772 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100773 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800774 return BAD_VALUE;
775 }
776
777 status_t err = writeInt32(utf16Len);
778 if (err) {
779 return err;
780 }
781
782 // Allocate enough bytes to hold our converted string and its terminating NULL.
783 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
784 if (!dst) {
785 return NO_MEMORY;
786 }
787
Sergio Girof4607432016-07-21 14:46:35 +0100788 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800789
790 return NO_ERROR;
791}
792
793status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
794 if (!str) {
795 return writeInt32(-1);
796 }
797 return writeUtf8AsUtf16(*str);
798}
799
Casey Dahlin185d3442016-02-09 11:08:35 -0800800namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800801
Casey Dahlin185d3442016-02-09 11:08:35 -0800802template<typename T>
803status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700804{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700805 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700806 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700807 status = BAD_VALUE;
808 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700809 }
810
Casey Dahlin185d3442016-02-09 11:08:35 -0800811 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700812 if (status != OK) {
813 return status;
814 }
815
Casey Dahlin185d3442016-02-09 11:08:35 -0800816 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700817 if (!data) {
818 status = BAD_VALUE;
819 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700820 }
821
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700822 memcpy(data, val.data(), val.size());
823 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700824}
825
Casey Dahlin185d3442016-02-09 11:08:35 -0800826template<typename T>
827status_t writeByteVectorInternalPtr(Parcel* parcel,
828 const std::unique_ptr<std::vector<T>>& val)
829{
830 if (!val) {
831 return parcel->writeInt32(-1);
832 }
833
834 return writeByteVectorInternal(parcel, *val);
835}
836
837} // namespace
838
839status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
840 return writeByteVectorInternal(this, val);
841}
842
843status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
844{
845 return writeByteVectorInternalPtr(this, val);
846}
847
848status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
849 return writeByteVectorInternal(this, val);
850}
851
852status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
853{
854 return writeByteVectorInternalPtr(this, val);
855}
856
Casey Dahlin451ff582015-10-19 18:12:18 -0700857status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
858{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800859 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700860}
861
Casey Dahlinb9872622015-11-25 15:09:45 -0800862status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
863{
864 return writeNullableTypedVector(val, &Parcel::writeInt32);
865}
866
Casey Dahlin451ff582015-10-19 18:12:18 -0700867status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
868{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800869 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700870}
871
Casey Dahlinb9872622015-11-25 15:09:45 -0800872status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
873{
874 return writeNullableTypedVector(val, &Parcel::writeInt64);
875}
876
Casey Dahlin451ff582015-10-19 18:12:18 -0700877status_t Parcel::writeFloatVector(const std::vector<float>& val)
878{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800879 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700880}
881
Casey Dahlinb9872622015-11-25 15:09:45 -0800882status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
883{
884 return writeNullableTypedVector(val, &Parcel::writeFloat);
885}
886
Casey Dahlin451ff582015-10-19 18:12:18 -0700887status_t Parcel::writeDoubleVector(const std::vector<double>& val)
888{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800889 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700890}
891
Casey Dahlinb9872622015-11-25 15:09:45 -0800892status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
893{
894 return writeNullableTypedVector(val, &Parcel::writeDouble);
895}
896
Casey Dahlin451ff582015-10-19 18:12:18 -0700897status_t Parcel::writeBoolVector(const std::vector<bool>& val)
898{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800899 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700900}
901
Casey Dahlinb9872622015-11-25 15:09:45 -0800902status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
903{
904 return writeNullableTypedVector(val, &Parcel::writeBool);
905}
906
Casey Dahlin451ff582015-10-19 18:12:18 -0700907status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
908{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800909 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700910}
911
Casey Dahlinb9872622015-11-25 15:09:45 -0800912status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
913{
914 return writeNullableTypedVector(val, &Parcel::writeChar);
915}
916
Casey Dahlin451ff582015-10-19 18:12:18 -0700917status_t Parcel::writeString16Vector(const std::vector<String16>& val)
918{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800919 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700920}
921
Casey Dahlinb9872622015-11-25 15:09:45 -0800922status_t Parcel::writeString16Vector(
923 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
924{
925 return writeNullableTypedVector(val, &Parcel::writeString16);
926}
927
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800928status_t Parcel::writeUtf8VectorAsUtf16Vector(
929 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
930 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
931}
932
933status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
934 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
935}
936
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700937status_t Parcel::writeInt32(int32_t val)
938{
Andreas Huber84a6d042009-08-17 13:33:27 -0700939 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700940}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800941
942status_t Parcel::writeUint32(uint32_t val)
943{
944 return writeAligned(val);
945}
946
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700947status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700948 if (len > INT32_MAX) {
949 // don't accept size_t values which may have come from an
950 // inadvertent conversion from a negative int.
951 return BAD_VALUE;
952 }
953
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700954 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700955 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700956 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700957 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700958 if (ret == NO_ERROR) {
959 ret = write(val, len * sizeof(*val));
960 }
961 return ret;
962}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700963status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700964 if (len > INT32_MAX) {
965 // don't accept size_t values which may have come from an
966 // inadvertent conversion from a negative int.
967 return BAD_VALUE;
968 }
969
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700970 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700971 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700972 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700973 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700974 if (ret == NO_ERROR) {
975 ret = write(val, len * sizeof(*val));
976 }
977 return ret;
978}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700979
Casey Dahlind6848f52015-10-15 15:44:59 -0700980status_t Parcel::writeBool(bool val)
981{
982 return writeInt32(int32_t(val));
983}
984
985status_t Parcel::writeChar(char16_t val)
986{
987 return writeInt32(int32_t(val));
988}
989
990status_t Parcel::writeByte(int8_t val)
991{
992 return writeInt32(int32_t(val));
993}
994
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700995status_t Parcel::writeInt64(int64_t val)
996{
Andreas Huber84a6d042009-08-17 13:33:27 -0700997 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700998}
999
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001000status_t Parcel::writeUint64(uint64_t val)
1001{
1002 return writeAligned(val);
1003}
1004
Serban Constantinescuf683e012013-11-05 16:53:55 +00001005status_t Parcel::writePointer(uintptr_t val)
1006{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001007 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001008}
1009
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001010status_t Parcel::writeFloat(float val)
1011{
Andreas Huber84a6d042009-08-17 13:33:27 -07001012 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001013}
1014
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001015#if defined(__mips__) && defined(__mips_hard_float)
1016
1017status_t Parcel::writeDouble(double val)
1018{
1019 union {
1020 double d;
1021 unsigned long long ll;
1022 } u;
1023 u.d = val;
1024 return writeAligned(u.ll);
1025}
1026
1027#else
1028
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001029status_t Parcel::writeDouble(double val)
1030{
Andreas Huber84a6d042009-08-17 13:33:27 -07001031 return writeAligned(val);
1032}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001033
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001034#endif
1035
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001036status_t Parcel::writeCString(const char* str)
1037{
1038 return write(str, strlen(str)+1);
1039}
1040
1041status_t Parcel::writeString8(const String8& str)
1042{
1043 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001044 // only write string if its length is more than zero characters,
1045 // as readString8 will only read if the length field is non-zero.
1046 // this is slightly different from how writeString16 works.
1047 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001048 err = write(str.string(), str.bytes()+1);
1049 }
1050 return err;
1051}
1052
Casey Dahlinb9872622015-11-25 15:09:45 -08001053status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1054{
1055 if (!str) {
1056 return writeInt32(-1);
1057 }
1058
1059 return writeString16(*str);
1060}
1061
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001062status_t Parcel::writeString16(const String16& str)
1063{
1064 return writeString16(str.string(), str.size());
1065}
1066
1067status_t Parcel::writeString16(const char16_t* str, size_t len)
1068{
Yi Kongfdd8da92018-06-07 17:52:27 -07001069 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001070
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001071 status_t err = writeInt32(len);
1072 if (err == NO_ERROR) {
1073 len *= sizeof(char16_t);
1074 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1075 if (data) {
1076 memcpy(data, str, len);
1077 *reinterpret_cast<char16_t*>(data+len) = 0;
1078 return NO_ERROR;
1079 }
1080 err = mError;
1081 }
1082 return err;
1083}
1084
1085status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1086{
1087 return flatten_binder(ProcessState::self(), val, this);
1088}
1089
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001090status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1091{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001092 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001093}
1094
Casey Dahlinb9872622015-11-25 15:09:45 -08001095status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1096{
1097 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1098}
1099
1100status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001101 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001102}
1103
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001104status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001105 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001106}
1107
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001108status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1109{
1110 return flatten_binder(ProcessState::self(), val, this);
1111}
1112
Casey Dahlinb9872622015-11-25 15:09:45 -08001113status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1114 if (!parcelable) {
1115 return writeInt32(0);
1116 }
1117
1118 return writeParcelable(*parcelable);
1119}
1120
Christopher Wiley97f048d2015-11-19 06:49:05 -08001121status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1122 status_t status = writeInt32(1); // parcelable is not null.
1123 if (status != OK) {
1124 return status;
1125 }
1126 return parcelable.writeToParcel(this);
1127}
1128
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001129status_t Parcel::writeValue(const binder::Value& value) {
1130 return value.writeToParcel(this);
1131}
1132
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001133status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001134{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001135 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001136 return BAD_TYPE;
1137
1138 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001139 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001140 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001142 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001143 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001144
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001145 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1146 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147
1148 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001149 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150 return err;
1151 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001152 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001153 return err;
1154}
1155
Jeff Brown93ff1f92011-11-04 19:01:44 -07001156status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001157{
1158 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001159 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001160 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001161 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001162 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001163 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001164 return writeObject(obj, true);
1165}
1166
1167status_t Parcel::writeDupFileDescriptor(int fd)
1168{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001169 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001170 if (dupFd < 0) {
1171 return -errno;
1172 }
1173 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001174 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001175 close(dupFd);
1176 }
1177 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001178}
1179
Dianne Hackborn1941a402016-08-29 12:30:43 -07001180status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1181{
1182 writeInt32(0);
1183 return writeFileDescriptor(fd, takeOwnership);
1184}
1185
Ryo Hashimotobf551892018-05-31 16:58:35 +09001186status_t Parcel::writeDupParcelFileDescriptor(int fd)
1187{
1188 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1189 if (dupFd < 0) {
1190 return -errno;
1191 }
1192 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1193 if (err != OK) {
1194 close(dupFd);
1195 }
1196 return err;
1197}
1198
Christopher Wiley2cf19952016-04-11 11:09:37 -07001199status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001200 return writeDupFileDescriptor(fd.get());
1201}
1202
Christopher Wiley2cf19952016-04-11 11:09:37 -07001203status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001204 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1205}
1206
Christopher Wiley2cf19952016-04-11 11:09:37 -07001207status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001208 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1209}
1210
Jeff Brown13b16042014-11-11 16:44:25 -08001211status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001212{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001213 if (len > INT32_MAX) {
1214 // don't accept size_t values which may have come from an
1215 // inadvertent conversion from a negative int.
1216 return BAD_VALUE;
1217 }
1218
Jeff Brown13b16042014-11-11 16:44:25 -08001219 status_t status;
1220 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001221 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001222 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001223 if (status) return status;
1224
1225 void* ptr = writeInplace(len);
1226 if (!ptr) return NO_MEMORY;
1227
Jeff Brown13b16042014-11-11 16:44:25 -08001228 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001229 return NO_ERROR;
1230 }
1231
Steve Block6807e592011-10-20 11:56:00 +01001232 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001233 int fd = ashmem_create_region("Parcel Blob", len);
1234 if (fd < 0) return NO_MEMORY;
1235
1236 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1237 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001238 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001239 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -07001240 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001241 if (ptr == MAP_FAILED) {
1242 status = -errno;
1243 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001244 if (!mutableCopy) {
1245 result = ashmem_set_prot_region(fd, PROT_READ);
1246 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001247 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001248 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001249 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001250 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001251 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001252 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001253 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001254 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001255 return NO_ERROR;
1256 }
1257 }
1258 }
1259 }
1260 ::munmap(ptr, len);
1261 }
1262 ::close(fd);
1263 return status;
1264}
1265
Jeff Brown13b16042014-11-11 16:44:25 -08001266status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1267{
1268 // Must match up with what's done in writeBlob.
1269 if (!mAllowFds) return FDS_NOT_ALLOWED;
1270 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1271 if (status) return status;
1272 return writeDupFileDescriptor(fd);
1273}
1274
Mathias Agopiane1424282013-07-29 21:24:40 -07001275status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001276{
1277 status_t err;
1278
1279 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001280 const size_t len = val.getFlattenedSize();
1281 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001282
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001283 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001284 // don't accept size_t values which may have come from an
1285 // inadvertent conversion from a negative int.
1286 return BAD_VALUE;
1287 }
1288
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001289 err = this->writeInt32(len);
1290 if (err) return err;
1291
1292 err = this->writeInt32(fd_count);
1293 if (err) return err;
1294
1295 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001296 void* const buf = this->writeInplace(len);
Yi Kongfdd8da92018-06-07 17:52:27 -07001297 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001298 return BAD_VALUE;
1299
Yi Kongfdd8da92018-06-07 17:52:27 -07001300 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001301 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001302 fds = new (std::nothrow) int[fd_count];
1303 if (fds == nullptr) {
1304 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1305 return BAD_VALUE;
1306 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001307 }
1308
1309 err = val.flatten(buf, len, fds, fd_count);
1310 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1311 err = this->writeDupFileDescriptor( fds[i] );
1312 }
1313
1314 if (fd_count) {
1315 delete [] fds;
1316 }
1317
1318 return err;
1319}
1320
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001321status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1322{
1323 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1324 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1325 if (enoughData && enoughObjects) {
1326restart_write:
1327 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001328
Christopher Tate98e67d32015-06-03 18:44:15 -07001329 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001330 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001331 if (!mAllowFds) {
1332 // fail before modifying our object index
1333 return FDS_NOT_ALLOWED;
1334 }
1335 mHasFds = mFdsKnown = true;
1336 }
1337
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001338 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001339 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001340 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001341 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001342 mObjectsSize++;
1343 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001344
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001345 return finishWrite(sizeof(flat_binder_object));
1346 }
1347
1348 if (!enoughData) {
1349 const status_t err = growData(sizeof(val));
1350 if (err != NO_ERROR) return err;
1351 }
1352 if (!enoughObjects) {
1353 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001354 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001355 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07001356 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001357 mObjects = objects;
1358 mObjectsCapacity = newSize;
1359 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001360
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001361 goto restart_write;
1362}
1363
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001364status_t Parcel::writeNoException()
1365{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001366 binder::Status status;
1367 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001368}
1369
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001370status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1371{
1372 using ::std::map;
1373 using ::android::binder::Value;
1374 using ::android::binder::Map;
1375
1376 Map::const_iterator iter;
1377 status_t ret;
1378
1379 ret = writeInt32(map_in.size());
1380
1381 if (ret != NO_ERROR) {
1382 return ret;
1383 }
1384
1385 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1386 ret = writeValue(Value(iter->first));
1387 if (ret != NO_ERROR) {
1388 return ret;
1389 }
1390
1391 ret = writeValue(iter->second);
1392 if (ret != NO_ERROR) {
1393 return ret;
1394 }
1395 }
1396
1397 return ret;
1398}
1399
1400status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1401{
Yi Kongfdd8da92018-06-07 17:52:27 -07001402 if (map == nullptr) {
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001403 return writeInt32(-1);
1404 }
1405
1406 return writeMap(*map.get());
1407}
1408
1409status_t Parcel::readMap(::android::binder::Map* map_out)const
1410{
1411 using ::std::map;
1412 using ::android::String16;
1413 using ::android::String8;
1414 using ::android::binder::Value;
1415 using ::android::binder::Map;
1416
1417 status_t ret = NO_ERROR;
1418 int32_t count;
1419
1420 ret = readInt32(&count);
1421 if (ret != NO_ERROR) {
1422 return ret;
1423 }
1424
1425 if (count < 0) {
1426 ALOGE("readMap: Unexpected count: %d", count);
1427 return (count == -1)
1428 ? UNEXPECTED_NULL
1429 : BAD_VALUE;
1430 }
1431
1432 map_out->clear();
1433
1434 while (count--) {
1435 Map::key_type key;
1436 Value value;
1437
1438 ret = readValue(&value);
1439 if (ret != NO_ERROR) {
1440 return ret;
1441 }
1442
1443 if (!value.getString(&key)) {
1444 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1445 return BAD_VALUE;
1446 }
1447
1448 ret = readValue(&value);
1449 if (ret != NO_ERROR) {
1450 return ret;
1451 }
1452
1453 (*map_out)[key] = value;
1454 }
1455
1456 return ret;
1457}
1458
1459status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1460{
1461 const size_t start = dataPosition();
1462 int32_t count;
1463 status_t status = readInt32(&count);
1464 map->reset();
1465
1466 if (status != OK || count == -1) {
1467 return status;
1468 }
1469
1470 setDataPosition(start);
1471 map->reset(new binder::Map());
1472
1473 status = readMap(map->get());
1474
1475 if (status != OK) {
1476 map->reset();
1477 }
1478
1479 return status;
1480}
1481
1482
1483
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001484void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001485{
1486 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1487}
1488
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001489status_t Parcel::validateReadData(size_t upperBound) const
1490{
1491 // Don't allow non-object reads on object data
1492 if (mObjectsSorted || mObjectsSize <= 1) {
1493data_sorted:
1494 // Expect to check only against the next object
1495 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1496 // For some reason the current read position is greater than the next object
1497 // hint. Iterate until we find the right object
1498 size_t nextObject = mNextObjectHint;
1499 do {
1500 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1501 // Requested info overlaps with an object
1502 ALOGE("Attempt to read from protected data in Parcel %p", this);
1503 return PERMISSION_DENIED;
1504 }
1505 nextObject++;
1506 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1507 mNextObjectHint = nextObject;
1508 }
1509 return NO_ERROR;
1510 }
1511 // Quickly determine if mObjects is sorted.
1512 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1513 binder_size_t* prevObj = currObj;
1514 while (currObj > mObjects) {
1515 prevObj--;
1516 if(*prevObj > *currObj) {
1517 goto data_unsorted;
1518 }
1519 currObj--;
1520 }
1521 mObjectsSorted = true;
1522 goto data_sorted;
1523
1524data_unsorted:
1525 // Insertion Sort mObjects
1526 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1527 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1528 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1529 binder_size_t temp = *iter0;
1530 binder_size_t* iter1 = iter0 - 1;
1531 while (iter1 >= mObjects && *iter1 > temp) {
1532 *(iter1 + 1) = *iter1;
1533 iter1--;
1534 }
1535 *(iter1 + 1) = temp;
1536 }
1537 mNextObjectHint = 0;
1538 mObjectsSorted = true;
1539 goto data_sorted;
1540}
1541
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001542status_t Parcel::read(void* outData, size_t len) const
1543{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001544 if (len > INT32_MAX) {
1545 // don't accept size_t values which may have come from an
1546 // inadvertent conversion from a negative int.
1547 return BAD_VALUE;
1548 }
1549
1550 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1551 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001552 if (mObjectsSize > 0) {
1553 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001554 if(err != NO_ERROR) {
1555 // Still increment the data position by the expected length
1556 mDataPos += pad_size(len);
1557 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1558 return err;
1559 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001560 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001561 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001562 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001563 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001564 return NO_ERROR;
1565 }
1566 return NOT_ENOUGH_DATA;
1567}
1568
1569const void* Parcel::readInplace(size_t len) const
1570{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001571 if (len > INT32_MAX) {
1572 // don't accept size_t values which may have come from an
1573 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -07001574 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001575 }
1576
1577 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1578 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001579 if (mObjectsSize > 0) {
1580 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001581 if(err != NO_ERROR) {
1582 // Still increment the data position by the expected length
1583 mDataPos += pad_size(len);
1584 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Xin Lif11e2bd2018-06-08 15:11:57 -07001585 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001586 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001587 }
1588
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001589 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001590 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001591 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001592 return data;
1593 }
Yi Kongfdd8da92018-06-07 17:52:27 -07001594 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001595}
1596
Andreas Huber84a6d042009-08-17 13:33:27 -07001597template<class T>
1598status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001599 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001600
1601 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001602 if (mObjectsSize > 0) {
1603 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001604 if(err != NO_ERROR) {
1605 // Still increment the data position by the expected length
1606 mDataPos += sizeof(T);
1607 return err;
1608 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001609 }
1610
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001611 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001612 mDataPos += sizeof(T);
1613 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001614 return NO_ERROR;
1615 } else {
1616 return NOT_ENOUGH_DATA;
1617 }
1618}
1619
Andreas Huber84a6d042009-08-17 13:33:27 -07001620template<class T>
1621T Parcel::readAligned() const {
1622 T result;
1623 if (readAligned(&result) != NO_ERROR) {
1624 result = 0;
1625 }
1626
1627 return result;
1628}
1629
1630template<class T>
1631status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001632 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001633
1634 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1635restart_write:
1636 *reinterpret_cast<T*>(mData+mDataPos) = val;
1637 return finishWrite(sizeof(val));
1638 }
1639
1640 status_t err = growData(sizeof(val));
1641 if (err == NO_ERROR) goto restart_write;
1642 return err;
1643}
1644
Casey Dahlin185d3442016-02-09 11:08:35 -08001645namespace {
1646
1647template<typename T>
1648status_t readByteVectorInternal(const Parcel* parcel,
1649 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001650 val->clear();
1651
1652 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001653 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001654
1655 if (status != OK) {
1656 return status;
1657 }
1658
Christopher Wiley4db672d2015-11-10 09:44:30 -08001659 if (size < 0) {
1660 status = UNEXPECTED_NULL;
1661 return status;
1662 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001663 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001664 status = BAD_VALUE;
1665 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001666 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001667
Paul Lietar433e87b2016-09-16 10:39:32 -07001668 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001669 if (!data) {
1670 status = BAD_VALUE;
1671 return status;
1672 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001673 val->reserve(size);
1674 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001675
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001676 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001677}
1678
Casey Dahlin185d3442016-02-09 11:08:35 -08001679template<typename T>
1680status_t readByteVectorInternalPtr(
1681 const Parcel* parcel,
1682 std::unique_ptr<std::vector<T>>* val) {
1683 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001684 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001685 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001686 val->reset();
1687
1688 if (status != OK || size < 0) {
1689 return status;
1690 }
1691
Casey Dahlin185d3442016-02-09 11:08:35 -08001692 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001693 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001694
Casey Dahlin185d3442016-02-09 11:08:35 -08001695 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001696
1697 if (status != OK) {
1698 val->reset();
1699 }
1700
1701 return status;
1702}
1703
Casey Dahlin185d3442016-02-09 11:08:35 -08001704} // namespace
1705
1706status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1707 return readByteVectorInternal(this, val);
1708}
1709
1710status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1711 return readByteVectorInternal(this, val);
1712}
1713
1714status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1715 return readByteVectorInternalPtr(this, val);
1716}
1717
1718status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1719 return readByteVectorInternalPtr(this, val);
1720}
1721
Casey Dahlinb9872622015-11-25 15:09:45 -08001722status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1723 return readNullableTypedVector(val, &Parcel::readInt32);
1724}
1725
Casey Dahlin451ff582015-10-19 18:12:18 -07001726status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001727 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001728}
1729
Casey Dahlinb9872622015-11-25 15:09:45 -08001730status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1731 return readNullableTypedVector(val, &Parcel::readInt64);
1732}
1733
Casey Dahlin451ff582015-10-19 18:12:18 -07001734status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001735 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001736}
1737
Casey Dahlinb9872622015-11-25 15:09:45 -08001738status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1739 return readNullableTypedVector(val, &Parcel::readFloat);
1740}
1741
Casey Dahlin451ff582015-10-19 18:12:18 -07001742status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001743 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001744}
1745
Casey Dahlinb9872622015-11-25 15:09:45 -08001746status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1747 return readNullableTypedVector(val, &Parcel::readDouble);
1748}
1749
Casey Dahlin451ff582015-10-19 18:12:18 -07001750status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001751 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001752}
1753
Casey Dahlinb9872622015-11-25 15:09:45 -08001754status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1755 const int32_t start = dataPosition();
1756 int32_t size;
1757 status_t status = readInt32(&size);
1758 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001759
Casey Dahlinb9872622015-11-25 15:09:45 -08001760 if (status != OK || size < 0) {
1761 return status;
1762 }
1763
1764 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001765 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001766
1767 status = readBoolVector(val->get());
1768
1769 if (status != OK) {
1770 val->reset();
1771 }
1772
1773 return status;
1774}
1775
1776status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001777 int32_t size;
1778 status_t status = readInt32(&size);
1779
1780 if (status != OK) {
1781 return status;
1782 }
1783
1784 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001785 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001786 }
1787
1788 val->resize(size);
1789
1790 /* C++ bool handling means a vector of bools isn't necessarily addressable
1791 * (we might use individual bits)
1792 */
Christopher Wiley97887982015-10-27 16:33:47 -07001793 bool data;
1794 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001795 status = readBool(&data);
1796 (*val)[i] = data;
1797
1798 if (status != OK) {
1799 return status;
1800 }
1801 }
1802
1803 return OK;
1804}
1805
Casey Dahlinb9872622015-11-25 15:09:45 -08001806status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1807 return readNullableTypedVector(val, &Parcel::readChar);
1808}
1809
Casey Dahlin451ff582015-10-19 18:12:18 -07001810status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001811 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001812}
1813
Casey Dahlinb9872622015-11-25 15:09:45 -08001814status_t Parcel::readString16Vector(
1815 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1816 return readNullableTypedVector(val, &Parcel::readString16);
1817}
1818
Casey Dahlin451ff582015-10-19 18:12:18 -07001819status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001820 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001821}
1822
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001823status_t Parcel::readUtf8VectorFromUtf16Vector(
1824 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1825 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1826}
1827
1828status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1829 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1830}
Casey Dahlin451ff582015-10-19 18:12:18 -07001831
Andreas Huber84a6d042009-08-17 13:33:27 -07001832status_t Parcel::readInt32(int32_t *pArg) const
1833{
1834 return readAligned(pArg);
1835}
1836
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001837int32_t Parcel::readInt32() const
1838{
Andreas Huber84a6d042009-08-17 13:33:27 -07001839 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001840}
1841
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001842status_t Parcel::readUint32(uint32_t *pArg) const
1843{
1844 return readAligned(pArg);
1845}
1846
1847uint32_t Parcel::readUint32() const
1848{
1849 return readAligned<uint32_t>();
1850}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001851
1852status_t Parcel::readInt64(int64_t *pArg) const
1853{
Andreas Huber84a6d042009-08-17 13:33:27 -07001854 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001855}
1856
1857
1858int64_t Parcel::readInt64() const
1859{
Andreas Huber84a6d042009-08-17 13:33:27 -07001860 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001861}
1862
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001863status_t Parcel::readUint64(uint64_t *pArg) const
1864{
1865 return readAligned(pArg);
1866}
1867
1868uint64_t Parcel::readUint64() const
1869{
1870 return readAligned<uint64_t>();
1871}
1872
Serban Constantinescuf683e012013-11-05 16:53:55 +00001873status_t Parcel::readPointer(uintptr_t *pArg) const
1874{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001875 status_t ret;
1876 binder_uintptr_t ptr;
1877 ret = readAligned(&ptr);
1878 if (!ret)
1879 *pArg = ptr;
1880 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001881}
1882
1883uintptr_t Parcel::readPointer() const
1884{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001885 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001886}
1887
1888
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001889status_t Parcel::readFloat(float *pArg) const
1890{
Andreas Huber84a6d042009-08-17 13:33:27 -07001891 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001892}
1893
1894
1895float Parcel::readFloat() const
1896{
Andreas Huber84a6d042009-08-17 13:33:27 -07001897 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001898}
1899
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001900#if defined(__mips__) && defined(__mips_hard_float)
1901
1902status_t Parcel::readDouble(double *pArg) const
1903{
1904 union {
1905 double d;
1906 unsigned long long ll;
1907 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001908 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001909 status_t status;
1910 status = readAligned(&u.ll);
1911 *pArg = u.d;
1912 return status;
1913}
1914
1915double Parcel::readDouble() const
1916{
1917 union {
1918 double d;
1919 unsigned long long ll;
1920 } u;
1921 u.ll = readAligned<unsigned long long>();
1922 return u.d;
1923}
1924
1925#else
1926
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001927status_t Parcel::readDouble(double *pArg) const
1928{
Andreas Huber84a6d042009-08-17 13:33:27 -07001929 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001930}
1931
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001932double Parcel::readDouble() const
1933{
Andreas Huber84a6d042009-08-17 13:33:27 -07001934 return readAligned<double>();
1935}
1936
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001937#endif
1938
Andreas Huber84a6d042009-08-17 13:33:27 -07001939status_t Parcel::readIntPtr(intptr_t *pArg) const
1940{
1941 return readAligned(pArg);
1942}
1943
1944
1945intptr_t Parcel::readIntPtr() const
1946{
1947 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001948}
1949
Casey Dahlind6848f52015-10-15 15:44:59 -07001950status_t Parcel::readBool(bool *pArg) const
1951{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001952 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001953 status_t ret = readInt32(&tmp);
1954 *pArg = (tmp != 0);
1955 return ret;
1956}
1957
1958bool Parcel::readBool() const
1959{
1960 return readInt32() != 0;
1961}
1962
1963status_t Parcel::readChar(char16_t *pArg) const
1964{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001965 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001966 status_t ret = readInt32(&tmp);
1967 *pArg = char16_t(tmp);
1968 return ret;
1969}
1970
1971char16_t Parcel::readChar() const
1972{
1973 return char16_t(readInt32());
1974}
1975
1976status_t Parcel::readByte(int8_t *pArg) const
1977{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001978 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001979 status_t ret = readInt32(&tmp);
1980 *pArg = int8_t(tmp);
1981 return ret;
1982}
1983
1984int8_t Parcel::readByte() const
1985{
1986 return int8_t(readInt32());
1987}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001988
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001989status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1990 size_t utf16Size = 0;
1991 const char16_t* src = readString16Inplace(&utf16Size);
1992 if (!src) {
1993 return UNEXPECTED_NULL;
1994 }
1995
1996 // Save ourselves the trouble, we're done.
1997 if (utf16Size == 0u) {
1998 str->clear();
1999 return NO_ERROR;
2000 }
2001
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002002 // Allow for closing '\0'
2003 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2004 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002005 return BAD_VALUE;
2006 }
2007 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002008 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002009 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002010 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2011 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002012 return NO_ERROR;
2013}
2014
2015status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2016 const int32_t start = dataPosition();
2017 int32_t size;
2018 status_t status = readInt32(&size);
2019 str->reset();
2020
2021 if (status != OK || size < 0) {
2022 return status;
2023 }
2024
2025 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002026 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002027 return readUtf8FromUtf16(str->get());
2028}
2029
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002030const char* Parcel::readCString() const
2031{
2032 const size_t avail = mDataSize-mDataPos;
2033 if (avail > 0) {
2034 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2035 // is the string's trailing NUL within the parcel's valid bounds?
2036 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2037 if (eos) {
2038 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002039 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002040 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002041 return str;
2042 }
2043 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002044 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002045}
2046
2047String8 Parcel::readString8() const
2048{
Roshan Pius87b64d22016-07-18 12:51:02 -07002049 String8 retString;
2050 status_t status = readString8(&retString);
2051 if (status != OK) {
2052 // We don't care about errors here, so just return an empty string.
2053 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002054 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002055 return retString;
2056}
2057
2058status_t Parcel::readString8(String8* pArg) const
2059{
2060 int32_t size;
2061 status_t status = readInt32(&size);
2062 if (status != OK) {
2063 return status;
2064 }
2065 // watch for potential int overflow from size+1
2066 if (size < 0 || size >= INT32_MAX) {
2067 return BAD_VALUE;
2068 }
2069 // |writeString8| writes nothing for empty string.
2070 if (size == 0) {
2071 *pArg = String8();
2072 return OK;
2073 }
2074 const char* str = (const char*)readInplace(size + 1);
Yi Kongfdd8da92018-06-07 17:52:27 -07002075 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002076 return BAD_VALUE;
2077 }
2078 pArg->setTo(str, size);
2079 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002080}
2081
2082String16 Parcel::readString16() const
2083{
2084 size_t len;
2085 const char16_t* str = readString16Inplace(&len);
2086 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002087 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002088 return String16();
2089}
2090
Casey Dahlinb9872622015-11-25 15:09:45 -08002091status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2092{
2093 const int32_t start = dataPosition();
2094 int32_t size;
2095 status_t status = readInt32(&size);
2096 pArg->reset();
2097
2098 if (status != OK || size < 0) {
2099 return status;
2100 }
2101
2102 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002103 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002104
2105 status = readString16(pArg->get());
2106
2107 if (status != OK) {
2108 pArg->reset();
2109 }
2110
2111 return status;
2112}
2113
Casey Dahlin451ff582015-10-19 18:12:18 -07002114status_t Parcel::readString16(String16* pArg) const
2115{
2116 size_t len;
2117 const char16_t* str = readString16Inplace(&len);
2118 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002119 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002120 return 0;
2121 } else {
2122 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002123 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002124 }
2125}
2126
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002127const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2128{
2129 int32_t size = readInt32();
2130 // watch for potential int overflow from size+1
2131 if (size >= 0 && size < INT32_MAX) {
2132 *outLen = size;
2133 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07002134 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002135 return str;
2136 }
2137 }
2138 *outLen = 0;
Yi Kongfdd8da92018-06-07 17:52:27 -07002139 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002140}
2141
Casey Dahlinf0c13772015-10-27 18:33:56 -07002142status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2143{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002144 status_t status = readNullableStrongBinder(val);
2145 if (status == OK && !val->get()) {
2146 status = UNEXPECTED_NULL;
2147 }
2148 return status;
2149}
2150
2151status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2152{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002153 return unflatten_binder(ProcessState::self(), *this, val);
2154}
2155
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002156sp<IBinder> Parcel::readStrongBinder() const
2157{
2158 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002159 // Note that a lot of code in Android reads binders by hand with this
2160 // method, and that code has historically been ok with getting nullptr
2161 // back (while ignoring error codes).
2162 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002163 return val;
2164}
2165
2166wp<IBinder> Parcel::readWeakBinder() const
2167{
2168 wp<IBinder> val;
2169 unflatten_binder(ProcessState::self(), *this, &val);
2170 return val;
2171}
2172
Christopher Wiley97f048d2015-11-19 06:49:05 -08002173status_t Parcel::readParcelable(Parcelable* parcelable) const {
2174 int32_t have_parcelable = 0;
2175 status_t status = readInt32(&have_parcelable);
2176 if (status != OK) {
2177 return status;
2178 }
2179 if (!have_parcelable) {
2180 return UNEXPECTED_NULL;
2181 }
2182 return parcelable->readFromParcel(this);
2183}
2184
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002185status_t Parcel::readValue(binder::Value* value) const {
2186 return value->readFromParcel(this);
2187}
2188
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002189int32_t Parcel::readExceptionCode() const
2190{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002191 binder::Status status;
2192 status.readFromParcel(*this);
2193 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002194}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002195
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002196native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002197{
2198 int numFds, numInts;
2199 status_t err;
2200 err = readInt32(&numFds);
Yi Kongfdd8da92018-06-07 17:52:27 -07002201 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002202 err = readInt32(&numInts);
Yi Kongfdd8da92018-06-07 17:52:27 -07002203 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002204
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002205 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002206 if (!h) {
Yi Kongfdd8da92018-06-07 17:52:27 -07002207 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002208 }
2209
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002210 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002211 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002212 if (h->data[i] < 0) {
2213 for (int j = 0; j < i; j++) {
2214 close(h->data[j]);
2215 }
2216 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07002217 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002218 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002219 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002220 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002221 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002222 native_handle_close(h);
2223 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07002224 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002225 }
2226 return h;
2227}
2228
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002229int Parcel::readFileDescriptor() const
2230{
2231 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002232
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002233 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002234 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002235 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002236
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002237 return BAD_TYPE;
2238}
2239
Dianne Hackborn1941a402016-08-29 12:30:43 -07002240int Parcel::readParcelFileDescriptor() const
2241{
2242 int32_t hasComm = readInt32();
2243 int fd = readFileDescriptor();
2244 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002245 // detach (owned by the binder driver)
2246 int comm = readFileDescriptor();
2247
2248 // warning: this must be kept in sync with:
2249 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2250 enum ParcelFileDescriptorStatus {
2251 DETACHED = 2,
2252 };
2253
2254#if BYTE_ORDER == BIG_ENDIAN
2255 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2256#endif
2257#if BYTE_ORDER == LITTLE_ENDIAN
2258 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2259#endif
2260
2261 ssize_t written = TEMP_FAILURE_RETRY(
2262 ::write(comm, &message, sizeof(message)));
2263
2264 if (written == -1 || written != sizeof(message)) {
2265 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2266 written, strerror(errno));
2267 return BAD_TYPE;
2268 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002269 }
2270 return fd;
2271}
2272
Christopher Wiley2cf19952016-04-11 11:09:37 -07002273status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002274{
2275 int got = readFileDescriptor();
2276
2277 if (got == BAD_TYPE) {
2278 return BAD_TYPE;
2279 }
2280
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002281 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002282
2283 if (val->get() < 0) {
2284 return BAD_VALUE;
2285 }
2286
2287 return OK;
2288}
2289
Ryo Hashimotobf551892018-05-31 16:58:35 +09002290status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2291{
2292 int got = readParcelFileDescriptor();
2293
2294 if (got == BAD_TYPE) {
2295 return BAD_TYPE;
2296 }
2297
2298 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2299
2300 if (val->get() < 0) {
2301 return BAD_VALUE;
2302 }
2303
2304 return OK;
2305}
Casey Dahlin06673e32015-11-23 13:24:23 -08002306
Christopher Wiley2cf19952016-04-11 11:09:37 -07002307status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002308 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2309}
2310
Christopher Wiley2cf19952016-04-11 11:09:37 -07002311status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002312 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2313}
2314
Jeff Brown5707dbf2011-09-23 21:17:56 -07002315status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2316{
Jeff Brown13b16042014-11-11 16:44:25 -08002317 int32_t blobType;
2318 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002319 if (status) return status;
2320
Jeff Brown13b16042014-11-11 16:44:25 -08002321 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002322 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002323 const void* ptr = readInplace(len);
2324 if (!ptr) return BAD_VALUE;
2325
Jeff Brown13b16042014-11-11 16:44:25 -08002326 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002327 return NO_ERROR;
2328 }
2329
Steve Block6807e592011-10-20 11:56:00 +01002330 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002331 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002332 int fd = readFileDescriptor();
2333 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2334
Yi Kongfdd8da92018-06-07 17:52:27 -07002335 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002336 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002337 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002338
Jeff Brown13b16042014-11-11 16:44:25 -08002339 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002340 return NO_ERROR;
2341}
2342
Mathias Agopiane1424282013-07-29 21:24:40 -07002343status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002344{
2345 // size
2346 const size_t len = this->readInt32();
2347 const size_t fd_count = this->readInt32();
2348
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002349 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002350 // don't accept size_t values which may have come from an
2351 // inadvertent conversion from a negative int.
2352 return BAD_VALUE;
2353 }
2354
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002355 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002356 void const* const buf = this->readInplace(pad_size(len));
Yi Kongfdd8da92018-06-07 17:52:27 -07002357 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002358 return BAD_VALUE;
2359
Yi Kongfdd8da92018-06-07 17:52:27 -07002360 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002361 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002362 fds = new (std::nothrow) int[fd_count];
2363 if (fds == nullptr) {
2364 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2365 return BAD_VALUE;
2366 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002367 }
2368
2369 status_t err = NO_ERROR;
2370 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002371 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002372 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002373 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002374 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 -07002375 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2376 // Close all the file descriptors that were dup-ed.
2377 for (size_t j=0; j<i ;j++) {
2378 close(fds[j]);
2379 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002380 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002381 }
2382
2383 if (err == NO_ERROR) {
2384 err = val.unflatten(buf, len, fds, fd_count);
2385 }
2386
2387 if (fd_count) {
2388 delete [] fds;
2389 }
2390
2391 return err;
2392}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002393const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2394{
2395 const size_t DPOS = mDataPos;
2396 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2397 const flat_binder_object* obj
2398 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2399 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002400 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002401 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002402 // the object list, so we don't want to check for it when
2403 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002404 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002405 return obj;
2406 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002407
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002408 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002409 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002410 const size_t N = mObjectsSize;
2411 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002412
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002413 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002414 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002415 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002416
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002417 // Start at the current hint position, looking for an object at
2418 // the current data position.
2419 if (opos < N) {
2420 while (opos < (N-1) && OBJS[opos] < DPOS) {
2421 opos++;
2422 }
2423 } else {
2424 opos = N-1;
2425 }
2426 if (OBJS[opos] == DPOS) {
2427 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002428 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002429 this, DPOS, opos);
2430 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002431 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002432 return obj;
2433 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002434
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435 // Look backwards for it...
2436 while (opos > 0 && OBJS[opos] > DPOS) {
2437 opos--;
2438 }
2439 if (OBJS[opos] == DPOS) {
2440 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002441 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002442 this, DPOS, opos);
2443 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002444 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002445 return obj;
2446 }
2447 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002448 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 -07002449 this, DPOS);
2450 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002451 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002452}
2453
2454void Parcel::closeFileDescriptors()
2455{
2456 size_t i = mObjectsSize;
2457 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002458 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002459 }
2460 while (i > 0) {
2461 i--;
2462 const flat_binder_object* flat
2463 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002464 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002465 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002466 close(flat->handle);
2467 }
2468 }
2469}
2470
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002471uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002472{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002473 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002474}
2475
2476size_t Parcel::ipcDataSize() const
2477{
2478 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2479}
2480
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002481uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002482{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002483 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002484}
2485
2486size_t Parcel::ipcObjectsCount() const
2487{
2488 return mObjectsSize;
2489}
2490
2491void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002492 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002493{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002494 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002495 freeDataNoInit();
2496 mError = NO_ERROR;
2497 mData = const_cast<uint8_t*>(data);
2498 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002499 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002500 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002501 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002502 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002503 mObjectsSize = mObjectsCapacity = objectsCount;
2504 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002505 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002506 mOwner = relFunc;
2507 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002508 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002509 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002510 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002511 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002512 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002513 mObjectsSize = 0;
2514 break;
2515 }
2516 minOffset = offset + sizeof(flat_binder_object);
2517 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002518 scanForFds();
2519}
2520
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002521void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002522{
2523 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002524
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002525 if (errorCheck() != NO_ERROR) {
2526 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002527 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002528 } else if (dataSize() > 0) {
2529 const uint8_t* DATA = data();
2530 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002531 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002532 const size_t N = objectsCount();
2533 for (size_t i=0; i<N; i++) {
2534 const flat_binder_object* flat
2535 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2536 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002537 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002538 << " = " << flat->binder;
2539 }
2540 } else {
2541 to << "NULL";
2542 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002543
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544 to << ")";
2545}
2546
2547void Parcel::releaseObjects()
2548{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002549 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002550 if (i == 0) {
2551 return;
2552 }
2553 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002554 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002555 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002556 while (i > 0) {
2557 i--;
2558 const flat_binder_object* flat
2559 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002560 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002561 }
2562}
2563
2564void Parcel::acquireObjects()
2565{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002566 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002567 if (i == 0) {
2568 return;
2569 }
2570 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002571 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002572 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002573 while (i > 0) {
2574 i--;
2575 const flat_binder_object* flat
2576 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002577 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002578 }
2579}
2580
2581void Parcel::freeData()
2582{
2583 freeDataNoInit();
2584 initState();
2585}
2586
2587void Parcel::freeDataNoInit()
2588{
2589 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002590 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002591 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002592 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2593 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002594 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002595 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002596 if (mData) {
2597 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002598 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002599 if (mDataCapacity <= gParcelGlobalAllocSize) {
2600 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2601 } else {
2602 gParcelGlobalAllocSize = 0;
2603 }
2604 if (gParcelGlobalAllocCount > 0) {
2605 gParcelGlobalAllocCount--;
2606 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002607 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002608 free(mData);
2609 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002610 if (mObjects) free(mObjects);
2611 }
2612}
2613
2614status_t Parcel::growData(size_t len)
2615{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002616 if (len > INT32_MAX) {
2617 // don't accept size_t values which may have come from an
2618 // inadvertent conversion from a negative int.
2619 return BAD_VALUE;
2620 }
2621
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002622 size_t newSize = ((mDataSize+len)*3)/2;
2623 return (newSize <= mDataSize)
2624 ? (status_t) NO_MEMORY
2625 : continueWrite(newSize);
2626}
2627
2628status_t Parcel::restartWrite(size_t desired)
2629{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002630 if (desired > INT32_MAX) {
2631 // don't accept size_t values which may have come from an
2632 // inadvertent conversion from a negative int.
2633 return BAD_VALUE;
2634 }
2635
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002636 if (mOwner) {
2637 freeData();
2638 return continueWrite(desired);
2639 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002640
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002641 uint8_t* data = (uint8_t*)realloc(mData, desired);
2642 if (!data && desired > mDataCapacity) {
2643 mError = NO_MEMORY;
2644 return NO_MEMORY;
2645 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002646
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002647 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002648
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002649 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002650 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002651 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002652 gParcelGlobalAllocSize += desired;
2653 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002654 if (!mData) {
2655 gParcelGlobalAllocCount++;
2656 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002657 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002658 mData = data;
2659 mDataCapacity = desired;
2660 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002661
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002662 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002663 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2664 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2665
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002666 free(mObjects);
Yi Kongfdd8da92018-06-07 17:52:27 -07002667 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002668 mObjectsSize = mObjectsCapacity = 0;
2669 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002670 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002671 mHasFds = false;
2672 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002673 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002674
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002675 return NO_ERROR;
2676}
2677
2678status_t Parcel::continueWrite(size_t desired)
2679{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002680 if (desired > INT32_MAX) {
2681 // don't accept size_t values which may have come from an
2682 // inadvertent conversion from a negative int.
2683 return BAD_VALUE;
2684 }
2685
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002686 // If shrinking, first adjust for any objects that appear
2687 // after the new data size.
2688 size_t objectsSize = mObjectsSize;
2689 if (desired < mDataSize) {
2690 if (desired == 0) {
2691 objectsSize = 0;
2692 } else {
2693 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002694 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002695 break;
2696 objectsSize--;
2697 }
2698 }
2699 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002700
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002701 if (mOwner) {
2702 // If the size is going to zero, just release the owner's data.
2703 if (desired == 0) {
2704 freeData();
2705 return NO_ERROR;
2706 }
2707
2708 // If there is a different owner, we need to take
2709 // posession.
2710 uint8_t* data = (uint8_t*)malloc(desired);
2711 if (!data) {
2712 mError = NO_MEMORY;
2713 return NO_MEMORY;
2714 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002715 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002716
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002717 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002718 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002719 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002720 free(data);
2721
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002722 mError = NO_MEMORY;
2723 return NO_MEMORY;
2724 }
2725
2726 // Little hack to only acquire references on objects
2727 // we will be keeping.
2728 size_t oldObjectsSize = mObjectsSize;
2729 mObjectsSize = objectsSize;
2730 acquireObjects();
2731 mObjectsSize = oldObjectsSize;
2732 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002733
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002734 if (mData) {
2735 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2736 }
2737 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002738 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002739 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002740 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002741 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kongfdd8da92018-06-07 17:52:27 -07002742 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002743
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002744 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002745 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002746 gParcelGlobalAllocSize += desired;
2747 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002748 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002749
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002750 mData = data;
2751 mObjects = objects;
2752 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002753 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002754 mDataCapacity = desired;
2755 mObjectsSize = mObjectsCapacity = objectsSize;
2756 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002757 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002758
2759 } else if (mData) {
2760 if (objectsSize < mObjectsSize) {
2761 // Need to release refs on any objects we are dropping.
2762 const sp<ProcessState> proc(ProcessState::self());
2763 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2764 const flat_binder_object* flat
2765 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002766 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002767 // will need to rescan because we may have lopped off the only FDs
2768 mFdsKnown = false;
2769 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002770 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002771 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002772 binder_size_t* objects =
2773 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002774 if (objects) {
2775 mObjects = objects;
2776 }
2777 mObjectsSize = objectsSize;
2778 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002779 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002780 }
2781
2782 // We own the data, so we can just do a realloc().
2783 if (desired > mDataCapacity) {
2784 uint8_t* data = (uint8_t*)realloc(mData, desired);
2785 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002786 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2787 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002788 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002789 gParcelGlobalAllocSize += desired;
2790 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002791 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002792 mData = data;
2793 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002794 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002795 mError = NO_MEMORY;
2796 return NO_MEMORY;
2797 }
2798 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002799 if (mDataSize > desired) {
2800 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002801 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002802 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002803 if (mDataPos > desired) {
2804 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002805 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002806 }
2807 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002808
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002809 } else {
2810 // This is the first data. Easy!
2811 uint8_t* data = (uint8_t*)malloc(desired);
2812 if (!data) {
2813 mError = NO_MEMORY;
2814 return NO_MEMORY;
2815 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002816
Yi Kongfdd8da92018-06-07 17:52:27 -07002817 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002818 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002819 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002820 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002821
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002822 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002823 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002824 gParcelGlobalAllocSize += desired;
2825 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002826 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002827
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002828 mData = data;
2829 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002830 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2831 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002832 mDataCapacity = desired;
2833 }
2834
2835 return NO_ERROR;
2836}
2837
2838void Parcel::initState()
2839{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002840 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002841 mError = NO_ERROR;
Yi Kongfdd8da92018-06-07 17:52:27 -07002842 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002843 mDataSize = 0;
2844 mDataCapacity = 0;
2845 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002846 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2847 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kongfdd8da92018-06-07 17:52:27 -07002848 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002849 mObjectsSize = 0;
2850 mObjectsCapacity = 0;
2851 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002852 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002853 mHasFds = false;
2854 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002855 mAllowFds = true;
Yi Kongfdd8da92018-06-07 17:52:27 -07002856 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002857 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002858
2859 // racing multiple init leads only to multiple identical write
2860 if (gMaxFds == 0) {
2861 struct rlimit result;
2862 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2863 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002864 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002865 } else {
2866 ALOGW("Unable to getrlimit: %s", strerror(errno));
2867 gMaxFds = 1024;
2868 }
2869 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002870}
2871
2872void Parcel::scanForFds() const
2873{
2874 bool hasFds = false;
2875 for (size_t i=0; i<mObjectsSize; i++) {
2876 const flat_binder_object* flat
2877 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002878 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002879 hasFds = true;
2880 break;
2881 }
2882 }
2883 mHasFds = hasFds;
2884 mFdsKnown = true;
2885}
2886
Dan Sandleraa5c2342015-04-10 10:08:45 -04002887size_t Parcel::getBlobAshmemSize() const
2888{
Adrian Roos6bb31142015-10-22 16:46:12 -07002889 // This used to return the size of all blobs that were written to ashmem, now we're returning
2890 // the ashmem currently referenced by this Parcel, which should be equivalent.
2891 // TODO: Remove method once ABI can be changed.
2892 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002893}
2894
Adrian Rooscbf37262015-10-22 16:12:53 -07002895size_t Parcel::getOpenAshmemSize() const
2896{
2897 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002898}
2899
2900// --- Parcel::Blob ---
2901
2902Parcel::Blob::Blob() :
Yi Kongfdd8da92018-06-07 17:52:27 -07002903 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002904}
2905
2906Parcel::Blob::~Blob() {
2907 release();
2908}
2909
2910void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002911 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002912 ::munmap(mData, mSize);
2913 }
2914 clear();
2915}
2916
Jeff Brown13b16042014-11-11 16:44:25 -08002917void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2918 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002919 mData = data;
2920 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002921 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002922}
2923
2924void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002925 mFd = -1;
Yi Kongfdd8da92018-06-07 17:52:27 -07002926 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002927 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002928 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002929}
2930
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002931}; // namespace android