blob: a0a634a7ecfd8093ce4e7e9d0c63309b41cb0eda [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080023#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080028#include <sys/stat.h>
29#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070030#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080031#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070032
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/Binder.h>
34#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080035#include <binder/IPCThreadState.h>
36#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070037#include <binder/ProcessState.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080038#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070039#include <binder/TextOutput.h>
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -080040#include <binder/Value.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041
Mark Salyzynabed7f72016-01-27 08:02:48 -080042#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070043#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080044#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080046#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String8.h>
48#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049
Mathias Agopian208059f2009-05-18 15:08:03 -070050#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080051#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053#ifndef INT32_MAX
54#define INT32_MAX ((int32_t)(2147483647))
55#endif
56
57#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080058//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080059#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080060//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070061
62// ---------------------------------------------------------------------------
63
Nick Kralevichb6b14232015-04-02 09:36:02 -070064// This macro should never be used at runtime, as a too large value
65// of s could cause an integer overflow. Instead, you should always
66// use the wrapper function pad_size()
67#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
68
69static size_t pad_size(size_t s) {
70 if (s > (SIZE_T_MAX - 3)) {
71 abort();
72 }
73 return PAD_SIZE_UNSAFE(s);
74}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070076// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060077#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070078
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070079// XXX This can be made public if we want to provide
80// support for typed data.
81struct small_flat_data
82{
83 uint32_t type;
84 uint32_t data;
85};
86
87namespace android {
88
Dianne Hackborna4cff882014-11-13 17:07:40 -080089static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
90static size_t gParcelGlobalAllocSize = 0;
91static size_t gParcelGlobalAllocCount = 0;
92
Christopher Tatee4e0ae82016-03-24 16:03:44 -070093static size_t gMaxFds = 0;
94
Jeff Brown13b16042014-11-11 16:44:25 -080095// Maximum size of a blob to transfer in-place.
96static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
97
98enum {
99 BLOB_INPLACE = 0,
100 BLOB_ASHMEM_IMMUTABLE = 1,
101 BLOB_ASHMEM_MUTABLE = 2,
102};
103
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700105 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700107 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108 case BINDER_TYPE_BINDER:
109 if (obj.binder) {
110 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800111 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700112 }
113 return;
114 case BINDER_TYPE_WEAK_BINDER:
115 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800116 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700117 return;
118 case BINDER_TYPE_HANDLE: {
119 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700120 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
122 b->incStrong(who);
123 }
124 return;
125 }
126 case BINDER_TYPE_WEAK_HANDLE: {
127 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700128 if (b != nullptr) b.get_refs()->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700129 return;
130 }
131 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000132 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700133 // If we own an ashmem fd, keep track of how much memory it refers to.
134 int size = ashmem_get_size_region(obj.handle);
135 if (size > 0) {
136 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700137 }
138 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 return;
140 }
141 }
142
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700143 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144}
145
Adrian Roos6bb31142015-10-22 16:46:12 -0700146void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 const flat_binder_object& obj, const void* who)
148{
Yi Kong91635562018-06-07 14:38:36 -0700149 acquire_object(proc, obj, who, nullptr);
Adrian Roos6bb31142015-10-22 16:46:12 -0700150}
151
152static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700153 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700154{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700155 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700156 case BINDER_TYPE_BINDER:
157 if (obj.binder) {
158 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800159 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 }
161 return;
162 case BINDER_TYPE_WEAK_BINDER:
163 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800164 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165 return;
166 case BINDER_TYPE_HANDLE: {
167 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700168 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
170 b->decStrong(who);
171 }
172 return;
173 }
174 case BINDER_TYPE_WEAK_HANDLE: {
175 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700176 if (b != nullptr) b.get_refs()->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700177 return;
178 }
179 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800180 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000181 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700182 int size = ashmem_get_size_region(obj.handle);
183 if (size > 0) {
184 *outAshmemSize -= size;
Adrian Roos6bb31142015-10-22 16:46:12 -0700185 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700186 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800187
188 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700189 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700190 return;
191 }
192 }
193
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700194 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700195}
196
Adrian Roos6bb31142015-10-22 16:46:12 -0700197void release_object(const sp<ProcessState>& proc,
198 const flat_binder_object& obj, const void* who)
199{
Yi Kong91635562018-06-07 14:38:36 -0700200 release_object(proc, obj, who, nullptr);
Adrian Roos6bb31142015-10-22 16:46:12 -0700201}
202
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700203inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800204 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205{
206 return out->writeObject(flat, false);
207}
208
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800209status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700210 const sp<IBinder>& binder, Parcel* out)
211{
212 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700213
Martijn Coenen2b631742017-05-05 11:16:59 -0700214 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
215 /* minimum priority for all nodes is nice 0 */
216 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
217 } else {
218 /* minimum priority for all nodes is MAX_NICE(19) */
219 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
220 }
221
Yi Kong91635562018-06-07 14:38:36 -0700222 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 IBinder *local = binder->localBinder();
224 if (!local) {
225 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700226 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000227 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700228 }
229 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700230 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800231 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700232 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800233 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700234 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700235 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800236 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
237 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238 }
239 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700240 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800241 obj.binder = 0;
242 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700244
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700245 return finish_flatten_binder(binder, obj, out);
246}
247
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800248status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700249 const wp<IBinder>& binder, Parcel* out)
250{
251 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700252
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700253 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Yi Kong91635562018-06-07 14:38:36 -0700254 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255 sp<IBinder> real = binder.promote();
Yi Kong91635562018-06-07 14:38:36 -0700256 if (real != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 IBinder *local = real->localBinder();
258 if (!local) {
259 BpBinder *proxy = real->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700260 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000261 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700262 }
263 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700264 obj.hdr.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800265 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700266 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800267 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700268 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700269 obj.hdr.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800270 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
271 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700272 }
273 return finish_flatten_binder(real, obj, out);
274 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700275
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700276 // XXX How to deal? In order to flatten the given binder,
277 // we need to probe it for information, which requires a primary
278 // reference... but we don't have one.
279 //
280 // The OpenBinder implementation uses a dynamic_cast<> here,
281 // but we can't do that with the different reference counting
282 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000283 ALOGE("Unable to unflatten Binder weak reference!");
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700284 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800285 obj.binder = 0;
286 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700287 return finish_flatten_binder(nullptr, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700288
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700289 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700290 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800291 obj.binder = 0;
292 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700293 return finish_flatten_binder(nullptr, obj, out);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700294 }
295}
296
297inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800298 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
299 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700300{
301 return NO_ERROR;
302}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700303
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700304status_t unflatten_binder(const sp<ProcessState>& proc,
305 const Parcel& in, sp<IBinder>* out)
306{
307 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700308
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700309 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700310 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700311 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800312 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700313 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700314 case BINDER_TYPE_HANDLE:
315 *out = proc->getStrongProxyForHandle(flat->handle);
316 return finish_unflatten_binder(
317 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700318 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700319 }
320 return BAD_TYPE;
321}
322
323status_t unflatten_binder(const sp<ProcessState>& proc,
324 const Parcel& in, wp<IBinder>* out)
325{
326 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700327
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700328 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700329 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700330 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800331 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700332 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700333 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800334 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700335 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800336 reinterpret_cast<IBinder*>(flat->cookie),
337 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700338 } else {
Yi Kong91635562018-06-07 14:38:36 -0700339 *out = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700340 }
Yi Kong91635562018-06-07 14:38:36 -0700341 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700342 case BINDER_TYPE_HANDLE:
343 case BINDER_TYPE_WEAK_HANDLE:
344 *out = proc->getWeakProxyForHandle(flat->handle);
345 return finish_unflatten_binder(
346 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
347 }
348 }
349 return BAD_TYPE;
350}
351
352// ---------------------------------------------------------------------------
353
354Parcel::Parcel()
355{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800356 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700357 initState();
358}
359
360Parcel::~Parcel()
361{
362 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800363 LOG_ALLOC("Parcel %p: destroyed", this);
364}
365
366size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800367 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
368 size_t size = gParcelGlobalAllocSize;
369 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
370 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800371}
372
373size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800374 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
375 size_t count = gParcelGlobalAllocCount;
376 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
377 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700378}
379
380const uint8_t* Parcel::data() const
381{
382 return mData;
383}
384
385size_t Parcel::dataSize() const
386{
387 return (mDataSize > mDataPos ? mDataSize : mDataPos);
388}
389
390size_t Parcel::dataAvail() const
391{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700392 size_t result = dataSize() - dataPosition();
393 if (result > INT32_MAX) {
394 abort();
395 }
396 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700397}
398
399size_t Parcel::dataPosition() const
400{
401 return mDataPos;
402}
403
404size_t Parcel::dataCapacity() const
405{
406 return mDataCapacity;
407}
408
409status_t Parcel::setDataSize(size_t size)
410{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700411 if (size > INT32_MAX) {
412 // don't accept size_t values which may have come from an
413 // inadvertent conversion from a negative int.
414 return BAD_VALUE;
415 }
416
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700417 status_t err;
418 err = continueWrite(size);
419 if (err == NO_ERROR) {
420 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700421 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700422 }
423 return err;
424}
425
426void Parcel::setDataPosition(size_t pos) const
427{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700428 if (pos > INT32_MAX) {
429 // don't accept size_t values which may have come from an
430 // inadvertent conversion from a negative int.
431 abort();
432 }
433
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 mDataPos = pos;
435 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800436 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437}
438
439status_t Parcel::setDataCapacity(size_t size)
440{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700441 if (size > INT32_MAX) {
442 // don't accept size_t values which may have come from an
443 // inadvertent conversion from a negative int.
444 return BAD_VALUE;
445 }
446
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700447 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 return NO_ERROR;
449}
450
451status_t Parcel::setData(const uint8_t* buffer, size_t len)
452{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700453 if (len > INT32_MAX) {
454 // don't accept size_t values which may have come from an
455 // inadvertent conversion from a negative int.
456 return BAD_VALUE;
457 }
458
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 status_t err = restartWrite(len);
460 if (err == NO_ERROR) {
461 memcpy(const_cast<uint8_t*>(data()), buffer, len);
462 mDataSize = len;
463 mFdsKnown = false;
464 }
465 return err;
466}
467
Andreas Huber51faf462011-04-13 10:21:56 -0700468status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700469{
470 const sp<ProcessState> proc(ProcessState::self());
471 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700472 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800473 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700474 size_t size = parcel->mObjectsSize;
475 int startPos = mDataPos;
476 int firstIndex = -1, lastIndex = -2;
477
478 if (len == 0) {
479 return NO_ERROR;
480 }
481
Nick Kralevichb6b14232015-04-02 09:36:02 -0700482 if (len > INT32_MAX) {
483 // don't accept size_t values which may have come from an
484 // inadvertent conversion from a negative int.
485 return BAD_VALUE;
486 }
487
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700488 // range checks against the source parcel size
489 if ((offset > parcel->mDataSize)
490 || (len > parcel->mDataSize)
491 || (offset + len > parcel->mDataSize)) {
492 return BAD_VALUE;
493 }
494
495 // Count objects in range
496 for (int i = 0; i < (int) size; i++) {
497 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700498 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700499 if (firstIndex == -1) {
500 firstIndex = i;
501 }
502 lastIndex = i;
503 }
504 }
505 int numObjects = lastIndex - firstIndex + 1;
506
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700507 if ((mDataSize+len) > mDataCapacity) {
508 // grow data
509 err = growData(len);
510 if (err != NO_ERROR) {
511 return err;
512 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700513 }
514
515 // append data
516 memcpy(mData + mDataPos, data + offset, len);
517 mDataPos += len;
518 mDataSize += len;
519
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400520 err = NO_ERROR;
521
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700522 if (numObjects > 0) {
523 // grow objects
524 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700525 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700526 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800527 binder_size_t *objects =
528 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700529 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700530 return NO_MEMORY;
531 }
532 mObjects = objects;
533 mObjectsCapacity = newSize;
534 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700535
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700536 // append and acquire objects
537 int idx = mObjectsSize;
538 for (int i = firstIndex; i <= lastIndex; i++) {
539 size_t off = objects[i] - offset + startPos;
540 mObjects[idx++] = off;
541 mObjectsSize++;
542
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700543 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700544 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700545 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700546
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700547 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700548 // If this is a file descriptor, we need to dup it so the
549 // new Parcel now owns its own fd, and can declare that we
550 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800551 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800552 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700553 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400554 if (!mAllowFds) {
555 err = FDS_NOT_ALLOWED;
556 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700557 }
558 }
559 }
560
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400561 return err;
562}
563
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700564int Parcel::compareData(const Parcel& other) {
565 size_t size = dataSize();
566 if (size != other.dataSize()) {
567 return size < other.dataSize() ? -1 : 1;
568 }
569 return memcmp(data(), other.data(), size);
570}
571
Jeff Brown13b16042014-11-11 16:44:25 -0800572bool Parcel::allowFds() const
573{
574 return mAllowFds;
575}
576
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700577bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400578{
579 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700580 if (!allowFds) {
581 mAllowFds = false;
582 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400583 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700584}
585
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700586void Parcel::restoreAllowFds(bool lastValue)
587{
588 mAllowFds = lastValue;
589}
590
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700591bool Parcel::hasFileDescriptors() const
592{
593 if (!mFdsKnown) {
594 scanForFds();
595 }
596 return mHasFds;
597}
598
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700599// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700600status_t Parcel::writeInterfaceToken(const String16& interface)
601{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000602 const IPCThreadState* threadState = IPCThreadState::self();
603 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
604 writeInt32(threadState->shouldPropagateWorkSource() ?
605 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700606 // currently the interface identification token is just its name as a string
607 return writeString16(interface);
608}
609
Mathias Agopian83c04462009-05-22 19:00:22 -0700610bool Parcel::checkInterface(IBinder* binder) const
611{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700612 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700613}
614
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700615bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700616 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700617{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100618 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700619 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700620 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700621 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700622 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700623 if ((threadState->getLastTransactionBinderFlags() &
624 IBinder::FLAG_ONEWAY) != 0) {
625 // For one-way calls, the callee is running entirely
626 // disconnected from the caller, so disable StrictMode entirely.
627 // Not only does disk/network usage not impact the caller, but
628 // there's no way to commuicate back any violations anyway.
629 threadState->setStrictModePolicy(0);
630 } else {
631 threadState->setStrictModePolicy(strictPolicy);
632 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100633 // WorkSource.
634 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000635 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100636 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700637 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700638 if (str == interface) {
639 return true;
640 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700641 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700642 String8(interface).string(), String8(str).string());
643 return false;
644 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700645}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700646
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800647const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700648{
649 return mObjects;
650}
651
652size_t Parcel::objectsCount() const
653{
654 return mObjectsSize;
655}
656
657status_t Parcel::errorCheck() const
658{
659 return mError;
660}
661
662void Parcel::setError(status_t err)
663{
664 mError = err;
665}
666
667status_t Parcel::finishWrite(size_t len)
668{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700669 if (len > INT32_MAX) {
670 // don't accept size_t values which may have come from an
671 // inadvertent conversion from a negative int.
672 return BAD_VALUE;
673 }
674
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700675 //printf("Finish write of %d\n", len);
676 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700677 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700678 if (mDataPos > mDataSize) {
679 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700680 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700681 }
682 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
683 return NO_ERROR;
684}
685
686status_t Parcel::writeUnpadded(const void* data, size_t len)
687{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700688 if (len > INT32_MAX) {
689 // don't accept size_t values which may have come from an
690 // inadvertent conversion from a negative int.
691 return BAD_VALUE;
692 }
693
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700694 size_t end = mDataPos + len;
695 if (end < mDataPos) {
696 // integer overflow
697 return BAD_VALUE;
698 }
699
700 if (end <= mDataCapacity) {
701restart_write:
702 memcpy(mData+mDataPos, data, len);
703 return finishWrite(len);
704 }
705
706 status_t err = growData(len);
707 if (err == NO_ERROR) goto restart_write;
708 return err;
709}
710
711status_t Parcel::write(const void* data, size_t len)
712{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700713 if (len > INT32_MAX) {
714 // don't accept size_t values which may have come from an
715 // inadvertent conversion from a negative int.
716 return BAD_VALUE;
717 }
718
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700719 void* const d = writeInplace(len);
720 if (d) {
721 memcpy(d, data, len);
722 return NO_ERROR;
723 }
724 return mError;
725}
726
727void* Parcel::writeInplace(size_t len)
728{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700729 if (len > INT32_MAX) {
730 // don't accept size_t values which may have come from an
731 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700732 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700733 }
734
735 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700736
737 // sanity check for integer overflow
738 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700739 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700740 }
741
742 if ((mDataPos+padded) <= mDataCapacity) {
743restart_write:
744 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
745 uint8_t* const data = mData+mDataPos;
746
747 // Need to pad at end?
748 if (padded != len) {
749#if BYTE_ORDER == BIG_ENDIAN
750 static const uint32_t mask[4] = {
751 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
752 };
753#endif
754#if BYTE_ORDER == LITTLE_ENDIAN
755 static const uint32_t mask[4] = {
756 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
757 };
758#endif
759 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
760 // *reinterpret_cast<void**>(data+padded-4));
761 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
762 }
763
764 finishWrite(padded);
765 return data;
766 }
767
768 status_t err = growData(padded);
769 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700770 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700771}
772
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800773status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
774 const uint8_t* strData = (uint8_t*)str.data();
775 const size_t strLen= str.length();
776 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100777 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800778 return BAD_VALUE;
779 }
780
781 status_t err = writeInt32(utf16Len);
782 if (err) {
783 return err;
784 }
785
786 // Allocate enough bytes to hold our converted string and its terminating NULL.
787 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
788 if (!dst) {
789 return NO_MEMORY;
790 }
791
Sergio Girof4607432016-07-21 14:46:35 +0100792 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800793
794 return NO_ERROR;
795}
796
797status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
798 if (!str) {
799 return writeInt32(-1);
800 }
801 return writeUtf8AsUtf16(*str);
802}
803
Casey Dahlin185d3442016-02-09 11:08:35 -0800804namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800805
Casey Dahlin185d3442016-02-09 11:08:35 -0800806template<typename T>
807status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700808{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700809 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700810 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700811 status = BAD_VALUE;
812 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700813 }
814
Casey Dahlin185d3442016-02-09 11:08:35 -0800815 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700816 if (status != OK) {
817 return status;
818 }
819
Casey Dahlin185d3442016-02-09 11:08:35 -0800820 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700821 if (!data) {
822 status = BAD_VALUE;
823 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700824 }
825
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700826 memcpy(data, val.data(), val.size());
827 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700828}
829
Casey Dahlin185d3442016-02-09 11:08:35 -0800830template<typename T>
831status_t writeByteVectorInternalPtr(Parcel* parcel,
832 const std::unique_ptr<std::vector<T>>& val)
833{
834 if (!val) {
835 return parcel->writeInt32(-1);
836 }
837
838 return writeByteVectorInternal(parcel, *val);
839}
840
841} // namespace
842
843status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
844 return writeByteVectorInternal(this, val);
845}
846
847status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
848{
849 return writeByteVectorInternalPtr(this, val);
850}
851
852status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
853 return writeByteVectorInternal(this, val);
854}
855
856status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
857{
858 return writeByteVectorInternalPtr(this, val);
859}
860
Casey Dahlin451ff582015-10-19 18:12:18 -0700861status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
862{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800863 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700864}
865
Casey Dahlinb9872622015-11-25 15:09:45 -0800866status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
867{
868 return writeNullableTypedVector(val, &Parcel::writeInt32);
869}
870
Casey Dahlin451ff582015-10-19 18:12:18 -0700871status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
872{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800873 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700874}
875
Casey Dahlinb9872622015-11-25 15:09:45 -0800876status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
877{
878 return writeNullableTypedVector(val, &Parcel::writeInt64);
879}
880
Casey Dahlin451ff582015-10-19 18:12:18 -0700881status_t Parcel::writeFloatVector(const std::vector<float>& val)
882{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800883 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700884}
885
Casey Dahlinb9872622015-11-25 15:09:45 -0800886status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
887{
888 return writeNullableTypedVector(val, &Parcel::writeFloat);
889}
890
Casey Dahlin451ff582015-10-19 18:12:18 -0700891status_t Parcel::writeDoubleVector(const std::vector<double>& val)
892{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800893 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700894}
895
Casey Dahlinb9872622015-11-25 15:09:45 -0800896status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
897{
898 return writeNullableTypedVector(val, &Parcel::writeDouble);
899}
900
Casey Dahlin451ff582015-10-19 18:12:18 -0700901status_t Parcel::writeBoolVector(const std::vector<bool>& val)
902{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800903 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700904}
905
Casey Dahlinb9872622015-11-25 15:09:45 -0800906status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
907{
908 return writeNullableTypedVector(val, &Parcel::writeBool);
909}
910
Casey Dahlin451ff582015-10-19 18:12:18 -0700911status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
912{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800913 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700914}
915
Casey Dahlinb9872622015-11-25 15:09:45 -0800916status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
917{
918 return writeNullableTypedVector(val, &Parcel::writeChar);
919}
920
Casey Dahlin451ff582015-10-19 18:12:18 -0700921status_t Parcel::writeString16Vector(const std::vector<String16>& val)
922{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800923 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700924}
925
Casey Dahlinb9872622015-11-25 15:09:45 -0800926status_t Parcel::writeString16Vector(
927 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
928{
929 return writeNullableTypedVector(val, &Parcel::writeString16);
930}
931
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800932status_t Parcel::writeUtf8VectorAsUtf16Vector(
933 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
934 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
935}
936
937status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
938 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
939}
940
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700941status_t Parcel::writeInt32(int32_t val)
942{
Andreas Huber84a6d042009-08-17 13:33:27 -0700943 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700944}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800945
946status_t Parcel::writeUint32(uint32_t val)
947{
948 return writeAligned(val);
949}
950
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700951status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700952 if (len > INT32_MAX) {
953 // don't accept size_t values which may have come from an
954 // inadvertent conversion from a negative int.
955 return BAD_VALUE;
956 }
957
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700958 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700959 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700960 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700961 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700962 if (ret == NO_ERROR) {
963 ret = write(val, len * sizeof(*val));
964 }
965 return ret;
966}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700967status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700968 if (len > INT32_MAX) {
969 // don't accept size_t values which may have come from an
970 // inadvertent conversion from a negative int.
971 return BAD_VALUE;
972 }
973
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700974 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700975 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700976 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700977 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700978 if (ret == NO_ERROR) {
979 ret = write(val, len * sizeof(*val));
980 }
981 return ret;
982}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700983
Casey Dahlind6848f52015-10-15 15:44:59 -0700984status_t Parcel::writeBool(bool val)
985{
986 return writeInt32(int32_t(val));
987}
988
989status_t Parcel::writeChar(char16_t val)
990{
991 return writeInt32(int32_t(val));
992}
993
994status_t Parcel::writeByte(int8_t val)
995{
996 return writeInt32(int32_t(val));
997}
998
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700999status_t Parcel::writeInt64(int64_t val)
1000{
Andreas Huber84a6d042009-08-17 13:33:27 -07001001 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001002}
1003
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001004status_t Parcel::writeUint64(uint64_t val)
1005{
1006 return writeAligned(val);
1007}
1008
Serban Constantinescuf683e012013-11-05 16:53:55 +00001009status_t Parcel::writePointer(uintptr_t val)
1010{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001011 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001012}
1013
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001014status_t Parcel::writeFloat(float val)
1015{
Andreas Huber84a6d042009-08-17 13:33:27 -07001016 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001017}
1018
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001019#if defined(__mips__) && defined(__mips_hard_float)
1020
1021status_t Parcel::writeDouble(double val)
1022{
1023 union {
1024 double d;
1025 unsigned long long ll;
1026 } u;
1027 u.d = val;
1028 return writeAligned(u.ll);
1029}
1030
1031#else
1032
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001033status_t Parcel::writeDouble(double val)
1034{
Andreas Huber84a6d042009-08-17 13:33:27 -07001035 return writeAligned(val);
1036}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001037
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001038#endif
1039
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001040status_t Parcel::writeCString(const char* str)
1041{
1042 return write(str, strlen(str)+1);
1043}
1044
1045status_t Parcel::writeString8(const String8& str)
1046{
1047 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001048 // only write string if its length is more than zero characters,
1049 // as readString8 will only read if the length field is non-zero.
1050 // this is slightly different from how writeString16 works.
1051 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001052 err = write(str.string(), str.bytes()+1);
1053 }
1054 return err;
1055}
1056
Casey Dahlinb9872622015-11-25 15:09:45 -08001057status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1058{
1059 if (!str) {
1060 return writeInt32(-1);
1061 }
1062
1063 return writeString16(*str);
1064}
1065
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001066status_t Parcel::writeString16(const String16& str)
1067{
1068 return writeString16(str.string(), str.size());
1069}
1070
1071status_t Parcel::writeString16(const char16_t* str, size_t len)
1072{
Yi Kong91635562018-06-07 14:38:36 -07001073 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001074
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001075 status_t err = writeInt32(len);
1076 if (err == NO_ERROR) {
1077 len *= sizeof(char16_t);
1078 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1079 if (data) {
1080 memcpy(data, str, len);
1081 *reinterpret_cast<char16_t*>(data+len) = 0;
1082 return NO_ERROR;
1083 }
1084 err = mError;
1085 }
1086 return err;
1087}
1088
1089status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1090{
1091 return flatten_binder(ProcessState::self(), val, this);
1092}
1093
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001094status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1095{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001096 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001097}
1098
Casey Dahlinb9872622015-11-25 15:09:45 -08001099status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1100{
1101 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1102}
1103
1104status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001105 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001106}
1107
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001108status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001109 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001110}
1111
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001112status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1113{
1114 return flatten_binder(ProcessState::self(), val, this);
1115}
1116
Casey Dahlinb9872622015-11-25 15:09:45 -08001117status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1118 if (!parcelable) {
1119 return writeInt32(0);
1120 }
1121
1122 return writeParcelable(*parcelable);
1123}
1124
Christopher Wiley97f048d2015-11-19 06:49:05 -08001125status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1126 status_t status = writeInt32(1); // parcelable is not null.
1127 if (status != OK) {
1128 return status;
1129 }
1130 return parcelable.writeToParcel(this);
1131}
1132
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001133status_t Parcel::writeValue(const binder::Value& value) {
1134 return value.writeToParcel(this);
1135}
1136
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001137status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001138{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001139 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001140 return BAD_TYPE;
1141
1142 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001143 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001144 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001146 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001147 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001149 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1150 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001151
1152 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001153 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001154 return err;
1155 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001156 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001157 return err;
1158}
1159
Jeff Brown93ff1f92011-11-04 19:01:44 -07001160status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001161{
1162 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001163 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001164 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001165 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001166 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001167 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001168 return writeObject(obj, true);
1169}
1170
1171status_t Parcel::writeDupFileDescriptor(int fd)
1172{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001173 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001174 if (dupFd < 0) {
1175 return -errno;
1176 }
1177 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001178 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001179 close(dupFd);
1180 }
1181 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001182}
1183
Dianne Hackborn1941a402016-08-29 12:30:43 -07001184status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1185{
1186 writeInt32(0);
1187 return writeFileDescriptor(fd, takeOwnership);
1188}
1189
Ryo Hashimotobf551892018-05-31 16:58:35 +09001190status_t Parcel::writeDupParcelFileDescriptor(int fd)
1191{
1192 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1193 if (dupFd < 0) {
1194 return -errno;
1195 }
1196 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1197 if (err != OK) {
1198 close(dupFd);
1199 }
1200 return err;
1201}
1202
Christopher Wiley2cf19952016-04-11 11:09:37 -07001203status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001204 return writeDupFileDescriptor(fd.get());
1205}
1206
Christopher Wiley2cf19952016-04-11 11:09:37 -07001207status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001208 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1209}
1210
Christopher Wiley2cf19952016-04-11 11:09:37 -07001211status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001212 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1213}
1214
Jeff Brown13b16042014-11-11 16:44:25 -08001215status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001216{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001217 if (len > INT32_MAX) {
1218 // don't accept size_t values which may have come from an
1219 // inadvertent conversion from a negative int.
1220 return BAD_VALUE;
1221 }
1222
Jeff Brown13b16042014-11-11 16:44:25 -08001223 status_t status;
1224 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001225 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001226 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001227 if (status) return status;
1228
1229 void* ptr = writeInplace(len);
1230 if (!ptr) return NO_MEMORY;
1231
Jeff Brown13b16042014-11-11 16:44:25 -08001232 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001233 return NO_ERROR;
1234 }
1235
Steve Block6807e592011-10-20 11:56:00 +01001236 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001237 int fd = ashmem_create_region("Parcel Blob", len);
1238 if (fd < 0) return NO_MEMORY;
1239
1240 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1241 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001242 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001243 } else {
Yi Kong91635562018-06-07 14:38:36 -07001244 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001245 if (ptr == MAP_FAILED) {
1246 status = -errno;
1247 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001248 if (!mutableCopy) {
1249 result = ashmem_set_prot_region(fd, PROT_READ);
1250 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001251 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001252 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001253 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001254 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001255 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001256 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001257 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001258 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001259 return NO_ERROR;
1260 }
1261 }
1262 }
1263 }
1264 ::munmap(ptr, len);
1265 }
1266 ::close(fd);
1267 return status;
1268}
1269
Jeff Brown13b16042014-11-11 16:44:25 -08001270status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1271{
1272 // Must match up with what's done in writeBlob.
1273 if (!mAllowFds) return FDS_NOT_ALLOWED;
1274 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1275 if (status) return status;
1276 return writeDupFileDescriptor(fd);
1277}
1278
Mathias Agopiane1424282013-07-29 21:24:40 -07001279status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001280{
1281 status_t err;
1282
1283 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001284 const size_t len = val.getFlattenedSize();
1285 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001286
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001287 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001288 // don't accept size_t values which may have come from an
1289 // inadvertent conversion from a negative int.
1290 return BAD_VALUE;
1291 }
1292
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001293 err = this->writeInt32(len);
1294 if (err) return err;
1295
1296 err = this->writeInt32(fd_count);
1297 if (err) return err;
1298
1299 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001300 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001301 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001302 return BAD_VALUE;
1303
Yi Kong91635562018-06-07 14:38:36 -07001304 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001305 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001306 fds = new (std::nothrow) int[fd_count];
1307 if (fds == nullptr) {
1308 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1309 return BAD_VALUE;
1310 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001311 }
1312
1313 err = val.flatten(buf, len, fds, fd_count);
1314 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1315 err = this->writeDupFileDescriptor( fds[i] );
1316 }
1317
1318 if (fd_count) {
1319 delete [] fds;
1320 }
1321
1322 return err;
1323}
1324
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001325status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1326{
1327 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1328 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1329 if (enoughData && enoughObjects) {
1330restart_write:
1331 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001332
Christopher Tate98e67d32015-06-03 18:44:15 -07001333 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001334 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001335 if (!mAllowFds) {
1336 // fail before modifying our object index
1337 return FDS_NOT_ALLOWED;
1338 }
1339 mHasFds = mFdsKnown = true;
1340 }
1341
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001342 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001343 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001344 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001345 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001346 mObjectsSize++;
1347 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001348
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001349 return finishWrite(sizeof(flat_binder_object));
1350 }
1351
1352 if (!enoughData) {
1353 const status_t err = growData(sizeof(val));
1354 if (err != NO_ERROR) return err;
1355 }
1356 if (!enoughObjects) {
1357 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001358 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001359 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001360 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001361 mObjects = objects;
1362 mObjectsCapacity = newSize;
1363 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001364
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001365 goto restart_write;
1366}
1367
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001368status_t Parcel::writeNoException()
1369{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001370 binder::Status status;
1371 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001372}
1373
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001374status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1375{
1376 using ::std::map;
1377 using ::android::binder::Value;
1378 using ::android::binder::Map;
1379
1380 Map::const_iterator iter;
1381 status_t ret;
1382
1383 ret = writeInt32(map_in.size());
1384
1385 if (ret != NO_ERROR) {
1386 return ret;
1387 }
1388
1389 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1390 ret = writeValue(Value(iter->first));
1391 if (ret != NO_ERROR) {
1392 return ret;
1393 }
1394
1395 ret = writeValue(iter->second);
1396 if (ret != NO_ERROR) {
1397 return ret;
1398 }
1399 }
1400
1401 return ret;
1402}
1403
1404status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1405{
Yi Kong91635562018-06-07 14:38:36 -07001406 if (map == nullptr) {
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001407 return writeInt32(-1);
1408 }
1409
1410 return writeMap(*map.get());
1411}
1412
1413status_t Parcel::readMap(::android::binder::Map* map_out)const
1414{
1415 using ::std::map;
1416 using ::android::String16;
1417 using ::android::String8;
1418 using ::android::binder::Value;
1419 using ::android::binder::Map;
1420
1421 status_t ret = NO_ERROR;
1422 int32_t count;
1423
1424 ret = readInt32(&count);
1425 if (ret != NO_ERROR) {
1426 return ret;
1427 }
1428
1429 if (count < 0) {
1430 ALOGE("readMap: Unexpected count: %d", count);
1431 return (count == -1)
1432 ? UNEXPECTED_NULL
1433 : BAD_VALUE;
1434 }
1435
1436 map_out->clear();
1437
1438 while (count--) {
1439 Map::key_type key;
1440 Value value;
1441
1442 ret = readValue(&value);
1443 if (ret != NO_ERROR) {
1444 return ret;
1445 }
1446
1447 if (!value.getString(&key)) {
1448 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1449 return BAD_VALUE;
1450 }
1451
1452 ret = readValue(&value);
1453 if (ret != NO_ERROR) {
1454 return ret;
1455 }
1456
1457 (*map_out)[key] = value;
1458 }
1459
1460 return ret;
1461}
1462
1463status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1464{
1465 const size_t start = dataPosition();
1466 int32_t count;
1467 status_t status = readInt32(&count);
1468 map->reset();
1469
1470 if (status != OK || count == -1) {
1471 return status;
1472 }
1473
1474 setDataPosition(start);
1475 map->reset(new binder::Map());
1476
1477 status = readMap(map->get());
1478
1479 if (status != OK) {
1480 map->reset();
1481 }
1482
1483 return status;
1484}
1485
1486
1487
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001488void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001489{
1490 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1491}
1492
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001493status_t Parcel::validateReadData(size_t upperBound) const
1494{
1495 // Don't allow non-object reads on object data
1496 if (mObjectsSorted || mObjectsSize <= 1) {
1497data_sorted:
1498 // Expect to check only against the next object
1499 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1500 // For some reason the current read position is greater than the next object
1501 // hint. Iterate until we find the right object
1502 size_t nextObject = mNextObjectHint;
1503 do {
1504 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1505 // Requested info overlaps with an object
1506 ALOGE("Attempt to read from protected data in Parcel %p", this);
1507 return PERMISSION_DENIED;
1508 }
1509 nextObject++;
1510 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1511 mNextObjectHint = nextObject;
1512 }
1513 return NO_ERROR;
1514 }
1515 // Quickly determine if mObjects is sorted.
1516 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1517 binder_size_t* prevObj = currObj;
1518 while (currObj > mObjects) {
1519 prevObj--;
1520 if(*prevObj > *currObj) {
1521 goto data_unsorted;
1522 }
1523 currObj--;
1524 }
1525 mObjectsSorted = true;
1526 goto data_sorted;
1527
1528data_unsorted:
1529 // Insertion Sort mObjects
1530 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1531 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1532 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1533 binder_size_t temp = *iter0;
1534 binder_size_t* iter1 = iter0 - 1;
1535 while (iter1 >= mObjects && *iter1 > temp) {
1536 *(iter1 + 1) = *iter1;
1537 iter1--;
1538 }
1539 *(iter1 + 1) = temp;
1540 }
1541 mNextObjectHint = 0;
1542 mObjectsSorted = true;
1543 goto data_sorted;
1544}
1545
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001546status_t Parcel::read(void* outData, size_t len) const
1547{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001548 if (len > INT32_MAX) {
1549 // don't accept size_t values which may have come from an
1550 // inadvertent conversion from a negative int.
1551 return BAD_VALUE;
1552 }
1553
1554 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1555 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001556 if (mObjectsSize > 0) {
1557 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001558 if(err != NO_ERROR) {
1559 // Still increment the data position by the expected length
1560 mDataPos += pad_size(len);
1561 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1562 return err;
1563 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001564 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001565 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001566 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001567 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001568 return NO_ERROR;
1569 }
1570 return NOT_ENOUGH_DATA;
1571}
1572
1573const void* Parcel::readInplace(size_t len) const
1574{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001575 if (len > INT32_MAX) {
1576 // don't accept size_t values which may have come from an
1577 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001578 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001579 }
1580
1581 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1582 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001583 if (mObjectsSize > 0) {
1584 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001585 if(err != NO_ERROR) {
1586 // Still increment the data position by the expected length
1587 mDataPos += pad_size(len);
1588 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001589 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001590 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001591 }
1592
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001593 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001594 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001595 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001596 return data;
1597 }
Yi Kong91635562018-06-07 14:38:36 -07001598 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001599}
1600
Andreas Huber84a6d042009-08-17 13:33:27 -07001601template<class T>
1602status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001603 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001604
1605 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001606 if (mObjectsSize > 0) {
1607 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001608 if(err != NO_ERROR) {
1609 // Still increment the data position by the expected length
1610 mDataPos += sizeof(T);
1611 return err;
1612 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001613 }
1614
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001615 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001616 mDataPos += sizeof(T);
1617 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001618 return NO_ERROR;
1619 } else {
1620 return NOT_ENOUGH_DATA;
1621 }
1622}
1623
Andreas Huber84a6d042009-08-17 13:33:27 -07001624template<class T>
1625T Parcel::readAligned() const {
1626 T result;
1627 if (readAligned(&result) != NO_ERROR) {
1628 result = 0;
1629 }
1630
1631 return result;
1632}
1633
1634template<class T>
1635status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001636 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001637
1638 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1639restart_write:
1640 *reinterpret_cast<T*>(mData+mDataPos) = val;
1641 return finishWrite(sizeof(val));
1642 }
1643
1644 status_t err = growData(sizeof(val));
1645 if (err == NO_ERROR) goto restart_write;
1646 return err;
1647}
1648
Casey Dahlin185d3442016-02-09 11:08:35 -08001649namespace {
1650
1651template<typename T>
1652status_t readByteVectorInternal(const Parcel* parcel,
1653 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001654 val->clear();
1655
1656 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001657 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001658
1659 if (status != OK) {
1660 return status;
1661 }
1662
Christopher Wiley4db672d2015-11-10 09:44:30 -08001663 if (size < 0) {
1664 status = UNEXPECTED_NULL;
1665 return status;
1666 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001667 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001668 status = BAD_VALUE;
1669 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001670 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001671
Paul Lietar433e87b2016-09-16 10:39:32 -07001672 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001673 if (!data) {
1674 status = BAD_VALUE;
1675 return status;
1676 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001677 val->reserve(size);
1678 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001679
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001680 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001681}
1682
Casey Dahlin185d3442016-02-09 11:08:35 -08001683template<typename T>
1684status_t readByteVectorInternalPtr(
1685 const Parcel* parcel,
1686 std::unique_ptr<std::vector<T>>* val) {
1687 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001688 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001689 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001690 val->reset();
1691
1692 if (status != OK || size < 0) {
1693 return status;
1694 }
1695
Casey Dahlin185d3442016-02-09 11:08:35 -08001696 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001697 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001698
Casey Dahlin185d3442016-02-09 11:08:35 -08001699 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001700
1701 if (status != OK) {
1702 val->reset();
1703 }
1704
1705 return status;
1706}
1707
Casey Dahlin185d3442016-02-09 11:08:35 -08001708} // namespace
1709
1710status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1711 return readByteVectorInternal(this, val);
1712}
1713
1714status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1715 return readByteVectorInternal(this, val);
1716}
1717
1718status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1719 return readByteVectorInternalPtr(this, val);
1720}
1721
1722status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1723 return readByteVectorInternalPtr(this, val);
1724}
1725
Casey Dahlinb9872622015-11-25 15:09:45 -08001726status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1727 return readNullableTypedVector(val, &Parcel::readInt32);
1728}
1729
Casey Dahlin451ff582015-10-19 18:12:18 -07001730status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001731 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001732}
1733
Casey Dahlinb9872622015-11-25 15:09:45 -08001734status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1735 return readNullableTypedVector(val, &Parcel::readInt64);
1736}
1737
Casey Dahlin451ff582015-10-19 18:12:18 -07001738status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001739 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001740}
1741
Casey Dahlinb9872622015-11-25 15:09:45 -08001742status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1743 return readNullableTypedVector(val, &Parcel::readFloat);
1744}
1745
Casey Dahlin451ff582015-10-19 18:12:18 -07001746status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001747 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001748}
1749
Casey Dahlinb9872622015-11-25 15:09:45 -08001750status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1751 return readNullableTypedVector(val, &Parcel::readDouble);
1752}
1753
Casey Dahlin451ff582015-10-19 18:12:18 -07001754status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001755 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001756}
1757
Casey Dahlinb9872622015-11-25 15:09:45 -08001758status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1759 const int32_t start = dataPosition();
1760 int32_t size;
1761 status_t status = readInt32(&size);
1762 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001763
Casey Dahlinb9872622015-11-25 15:09:45 -08001764 if (status != OK || size < 0) {
1765 return status;
1766 }
1767
1768 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001769 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001770
1771 status = readBoolVector(val->get());
1772
1773 if (status != OK) {
1774 val->reset();
1775 }
1776
1777 return status;
1778}
1779
1780status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001781 int32_t size;
1782 status_t status = readInt32(&size);
1783
1784 if (status != OK) {
1785 return status;
1786 }
1787
1788 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001789 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001790 }
1791
1792 val->resize(size);
1793
1794 /* C++ bool handling means a vector of bools isn't necessarily addressable
1795 * (we might use individual bits)
1796 */
Christopher Wiley97887982015-10-27 16:33:47 -07001797 bool data;
1798 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001799 status = readBool(&data);
1800 (*val)[i] = data;
1801
1802 if (status != OK) {
1803 return status;
1804 }
1805 }
1806
1807 return OK;
1808}
1809
Casey Dahlinb9872622015-11-25 15:09:45 -08001810status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1811 return readNullableTypedVector(val, &Parcel::readChar);
1812}
1813
Casey Dahlin451ff582015-10-19 18:12:18 -07001814status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001815 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001816}
1817
Casey Dahlinb9872622015-11-25 15:09:45 -08001818status_t Parcel::readString16Vector(
1819 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1820 return readNullableTypedVector(val, &Parcel::readString16);
1821}
1822
Casey Dahlin451ff582015-10-19 18:12:18 -07001823status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001824 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001825}
1826
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001827status_t Parcel::readUtf8VectorFromUtf16Vector(
1828 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1829 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1830}
1831
1832status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1833 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1834}
Casey Dahlin451ff582015-10-19 18:12:18 -07001835
Andreas Huber84a6d042009-08-17 13:33:27 -07001836status_t Parcel::readInt32(int32_t *pArg) const
1837{
1838 return readAligned(pArg);
1839}
1840
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001841int32_t Parcel::readInt32() const
1842{
Andreas Huber84a6d042009-08-17 13:33:27 -07001843 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001844}
1845
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001846status_t Parcel::readUint32(uint32_t *pArg) const
1847{
1848 return readAligned(pArg);
1849}
1850
1851uint32_t Parcel::readUint32() const
1852{
1853 return readAligned<uint32_t>();
1854}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001855
1856status_t Parcel::readInt64(int64_t *pArg) const
1857{
Andreas Huber84a6d042009-08-17 13:33:27 -07001858 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001859}
1860
1861
1862int64_t Parcel::readInt64() const
1863{
Andreas Huber84a6d042009-08-17 13:33:27 -07001864 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001865}
1866
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001867status_t Parcel::readUint64(uint64_t *pArg) const
1868{
1869 return readAligned(pArg);
1870}
1871
1872uint64_t Parcel::readUint64() const
1873{
1874 return readAligned<uint64_t>();
1875}
1876
Serban Constantinescuf683e012013-11-05 16:53:55 +00001877status_t Parcel::readPointer(uintptr_t *pArg) const
1878{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001879 status_t ret;
1880 binder_uintptr_t ptr;
1881 ret = readAligned(&ptr);
1882 if (!ret)
1883 *pArg = ptr;
1884 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001885}
1886
1887uintptr_t Parcel::readPointer() const
1888{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001889 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001890}
1891
1892
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001893status_t Parcel::readFloat(float *pArg) const
1894{
Andreas Huber84a6d042009-08-17 13:33:27 -07001895 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001896}
1897
1898
1899float Parcel::readFloat() const
1900{
Andreas Huber84a6d042009-08-17 13:33:27 -07001901 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001902}
1903
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001904#if defined(__mips__) && defined(__mips_hard_float)
1905
1906status_t Parcel::readDouble(double *pArg) const
1907{
1908 union {
1909 double d;
1910 unsigned long long ll;
1911 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001912 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001913 status_t status;
1914 status = readAligned(&u.ll);
1915 *pArg = u.d;
1916 return status;
1917}
1918
1919double Parcel::readDouble() const
1920{
1921 union {
1922 double d;
1923 unsigned long long ll;
1924 } u;
1925 u.ll = readAligned<unsigned long long>();
1926 return u.d;
1927}
1928
1929#else
1930
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001931status_t Parcel::readDouble(double *pArg) const
1932{
Andreas Huber84a6d042009-08-17 13:33:27 -07001933 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001934}
1935
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001936double Parcel::readDouble() const
1937{
Andreas Huber84a6d042009-08-17 13:33:27 -07001938 return readAligned<double>();
1939}
1940
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001941#endif
1942
Andreas Huber84a6d042009-08-17 13:33:27 -07001943status_t Parcel::readIntPtr(intptr_t *pArg) const
1944{
1945 return readAligned(pArg);
1946}
1947
1948
1949intptr_t Parcel::readIntPtr() const
1950{
1951 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001952}
1953
Casey Dahlind6848f52015-10-15 15:44:59 -07001954status_t Parcel::readBool(bool *pArg) const
1955{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001956 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001957 status_t ret = readInt32(&tmp);
1958 *pArg = (tmp != 0);
1959 return ret;
1960}
1961
1962bool Parcel::readBool() const
1963{
1964 return readInt32() != 0;
1965}
1966
1967status_t Parcel::readChar(char16_t *pArg) const
1968{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001969 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001970 status_t ret = readInt32(&tmp);
1971 *pArg = char16_t(tmp);
1972 return ret;
1973}
1974
1975char16_t Parcel::readChar() const
1976{
1977 return char16_t(readInt32());
1978}
1979
1980status_t Parcel::readByte(int8_t *pArg) const
1981{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001982 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001983 status_t ret = readInt32(&tmp);
1984 *pArg = int8_t(tmp);
1985 return ret;
1986}
1987
1988int8_t Parcel::readByte() const
1989{
1990 return int8_t(readInt32());
1991}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001992
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001993status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1994 size_t utf16Size = 0;
1995 const char16_t* src = readString16Inplace(&utf16Size);
1996 if (!src) {
1997 return UNEXPECTED_NULL;
1998 }
1999
2000 // Save ourselves the trouble, we're done.
2001 if (utf16Size == 0u) {
2002 str->clear();
2003 return NO_ERROR;
2004 }
2005
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002006 // Allow for closing '\0'
2007 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2008 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002009 return BAD_VALUE;
2010 }
2011 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002012 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002013 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002014 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2015 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002016 return NO_ERROR;
2017}
2018
2019status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2020 const int32_t start = dataPosition();
2021 int32_t size;
2022 status_t status = readInt32(&size);
2023 str->reset();
2024
2025 if (status != OK || size < 0) {
2026 return status;
2027 }
2028
2029 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002030 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002031 return readUtf8FromUtf16(str->get());
2032}
2033
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002034const char* Parcel::readCString() const
2035{
2036 const size_t avail = mDataSize-mDataPos;
2037 if (avail > 0) {
2038 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2039 // is the string's trailing NUL within the parcel's valid bounds?
2040 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2041 if (eos) {
2042 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002043 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002044 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002045 return str;
2046 }
2047 }
Yi Kong91635562018-06-07 14:38:36 -07002048 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002049}
2050
2051String8 Parcel::readString8() const
2052{
Roshan Pius87b64d22016-07-18 12:51:02 -07002053 String8 retString;
2054 status_t status = readString8(&retString);
2055 if (status != OK) {
2056 // We don't care about errors here, so just return an empty string.
2057 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002058 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002059 return retString;
2060}
2061
2062status_t Parcel::readString8(String8* pArg) const
2063{
2064 int32_t size;
2065 status_t status = readInt32(&size);
2066 if (status != OK) {
2067 return status;
2068 }
2069 // watch for potential int overflow from size+1
2070 if (size < 0 || size >= INT32_MAX) {
2071 return BAD_VALUE;
2072 }
2073 // |writeString8| writes nothing for empty string.
2074 if (size == 0) {
2075 *pArg = String8();
2076 return OK;
2077 }
2078 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07002079 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002080 return BAD_VALUE;
2081 }
2082 pArg->setTo(str, size);
2083 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002084}
2085
2086String16 Parcel::readString16() const
2087{
2088 size_t len;
2089 const char16_t* str = readString16Inplace(&len);
2090 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002091 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002092 return String16();
2093}
2094
Casey Dahlinb9872622015-11-25 15:09:45 -08002095status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2096{
2097 const int32_t start = dataPosition();
2098 int32_t size;
2099 status_t status = readInt32(&size);
2100 pArg->reset();
2101
2102 if (status != OK || size < 0) {
2103 return status;
2104 }
2105
2106 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002107 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002108
2109 status = readString16(pArg->get());
2110
2111 if (status != OK) {
2112 pArg->reset();
2113 }
2114
2115 return status;
2116}
2117
Casey Dahlin451ff582015-10-19 18:12:18 -07002118status_t Parcel::readString16(String16* pArg) const
2119{
2120 size_t len;
2121 const char16_t* str = readString16Inplace(&len);
2122 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002123 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002124 return 0;
2125 } else {
2126 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002127 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002128 }
2129}
2130
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002131const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2132{
2133 int32_t size = readInt32();
2134 // watch for potential int overflow from size+1
2135 if (size >= 0 && size < INT32_MAX) {
2136 *outLen = size;
2137 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002138 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002139 return str;
2140 }
2141 }
2142 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002143 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002144}
2145
Casey Dahlinf0c13772015-10-27 18:33:56 -07002146status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2147{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002148 status_t status = readNullableStrongBinder(val);
2149 if (status == OK && !val->get()) {
2150 status = UNEXPECTED_NULL;
2151 }
2152 return status;
2153}
2154
2155status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2156{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002157 return unflatten_binder(ProcessState::self(), *this, val);
2158}
2159
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002160sp<IBinder> Parcel::readStrongBinder() const
2161{
2162 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002163 // Note that a lot of code in Android reads binders by hand with this
2164 // method, and that code has historically been ok with getting nullptr
2165 // back (while ignoring error codes).
2166 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002167 return val;
2168}
2169
2170wp<IBinder> Parcel::readWeakBinder() const
2171{
2172 wp<IBinder> val;
2173 unflatten_binder(ProcessState::self(), *this, &val);
2174 return val;
2175}
2176
Christopher Wiley97f048d2015-11-19 06:49:05 -08002177status_t Parcel::readParcelable(Parcelable* parcelable) const {
2178 int32_t have_parcelable = 0;
2179 status_t status = readInt32(&have_parcelable);
2180 if (status != OK) {
2181 return status;
2182 }
2183 if (!have_parcelable) {
2184 return UNEXPECTED_NULL;
2185 }
2186 return parcelable->readFromParcel(this);
2187}
2188
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002189status_t Parcel::readValue(binder::Value* value) const {
2190 return value->readFromParcel(this);
2191}
2192
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002193int32_t Parcel::readExceptionCode() const
2194{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002195 binder::Status status;
2196 status.readFromParcel(*this);
2197 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002198}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002199
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002200native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002201{
2202 int numFds, numInts;
2203 status_t err;
2204 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002205 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002206 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002207 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002208
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002209 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002210 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002211 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002212 }
2213
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002214 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002215 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002216 if (h->data[i] < 0) {
2217 for (int j = 0; j < i; j++) {
2218 close(h->data[j]);
2219 }
2220 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002221 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002222 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002223 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002224 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002225 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002226 native_handle_close(h);
2227 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002228 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002229 }
2230 return h;
2231}
2232
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002233int Parcel::readFileDescriptor() const
2234{
2235 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002236
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002237 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002238 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002239 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002240
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002241 return BAD_TYPE;
2242}
2243
Dianne Hackborn1941a402016-08-29 12:30:43 -07002244int Parcel::readParcelFileDescriptor() const
2245{
2246 int32_t hasComm = readInt32();
2247 int fd = readFileDescriptor();
2248 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002249 // detach (owned by the binder driver)
2250 int comm = readFileDescriptor();
2251
2252 // warning: this must be kept in sync with:
2253 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2254 enum ParcelFileDescriptorStatus {
2255 DETACHED = 2,
2256 };
2257
2258#if BYTE_ORDER == BIG_ENDIAN
2259 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2260#endif
2261#if BYTE_ORDER == LITTLE_ENDIAN
2262 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2263#endif
2264
2265 ssize_t written = TEMP_FAILURE_RETRY(
2266 ::write(comm, &message, sizeof(message)));
2267
2268 if (written == -1 || written != sizeof(message)) {
2269 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2270 written, strerror(errno));
2271 return BAD_TYPE;
2272 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002273 }
2274 return fd;
2275}
2276
Christopher Wiley2cf19952016-04-11 11:09:37 -07002277status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002278{
2279 int got = readFileDescriptor();
2280
2281 if (got == BAD_TYPE) {
2282 return BAD_TYPE;
2283 }
2284
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002285 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002286
2287 if (val->get() < 0) {
2288 return BAD_VALUE;
2289 }
2290
2291 return OK;
2292}
2293
Ryo Hashimotobf551892018-05-31 16:58:35 +09002294status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2295{
2296 int got = readParcelFileDescriptor();
2297
2298 if (got == BAD_TYPE) {
2299 return BAD_TYPE;
2300 }
2301
2302 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2303
2304 if (val->get() < 0) {
2305 return BAD_VALUE;
2306 }
2307
2308 return OK;
2309}
Casey Dahlin06673e32015-11-23 13:24:23 -08002310
Christopher Wiley2cf19952016-04-11 11:09:37 -07002311status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002312 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2313}
2314
Christopher Wiley2cf19952016-04-11 11:09:37 -07002315status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002316 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2317}
2318
Jeff Brown5707dbf2011-09-23 21:17:56 -07002319status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2320{
Jeff Brown13b16042014-11-11 16:44:25 -08002321 int32_t blobType;
2322 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002323 if (status) return status;
2324
Jeff Brown13b16042014-11-11 16:44:25 -08002325 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002326 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002327 const void* ptr = readInplace(len);
2328 if (!ptr) return BAD_VALUE;
2329
Jeff Brown13b16042014-11-11 16:44:25 -08002330 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002331 return NO_ERROR;
2332 }
2333
Steve Block6807e592011-10-20 11:56:00 +01002334 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002335 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002336 int fd = readFileDescriptor();
2337 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2338
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002339 if (!ashmem_valid(fd)) {
2340 ALOGE("invalid fd");
2341 return BAD_VALUE;
2342 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002343 int size = ashmem_get_size_region(fd);
2344 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002345 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002346 return BAD_VALUE;
2347 }
Yi Kong91635562018-06-07 14:38:36 -07002348 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002349 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002350 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002351
Jeff Brown13b16042014-11-11 16:44:25 -08002352 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002353 return NO_ERROR;
2354}
2355
Mathias Agopiane1424282013-07-29 21:24:40 -07002356status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002357{
2358 // size
2359 const size_t len = this->readInt32();
2360 const size_t fd_count = this->readInt32();
2361
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002362 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002363 // don't accept size_t values which may have come from an
2364 // inadvertent conversion from a negative int.
2365 return BAD_VALUE;
2366 }
2367
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002368 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002369 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002370 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002371 return BAD_VALUE;
2372
Yi Kong91635562018-06-07 14:38:36 -07002373 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002374 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002375 fds = new (std::nothrow) int[fd_count];
2376 if (fds == nullptr) {
2377 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2378 return BAD_VALUE;
2379 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002380 }
2381
2382 status_t err = NO_ERROR;
2383 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002384 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002385 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002386 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002387 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 -07002388 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2389 // Close all the file descriptors that were dup-ed.
2390 for (size_t j=0; j<i ;j++) {
2391 close(fds[j]);
2392 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002393 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002394 }
2395
2396 if (err == NO_ERROR) {
2397 err = val.unflatten(buf, len, fds, fd_count);
2398 }
2399
2400 if (fd_count) {
2401 delete [] fds;
2402 }
2403
2404 return err;
2405}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002406const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2407{
2408 const size_t DPOS = mDataPos;
2409 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2410 const flat_binder_object* obj
2411 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2412 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002413 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002414 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002415 // the object list, so we don't want to check for it when
2416 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002417 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002418 return obj;
2419 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002420
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002422 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002423 const size_t N = mObjectsSize;
2424 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002425
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002426 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002427 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002428 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002429
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002430 // Start at the current hint position, looking for an object at
2431 // the current data position.
2432 if (opos < N) {
2433 while (opos < (N-1) && OBJS[opos] < DPOS) {
2434 opos++;
2435 }
2436 } else {
2437 opos = N-1;
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 forward 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 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002447
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002448 // Look backwards for it...
2449 while (opos > 0 && OBJS[opos] > DPOS) {
2450 opos--;
2451 }
2452 if (OBJS[opos] == DPOS) {
2453 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002454 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002455 this, DPOS, opos);
2456 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002457 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002458 return obj;
2459 }
2460 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002461 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 -07002462 this, DPOS);
2463 }
Yi Kong91635562018-06-07 14:38:36 -07002464 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002465}
2466
2467void Parcel::closeFileDescriptors()
2468{
2469 size_t i = mObjectsSize;
2470 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002471 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002472 }
2473 while (i > 0) {
2474 i--;
2475 const flat_binder_object* flat
2476 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002477 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002478 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479 close(flat->handle);
2480 }
2481 }
2482}
2483
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002484uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002485{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002486 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002487}
2488
2489size_t Parcel::ipcDataSize() const
2490{
2491 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2492}
2493
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002494uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002495{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002496 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002497}
2498
2499size_t Parcel::ipcObjectsCount() const
2500{
2501 return mObjectsSize;
2502}
2503
2504void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002505 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002506{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002507 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508 freeDataNoInit();
2509 mError = NO_ERROR;
2510 mData = const_cast<uint8_t*>(data);
2511 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002512 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002514 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002515 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516 mObjectsSize = mObjectsCapacity = objectsCount;
2517 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002518 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002519 mOwner = relFunc;
2520 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002521 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002522 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002523 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002524 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002525 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002526 mObjectsSize = 0;
2527 break;
2528 }
2529 minOffset = offset + sizeof(flat_binder_object);
2530 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002531 scanForFds();
2532}
2533
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002534void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002535{
2536 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002537
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002538 if (errorCheck() != NO_ERROR) {
2539 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002540 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002541 } else if (dataSize() > 0) {
2542 const uint8_t* DATA = data();
2543 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002544 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002545 const size_t N = objectsCount();
2546 for (size_t i=0; i<N; i++) {
2547 const flat_binder_object* flat
2548 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2549 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002550 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002551 << " = " << flat->binder;
2552 }
2553 } else {
2554 to << "NULL";
2555 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002556
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002557 to << ")";
2558}
2559
2560void Parcel::releaseObjects()
2561{
2562 const sp<ProcessState> proc(ProcessState::self());
2563 size_t i = mObjectsSize;
2564 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002565 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002566 while (i > 0) {
2567 i--;
2568 const flat_binder_object* flat
2569 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002570 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002571 }
2572}
2573
2574void Parcel::acquireObjects()
2575{
2576 const sp<ProcessState> proc(ProcessState::self());
2577 size_t i = mObjectsSize;
2578 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002579 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002580 while (i > 0) {
2581 i--;
2582 const flat_binder_object* flat
2583 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002584 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002585 }
2586}
2587
2588void Parcel::freeData()
2589{
2590 freeDataNoInit();
2591 initState();
2592}
2593
2594void Parcel::freeDataNoInit()
2595{
2596 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002597 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002598 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002599 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2600 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002601 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002602 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002603 if (mData) {
2604 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002605 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002606 if (mDataCapacity <= gParcelGlobalAllocSize) {
2607 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2608 } else {
2609 gParcelGlobalAllocSize = 0;
2610 }
2611 if (gParcelGlobalAllocCount > 0) {
2612 gParcelGlobalAllocCount--;
2613 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002614 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002615 free(mData);
2616 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002617 if (mObjects) free(mObjects);
2618 }
2619}
2620
2621status_t Parcel::growData(size_t len)
2622{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002623 if (len > INT32_MAX) {
2624 // don't accept size_t values which may have come from an
2625 // inadvertent conversion from a negative int.
2626 return BAD_VALUE;
2627 }
2628
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002629 size_t newSize = ((mDataSize+len)*3)/2;
2630 return (newSize <= mDataSize)
2631 ? (status_t) NO_MEMORY
2632 : continueWrite(newSize);
2633}
2634
2635status_t Parcel::restartWrite(size_t desired)
2636{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002637 if (desired > INT32_MAX) {
2638 // don't accept size_t values which may have come from an
2639 // inadvertent conversion from a negative int.
2640 return BAD_VALUE;
2641 }
2642
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002643 if (mOwner) {
2644 freeData();
2645 return continueWrite(desired);
2646 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002647
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002648 uint8_t* data = (uint8_t*)realloc(mData, desired);
2649 if (!data && desired > mDataCapacity) {
2650 mError = NO_MEMORY;
2651 return NO_MEMORY;
2652 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002653
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002654 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002655
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002656 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002657 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002658 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002659 gParcelGlobalAllocSize += desired;
2660 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002661 if (!mData) {
2662 gParcelGlobalAllocCount++;
2663 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002664 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002665 mData = data;
2666 mDataCapacity = desired;
2667 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002668
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002669 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002670 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2671 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2672
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002673 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002674 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002675 mObjectsSize = mObjectsCapacity = 0;
2676 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002677 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002678 mHasFds = false;
2679 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002680 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002681
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002682 return NO_ERROR;
2683}
2684
2685status_t Parcel::continueWrite(size_t desired)
2686{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002687 if (desired > INT32_MAX) {
2688 // don't accept size_t values which may have come from an
2689 // inadvertent conversion from a negative int.
2690 return BAD_VALUE;
2691 }
2692
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002693 // If shrinking, first adjust for any objects that appear
2694 // after the new data size.
2695 size_t objectsSize = mObjectsSize;
2696 if (desired < mDataSize) {
2697 if (desired == 0) {
2698 objectsSize = 0;
2699 } else {
2700 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002701 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002702 break;
2703 objectsSize--;
2704 }
2705 }
2706 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002707
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002708 if (mOwner) {
2709 // If the size is going to zero, just release the owner's data.
2710 if (desired == 0) {
2711 freeData();
2712 return NO_ERROR;
2713 }
2714
2715 // If there is a different owner, we need to take
2716 // posession.
2717 uint8_t* data = (uint8_t*)malloc(desired);
2718 if (!data) {
2719 mError = NO_MEMORY;
2720 return NO_MEMORY;
2721 }
Yi Kong91635562018-06-07 14:38:36 -07002722 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002723
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002724 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002725 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002726 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002727 free(data);
2728
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002729 mError = NO_MEMORY;
2730 return NO_MEMORY;
2731 }
2732
2733 // Little hack to only acquire references on objects
2734 // we will be keeping.
2735 size_t oldObjectsSize = mObjectsSize;
2736 mObjectsSize = objectsSize;
2737 acquireObjects();
2738 mObjectsSize = oldObjectsSize;
2739 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002740
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002741 if (mData) {
2742 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2743 }
2744 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002745 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002746 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002747 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002748 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002749 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002750
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002751 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002752 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002753 gParcelGlobalAllocSize += desired;
2754 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002755 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002756
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002757 mData = data;
2758 mObjects = objects;
2759 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002760 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002761 mDataCapacity = desired;
2762 mObjectsSize = mObjectsCapacity = objectsSize;
2763 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002764 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002765
2766 } else if (mData) {
2767 if (objectsSize < mObjectsSize) {
2768 // Need to release refs on any objects we are dropping.
2769 const sp<ProcessState> proc(ProcessState::self());
2770 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2771 const flat_binder_object* flat
2772 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002773 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002774 // will need to rescan because we may have lopped off the only FDs
2775 mFdsKnown = false;
2776 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002777 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002778 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002779 binder_size_t* objects =
2780 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002781 if (objects) {
2782 mObjects = objects;
2783 }
2784 mObjectsSize = objectsSize;
2785 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002786 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002787 }
2788
2789 // We own the data, so we can just do a realloc().
2790 if (desired > mDataCapacity) {
2791 uint8_t* data = (uint8_t*)realloc(mData, desired);
2792 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002793 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2794 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002795 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002796 gParcelGlobalAllocSize += desired;
2797 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002798 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002799 mData = data;
2800 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002801 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002802 mError = NO_MEMORY;
2803 return NO_MEMORY;
2804 }
2805 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002806 if (mDataSize > desired) {
2807 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002808 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002809 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002810 if (mDataPos > desired) {
2811 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002812 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002813 }
2814 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002815
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002816 } else {
2817 // This is the first data. Easy!
2818 uint8_t* data = (uint8_t*)malloc(desired);
2819 if (!data) {
2820 mError = NO_MEMORY;
2821 return NO_MEMORY;
2822 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002823
Yi Kong91635562018-06-07 14:38:36 -07002824 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002825 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002826 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002827 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002828
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002829 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002830 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002831 gParcelGlobalAllocSize += desired;
2832 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002833 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002834
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002835 mData = data;
2836 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002837 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2838 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002839 mDataCapacity = desired;
2840 }
2841
2842 return NO_ERROR;
2843}
2844
2845void Parcel::initState()
2846{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002847 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002848 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002849 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002850 mDataSize = 0;
2851 mDataCapacity = 0;
2852 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002853 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2854 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002855 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002856 mObjectsSize = 0;
2857 mObjectsCapacity = 0;
2858 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002859 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002860 mHasFds = false;
2861 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002862 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002863 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002864 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002865
2866 // racing multiple init leads only to multiple identical write
2867 if (gMaxFds == 0) {
2868 struct rlimit result;
2869 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2870 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002871 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002872 } else {
2873 ALOGW("Unable to getrlimit: %s", strerror(errno));
2874 gMaxFds = 1024;
2875 }
2876 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002877}
2878
2879void Parcel::scanForFds() const
2880{
2881 bool hasFds = false;
2882 for (size_t i=0; i<mObjectsSize; i++) {
2883 const flat_binder_object* flat
2884 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002885 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002886 hasFds = true;
2887 break;
2888 }
2889 }
2890 mHasFds = hasFds;
2891 mFdsKnown = true;
2892}
2893
Dan Sandleraa5c2342015-04-10 10:08:45 -04002894size_t Parcel::getBlobAshmemSize() const
2895{
Adrian Roos6bb31142015-10-22 16:46:12 -07002896 // This used to return the size of all blobs that were written to ashmem, now we're returning
2897 // the ashmem currently referenced by this Parcel, which should be equivalent.
2898 // TODO: Remove method once ABI can be changed.
2899 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002900}
2901
Adrian Rooscbf37262015-10-22 16:12:53 -07002902size_t Parcel::getOpenAshmemSize() const
2903{
2904 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002905}
2906
2907// --- Parcel::Blob ---
2908
2909Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002910 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002911}
2912
2913Parcel::Blob::~Blob() {
2914 release();
2915}
2916
2917void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002918 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002919 ::munmap(mData, mSize);
2920 }
2921 clear();
2922}
2923
Jeff Brown13b16042014-11-11 16:44:25 -08002924void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2925 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002926 mData = data;
2927 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002928 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002929}
2930
2931void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002932 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002933 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002934 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002935 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002936}
2937
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002938}; // namespace android